### Question:
GorillaDecoder extends Decoder { @Override public boolean hasNext(ByteBuffer buffer) throws IOException { if (buffer.remaining() > 0 || !isEnd) { return true; } return false; } GorillaDecoder(); @Override void reset(); @Override boolean hasNext(ByteBuffer buffer);  }### Answer:
@Test public void testNegativeNumber() throws IOException { Encoder encoder = new SinglePrecisionEncoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); float value = -7.101f; encoder.encode(value, baos); encoder.encode(value - 2, baos); encoder.encode(value - 4, baos); encoder.flush(baos); encoder.encode(value, baos); encoder.encode(value - 2, baos); encoder.encode(value - 4, baos); encoder.flush(baos); ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); for (int i = 0; i < 2; i++) { Decoder decoder = new SinglePrecisionDecoder(); if (decoder.hasNext(buffer)) { assertEquals(value, decoder.readFloat(buffer), delta); } if (decoder.hasNext(buffer)) { assertEquals(value - 2, decoder.readFloat(buffer), delta); } if (decoder.hasNext(buffer)) { assertEquals(value - 4, decoder.readFloat(buffer), delta); } } }
@Test public void testZeroNumber() throws IOException { Encoder encoder = new DoublePrecisionEncoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); double value = 0f; encoder.encode(value, baos); encoder.encode(value, baos); encoder.encode(value, baos); encoder.flush(baos); encoder.encode(value, baos); encoder.encode(value, baos); encoder.encode(value, baos); encoder.flush(baos); ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); for (int i = 0; i < 2; i++) { Decoder decoder = new DoublePrecisionDecoder(); if (decoder.hasNext(buffer)) { assertEquals(value, decoder.readDouble(buffer), delta); } if (decoder.hasNext(buffer)) { assertEquals(value, decoder.readDouble(buffer), delta); } if (decoder.hasNext(buffer)) { assertEquals(value, decoder.readDouble(buffer), delta); } } }
@Test public void testFloat() throws IOException { Encoder encoder = new SinglePrecisionEncoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); float value = 7.101f; int num = 10000; for (int i = 0; i < num; i++) { encoder.encode(value + 2 * i, baos); } encoder.flush(baos); ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); Decoder decoder = new SinglePrecisionDecoder(); for (int i = 0; i < num; i++) { if (decoder.hasNext(buffer)) { assertEquals(value + 2 * i, decoder.readFloat(buffer), delta); continue; } fail(); } }
@Test public void testDouble() throws IOException { Encoder encoder = new DoublePrecisionEncoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); double value = 7.101f; int num = 1000; for (int i = 0; i < num; i++) { encoder.encode(value + 2 * i, baos); } encoder.flush(baos); ByteBuffer buffer = ByteBuffer.wrap(baos.toByteArray()); Decoder decoder = new DoublePrecisionDecoder(); for (int i = 0; i < num; i++) { if (decoder.hasNext(buffer)) { assertEquals(value + 2 * i, decoder.readDouble(buffer), delta); continue; } fail(); } }