|
| 1 | +package net.stef; |
| 2 | + |
| 3 | +import net.stef.codecs.Float64Decoder; |
| 4 | +import net.stef.codecs.Float64Encoder; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.ByteArrayOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | + |
| 13 | +public class Float64EncoderTest { |
| 14 | + // This sequence was failing before bug fix https://github.com/splunk/stef/issues/268 |
| 15 | + private double testVals[] = { |
| 16 | + 0.516459, |
| 17 | + 0.516459, |
| 18 | + 0.516459, |
| 19 | + 0.026014, |
| 20 | + 0.516459, |
| 21 | + 0.026014, |
| 22 | + 0.516459, |
| 23 | + 0.026014, |
| 24 | + 0.516459, |
| 25 | + 0.026014, |
| 26 | + 0.516459, |
| 27 | + 0.026014, |
| 28 | + 0.516459, |
| 29 | + 0.026014, |
| 30 | + 0.516459, |
| 31 | + 0.026014, |
| 32 | + 0.516459, |
| 33 | + 0.026014, |
| 34 | + 0.516459, |
| 35 | + 0.026014, |
| 36 | + 0.516459, |
| 37 | + 0.026014, |
| 38 | + 0.516459, |
| 39 | + 0.026014, |
| 40 | + 0.516459, |
| 41 | + 0.026014, |
| 42 | + 0.516459, |
| 43 | + 0.404796, |
| 44 | + 0.516459, |
| 45 | + 0.404796, |
| 46 | + 0.516459, |
| 47 | + 0.404796, |
| 48 | + 0.516459, |
| 49 | + }; |
| 50 | + |
| 51 | + @Test |
| 52 | + void testEncodeDecode() throws IOException { |
| 53 | + Float64Encoder encoder = new Float64Encoder(); |
| 54 | + WriteBufs writeBufs = new WriteBufs(); |
| 55 | + encoder.init(new SizeLimiter(), writeBufs.columns); |
| 56 | + for (int i = 0; i < testVals.length; i++) { |
| 57 | + encoder.encode(testVals[i]); |
| 58 | + } |
| 59 | + |
| 60 | + encoder.collectColumns(writeBufs.columns); |
| 61 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 62 | + writeBufs.writeTo(out); |
| 63 | + |
| 64 | + ReadBufs readBufs = new ReadBufs(); |
| 65 | + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
| 66 | + readBufs.readFrom(in); |
| 67 | + |
| 68 | + Float64Decoder decoder = new Float64Decoder(); |
| 69 | + decoder.init(readBufs.columns); |
| 70 | + decoder.continueDecoding(); |
| 71 | + for (int i = 0; i < testVals.length; i++) { |
| 72 | + double decodedVal = decoder.decode(); |
| 73 | + assertEquals(testVals[i], decodedVal, "decoded value at index " + i + " does not match"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testEncodeDecodeRandomSequence() throws IOException { |
| 79 | + final int N = 100000; |
| 80 | + final long seed = System.nanoTime();; |
| 81 | + java.util.Random rand = new java.util.Random(seed); |
| 82 | + Float64Encoder encoder = new Float64Encoder(); |
| 83 | + WriteBufs writeBufs = new WriteBufs(); |
| 84 | + encoder.init(new SizeLimiter(), writeBufs.columns); |
| 85 | + for (int i = 0; i < N; i++) { |
| 86 | + encoder.encode(rand.nextDouble()*rand.nextInt()); |
| 87 | + } |
| 88 | + encoder.collectColumns(writeBufs.columns); |
| 89 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 90 | + writeBufs.writeTo(out); |
| 91 | + |
| 92 | + ReadBufs readBufs = new ReadBufs(); |
| 93 | + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
| 94 | + readBufs.readFrom(in); |
| 95 | + |
| 96 | + Float64Decoder decoder = new Float64Decoder(); |
| 97 | + decoder.init(readBufs.columns); |
| 98 | + decoder.continueDecoding(); |
| 99 | + java.util.Random rand2 = new java.util.Random(seed); |
| 100 | + for (int i = 0; i < N; i++) { |
| 101 | + double expected = rand2.nextDouble()*rand2.nextInt(); |
| 102 | + double decoded = decoder.decode(); |
| 103 | + assertEquals(expected, decoded, "seed "+seed+", index " + i + " does not match"); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments