public void IDictionary_Indexer_Set()
        {
            var map = new MapField <string, string> {
                { "x", "y" }
            };
            IDictionary dictionary = map;

            map["a"] = "b";
            Assert.AreEqual("b", map["a"]);
            map["a"] = "c";
            Assert.AreEqual("c", map["a"]);
            Assert.Throws <InvalidCastException>(() => dictionary[5]     = "x");
            Assert.Throws <InvalidCastException>(() => dictionary["x"]   = 5);
            Assert.Throws <ArgumentNullException>(() => dictionary[null] = "z");
            Assert.Throws <ArgumentNullException>(() => dictionary["x"]  = null);
        }
        public void Remove_Pair()
        {
            var map = new MapField <string, string>();

            map.Add("foo", "bar");
            ICollection <KeyValuePair <string, string> > collection = map;

            Assert.AreEqual(1, map.Count);
            Assert.IsFalse(collection.Remove(NewKeyValuePair("wrong key", "bar")));
            Assert.AreEqual(1, map.Count);
            Assert.IsFalse(collection.Remove(NewKeyValuePair("foo", "wrong value")));
            Assert.AreEqual(1, map.Count);
            Assert.IsTrue(collection.Remove(NewKeyValuePair("foo", "bar")));
            Assert.AreEqual(0, map.Count);
            Assert.Throws <ArgumentException>(() => collection.Remove(new KeyValuePair <string, string>(null, "")));
        }
        public void EqualityIsValueSensitive()
        {
            // Note: Without some care, it's a little easier than one might
            // hope to see hash collisions, but only in some environments...
            var map1 = new MapField <string, string>();

            map1.Add("a", "first value");
            map1.Add("b", "second value");

            var map2 = new MapField <string, string>();

            map2.Add("a", "third value");
            map2.Add("b", "fourth value");

            EqualityTester.AssertInequality(map1, map2);
        }
Beispiel #4
0
 /// <summary>
 /// Merge from another MapField
 /// </summary>
 /// <param name="other">MapField to merge from</param>
 public void MergeFrom(MapField <TKey, TValue> other)
 {
     ProtoPreconditions.CheckNotNull(other, nameof(other));
     foreach (var item in other)
     {
         var cloneable = item.Value as IDeepCloneable <TValue>;
         if (cloneable != null)
         {
             this[item.Key] = cloneable.Clone();
         }
         else
         {
             this[item.Key] = item.Value;
         }
     }
 }
        public void IDictionary_CopyTo()
        {
            var map = new MapField <string, string> {
                { "x", "y" }
            };
            IDictionary dictionary = map;
            var         array      = new DictionaryEntry[3];

            dictionary.CopyTo(array, 1);
            CollectionAssert.AreEqual(new[] { default(DictionaryEntry), new DictionaryEntry("x", "y"), default(DictionaryEntry) },
                                      array);
            var objectArray = new object[3];

            dictionary.CopyTo(objectArray, 1);
            CollectionAssert.AreEqual(new object[] { null, new DictionaryEntry("x", "y"), null },
                                      objectArray);
        }
        public void IDictionary_Remove()
        {
            var map = new MapField <string, string> {
                { "x", "y" }
            };
            IDictionary dictionary = map;

            dictionary.Remove("a");
            Assert.AreEqual(1, dictionary.Count);
            dictionary.Remove(5);
            Assert.AreEqual(1, dictionary.Count);
            dictionary.Remove(new DictionaryEntry("x", "y"));
            Assert.AreEqual(1, dictionary.Count);
            dictionary.Remove("x");
            Assert.AreEqual(0, dictionary.Count);
            Assert.Throws <ArgumentNullException>(() => dictionary.Remove(null));
        }
        private void TestNullValues <T>(T nonNullValue)
        {
            var map       = new MapField <int, T>(false);
            var nullValue = (T)(object)null;

            Assert.Throws <ArgumentNullException>(() => map.Add(0, nullValue));
            Assert.Throws <ArgumentNullException>(() => map[0] = nullValue);
            map.Add(1, nonNullValue);
            map[1] = nonNullValue;

            // Doesn't throw...
            map = new MapField <int, T>(true);
            map.Add(0, nullValue);
            map[0] = nullValue;
            map.Add(1, nonNullValue);
            map[1] = nonNullValue;
        }
Beispiel #8
0
        /// <summary>
        /// Creates a deep clone of this object.
        /// </summary>
        /// <returns>
        /// A deep clone of this object.
        /// </returns>
        public MapField <TKey, TValue> Clone()
        {
            var clone = new MapField <TKey, TValue>();

            // Keys are never cloneable. Values might be.
            if (typeof(IDeepCloneable <TValue>).IsAssignableFrom(typeof(TValue)))
            {
                foreach (var pair in list)
                {
                    clone.Add(pair.Key, ((IDeepCloneable <TValue>)pair.Value).Clone());
                }
            }
            else
            {
                // Nothing is cloneable, so we don't need to worry.
                clone.Add(this);
            }
            return(clone);
        }
        public void IDictionary_GetEnumerator()
        {
            IDictionary map = new MapField <string, string> {
                { "x", "y" }
            };
            var enumerator = map.GetEnumerator();

            // Commented assertions show an ideal situation - it looks like
            // the LinkedList enumerator doesn't throw when you ask for the current entry
            // at an inappropriate time; fixing this would be more work than it's worth.
            // Assert.Throws<InvalidOperationException>(() => enumerator.Current.GetHashCode());
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual("x", enumerator.Key);
            Assert.AreEqual("y", enumerator.Value);
            Assert.AreEqual(new DictionaryEntry("x", "y"), enumerator.Current);
            Assert.AreEqual(new DictionaryEntry("x", "y"), enumerator.Entry);
            Assert.IsFalse(enumerator.MoveNext());
            // Assert.Throws<InvalidOperationException>(() => enumerator.Current.GetHashCode());
            enumerator.Reset();
            // Assert.Throws<InvalidOperationException>(() => enumerator.Current.GetHashCode());
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual("x", enumerator.Key); // Assume the rest are okay
        }
        public void Add_Dictionary()
        {
            var map1 = new MapField <string, string>
            {
                { "x", "y" },
                { "a", "b" }
            };
            var map2 = new MapField <string, string>
            {
                { "before", "" },
                map1,
                { "after", "" }
            };
            var expected = new MapField <string, string>
            {
                { "before", "" },
                { "x", "y" },
                { "a", "b" },
                { "after", "" }
            };

            Assert.AreEqual(expected, map2);
            CollectionAssert.AreEqual(new[] { "before", "x", "a", "after" }, map2.Keys);
        }
Beispiel #11
0
        public void AddEntriesFrom_CodedInputStream_MissingKey()
        {
            // map will have string key and string value
            var keyTag   = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
            var valueTag = WireFormat.MakeTag(2, WireFormat.WireType.LengthDelimited);

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

            output.WriteLength(11);  // total of valueTag + value
            output.WriteTag(valueTag);
            output.WriteString("the_value");
            output.Flush();

            var field    = new MapField <string, string>();
            var mapCodec = new MapField <string, string> .Codec(FieldCodec.ForString(keyTag, ""), FieldCodec.ForString(valueTag, ""), 10);

            var input = new CodedInputStream(memoryStream.ToArray());

            field.AddEntriesFrom(input, mapCodec);
            CollectionAssert.AreEquivalent(new[] { "" }, field.Keys);
            CollectionAssert.AreEquivalent(new[] { "the_value" }, field.Values);
            Assert.IsTrue(input.IsAtEnd);
        }
        public void Indexer_ForbidsNullKeys()
        {
            var map = new MapField <string, ForeignMessage>();

            Assert.Throws <ArgumentNullException>(() => map[null] = new ForeignMessage());
        }
        public void Add_ForbidsNullKeys()
        {
            var map = new MapField <string, ForeignMessage>();

            Assert.Throws <ArgumentNullException>(() => map.Add(null, new ForeignMessage()));
        }
Beispiel #14
0
    // Use this for initialization
    IEnumerator Start()
    {
        yield return(StartCoroutine(ConnectWebsocket()));

        w.Send("{ \"username\":\"" + SystemInfo.deviceUniqueIdentifier + "\"}");
        while (socketError == null)
        {
            lock (updateLock) {
                if (mocapMsg != null)
                {
                    if (OnMocapMsg != null)
                    {
                        OnMocapMsg(mocapMsg);
                    }
                    Google.Protobuf.Collections.MapField <string, Google.Protobuf.VRCom.MocapSubject> subjects = mocapMsg.Mocap.Subjects;
                    foreach (KeyValuePair <string, MocapHandler> pair in mocapHandlers)
                    {
                        if (subjects.ContainsKey(pair.Key))
                        {
                            mocapHandlers [pair.Key] (subjects [pair.Key]);
                        }
                    }
                    mocapMsg = null;
                }
                if (hydraMsg != null)
                {
                    if (OnHydraMsg != null)
                    {
                        OnHydraMsg(hydraMsg);
                    }
                    hydraMsg = null;
                }
                while (wiimoteMsgs.Count != 0)
                {
                    currMsg = wiimoteMsgs.Dequeue();
                    if (OnWiimoteMsg != null)
                    {
                        OnWiimoteMsg(currMsg);
                    }
                }
            }
            yield return(0);
        }
        Debug.LogError("Error: " + socketError);
        w.Close();
    }
Beispiel #15
0
 protected abstract void ApplyMeta(Google.Protobuf.Collections.MapField <uint, SerializedMetadata.Types.MetadataEntry> meta);