Mercurial > stress-tester
changeset 692:a27658c9e2e3
in progress
author | Devel 1 |
---|---|
date | Mon, 27 Nov 2017 10:55:36 +0100 |
parents | 9ccc2452b92c |
children | 3b6dbbe80eff |
files | stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java stress-tester/src/test/java/com/passus/st/reader/nc/option/VarOptionCoderTest.java |
diffstat | 3 files changed, 62 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java Mon Nov 27 10:49:54 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java Mon Nov 27 10:55:36 2017 +0100 @@ -65,4 +65,10 @@ return NC_HELPER.readStringNullTerminated(buff); } } + + public static final IntCoder INT_CODER = new IntCoder(); + public static final IntVlcCoder INT_VLC_CODER = new IntVlcCoder(); + public static final ByteStringNTCoder BYTE_STRING_NT_CODER = new ByteStringNTCoder(); + public static final StringNTCoder STRING_NT_CODER = new StringNTCoder(); + }
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java Mon Nov 27 10:49:54 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java Mon Nov 27 10:55:36 2017 +0100 @@ -14,17 +14,15 @@ @Test public void testIntCoder() { - ValueCoders.IntCoder c = new ValueCoders.IntCoder(); ByteBuff buff = new HeapByteBuff(8); - c.encode(123, buff); - + ValueCoders.INT_CODER.encode(123, buff); byte[] expected = {0, 0, 0, 123, 0, 0, 0, 0}; assertEquals(0, buff.startIndex()); assertEquals(4, buff.endIndex()); assertEquals(expected, buff.buffer()); - int value = c.decode(buff); + int value = ValueCoders.INT_CODER.decode(buff); assertEquals(4, buff.startIndex()); assertEquals(4, buff.endIndex()); assertEquals(123, value); @@ -32,17 +30,15 @@ @Test public void testIntVlcCoder() { - ValueCoders.IntVlcCoder c = new ValueCoders.IntVlcCoder(); ByteBuff buff = new HeapByteBuff(8); - c.encode(123, buff); - + ValueCoders.INT_VLC_CODER.encode(123, buff); byte[] expected = {123, 0, 0, 0, 0, 0, 0, 0}; assertEquals(0, buff.startIndex()); assertEquals(1, buff.endIndex()); assertEquals(expected, buff.buffer()); - int value = c.decode(buff); + int value = ValueCoders.INT_VLC_CODER.decode(buff); assertEquals(1, buff.startIndex()); assertEquals(1, buff.endIndex()); assertEquals(123, value); @@ -50,18 +46,16 @@ @Test public void testByteStringNTCoder() { - ValueCoders.ByteStringNTCoder c = new ValueCoders.ByteStringNTCoder(); ByteBuff buff = new HeapByteBuff(8); ByteString bs = ByteString.create("12\n45"); - c.encode(bs, buff); - + ValueCoders.BYTE_STRING_NT_CODER.encode(bs, buff); byte[] expected = {'1', '2', '\n', '4', '5', 0, 0, 0}; assertEquals(0, buff.startIndex()); assertEquals(6, buff.endIndex()); assertEquals(expected, buff.buffer()); - ByteString value = c.decode(buff); + ByteString value = ValueCoders.BYTE_STRING_NT_CODER.decode(buff); assertEquals(6, buff.startIndex()); assertEquals(6, buff.endIndex()); assertEquals(bs, value); @@ -69,17 +63,15 @@ @Test public void testStringNTCoder() { - ValueCoders.StringNTCoder c = new ValueCoders.StringNTCoder(); ByteBuff buff = new HeapByteBuff(8); - c.encode("ąć", buff); - + ValueCoders.STRING_NT_CODER.encode("ąć", buff); byte[] expected = {(byte) 0xc4, (byte) 0x85, (byte) 0xc4, (byte) 0x87, 0, 0, 0, 0}; assertEquals(0, buff.startIndex()); assertEquals(5, buff.endIndex()); assertEquals(expected, buff.buffer()); - String value = c.decode(buff); + String value = ValueCoders.STRING_NT_CODER.decode(buff); assertEquals(5, buff.startIndex()); assertEquals(5, buff.endIndex()); assertEquals("ąć", value);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/VarOptionCoderTest.java Mon Nov 27 10:55:36 2017 +0100 @@ -0,0 +1,48 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.HeapByteBuff; +import static org.testng.AssertJUnit.*; +import org.testng.annotations.Test; + +/** + * + * @author mikolaj.podbielski + */ +public class VarOptionCoderTest { + + VarOptionCoder coder = new VarOptionCoder(); + + @Test + public void testEncode() { + VarOption vo = new VarOption(); + vo.setName("user"); // 5B + vo.setStartOffset(120); // 2B + vo.setEndOffset(140); // 2B + + HeapByteBuff dst = new HeapByteBuff(); + coder.encode(vo, dst); + + byte[] expected = {'u', 's', 'e', 'r', 0, 0, 120, 0, (byte) 140}; + assertEquals(0, dst.startIndex()); + assertEquals(9, dst.endIndex()); + assertEquals(expected, dst.toArray()); + } + + @Test + public void testDecode() { + byte[] zeros = new byte[4]; + byte[] expected = {'u', 's', 'e', 'r', 0, 0, 120, 0, (byte) 140}; + + HeapByteBuff src = new HeapByteBuff(); + src.append(zeros); + src.append(expected); + src.append(zeros); + src.skipBytes(zeros.length); + + VarOption vo = coder.decode(src); + + assertEquals("user", vo.getName()); + assertEquals(120, vo.getStartOffset()); + assertEquals(140, vo.getEndOffset()); + } +}