Ejemplo n.º 1
0
        public void RepeatedWrappersBinaryFormat()
        {
            // At one point we accidentally used a packed format for repeated wrappers, which is wrong (and weird).
            // This test is just to prove that we use the right format.

            var rawOutput = new MemoryStream();
            var output    = new CodedOutputStream(rawOutput);

            // Write a value of 5
            output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(2);
            output.WriteTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint);
            output.WriteInt32(5);
            // Write a value of 0 (empty message)
            output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(0);
            output.Flush();
            var expectedBytes = rawOutput.ToArray();

            var message = new RepeatedWellKnownTypes {
                Int32Field = { 5, 0 }
            };
            var actualBytes = message.ToByteArray();

            Assert.AreEqual(expectedBytes, actualBytes);
        }
Ejemplo n.º 2
0
        public void RepeatedWrappersProhibitNullItems()
        {
            var message = new RepeatedWellKnownTypes();

            Assert.Throws <ArgumentNullException>(() => message.BoolField.Add((bool?)null));
            Assert.Throws <ArgumentNullException>(() => message.Int32Field.Add((int?)null));
            Assert.Throws <ArgumentNullException>(() => message.StringField.Add((string)null));
            Assert.Throws <ArgumentNullException>(() => message.BytesField.Add((ByteString)null));
        }
Ejemplo n.º 3
0
        public void Reflection_RepeatedFields()
        {
            // Just a single example... note that we can't have a null value here
            var message = new RepeatedWellKnownTypes {
                Int32Field = { 1, 2 }
            };
            var fields = RepeatedWellKnownTypes.Descriptor.Fields;
            var list   = (IList)fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);

            CollectionAssert.AreEqual(new[] { 1, 2 }, list);
        }
Ejemplo n.º 4
0
        public void RepeatedWrappers()
        {
            var message = new RepeatedWellKnownTypes
            {
                BoolField   = { true, false },
                BytesField  = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom(4, 5, 6), ByteString.Empty },
                DoubleField = { 12.5, -1.5, 0d },
                FloatField  = { 123.25f, -20f, 0f },
                Int32Field  = { int.MaxValue, int.MinValue, 0 },
                Int64Field  = { long.MaxValue, long.MinValue, 0L },
                StringField = { "First", "Second", "" },
                Uint32Field = { uint.MaxValue, uint.MinValue, 0U },
                Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL },
            };

            AssertRoundtrip(message);
        }
Ejemplo n.º 5
0
        public void RepeatedWrappersSerializeDeserialize()
        {
            var message = new RepeatedWellKnownTypes
            {
                BoolField   = { true, false },
                BytesField  = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom(4, 5, 6), ByteString.Empty },
                DoubleField = { 12.5, -1.5, 0d },
                FloatField  = { 123.25f, -20f, 0f },
                Int32Field  = { int.MaxValue, int.MinValue, 0 },
                Int64Field  = { long.MaxValue, long.MinValue, 0L },
                StringField = { "First", "Second", "" },
                Uint32Field = { uint.MaxValue, uint.MinValue, 0U },
                Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL },
            };

            // Just to test a single value for sanity...
            Assert.AreEqual("Second", message.StringField[1]);

            MessageParsingHelpers.AssertRoundtrip(RepeatedWellKnownTypes.Parser, message);
        }
Ejemplo n.º 6
0
 public void Reflection_RepeatedFields()
 {
     // Just a single example... note that we can't have a null value here
     var message = new RepeatedWellKnownTypes { Int32Field = { 1, 2 } };
     var fields = RepeatedWellKnownTypes.Descriptor.Fields;
     var list = (IList) fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);
     CollectionAssert.AreEqual(new[] { 1, 2 }, list);
 }
Ejemplo n.º 7
0
        public void RepeatedWrappersSerializeDeserialize()
        {
            var message = new RepeatedWellKnownTypes
            {
                BoolField = { true, false },
                BytesField = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom(4, 5, 6), ByteString.Empty },
                DoubleField = { 12.5, -1.5, 0d },
                FloatField = { 123.25f, -20f, 0f },
                Int32Field = { int.MaxValue, int.MinValue, 0 },
                Int64Field = { long.MaxValue, long.MinValue, 0L },                
                StringField = { "First", "Second", "" },
                Uint32Field = { uint.MaxValue, uint.MinValue, 0U },
                Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL },
            };
            var bytes = message.ToByteArray();
            var parsed = RepeatedWellKnownTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
            // Just to test a single value for sanity...
            Assert.AreEqual("Second", message.StringField[1]);
        }
Ejemplo n.º 8
0
 public void RepeatedWrappersProhibitNullItems()
 {
     var message = new RepeatedWellKnownTypes();
     Assert.Throws<ArgumentNullException>(() => message.BoolField.Add((bool?) null));
     Assert.Throws<ArgumentNullException>(() => message.Int32Field.Add((int?) null));
     Assert.Throws<ArgumentNullException>(() => message.StringField.Add((string) null));
     Assert.Throws<ArgumentNullException>(() => message.BytesField.Add((ByteString) null));
 }
Ejemplo n.º 9
0
        public void RepeatedWrappersBinaryFormat()
        {
            // At one point we accidentally used a packed format for repeated wrappers, which is wrong (and weird).
            // This test is just to prove that we use the right format.

            var rawOutput = new MemoryStream();
            var output = new CodedOutputStream(rawOutput);
            // Write a value of 5
            output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(2);
            output.WriteTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint);
            output.WriteInt32(5);
            // Write a value of 0 (empty message)
            output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
            output.WriteLength(0);
            output.Flush();
            var expectedBytes = rawOutput.ToArray();

            var message = new RepeatedWellKnownTypes { Int32Field = { 5, 0 } };
            var actualBytes = message.ToByteArray();
            Assert.AreEqual(expectedBytes, actualBytes);
        }