Mercurial > stress-tester
changeset 688:a10f49da9e45
in progress
author | Devel 1 |
---|---|
date | Fri, 24 Nov 2017 16:12:54 +0100 |
parents | ca40b6492c9f |
children | 70cfe98e9556 |
files | stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionCoder.java stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoder.java stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java |
diffstat | 7 files changed, 186 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionCoder.java Fri Nov 24 15:49:56 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -package com.passus.st.reader.nc.option; - -import com.passus.data.ByteBuff; -import com.passus.data.ByteString; -import com.passus.data.DataHelper; -import com.passus.st.reader.nc.NcDataHelper; - -/** - * - * @author mikolaj.podbielski - * @param <T> - */ -public interface OptionCoder<T> { - - public static DataHelper HELPER = DataHelper.BIG_ENDIAN; - public static NcDataHelper NC_HELPER = NcDataHelper.getInstance(); - - public void encode(T value, ByteBuff buff); - - public Object decode(ByteBuff buff); - - public static class IntCoder implements OptionCoder<Integer> { - - @Override - public void encode(Integer value, ByteBuff buff) { - HELPER.writeInt4(buff, value); - } - - @Override - public Object decode(ByteBuff buff) { - return HELPER.readInt4(buff); - } - } - - public static class IntVlcCoder implements OptionCoder<Integer> { - - @Override - public void encode(Integer value, ByteBuff buff) { - NC_HELPER.writeIntVLC(value, buff); - } - - @Override - public Object decode(ByteBuff buff) { - return NC_HELPER.readIntVLC(buff); - } - } - - public static class ByteStringNTCoder implements OptionCoder<ByteString> { - - @Override - public void encode(ByteString value, ByteBuff buff) { - NC_HELPER.writeByteStringNullTerminated(value, buff); - } - - @Override - public Object decode(ByteBuff buff) { - return NC_HELPER.readByteStringNullTerminated(buff); - } - } - - public static class StringNTCoder implements OptionCoder<String> { - - @Override - public void encode(String value, ByteBuff buff) { - NC_HELPER.writeStringNullTerminated(value, buff); - } - - @Override - public Object decode(ByteBuff buff) { - return NC_HELPER.readStringNullTerminated(buff); - } - } -}
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java Fri Nov 24 15:49:56 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/Options.java Fri Nov 24 16:12:54 2017 +0100 @@ -13,7 +13,7 @@ private static final Map<String, Byte> OPTION_CODES = new HashMap<>(); - private static final Map<String, OptionCoder> OPTION_CODERS = new HashMap<>(); + private static final Map<String, ValueCoder> VALUE_CODERS = new HashMap<>(); static void addOption(String name, byte code) { if (code == 0) { @@ -30,9 +30,13 @@ OPTION_CODES.put(name, code); } - static void addOption(String name, byte code, OptionCoder coder) { + static void addCoder(String name, ValueCoder coder) { + VALUE_CODERS.put(name, coder); + } + + static void addOptionAndCoder(String name, byte code, ValueCoder coder) { addOption(name, code); - OPTION_CODERS.put(name, coder); + addCoder(name, coder); } public static String getOptionName(byte code) { @@ -44,8 +48,8 @@ return code == null ? 0 : code; } - public static OptionCoder getOptionCoder(String name) { - return OPTION_CODERS.get(name); + public static ValueCoder getValueCoder(String name) { + return VALUE_CODERS.get(name); } public static final String OPTION_VARS = "vars"; @@ -53,7 +57,7 @@ public static final String OPTION_Y = "opt_y"; static { - addOption(OPTION_VARS, (byte) 10, new VarOptionCoder()); + addOptionAndCoder(OPTION_VARS, (byte) 10, new VarOptionCoder()); addOption(OPTION_X, (byte) 101); addOption(OPTION_Y, (byte) 102); }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java Fri Nov 24 15:49:56 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/OptionsReaderWriter.java Fri Nov 24 16:12:54 2017 +0100 @@ -19,7 +19,7 @@ for (Option option : options) { String name = option.getName(); byte code = Options.getOptionCode(name); - OptionCoder coder = Options.getOptionCoder(name); + ValueCoder coder = Options.getValueCoder(name); buff.append(code); if (code == 0) { @@ -41,7 +41,7 @@ } else { name = Options.getOptionName(code); } - OptionCoder coder = Options.getOptionCoder(name); + ValueCoder coder = Options.getValueCoder(name); Object value = coder.decode(buff); options.add(new Option(name, value));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoder.java Fri Nov 24 16:12:54 2017 +0100 @@ -0,0 +1,16 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; + +/** + * + * @author mikolaj.podbielski + * @param <T> + */ +public interface ValueCoder<T> { + + public void encode(T value, ByteBuff buff); + + public T decode(ByteBuff buff); + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoders.java Fri Nov 24 16:12:54 2017 +0100 @@ -0,0 +1,68 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; +import com.passus.data.ByteString; +import com.passus.data.DataHelper; +import com.passus.st.reader.nc.NcDataHelper; + +/** + * + * @author mikolaj.podbielski + */ +public class ValueCoders { + + public static DataHelper HELPER = DataHelper.BIG_ENDIAN; + public static NcDataHelper NC_HELPER = NcDataHelper.getInstance(); + + public static class IntCoder implements ValueCoder<Integer> { + + @Override + public void encode(Integer value, ByteBuff buff) { + HELPER.writeInt4(buff, value); + } + + @Override + public Integer decode(ByteBuff buff) { + return HELPER.readInt4(buff); + } + } + + public static class IntVlcCoder implements ValueCoder<Integer> { + + @Override + public void encode(Integer value, ByteBuff buff) { + NC_HELPER.writeIntVLC(value, buff); + } + + @Override + public Integer decode(ByteBuff buff) { + return NC_HELPER.readIntVLC(buff); + } + } + + public static class ByteStringNTCoder implements ValueCoder<ByteString> { + + @Override + public void encode(ByteString value, ByteBuff buff) { + NC_HELPER.writeByteStringNullTerminated(value, buff); + } + + @Override + public ByteString decode(ByteBuff buff) { + return NC_HELPER.readByteStringNullTerminated(buff); + } + } + + public static class StringNTCoder implements ValueCoder<String> { + + @Override + public void encode(String value, ByteBuff buff) { + NC_HELPER.writeStringNullTerminated(value, buff); + } + + @Override + public String decode(ByteBuff buff) { + return NC_HELPER.readStringNullTerminated(buff); + } + } +}
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java Fri Nov 24 15:49:56 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/VarOptionCoder.java Fri Nov 24 16:12:54 2017 +0100 @@ -6,7 +6,7 @@ * * @author mikolaj.podbielski */ -public class VarOptionCoder implements OptionCoder<VarOption> { +public class VarOptionCoder implements ValueCoder<VarOption> { @Override public void encode(VarOption value, ByteBuff buff) { @@ -16,7 +16,7 @@ } @Override - public Object decode(ByteBuff buff) { + public VarOption decode(ByteBuff buff) { VarOption value = new VarOption(); value.setName(NC_HELPER.readStringNullTerminated(buff)); value.setStartOffset(NC_HELPER.readLongVLC(buff));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/ValueCodersTest.java Fri Nov 24 16:12:54 2017 +0100 @@ -0,0 +1,88 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; +import com.passus.data.ByteString; +import com.passus.data.HeapByteBuff; +import static org.testng.AssertJUnit.*; +import org.testng.annotations.Test; + +/** + * + * @author mikolaj.podbielski + */ +public class ValueCodersTest { + + @Test + public void testIntCoder() { + ValueCoders.IntCoder c = new ValueCoders.IntCoder(); + ByteBuff buff = new HeapByteBuff(8); + + c.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); + assertEquals(4, buff.startIndex()); + assertEquals(4, buff.endIndex()); + assertEquals(123, value); + } + + @Test + public void testIntVlcCoder() { + ValueCoders.IntVlcCoder c = new ValueCoders.IntVlcCoder(); + ByteBuff buff = new HeapByteBuff(8); + + c.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); + assertEquals(1, buff.startIndex()); + assertEquals(1, buff.endIndex()); + assertEquals(123, value); + } + + @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); + + 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); + assertEquals(6, buff.startIndex()); + assertEquals(6, buff.endIndex()); + assertEquals(bs, value); + } + + @Test + public void testStringNTCoder() { + ValueCoders.StringNTCoder c = new ValueCoders.StringNTCoder(); + ByteBuff buff = new HeapByteBuff(8); + + c.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); + assertEquals(5, buff.startIndex()); + assertEquals(5, buff.endIndex()); + assertEquals("ąć", value); + } + +}