2016-02-10 14:23:14 +01:00
|
|
|
/*
|
|
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Ziver Koc
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-03-04 17:48:59 +01:00
|
|
|
package zutil.parser.binary;
|
2016-01-28 17:34:51 +01:00
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2016-04-14 13:15:49 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
2016-02-19 20:28:26 +01:00
|
|
|
import static junit.framework.TestCase.assertEquals;
|
|
|
|
|
import static junit.framework.TestCase.assertFalse;
|
2016-01-28 17:34:51 +01:00
|
|
|
|
2016-03-04 17:48:59 +01:00
|
|
|
|
2016-01-28 17:34:51 +01:00
|
|
|
/**
|
2016-03-05 00:42:26 +01:00
|
|
|
* Created by Ziver
|
2016-01-28 17:34:51 +01:00
|
|
|
*/
|
2016-03-05 00:42:26 +01:00
|
|
|
public class BinaryStructInputStreamTest {
|
2016-01-28 17:34:51 +01:00
|
|
|
interface BinaryTestStruct extends BinaryStruct{
|
|
|
|
|
void assertObj();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-02-01 17:52:41 +01:00
|
|
|
public void basicIntTest(){
|
2016-01-28 17:34:51 +01:00
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@BinaryField(index=1, length=32)
|
|
|
|
|
public int i1;
|
|
|
|
|
@BinaryField(index=2, length=32)
|
|
|
|
|
public int i2;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
|
|
|
|
assertEquals(1, i1);
|
|
|
|
|
assertEquals(2, i2);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-10 16:17:07 +01:00
|
|
|
BinaryStructInputStream.read(struct, new byte[]{0,0,0,1, 0,0,0,2});
|
2016-01-28 17:34:51 +01:00
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
2016-02-01 17:52:41 +01:00
|
|
|
|
|
|
|
|
@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);
|
2016-03-10 16:17:07 +01:00
|
|
|
assert(b2);
|
2016-02-01 17:52:41 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-10 16:17:07 +01:00
|
|
|
BinaryStructInputStream.read(struct, new byte[]{0b0100_0000});
|
2016-02-01 17:52:41 +01:00
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void basicStringTest(){
|
|
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@BinaryField(index=1, length=8*12)
|
|
|
|
|
public String s1;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
2016-03-05 00:42:26 +01:00
|
|
|
assertEquals("hello world!", s1);
|
2016-02-01 17:52:41 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-10 16:17:07 +01:00
|
|
|
BinaryStructInputStream.read(struct, "hello world!".getBytes());
|
2016-02-01 17:52:41 +01:00
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
2016-02-02 17:06:06 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void nonLinedLength(){
|
2016-02-09 13:12:44 +01:00
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@BinaryField(index=1, length=12)
|
|
|
|
|
public int i1;
|
|
|
|
|
@BinaryField(index=2, length=12)
|
|
|
|
|
public int i2;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
|
|
|
|
assertEquals(1, i1);
|
|
|
|
|
assertEquals(2048, i2);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-10 16:17:07 +01:00
|
|
|
BinaryStructInputStream.read(struct, new byte[]{0b0000_0000,0b0001_1000,0b0000_0000});
|
2016-02-09 13:12:44 +01:00
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 17:11:18 +02:00
|
|
|
@Test
|
2016-02-09 13:12:44 +01:00
|
|
|
public void nonLinedLength2(){
|
2016-02-02 17:06:06 +01:00
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@BinaryField(index=1, length=12)
|
|
|
|
|
public int i1;
|
|
|
|
|
@BinaryField(index=2, length=12)
|
|
|
|
|
public int i2;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
|
|
|
|
assertEquals(17, i1);
|
|
|
|
|
assertEquals(2048, i2);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-10 16:17:07 +01:00
|
|
|
BinaryStructInputStream.read(struct, new byte[]{0b0000_0001,0b0001_1000,0b0000_0000});
|
2016-02-02 17:06:06 +01:00
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
2016-04-14 13:15:49 +02:00
|
|
|
|
2016-05-12 23:00:28 +02:00
|
|
|
@Test
|
|
|
|
|
public void mixedType(){
|
|
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@BinaryField(index=1, length=4)
|
|
|
|
|
public int i1;
|
|
|
|
|
@BinaryField(index=2, length=1)
|
|
|
|
|
public boolean b2;
|
|
|
|
|
@BinaryField(index=3, length=1)
|
|
|
|
|
public boolean b3;
|
|
|
|
|
@BinaryField(index=4, length=1)
|
|
|
|
|
public boolean b4;
|
|
|
|
|
@BinaryField(index=5, length=1)
|
|
|
|
|
public boolean b5;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
|
|
|
|
assertEquals("i1", 6, i1);
|
|
|
|
|
assertEquals("b2", true, b2);
|
|
|
|
|
assertEquals("b3", true, b3);
|
|
|
|
|
assertEquals("b4", false, b4);
|
|
|
|
|
assertEquals("b5", true, b5);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BinaryStructInputStream.read(struct, new byte[]{0b0110_1101});
|
|
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 13:15:49 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void customBinaryField(){
|
|
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@CustomBinaryField(index=1, serializer=ByteStringSerializer.class)
|
|
|
|
|
public String s1;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
|
|
|
|
assertEquals("1234", s1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BinaryStructInputStream.read(struct, new byte[]{0b0000_0001,0b0000_0010,0b0000_0011,0b0000_0100});
|
|
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
|
|
|
|
public static class ByteStringSerializer implements BinaryFieldSerializer<String>{
|
|
|
|
|
public String read(InputStream in, BinaryFieldData field) throws IOException {
|
|
|
|
|
String ret = "";
|
|
|
|
|
for (int c; (c=in.read()) > 0; )
|
|
|
|
|
ret += c;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
public void write(OutputStream out, String obj, BinaryFieldData field) throws IOException {}
|
|
|
|
|
}
|
2016-04-14 16:44:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void variableLengthField(){
|
|
|
|
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
|
|
|
|
@BinaryField(index=1, length=8)
|
|
|
|
|
public int i1;
|
|
|
|
|
@VariableLengthBinaryField(index=2, lengthField="i1")
|
|
|
|
|
public String s2;
|
|
|
|
|
|
|
|
|
|
public void assertObj(){
|
|
|
|
|
assertEquals(3, i1);
|
|
|
|
|
assertEquals("123", s2);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
byte[] data = "0123456".getBytes();
|
|
|
|
|
data[0] = 3;
|
|
|
|
|
BinaryStructInputStream.read(struct, data);
|
|
|
|
|
struct.assertObj();
|
|
|
|
|
}
|
2016-05-12 23:00:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2016-05-16 23:14:58 +02:00
|
|
|
public void shiftLeftBy(){
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(0, 1));
|
|
|
|
|
assertEquals(1, BinaryStructInputStream.shiftLeftBy(1, 1));
|
|
|
|
|
assertEquals(3, BinaryStructInputStream.shiftLeftBy(3, 1));
|
|
|
|
|
assertEquals(4, BinaryStructInputStream.shiftLeftBy(4, 1));
|
|
|
|
|
assertEquals(5, BinaryStructInputStream.shiftLeftBy(5, 1));
|
|
|
|
|
assertEquals(6, BinaryStructInputStream.shiftLeftBy(6, 1));
|
|
|
|
|
assertEquals(7, BinaryStructInputStream.shiftLeftBy(7, 1));
|
|
|
|
|
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(1, 2));
|
|
|
|
|
assertEquals(2, BinaryStructInputStream.shiftLeftBy(3, 2));
|
|
|
|
|
assertEquals(6, BinaryStructInputStream.shiftLeftBy(7, 2));
|
|
|
|
|
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(7, 8));
|
|
|
|
|
assertEquals(2, BinaryStructInputStream.shiftLeftBy(7, 6));
|
|
|
|
|
assertEquals(3, BinaryStructInputStream.shiftLeftBy(7, 5));
|
|
|
|
|
assertEquals(6, BinaryStructInputStream.shiftLeftBy(7, 2));
|
2016-05-12 23:00:28 +02:00
|
|
|
|
|
|
|
|
// Cross 1 byte border
|
2016-05-16 23:14:58 +02:00
|
|
|
assertEquals(7, BinaryStructInputStream.shiftLeftBy(0, 2));
|
|
|
|
|
assertEquals(6, BinaryStructInputStream.shiftLeftBy(1, 4));
|
|
|
|
|
assertEquals(4, BinaryStructInputStream.shiftLeftBy(3, 8));
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(3, 12));
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(7, 16));
|
2016-05-12 23:00:28 +02:00
|
|
|
|
|
|
|
|
// Cross 2 byte borders
|
2016-05-16 23:14:58 +02:00
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(7, 32));
|
|
|
|
|
assertEquals(7, BinaryStructInputStream.shiftLeftBy(7, 33));
|
|
|
|
|
assertEquals(6, BinaryStructInputStream.shiftLeftBy(7, 34));
|
|
|
|
|
assertEquals(5, BinaryStructInputStream.shiftLeftBy(7, 35));
|
|
|
|
|
assertEquals(4, BinaryStructInputStream.shiftLeftBy(7, 36));
|
|
|
|
|
assertEquals(3, BinaryStructInputStream.shiftLeftBy(7, 37));
|
|
|
|
|
assertEquals(2, BinaryStructInputStream.shiftLeftBy(7, 38));
|
|
|
|
|
assertEquals(1, BinaryStructInputStream.shiftLeftBy(7, 39));
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(7, 40));
|
|
|
|
|
assertEquals(7, BinaryStructInputStream.shiftLeftBy(7, 41));
|
|
|
|
|
assertEquals(6, BinaryStructInputStream.shiftLeftBy(6, 41));
|
|
|
|
|
assertEquals(5, BinaryStructInputStream.shiftLeftBy(5, 41));
|
|
|
|
|
assertEquals(4, BinaryStructInputStream.shiftLeftBy(4, 41));
|
|
|
|
|
assertEquals(3, BinaryStructInputStream.shiftLeftBy(3, 41));
|
|
|
|
|
assertEquals(2, BinaryStructInputStream.shiftLeftBy(2, 41));
|
|
|
|
|
assertEquals(1, BinaryStructInputStream.shiftLeftBy(1, 41));
|
|
|
|
|
assertEquals(0, BinaryStructInputStream.shiftLeftBy(7, 64));
|
2016-05-12 23:00:28 +02:00
|
|
|
}
|
2016-01-28 17:34:51 +01:00
|
|
|
}
|