Added more testcases
This commit is contained in:
parent
b7bedc94cd
commit
644c5ab6ea
2 changed files with 36 additions and 2 deletions
|
|
@ -14,12 +14,13 @@ import zutil.parser.binary.BinaryStruct.*;
|
||||||
*/
|
*/
|
||||||
public class BinaryStructParser {
|
public class BinaryStructParser {
|
||||||
|
|
||||||
public static void parse(BinaryStruct struct, byte[] data) {
|
public static int parse(BinaryStruct struct, byte[] data) {
|
||||||
List<BinaryFieldData> structDataList = getStructDataList(struct.getClass());
|
List<BinaryFieldData> structDataList = getStructDataList(struct.getClass());
|
||||||
int bitOffset = 0;
|
int bitOffset = 0;
|
||||||
for (BinaryFieldData field : structDataList){
|
for (BinaryFieldData field : structDataList){
|
||||||
bitOffset += field.setValue(struct, data, bitOffset);
|
bitOffset += field.setValue(struct, data, bitOffset);
|
||||||
}
|
}
|
||||||
|
return bitOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ public class BinaryStructTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void basicTest(){
|
public void basicIntTest(){
|
||||||
BinaryTestStruct struct = new BinaryTestStruct() {
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
||||||
@BinaryField(index=1, length=32)
|
@BinaryField(index=1, length=32)
|
||||||
public int i1;
|
public int i1;
|
||||||
|
|
@ -32,4 +32,37 @@ public class BinaryStructTest {
|
||||||
BinaryStructParser.parse(struct, new byte[]{0,0,0,1, 0,0,0,2});
|
BinaryStructParser.parse(struct, new byte[]{0,0,0,1, 0,0,0,2});
|
||||||
struct.assertObj();
|
struct.assertObj();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void basicBooleanTest(){
|
||||||
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
||||||
|
@BinaryField(index=1, length=1)
|
||||||
|
public boolean b1;
|
||||||
|
@BinaryField(index=2, length=1)
|
||||||
|
public boolean b2;
|
||||||
|
|
||||||
|
public void assertObj(){
|
||||||
|
assertFalse(b1);
|
||||||
|
assertFalse(b2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BinaryStructParser.parse(struct, new byte[]{0b0100_000});
|
||||||
|
struct.assertObj();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void basicStringTest(){
|
||||||
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
||||||
|
@BinaryField(index=1, length=8*12)
|
||||||
|
public String s1;
|
||||||
|
|
||||||
|
public void assertObj(){
|
||||||
|
assertEquals(s1, "hello world!");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BinaryStructParser.parse(struct, "hello world!".getBytes());
|
||||||
|
struct.assertObj();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue