@Test
  public void test16Bit_WithBitRead() {
    ModbusAddress[] addresses =
        new ModbusAddress[] {
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.Int16, 1)
              .setBit(0)
              .build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.Int16, 2)
              .setBit(8)
              .build()
        };

    List<TestReadItem> items = new ArrayList<TestReadItem>();
    for (ModbusAddress address : addresses) {
      items.add(new TestReadItem(address));
    }

    byte[] data = new byte[] {0x00, 0x01, 0x01, 0x00};
    handle(items, data);

    boolean b1 = getValue(items.get(0), Boolean.class);
    assertTrue("Expected true.", b1);

    boolean b2 = getValue(items.get(1), Boolean.class);
    assertTrue("Expected true.", b2);
  }
  @Override
  protected Object getKey(T item) {
    ModbusAddress address = (ModbusAddress) item.getAddressObject();

    int bit = address.getBit();

    return bit < 0 ? bit : bit / 16;
  }
  @Test
  public void testBCD32_WithBitRead() {
    ModbusAddress[] addresses =
        new ModbusAddress[] {
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.BCD32, 1)
              .setBit(0)
              .build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.BCD32, 3)
              .setBit(8)
              .build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.BCD32, 5)
              .setBit(16)
              .build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.BCD32, 7)
              .setBit(24)
              .build()
        };

    List<TestReadItem> items = new ArrayList<TestReadItem>();
    for (ModbusAddress address : addresses) {
      items.add(new TestReadItem(address));
    }

    byte[] data =
        new byte[] {
          0x00,
          0x00,
          0x00,
          0x01, // (1<<0)
          0x00,
          0x00,
          0x02,
          0x56, // (1<<8)
          0x00,
          0x06,
          0x55,
          0x36, // (1<<16)
          0x16,
          0x77,
          0x72,
          0x16 // (1<<24)
        };

    handle(items, data);

    boolean b1 = getValue(items.get(0), Boolean.class);
    assertTrue("Expected true.", b1);

    boolean b2 = getValue(items.get(1), Boolean.class);
    assertTrue("Expected true.", b2);

    boolean b3 = getValue(items.get(2), Boolean.class);
    assertTrue("Expected true.", b3);

    boolean b4 = getValue(items.get(3), Boolean.class);
    assertTrue("Expected true.", b4);
  }
  @Test
  public void test16Bit() {
    ModbusAddress[] addresses =
        new ModbusAddress[] {
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.Int16, 1).build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.Int16, 2).build()
        };

    List<TestReadItem> items = new ArrayList<TestReadItem>();
    for (ModbusAddress address : addresses) {
      items.add(new TestReadItem(address));
    }

    byte[] data = new byte[] {0x01, 0x02, 0x03, 0x04};
    handle(items, data);

    short s1 = getValue(items.get(0), Short.class);
    assertEquals("Expected 0x0102.", 0x0102, s1);

    short s2 = getValue(items.get(1), Short.class);
    assertEquals("Expected 0x0304.", 0x0304, s2);
  }
  @Test
  public void testBCD32() {
    ModbusAddress[] addresses =
        new ModbusAddress[] {
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.BCD32, 1).build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.BCD32, 3).build()
        };

    List<TestReadItem> items = new ArrayList<TestReadItem>();
    for (ModbusAddress address : addresses) {
      items.add(new TestReadItem(address));
    }

    byte[] data = new byte[] {0x01, 0x22, 0x33, 0x44, 0x05, 0x66, 0x77, 0x78};
    handle(items, data);

    int i1 = getValue(items.get(0), Integer.class);
    assertEquals("Expected 1223344.", 1223344, i1);

    int i2 = getValue(items.get(1), Integer.class);
    assertEquals("Expected 5667778.", 5667778, i2);
  }
  @Test
  public void test32Bit() {
    ModbusAddress[] addresses =
        new ModbusAddress[] {
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.Int32, 1).build(),
          ModbusAddress.builder(ModbusTable.HoldingRegisters, ModbusDataType.Int32, 3).build()
        };

    List<TestReadItem> items = new ArrayList<TestReadItem>();
    for (ModbusAddress address : addresses) {
      items.add(new TestReadItem(address));
    }

    byte[] data = new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    handle(items, data);

    int i1 = getValue(items.get(0), Integer.class);
    assertEquals("Expected 0x01020304.", 0x01020304, i1);

    int i2 = getValue(items.get(1), Integer.class);
    assertEquals("Expected 0x05060708.", 0x05060708, i2);
  }