@Test(dataProvider = "BCF2EncodingTestProviderSingletons") public void testBCF2EncodingSingletons(final List<BCF2TypedValue> toEncode) throws IOException { final byte[] record = encodeRecord(toEncode); decodeRecord(toEncode, record); }
@Test(dataProvider = "BCF2EncodingTestProviderBasicTypes") public void testBCF2BasicTypesWithObjectType(final List<BCF2TypedValue> toEncode) throws IOException { testBCF2BasicTypesWithEncodeMe(toEncode, new EncodeMe() { @Override public void encode(final BCF2Encoder encoder, final BCF2TypedValue tv) throws IOException { encoder.encodeTyped(tv.value, tv.type); } }); }
@Test(dataProvider = "BCF2EncodingTestProviderSequences", dependsOnMethods = "testBCF2EncodingTestProviderSequences") public void testReadAndSkipWithMultipleBlocks(final List<BCF2TypedValue> block) throws IOException { testReadAndSkipWithMultipleBlocks(block, forCombinations); testReadAndSkipWithMultipleBlocks(forCombinations, block); }
public void testReadAndSkipWithMultipleBlocks(final List<BCF2TypedValue> block1, final List<BCF2TypedValue> block2) throws IOException { final byte[] record1 = encodeRecord(block1); final byte[] record2 = encodeRecord(block2); // each record is individually good decodeRecord(block1, record1); decodeRecord(block2, record2); BCF2Decoder decoder = new BCF2Decoder(); // test setting decoder.setRecordBytes(record1); decodeRecord(block1, decoder); decoder.setRecordBytes(record2); decodeRecord(block2, decoder); // test combining the streams final byte[] combined = combineRecords(record1, record2); final List<BCF2TypedValue> combinedObjects = new ArrayList<BCF2TypedValue>(block1); combinedObjects.addAll(block2); // the combined bytes is the same as the combined objects InputStream stream = new ByteArrayInputStream(combined); decoder.readNextBlock(record1.length, stream); decodeRecord(block1, decoder); decoder.readNextBlock(record2.length, stream); decodeRecord(block2, decoder); // skipping the first block allows us to read the second block directly stream = new ByteArrayInputStream(combined); decoder.skipNextBlock(record1.length, stream); decoder.readNextBlock(record2.length, stream); decodeRecord(block2, decoder); }
private final void decodeRecord(final List<BCF2TypedValue> toEncode, final BCF2Decoder decoder) throws IOException { for ( final BCF2TypedValue tv : toEncode ) { Assert.assertFalse(decoder.blockIsFullyDecoded()); final Object decoded = decoder.decodeTypedValue(); myAssertEquals(tv, decoded); } Assert.assertTrue(decoder.blockIsFullyDecoded()); }
private final void decodeRecord(final List<BCF2TypedValue> toEncode, final byte[] record) throws IOException { decodeRecord(toEncode, new BCF2Decoder(record)); }
@Test(dataProvider = "BestIntTypeTests") public void determineBestEncoding(final List<Integer> ints, final BCF2Type expectedType) throws IOException { BCF2Encoder encoder = new BCF2Encoder(); Assert.assertEquals(BCF2Utils.determineIntegerType(ints), expectedType); Assert.assertEquals(BCF2Utils.determineIntegerType(toPrimitive(ints.toArray(new Integer[0]))), expectedType); }
@Test(dataProvider = "IntArrays") public void testIntArrays(final List<Integer> ints) throws IOException { final BCF2Encoder encoder = new BCF2Encoder(); encoder.encodeTyped(ints, BCF2Type.INT16); final BCF2Decoder decoder = new BCF2Decoder(encoder.getRecordBytes()); final byte typeDescriptor = decoder.readTypeDescriptor(); // read the int[] with the low-level version final int size = decoder.decodeNumberOfElements(typeDescriptor); final int[] decoded = decoder.decodeIntArray(typeDescriptor, size); if ( isMissing(ints) ) { // we expect that the result is null in this case Assert.assertNull(decoded, "Encoded all missing values -- expected null"); } else { // we expect at least some values to come back Assert.assertTrue(decoded.length > 0, "Must have at least 1 element for non-null encoded data"); // check corresponding values for ( int i = 0; i < ints.size(); i++ ) { final Integer expected = ints.get(i); if ( expected == null ) { Assert.assertTrue(decoded.length <= i, "we expect decoded to be truncated for missing values"); } else { Assert.assertTrue(decoded.length > i, "we expected at least " + i + " values in decoded array"); Assert.assertEquals(decoded[i], (int)expected); } } } }
@Test(dataProvider = "BCF2EncodingTestProviderBasicTypes") public void testBCF2EncodingVectors(final List<BCF2TypedValue> toEncode) throws IOException { for ( final BCF2TypedValue tv : toEncode ) { for ( final int length : Arrays.asList(2, 5, 10, 15, 20, 25) ) { BCF2Encoder encoder = new BCF2Encoder(); List<Object> expected = Collections.nCopies(length, tv.value); encoder.encodeTyped(expected, tv.type); BCF2Decoder decoder = new BCF2Decoder(encoder.getRecordBytes()); final Object decoded = decoder.decodeTypedValue(); Assert.assertTrue(decoded instanceof List); final List<Object> decodedList = (List<Object>)decoded; Assert.assertEquals(decodedList.size(), expected.size()); for ( Object decodedValue : decodedList ) myAssertEquals(tv, decodedValue); } } }
@Test(dataProvider = "BCF2EncodingTestProviderSequences", dependsOnMethods = "testBCF2EncodingSingletons") public void testBCF2EncodingTestProviderSequences(final List<BCF2TypedValue> toEncode) throws IOException { final byte[] record = encodeRecord(toEncode); decodeRecord(toEncode, record); }
public void testBCF2BasicTypesWithEncodeMe(final List<BCF2TypedValue> toEncode, final EncodeMe func) throws IOException { for ( final BCF2TypedValue tv : toEncode ) { BCF2Encoder encoder = new BCF2Encoder(); func.encode(encoder, tv); BCF2Decoder decoder = new BCF2Decoder(encoder.getRecordBytes()); final Object decoded = decoder.decodeTypedValue(); Assert.assertNotNull(decoded); Assert.assertFalse(decoded instanceof List); myAssertEquals(tv, decoded); } }
@Test(dataProvider = "BCF2EncodingTestProviderBasicTypes") public void testBCF2BasicTypesWithObjectNoType(final List<BCF2TypedValue> toEncode) throws IOException { testBCF2BasicTypesWithEncodeMe(toEncode, new EncodeMe() { @Override public void encode(final BCF2Encoder encoder, final BCF2TypedValue tv) throws IOException { encoder.encode(tv.value); } }); }
@Test(dataProvider = "BCF2EncodingTestProviderBasicTypes") public void testBCF2EncodingVectorsWithMissing(final List<BCF2TypedValue> toEncode) throws IOException { for ( final BCF2TypedValue tv : toEncode ) { if ( tv.type != BCF2Type.CHAR ) { for ( final int length : Arrays.asList(2, 5, 10, 15, 20, 25) ) { final byte td = BCF2Utils.encodeTypeDescriptor(1, tv.type); final BCF2Encoder encoder = new BCF2Encoder(); for ( int i = 0; i < length; i++ ) { encoder.encodeRawValue(i % 2 == 0 ? null : tv.value, tv.type); } final BCF2Decoder decoder = new BCF2Decoder(encoder.getRecordBytes()); for ( int i = 0; i < length; i++ ) { final Object decoded = decoder.decodeTypedValue(td); myAssertEquals(i % 2 == 0 ? new BCF2TypedValue(null, tv.type) : tv, decoded); } } } } }
@Test(dataProvider = "BCF2EncodingTestProviderBasicTypes") public void testBCF2BasicTypesWithStaticCalls(final List<BCF2TypedValue> toEncode) throws IOException { testBCF2BasicTypesWithEncodeMe(toEncode, new EncodeMe() { @Override public void encode(final BCF2Encoder encoder, final BCF2TypedValue tv) throws IOException { switch ( tv.type ) { case INT8: case INT16: case INT32: encoder.encodeTypedInt((Integer)tv.value, tv.type); break; case FLOAT: encoder.encodeTypedFloat((Double)tv.value); break; case CHAR: encoder.encodeTypedString((String)tv.value); break; } } }); }