Exemple #1
0
        public void CheckDateTime()
        {
            MemoryStream buffer        = new MemoryStream();
            IFieldWriter writer        = new FieldWriter(buffer);
            IFieldReader reader        = new FieldReader(buffer);
            DateTime     referenceTime = DateTime.Now; //we convert to UTC during serialization, we want local time.

            buffer.Position = 0;
            writer.Write(referenceTime);
            Assert.AreEqual(12, buffer.Position);
            writer.Write(referenceTime.AddTicks(1));
            Assert.AreEqual(16, buffer.Position);
            writer.Write(referenceTime.AddMilliseconds(50));
            Assert.AreEqual(22, buffer.Position);
            writer.Write(referenceTime.AddHours(1));
            Assert.AreEqual(31, buffer.Position);
            writer.Write(referenceTime.AddDays(1));
            Assert.AreEqual(40, buffer.Position);
            buffer.Position = 0;
            Assert.AreEqual(referenceTime, reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddTicks(1), reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddMilliseconds(50), reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddHours(1), reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddDays(1), reader.ReadDateTime());
        }
        public void CheckDateTime()
        {
            MemoryStream buffer        = new MemoryStream();
            IFieldWriter writer        = new FieldWriter(buffer);
            IFieldReader reader        = new FieldReader(buffer);
            var          referenceTime = new DateTime(1973, 6, 25, 8, 30, 12, DateTimeKind.Utc); //UTC to ensure tests are portable

            buffer.Position = 0;
            writer.Write(referenceTime);
            Assert.AreEqual(11, buffer.Position,
                            "Serialized value isn't the expected length.  Position is: {1}, expected {0}.\r\nSerialized Value: {2}",
                            buffer.Position, 11, buffer.ToArray().ToDisplayString());

            writer.Write(referenceTime.AddTicks(1));
            Assert.AreEqual(14, buffer.Position);
            writer.Write(referenceTime.AddMilliseconds(50));
            Assert.AreEqual(19, buffer.Position);
            writer.Write(referenceTime.AddHours(1));
            Assert.AreEqual(27, buffer.Position);
            writer.Write(referenceTime.AddDays(1));
            Assert.AreEqual(35, buffer.Position);
            buffer.Position = 0;
            Assert.AreEqual(referenceTime, reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddTicks(1), reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddMilliseconds(50), reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddHours(1), reader.ReadDateTime());
            Assert.AreEqual(referenceTime.AddDays(1), reader.ReadDateTime());
        }
Exemple #3
0
        public void CheckBoolArray()
        {
            MemoryStream buffer = new MemoryStream();
            IFieldWriter writer = new FieldWriter(buffer);
            IFieldReader reader = new FieldReader(buffer);

            bool[][] list = new bool[][] { new bool[0], new bool[1], new bool[2], new bool[31], new bool[32], new bool[33],
                                           new bool[63], new bool[64], new bool[65], new bool[127], new bool[128], new bool[129] };
            Random rand     = new Random(0);
            int    bitCount = 0;

            for (int i = 0; i < list.Length; i++)
            {
                bool[] current = list[i];
                bitCount += current.Length;
                for (int j = 0; j < current.Length; j++)
                {
                    current[j] = rand.NextDouble() >= 0.5;
                }
                writer.Write(current);
            }

            Assert.AreEqual(675, bitCount);
            Assert.AreEqual(buffer.Length, buffer.Position);
            buffer.Position = 0;
            for (int i = 0; i < list.Length; i++)
            {
                bool[] current = list[i];
                bool[] actual  = reader.ReadBoolArray();
                CompareArray(current, actual);
            }
            Assert.AreEqual(buffer.Length, buffer.Position);
        }
Exemple #4
0
        public void FieldWriter()
        {
            FieldWriter writer = new FieldWriter(this.output.Language.CastTo <BaseLanguage>());

            writer.Write(new FieldTemplate(null, "test", Code.Type("type")), this.output);
            Assert.AreEqual("private type test;", this.output.ToString());
        }
Exemple #5
0
        private long DateTimeOffsetSerializedLength()
        {
            MemoryStream buffer = new MemoryStream();
            IFieldWriter writer = new FieldWriter(buffer);

            buffer.Position = 0;
            writer.Write(DateTimeOffset.Now);
            return(buffer.Position);
        }
Exemple #6
0
        public void FieldDefaultValue()
        {
            FieldTemplate template = new FieldTemplate(null, "test", Code.Type("type"));

            template.DefaultValue = Code.String("default");
            FieldWriter writer = new FieldWriter(this.output.Language.CastTo <BaseLanguage>());

            writer.Write(template, this.output);
            Assert.AreEqual("private type test = \"default\";", this.output.ToString());
        }
Exemple #7
0
        private static void CheckDouble(double expectedValue, int expectedSize)
        {
            IFieldWriter writer = new FieldWriter();

            writer.Write(expectedValue);

            IFieldReader reader      = new FieldReader(writer.ToArray());
            double       actualValue = reader.ReadDouble();

            Assert.AreEqual(expectedValue, actualValue, "Expected deserialized value ");
            Assert.AreEqual(expectedSize, writer.Position, "Unexpected size for {0} ", expectedValue);
        }
Exemple #8
0
        public void CheckString()
        {
            IFieldWriter writer = new FieldWriter();

            writer.Write((string)null);
            Assert.AreEqual(2, writer.Position, "Expected position ");

            writer.Write("");
            Assert.AreEqual(3, writer.Position, "Expected position ");

            writer.Write(" ");
            Assert.AreEqual(5, writer.Position, "Expected position ");

            writer.Write("Hello");
            Assert.AreEqual(11, writer.Position, "Expected position ");

            writer.Write("Hello");
            Assert.AreEqual(17, writer.Position, "Expected position ");

            writer.Write("Hi");
            Assert.AreEqual(20, writer.Position, "Expected position ");

            IFieldReader reader = new FieldReader(writer.ToArray());

            Assert.AreEqual(null, reader.ReadString(), "Expected string ");
            Assert.AreEqual("", reader.ReadString(), "Expected string ");
            Assert.AreEqual(" ", reader.ReadString(), "Expected string ");
            Assert.AreEqual("Hello", reader.ReadString(), "Expected string ");
            Assert.AreEqual("Hello", reader.ReadString(), "Expected string2 ");
            Assert.AreEqual("Hi", reader.ReadString(), "Expected string ");
        }
        public void CheckUInt64Array()
        {
            MemoryStream buffer = new MemoryStream();
            IFieldWriter writer = new FieldWriter(buffer);
            IFieldReader reader = new FieldReader(buffer);

            UInt64[] array1 = new UInt64[] {};
            UInt64[] array2 = new UInt64[] { 1 };
            UInt64[] array3 = new UInt64[] { 0, 0, 0, 0, 0, 0 };
            UInt64[] array4 = new UInt64[] { 0, 1, 2, 3, 4, 5 };
            UInt64[] array5 = new UInt64[] { 0, 1, 1, 2, 2, 2 };
            UInt64[] array6 = new UInt64[] { 0, 1, 1, 2, 2, 2, 3 };
            writer.Write(array1);
            writer.Write(array2);
            writer.Write(array3);
            writer.Write(array4);
            writer.Write(array5);
            writer.Write(array6);
            buffer.Position = 0;
            array1.CompareArray(reader.ReadUInt64Array());
            array2.CompareArray(reader.ReadUInt64Array());
            array3.CompareArray(reader.ReadUInt64Array());
            array4.CompareArray(reader.ReadUInt64Array());
            array5.CompareArray(reader.ReadUInt64Array());
            array6.CompareArray(reader.ReadUInt64Array());
        }
Exemple #10
0
        public void CheckDoubleArray()
        {
            MemoryStream buffer = new MemoryStream();
            IFieldWriter writer = new FieldWriter(buffer);
            IFieldReader reader = new FieldReader(buffer);

            Double[] array1 = new Double[] {};
            Double[] array2 = new Double[] { 1 };
            Double[] array3 = new Double[] { 0, 0, 0, 0, 0, 0 };
            Double[] array4 = new Double[] { 0, 1, 2, 3, 4, 5 };
            Double[] array5 = new Double[] { 0, 1, 1, 2, 2, 2 };
            Double[] array6 = new Double[] { 0, 1, 1, 2, 2, 2, 3 };
            writer.Write(array1);
            writer.Write(array2);
            writer.Write(array3);
            writer.Write(array4);
            writer.Write(array5);
            writer.Write(array6);
            buffer.Position = 0;
            CompareArray(array1, reader.ReadDoubleArray());
            CompareArray(array2, reader.ReadDoubleArray());
            CompareArray(array3, reader.ReadDoubleArray());
            CompareArray(array4, reader.ReadDoubleArray());
            CompareArray(array5, reader.ReadDoubleArray());
            CompareArray(array6, reader.ReadDoubleArray());
        }
        public override void Write(CodeWriter writer)
        {
            delegate_field.Write(writer);

            writer.WriteLineNoIndent("#pragma warning disable 0169");

            delegate_getter.Write(writer);
            writer.WriteLine();

            base.Write(writer);

            writer.WriteLineNoIndent("#pragma warning restore 0169");
            writer.WriteLine();
        }
Exemple #12
0
        private static void CheckInt32(int expectedValue, int expectedSize)
        {
            IFieldWriter writer = new FieldWriter(new MemoryStream());

            writer.Write(expectedValue);

            IFieldReader reader      = new FieldReader(writer.ToArray());
            int          actualValue = reader.ReadInt32();

            Assert.AreEqual(expectedValue, actualValue, "Expected deserialized value ");
            if (expectedSize > 0)
            {
                Assert.AreEqual(expectedSize, writer.Position, "Unexpected size for {0} ", expectedValue);
            }
        }
        public void TestProtocolVersion1()
        {
            // Test with protocol version 1.0
            var writer = new FieldWriter(new MemoryStream(), new UniqueStringList(), 1, 0);

            /*
             * These first few writes are identical to TestStringCompressionDisabled
             */

            // Test that a null string takes only one byte
            writer.Write((string)null);
            Assert.AreEqual(1, writer.Position, "Expected position ");

            // Test that an empty string takes 2 bytes
            writer.Write("");
            Assert.AreEqual(3, writer.Position, "Expected position ");

            // Test that passing a single character string takes 3 bytes
            writer.Write(" ");
            Assert.AreEqual(6, writer.Position, "Expected position ");

            // Test that passing a two character string takes 4 bytes
            writer.Write("Hi");
            Assert.AreEqual(10, writer.Position, "Expected position ");

            // Test that 5 characters takes 7 bytes
            writer.Write("Hello");
            Assert.AreEqual(17, writer.Position, "Expected position ");

            /*
             * Now, let's try some repeated strings
             */

            // Test that a second occurence should only take 1 byte with compression enabled
            writer.Write("Hello");
            Assert.AreEqual(18, writer.Position, "Expected position ");

            writer.Write("Hi");
            Assert.AreEqual(19, writer.Position, "Expected position ");

            // Verify that the strings read back are the same as those written
            IFieldReader reader = new FieldReader(new MemoryStream(writer.ToArray()), new UniqueStringList(), 1, 0);

            Assert.AreEqual(null, reader.ReadString());
            Assert.AreEqual("", reader.ReadString());
            Assert.AreEqual(" ", reader.ReadString());
            Assert.AreEqual("Hi", reader.ReadString());
            Assert.AreEqual("Hello", reader.ReadString());
            Assert.AreEqual("Hello", reader.ReadString());
            Assert.AreEqual("Hi", reader.ReadString());
        }
Exemple #14
0
        private static void CheckUInt64(ulong expectedValue, int expectedSize)
        {
            IFieldWriter writer = new FieldWriter();

            writer.Write(expectedValue);

            IFieldReader reader      = new FieldReader(writer.ToArray());
            ulong        actualValue = reader.ReadUInt64();

            Assert.AreEqual(expectedValue, actualValue, "Expected deserialized value ");
            if (expectedSize > 0)
            {
                Assert.AreEqual(expectedSize, writer.Position, "Unexpected size for {0} ", expectedValue);
            }
        }
        public void Basics()
        {
            var field = new FieldWriter {
                Name = "MyField", IsPublic = true, Type = TypeReferenceWriter.IntPtr
            };

            var sw     = new StringWriter();
            var writer = new CodeWriter(sw);

            field.Write(writer);

            var expected = @"public IntPtr MyField;
";

            Assert.AreEqual(expected, sw.ToString());
        }
Exemple #16
0
        public void CheckGuid()
        {
            MemoryStream buffer = new MemoryStream();
            IFieldWriter writer = new FieldWriter(buffer);
            IFieldReader reader = new FieldReader(buffer);

            foreach (Guid expected in new Guid[]
            {
                Guid.Empty, Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid()
            })
            {
                buffer.Position = 0;
                writer.Write(expected);
                buffer.Position = 0;
                Guid actual = reader.ReadGuid();
                Assert.AreEqual(expected, actual);
            }
        }
        public void TestProtocolVersion2()
        {
            // Test with protocol version 2.0
            var writer = new FieldWriter(new MemoryStream(), new UniqueStringList(), 2, 0);

            // Test that a null string takes only one byte
            writer.Write((string)null);
            Assert.AreEqual(2, writer.Position, "Expected position ");

            // Test that an empty string takes 2 bytes
            writer.Write("");
            Assert.AreEqual(3, writer.Position, "Expected position ");

            // Test that passing a single character string takes 2 bytes
            writer.Write(" ");
            Assert.AreEqual(5, writer.Position, "Expected position ");

            // Test that passing a two character string takes 3 bytes
            writer.Write("Hi");
            Assert.AreEqual(8, writer.Position, "Expected position ");

            // Test that 5 characters takes 6 bytes
            writer.Write("Hello");
            Assert.AreEqual(14, writer.Position, "Expected position ");

            // Test that a second occurence still takes 6 bytes
            writer.Write("Hello");
            Assert.AreEqual(20, writer.Position, "Expected position ");

            // Likewise, a second occurence of an earlier string still takes a much space
            writer.Write("Hi");
            Assert.AreEqual(23, writer.Position, "Expected position ");

            // Verify that the strings read back are the same as those written
            IFieldReader reader = new FieldReader(new MemoryStream(writer.ToArray()), null, 2, 0);

            Assert.AreEqual(null, reader.ReadString());
            Assert.AreEqual("", reader.ReadString());
            Assert.AreEqual(" ", reader.ReadString());
            Assert.AreEqual("Hi", reader.ReadString());
            Assert.AreEqual("Hello", reader.ReadString());
            Assert.AreEqual("Hello", reader.ReadString());
            Assert.AreEqual("Hi", reader.ReadString());
        }
Exemple #18
0
        public void CheckTimeSpan()
        {
            MemoryStream buffer = new MemoryStream();
            IFieldWriter writer = new FieldWriter(buffer);
            IFieldReader reader = new FieldReader(buffer);

            foreach (ulong value in new UInt64[]
            {
                0x0, 0x1, 0x9999, 0xffff, 0xffffffff, 0xffffffffffff,
                0xffffffffffffffff, 0x7fffffffffffffff, 0x9999999999999999
            })
            {
                buffer.Position = 0;
                TimeSpan expected = new TimeSpan((long)value);
                writer.Write(expected);
                buffer.Position = 0;
                TimeSpan actual = reader.ReadTimeSpan();
                Assert.AreEqual(expected, actual);
            }
        }