use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class OptimizedBinaryWriterLengthPatchingTest method testOptimizedWriteLengthPatching.
/**
* Tests that the internal implementation of binary {@link IonWriter}'s
* patching mechanism is correct.
*
* @see IonWriterSystemBinary
*/
@Test
public void testOptimizedWriteLengthPatching() throws Exception {
iw = makeWriter();
// The expected datagram - as values are copied to the IonWriter, we
// book-keep the values written so as to do an equality check at the end.
IonDatagram expected = system().newDatagram();
// === top level ===
writeAndAddTypeDescLengthVariants(expected);
// === nested list ===
iw.stepIn(IonType.LIST);
IonList expectedNestedList = expected.add().newEmptyList();
{
writeAndAddTypeDescLengthVariants(expectedNestedList);
}
iw.stepOut();
// === nested sexp ===
iw.stepIn(IonType.SEXP);
IonSexp expectedNestedSexp = expected.add().newEmptySexp();
{
writeAndAddTypeDescLengthVariants(expectedNestedSexp);
}
iw.stepOut();
// === nested struct ===
iw.stepIn(IonType.STRUCT);
IonStruct expectedNestedStruct = expected.add().newEmptyStruct();
{
writeAndAddTypeDescLengthVariants(expectedNestedStruct);
}
iw.stepOut();
// === deeply nested list ===
iw.stepIn(IonType.LIST);
IonList nestedList1 = expected.add().newEmptyList();
{
writeAndAddTypeDescLengthVariants(nestedList1);
iw.stepIn(IonType.LIST);
{
IonList nestedList2 = nestedList1.add().newEmptyList();
writeAndAddTypeDescLengthVariants(nestedList2);
}
iw.stepOut();
}
iw.stepOut();
IonDatagram actual = loader().load(outputByteArray());
assertIonEquals(expected, actual);
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class OptimizedBinaryWriterSymbolTableTest method testOptimizedWriteValue_InterspersedReaderLSTs.
/**
* Reader's source contains interspersed LSTs.
* TODO amzn/ion-java/issues/39 Investigate allowing a config. option to copy reader's LST
* over to the writer.
*/
@Test
public void testOptimizedWriteValue_InterspersedReaderLSTs() throws Exception {
String readerLST1 = printLocalSymtab("a", "aa");
String readerLST2 = printLocalSymtab("b", "bb");
byte[] source = encode(readerLST1 + "a aa " + readerLST2 + "b bb");
ir = makeReaderProxy(source);
iw = makeWriterWithLocalSymtab("a", "aa");
// writer's symtab same as reader's - optimize
// a
checkWriteValueWithCompatibleSymtab();
// aa
checkWriteValueWithCompatibleSymtab();
// writer's symtab diff. from reader's - no optimize
// b
checkWriteValueWithIncompatibleSymtab();
// bb
checkWriteValueWithIncompatibleSymtab();
iw.close();
IonDatagram expected = loader().load("a aa b bb");
IonDatagram actual = loader().load(outputByteArray());
assertIonEquals(expected, actual);
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class IonManagedBinaryWriterTest method testLocalSymbolTableAppend.
@Test
public void testLocalSymbolTableAppend() throws Exception {
writer.writeSymbol("taco");
writer.flush();
writer.writeSymbol("burrito");
writer.finish();
IonReader reader = system().newReader(writer.getBytes());
reader.next();
assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
assertEquals(reader.getSymbolTable().findSymbol("burrito"), lstAppendMode.isEnabled() ? -1 : 16);
reader.next();
assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
assertEquals(reader.getSymbolTable().findSymbol("burrito"), 16);
assertNull(reader.next());
IonDatagram dg = system().getLoader().load(writer.getBytes());
assertEquals(2, dg.size());
assertEquals("taco", ((IonSymbol) dg.get(0)).stringValue());
assertEquals("burrito", ((IonSymbol) dg.get(1)).stringValue());
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class IonManagedBinaryWriterTest method testManuallyWriteLSTAppendWithImportsFirst.
@Test
public void testManuallyWriteLSTAppendWithImportsFirst() throws Exception {
writer.writeSymbol("taco");
writer.addTypeAnnotation("$ion_symbol_table");
writer.stepIn(IonType.STRUCT);
writer.setFieldName("imports");
writer.writeSymbol("$ion_symbol_table");
writer.setFieldName("symbols");
writer.stepIn(IonType.LIST);
writer.writeString("burrito");
writer.stepOut();
writer.stepOut();
writer.writeSymbol("burrito");
writer.finish();
IonReader reader = system().newReader(writer.getBytes());
reader.next();
assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
assertEquals(reader.getSymbolTable().findSymbol("burrito"), lstAppendMode.isEnabled() ? -1 : 16);
reader.next();
assertEquals(reader.getSymbolTable().findSymbol("taco"), 15);
assertEquals(reader.getSymbolTable().findSymbol("burrito"), 16);
assertNull(reader.next());
IonDatagram dg = system().getLoader().load(writer.getBytes());
assertEquals(2, dg.size());
assertEquals("taco", ((IonSymbol) dg.get(0)).stringValue());
assertEquals("burrito", ((IonSymbol) dg.get(1)).stringValue());
}
use of com.amazon.ion.IonDatagram in project ion-java by amzn.
the class IonManagedBinaryWriterTest method testNoNewSymbolsAfterFlush.
@Test
public void testNoNewSymbolsAfterFlush() throws Exception {
writer.writeSymbol("taco");
writer.flush();
writer.writeInt(123);
writer.finish();
IonDatagram dg = system().getLoader().load(writer.getBytes());
// Should be IVM SYMTAB taco 123
assertEquals(4, dg.systemSize());
assertEquals("$ion_symbol_table", ((IonStruct) dg.systemGet(1)).getTypeAnnotations()[0]);
assertEquals("taco", ((IonSymbol) dg.systemGet(2)).stringValue());
assertEquals(123, ((IonInt) dg.systemGet(3)).intValue());
}
Aggregations