예제 #1
0
  @Test
  public void customBinaryFieldTest() throws IOException {
    BinaryStruct struct =
        new BinaryStruct() {
          @BinaryStruct.CustomBinaryField(index = 1, serializer = ByteStringSerializer.class)
          public String s1 = "1234";
        };

    byte[] data = BinaryStructOutputStream.serialize(struct);
    assertArrayEquals(new byte[] {0b0000_0001, 0b0000_0010, 0b0000_0011, 0b0000_0100}, data);
  }
예제 #2
0
  @Test
  public void basicBooleanTest() throws IOException {
    BinaryStruct struct =
        new BinaryStruct() {
          @BinaryField(index = 1, length = 1)
          public boolean b1 = false;

          @BinaryField(index = 2, length = 1)
          public boolean b2 = true;
        };

    byte[] data = BinaryStructOutputStream.serialize(struct);
    assertArrayEquals(new byte[] {(byte) 0b0100_0000}, data);
  }
예제 #3
0
  @Test
  public void shortIntTest() throws IOException {
    BinaryStruct struct =
        new BinaryStruct() {
          @BinaryField(index = 1, length = 16)
          int i1 = 1;

          @BinaryField(index = 2, length = 16)
          int i2 = 2;
        };

    byte[] data = BinaryStructOutputStream.serialize(struct);
    assertArrayEquals(new byte[] {0, 1, 0, 2}, data);
  }
예제 #4
0
  @Test
  public void basicStringTest() throws IOException {
    BinaryStruct struct =
        new BinaryStruct() {
          @BinaryField(index = 1, length = 8)
          int i1 = Integer.MAX_VALUE;

          @BinaryField(index = 2, length = 12 * 8)
          public String s2 = "hello world!";
        };

    byte[] expected = "0hello world!".getBytes();
    expected[0] = (byte) 0xFF;
    byte[] data = BinaryStructOutputStream.serialize(struct);
    assertArrayEquals(expected, data);
  }
예제 #5
0
  @Test
  public void variableLengthFieldTest() throws IOException {
    BinaryStruct struct =
        new BinaryStruct() {
          @BinaryField(index = 1, length = 8)
          private int s1 = 2;

          @VariableLengthBinaryField(index = 2, lengthField = "s1")
          private String s2 = "12345";
        };

    byte[] expected = "012".getBytes();
    expected[0] = 2;
    byte[] data = BinaryStructOutputStream.serialize(struct);
    assertArrayEquals(expected, data);
  }
예제 #6
0
  @Test
  public void mixedTypeTest() throws IOException {
    BinaryStruct struct =
        new BinaryStruct() {
          @BinaryField(index = 1, length = 26)
          private int house = 11_772_006;

          @BinaryField(index = 2, length = 1)
          private boolean group = false;

          @BinaryField(index = 3, length = 1)
          private boolean enable = true;

          @BinaryField(index = 4, length = 4)
          private int unit = 0;
        };

    byte[] expected =
        new byte[] {
          (byte) 0b0010_1100, (byte) 0b1110_1000, (byte) 0b0001_1001, (byte) 0b10_0_1_0000
        };
    byte[] actual = BinaryStructOutputStream.serialize(struct);
    assertArrayEquals(expected, actual);
  }