Exemple #1
0
        public void MapWrappersSerializeDeserialize()
        {
            var message = new MapWellKnownTypes
            {
                BoolField  = { { 10, false }, { 20, true } },
                BytesField =
                {
                    {    -1, ByteString.CopyFrom(1, 2, 3) },
                    {    10, ByteString.CopyFrom(4, 5, 6) },
                    {  1000, ByteString.Empty },
                    { 10000, null }
                },
                DoubleField = { { 1, 12.5 }, { 10, -1.5 }, { 20, 0d } },
                FloatField  = { { 2, 123.25f }, { 3, -20f }, { 4, 0f } },
                Int32Field  = { { 5, int.MaxValue }, { 6, int.MinValue }, { 7, 0 } },
                Int64Field  = { { 8, long.MaxValue }, { 9, long.MinValue }, { 10, 0L } },
                StringField = { { 11, "First" }, { 12, "Second" }, { 13, "" }, { 14, null } },
                Uint32Field = { { 15, uint.MaxValue }, { 16, uint.MinValue }, { 17, 0U } },
                Uint64Field = { { 18, ulong.MaxValue }, { 19, ulong.MinValue }, { 20, 0UL } },
            };

            var bytes  = message.ToByteArray();
            var parsed = MapWellKnownTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
            // Just to test a single value for sanity...
            Assert.AreEqual("Second", message.StringField[12]);
        }
Exemple #2
0
        public void MapWrappersSerializeDeserialize()
        {
            // Note: no null values here, as they are prohibited in map fields
            // (despite being representable).
            var message = new MapWellKnownTypes
            {
                BoolField  = { { 10, false }, { 20, true } },
                BytesField =
                {
                    {   -1, ByteString.CopyFrom(1, 2, 3) },
                    {   10, ByteString.CopyFrom(4, 5, 6) },
                    { 1000, ByteString.Empty },
                },
                DoubleField = { { 1, 12.5 }, { 10, -1.5 }, { 20, 0d } },
                FloatField  = { { 2, 123.25f }, { 3, -20f }, { 4, 0f } },
                Int32Field  = { { 5, int.MaxValue }, { 6, int.MinValue }, { 7, 0 } },
                Int64Field  = { { 8, long.MaxValue }, { 9, long.MinValue }, { 10, 0L } },
                StringField = { { 11, "First" }, { 12, "Second" }, { 13, "" } },
                Uint32Field = { { 15, uint.MaxValue }, { 16, uint.MinValue }, { 17, 0U } },
                Uint64Field = { { 18, ulong.MaxValue }, { 19, ulong.MinValue }, { 20, 0UL } },
            };

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

            MessageParsingHelpers.AssertRoundtrip(MapWellKnownTypes.Parser, message);
        }
Exemple #3
0
        public void MapWrappersSerializeDeserialize()
        {
            var message = new MapWellKnownTypes
            {
                BoolField = { { 10, false }, { 20, true } },
                BytesField = {
                    { -1, ByteString.CopyFrom(1, 2, 3) },
                    { 10, ByteString.CopyFrom(4, 5, 6) },
                    { 1000, ByteString.Empty },
                    { 10000, null }
                },
                DoubleField = { { 1, 12.5 }, { 10, -1.5 }, { 20, 0d } },
                FloatField = { { 2, 123.25f }, { 3, -20f }, { 4, 0f } },
                Int32Field = { { 5, int.MaxValue }, { 6, int.MinValue }, { 7, 0 } },
                Int64Field = { { 8, long.MaxValue }, { 9, long.MinValue }, { 10, 0L } },
                StringField = { { 11, "First" }, { 12, "Second" }, { 13, "" }, { 14, null } },
                Uint32Field = { { 15, uint.MaxValue }, { 16, uint.MinValue }, { 17, 0U } },
                Uint64Field = { { 18, ulong.MaxValue }, { 19, ulong.MinValue }, { 20, 0UL } },
            };

            var bytes = message.ToByteArray();
            var parsed = MapWellKnownTypes.Parser.ParseFrom(bytes);

            Assert.AreEqual(message, parsed);
            // Just to test a single value for sanity...
            Assert.AreEqual("Second", message.StringField[12]);
        }
Exemple #4
0
        public void Reflection_MapFields()
        {
            // Just a single example... note that we can't have a null value here despite the value type being int?
            var message = new MapWellKnownTypes {
                Int32Field = { { 1, 2 } }
            };
            var fields     = MapWellKnownTypes.Descriptor.Fields;
            var dictionary = (IDictionary)fields[MapWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);

            Assert.AreEqual(2, dictionary[1]);
        }
Exemple #5
0
        public void Reflection_MapFields()
        {
            // Just a single example... note that we can't have a null value here
            var message = new MapWellKnownTypes {
                Int32Field = { { 1, 2 }, { 3, null } }
            };
            var fields     = MapWellKnownTypes.Descriptor.Fields;
            var dictionary = (IDictionary)fields[MapWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);

            Assert.AreEqual(2, dictionary[1]);
            Assert.IsNull(dictionary[3]);
            Assert.IsTrue(dictionary.Contains(3));
        }
Exemple #6
0
 public void Reflection_MapFields()
 {
     // Just a single example... note that we can't have a null value here
     var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } };
     var fields = MapWellKnownTypes.Descriptor.Fields;
     var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);
     Assert.AreEqual(2, dictionary[1]);
     Assert.IsNull(dictionary[3]);
     Assert.IsTrue(dictionary.Contains(3));
 }
Exemple #7
0
 public void Reflection_MapFields()
 {
     // Just a single example... note that we can't have a null value here despite the value type being int?
     var message = new MapWellKnownTypes { Int32Field = { { 1, 2 } } };
     var fields = MapWellKnownTypes.Descriptor.Fields;
     var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message);
     Assert.AreEqual(2, dictionary[1]);
 }