コード例 #1
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());
            }
        }
コード例 #2
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());
            }
        }
コード例 #3
0
        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();
            }
        }
コード例 #4
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();
        }