diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-08-30 16:51:11 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-08-30 16:51:11 +0200 |
commit | 99531f22f32354317cc3dff9e5d48730270aafa7 (patch) | |
tree | bb33ab9c9f39eb4fd764a51f3a9b143bc5928a87 /vespajlib/src/test | |
parent | beea81233a2b9334aac10ed32d14ad9ad8f73d9a (diff) |
Avoid copying data just to compress them when it is not necessary.
Diffstat (limited to 'vespajlib/src/test')
-rw-r--r-- | vespajlib/src/test/java/com/yahoo/slime/BinaryFormatTestCase.java | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/vespajlib/src/test/java/com/yahoo/slime/BinaryFormatTestCase.java b/vespajlib/src/test/java/com/yahoo/slime/BinaryFormatTestCase.java index 7cf4bddaa01..cd67e0653dd 100644 --- a/vespajlib/src/test/java/com/yahoo/slime/BinaryFormatTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/slime/BinaryFormatTestCase.java @@ -1,35 +1,42 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.slime; +import com.yahoo.compress.CompressionType; +import com.yahoo.compress.Compressor; import org.junit.Test; -import static org.junit.Assert.assertTrue; +import static com.yahoo.slime.BinaryFormat.decode_double; +import static com.yahoo.slime.BinaryFormat.decode_meta; +import static com.yahoo.slime.BinaryFormat.decode_type; +import static com.yahoo.slime.BinaryFormat.decode_zigzag; +import static com.yahoo.slime.BinaryFormat.encode_double; +import static com.yahoo.slime.BinaryFormat.encode_type_and_meta; +import static com.yahoo.slime.BinaryFormat.encode_zigzag; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.hamcrest.CoreMatchers.*; -import static com.yahoo.slime.BinaryFormat.*; public class BinaryFormatTestCase { static final int TYPE_LIMIT = 8; static final int META_LIMIT = 32; - static final int MAX_CMPR_SIZE = 10; static final int MAX_NUM_SIZE = 8; - static final byte enc_t_and_sz(Type t, int size) { + static byte enc_t_and_sz(Type t, int size) { assert size <= 30; return encode_type_and_meta(t.ID, size + 1); } - static final byte enc_t_and_m(Type t, int meta) { + static byte enc_t_and_m(Type t, int meta) { assert meta <= 31; return encode_type_and_meta(t.ID, meta); } void verify_cmpr_long(long value, byte[] expect) { - BinaryEncoder bof = new BinaryEncoder(); + BufferedOutput output = new BufferedOutput(); + BinaryEncoder bof = new BinaryEncoder(output); bof.encode_cmpr_long(value); - byte[] actual = bof.out.toArray(); + byte[] actual = output.toArray(); assertThat(actual, is(expect)); BinaryDecoder bif = new BinaryDecoder(); @@ -41,6 +48,10 @@ public class BinaryFormatTestCase { // was verifyBasic void verifyEncoding(Slime slime, byte[] expect) { assertThat(BinaryFormat.encode(slime), is(expect)); + Compressor compressor = new Compressor(CompressionType.LZ4, 3, 2, 0); + Compressor.Compression result = BinaryFormat.encode_and_compress(slime, compressor); + byte [] decompressed = compressor.decompress(result); + assertThat(decompressed, is(expect)); verifyMultiEncode(expect); } @@ -223,13 +234,11 @@ public class BinaryFormatTestCase { expect.put(encode_type_and_meta((int)type, (int)(size +1))); } else { expect.put(type); - BinaryEncoder encoder = new BinaryEncoder(); - encoder.out = expect; + BinaryEncoder encoder = new BinaryEncoder(expect); encoder.encode_cmpr_long(size); } { - BinaryEncoder encoder = new BinaryEncoder(); - encoder.out = actual; + BinaryEncoder encoder = new BinaryEncoder(actual); encoder.write_type_and_size(type, size); } assertThat(actual.toArray(), is(expect.toArray())); @@ -273,14 +282,14 @@ public class BinaryFormatTestCase { byte[] expect = expbuf.toArray(); // test output: - BinaryEncoder bof = new BinaryEncoder(); - bof.out = new BufferedOutput(); + BufferedOutput output = new BufferedOutput(); + BinaryEncoder bof = new BinaryEncoder(output); if (hi != 0) { bof.write_type_and_bytes_be(type, bits); } else { bof.write_type_and_bytes_le(type, bits); } - byte[] actual = bof.out.toArray(); + byte[] actual = output.toArray(); assertThat(actual, is(expect)); // test input: @@ -530,9 +539,8 @@ public class BinaryFormatTestCase { 2, enc_t_and_sz(Type.DATA, 4), // f 'd', 'a', 't', 'a' }; - Slime slime = new Slime(); BinaryDecoder decoder = new BinaryDecoder(); - slime = decoder.decode(data); + Slime slime = decoder.decode(data); int consumed = decoder.in.getConsumedSize(); assertThat(consumed, is(data.length)); Cursor c = slime.get(); |