public void FlushAfterFinish() { using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); writer.SetFieldName("key"); writer.WriteString("value"); writer.StepOut(); writer.Finish(); var size = _memoryStream.Length; writer.Flush(); Assert.AreEqual(size + BinaryConstants.BinaryVersionMarkerLength, _memoryStream.Length); _memoryStream.Seek(0, SeekOrigin.Begin); var reader = new UserBinaryReader(_memoryStream); Assert.AreEqual(IonType.Struct, reader.MoveNext()); reader.StepIn(); Assert.AreEqual(IonType.String, reader.MoveNext()); Assert.AreEqual("key", reader.CurrentFieldName); Assert.AreEqual("value", reader.StringValue()); Assert.AreEqual(IonType.None, reader.MoveNext()); reader.StepOut(); //movenext() should skip over bvm Assert.AreEqual(IonType.None, reader.MoveNext()); } }
public void WriteStructWithSingleBlob(int blobSize) { var blob = new byte[blobSize]; new Random().NextBytes(blob); using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); writer.SetFieldName("blob"); writer.WriteBlob(blob); writer.StepOut(); writer.Flush(); } var reader = new UserBinaryReader(new MemoryStream(_memoryStream.GetWrittenBuffer())); reader.MoveNext(); reader.StepIn(); Assert.AreEqual(IonType.Blob, reader.MoveNext()); var size = reader.GetLobByteSize(); Assert.AreEqual(blobSize, size); var readBlob = new byte[size]; reader.GetBytes(readBlob); Assert.IsTrue(blob.SequenceEqual(readBlob)); }
public void MultipleFlushes_SameOutput(int count) { using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); writer.SetFieldName("key"); writer.WriteString("value"); writer.StepOut(); writer.Flush(); var size = _memoryStream.Length; for (var i = 0; i < count; i++) { writer.Flush(); Assert.AreEqual(size, _memoryStream.Length); } writer.Finish(); Assert.AreEqual(size, _memoryStream.Length); _memoryStream.Seek(0, SeekOrigin.Begin); var reader = new UserBinaryReader(_memoryStream); Assert.AreEqual(IonType.Struct, reader.MoveNext()); reader.StepIn(); Assert.AreEqual(IonType.String, reader.MoveNext()); Assert.AreEqual("key", reader.CurrentFieldName); Assert.AreEqual("value", reader.StringValue()); Assert.AreEqual(IonType.None, reader.MoveNext()); reader.StepOut(); Assert.AreEqual(IonType.None, reader.MoveNext()); } }
public void WriteObjectWithAnnotations(int annotationCount) { using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); writer.SetFieldName("FieldName"); for (var i = 0; i < annotationCount; i++) { writer.AddTypeAnnotation($"annot_{i}"); } writer.WriteString("FieldValue"); writer.StepOut(); writer.Flush(); } var reader = new UserBinaryReader(new MemoryStream(_memoryStream.GetWrittenBuffer())); reader.MoveNext(); reader.StepIn(); reader.MoveNext(); //load the value reader.StringValue(); var annotations = reader.GetTypeAnnotations().ToList(); for (var i = 0; i < annotationCount; i++) { Assert.IsTrue(annotations.Any(s => s.Text == $"annot_{i}")); } }
public void WriteLayersDeep(int depth) { List <(string key, object value)> kvps; using (var writer = new ManagedBinaryWriter(_memoryStream, Symbols.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); for (var i = 0; i < depth - 1; i++) { writer.SetFieldName($"layer{i}"); writer.StepIn(IonType.Struct); } kvps = WriteFlat(writer); for (var i = 0; i < depth; i++) { writer.StepOut(); } writer.Flush(); } var reader = new UserBinaryReader(new MemoryStream(_memoryStream.GetWrittenBuffer())); for (var i = 0; i < depth - 1; i++) { Console.WriteLine(i); Assert.AreEqual(IonType.Struct, reader.MoveNext()); Console.WriteLine(reader.CurrentFieldName); reader.StepIn(); } ReadUtils.AssertFlatStruct(reader, kvps); }
public void WriteObjectWithAnnotations(int annotationCount) { using (var stream = new MemoryStream()) { IIonWriter writer; using (writer = new ManagedBinaryWriter(BinaryConstants.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); writer.SetFieldName("FieldName"); for (var i = 0; i < annotationCount; i++) { writer.AddTypeAnnotation($"annot_{i}"); } writer.WriteString("FieldValue"); writer.StepOut(); writer.Flush(stream); } var annotReader = new SaveAnnotationsReaderRoutine(); var reader = new UserBinaryReader(new MemoryStream(stream.ToArray()), annotReader); reader.MoveNext(); reader.StepIn(); reader.MoveNext(); //load the value reader.StringValue(); for (var i = 0; i < annotationCount; i++) { Assert.IsTrue(annotReader.Symbols.Contains($"annot_{i}")); } } }
public void WriteNulls() { using (var stream = new MemoryStream()) { IIonWriter writer; using (writer = new ManagedBinaryWriter(BinaryConstants.EmptySymbolTablesArray)) { writer.StepIn(IonType.Struct); foreach (var iType in Enum.GetValues(typeof(IonType))) { if ((IonType)iType == IonType.Datagram || (IonType)iType == IonType.None) { continue; } var name = Enum.GetName(typeof(IonType), iType); writer.SetFieldName($"null_{name}"); writer.WriteNull((IonType)iType); } writer.StepOut(); writer.Flush(stream); } var reader = new UserBinaryReader(new MemoryStream(stream.ToArray())); reader.MoveNext(); reader.StepIn(); foreach (var iType in Enum.GetValues(typeof(IonType))) { if ((IonType)iType == IonType.Datagram || (IonType)iType == IonType.None) { continue; } var name = Enum.GetName(typeof(IonType), iType); Assert.AreEqual((IonType)iType, reader.MoveNext()); Assert.AreEqual($"null_{name}", reader.CurrentFieldName); Assert.IsTrue(reader.CurrentIsNull); } reader.StepOut(); } }
public void Run(string[] args) { var outputStream = new MemoryStream(); var writer = new ManagedBinaryWriter(new ISymbolTable[0]); writer.StepIn(IonType.Struct); writer.SetFieldName("yes"); writer.WriteBool(true); writer.SetFieldName("strings"); writer.WriteString("abcd def adsd dasdas tiếng việt 😂"); writer.SetFieldName("number_struct"); writer.StepIn(IonType.Struct); writer.SetFieldName("number"); writer.WriteInt(int.MaxValue / 2); writer.StepOut(); writer.SetFieldName("float_min"); writer.WriteFloat(float.MinValue); writer.SetFieldName("float_rand"); writer.WriteFloat(float.MaxValue / 2); writer.SetFieldName("double_max"); writer.WriteFloat(double.MaxValue); writer.SetFieldName("double_rand"); writer.WriteFloat(3.12345678901); writer.StepOut(); writer.WriteInt(int.MaxValue); writer.Flush(outputStream); writer.Dispose(); var bytes = outputStream.ToArray(); Console.WriteLine(bytes.Length); Console.WriteLine(string.Join(" ", bytes.Select(b => $"{b:X2}"))); var reader = new UserBinaryReader(new MemoryStream(bytes)); reader.MoveNext(); Console.WriteLine(reader.CurrentType); reader.StepIn(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.BoolValue()); Console.WriteLine(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.StringValue()); Console.WriteLine(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); reader.StepIn(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.IntValue()); reader.StepOut(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.DoubleValue()); Console.WriteLine(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.DoubleValue()); Console.WriteLine(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.DoubleValue()); Console.WriteLine(); reader.MoveNext(); Console.WriteLine(reader.CurrentFieldName); Console.WriteLine(reader.CurrentType); Console.WriteLine(reader.DoubleValue()); Console.WriteLine(); }
public void WriterReader() { var catalog = new SimpleCatalog(); var table1 = SharedSymbolTable.NewSharedSymbolTable("table1", 1, null, new[] { "s1", "s2" }); var table2 = SharedSymbolTable.NewSharedSymbolTable("table2", 1, null, new[] { "s3", "s4" }); catalog.PutTable(table1); catalog.PutTable(table2); var stream = new MemoryStream(); byte[] output; using (var binWriter = new ManagedBinaryWriter(stream, new[] { table1, table2 })) { binWriter.StepIn(IonType.Struct); binWriter.SetFieldName("s1"); binWriter.WriteSymbol("s2"); binWriter.SetFieldName("s3"); binWriter.WriteSymbol("s4"); binWriter.StepOut(); binWriter.Finish(); Assert.AreEqual(binWriter.SymbolTable.GetImportedMaxId(), binWriter.SymbolTable.MaxId); output = stream.ToArray(); } var reader = IonReaderBuilder.Build(new MemoryStream(output)); Assert.AreEqual(IonType.Struct, reader.MoveNext()); reader.StepIn(); Assert.AreEqual(IonType.Symbol, reader.MoveNext()); var fns = reader.GetFieldNameSymbol(); Assert.AreEqual(10, fns.Sid); Assert.IsNull(fns.Text); Assert.ThrowsException <UnknownSymbolException>(() => reader.CurrentFieldName); Assert.AreEqual(11, reader.SymbolValue().Sid); Assert.AreEqual(IonType.Symbol, reader.MoveNext()); fns = reader.GetFieldNameSymbol(); Assert.AreEqual(12, fns.Sid); Assert.IsNull(fns.Text); Assert.ThrowsException <UnknownSymbolException>(() => reader.CurrentFieldName); Assert.AreEqual(13, reader.SymbolValue().Sid); //make sure that a reader with the correct imports can read it var reader2 = new UserBinaryReader(new MemoryStream(output), catalog); Assert.AreEqual(IonType.Struct, reader2.MoveNext()); var localTable = reader2.GetSymbolTable(); Console.WriteLine(localTable.Find("s1").ToString()); reader2.StepIn(); Assert.AreEqual(IonType.Symbol, reader2.MoveNext()); Assert.AreEqual("s1", reader2.CurrentFieldName); Assert.AreEqual("s2", reader2.SymbolValue().Text); Assert.AreEqual(IonType.Symbol, reader2.MoveNext()); Assert.AreEqual("s3", reader2.CurrentFieldName); Assert.AreEqual("s4", reader2.SymbolValue().Text); }