Ejemplo n.º 1
0
        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());
            }
        }
Ejemplo n.º 2
0
        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());
            }
        }
Ejemplo n.º 3
0
        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}"));
            }
        }
Ejemplo n.º 4
0
        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}"));
                }
            }
        }
Ejemplo n.º 5
0
        private static void RunOnce()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new ManagedBinaryWriter(stream, Symbols.EmptySymbolTablesArray))
                {
                    writer.StepIn(IonType.List);
                    for (var i = 0; i < 1000; i++)
                    {
                        writer.StepIn(IonType.Struct);

                        writer.SetFieldName("boolean");
                        writer.WriteBool(true);
                        writer.SetFieldName("string");
                        writer.WriteString("this is a string");
                        writer.SetFieldName("integer");
                        writer.WriteInt(int.MaxValue);
                        writer.SetFieldName("float");
                        writer.WriteFloat(432.23123f);
                        writer.SetFieldName("timestamp");
                        writer.WriteTimestamp(new Timestamp(new DateTime(2000, 11, 11)));

                        writer.StepOut();
                    }

                    writer.StepOut();
                    writer.Flush();
                }
            }
        }
Ejemplo n.º 6
0
        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();
        }