From 59eeebee870332ea2b9085688ba524c69311f662 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 17 Jul 2015 08:26:04 +0100 Subject: First pass at the big rename from ProtocolBuffers to Google.Protobuf. We'll see what I've missed when CI fails... --- .../Collections/MapFieldTest.cs | 541 +++++++++++++++++++++ 1 file changed, 541 insertions(+) create mode 100644 csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs (limited to 'csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs') diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs new file mode 100644 index 00000000..46d3bd9a --- /dev/null +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -0,0 +1,541 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using System; +using System.Collections.Generic; +using Google.Protobuf.TestProtos; +using NUnit.Framework; +using System.Collections; +using System.Linq; + +namespace Google.Protobuf.Collections +{ + /// + /// Tests for MapField which aren't reliant on the encoded format - + /// tests for serialization/deserialization are part of GeneratedMessageTest. + /// + public class MapFieldTest + { + // Protobuf-specific tests + [Test] + public void Freeze_FreezesMessages() + { + var message = new ForeignMessage { C = 20 }; + var map = new MapField { { "x", message } }; + map.Freeze(); + Assert.IsTrue(message.IsFrozen); + } + + [Test] + public void Freeze_Idempotent() + { + var message = new ForeignMessage { C = 20 }; + var map = new MapField { { "x", message } }; + Assert.IsFalse(map.IsFrozen); + map.Freeze(); + Assert.IsTrue(message.IsFrozen); + map.Freeze(); + Assert.IsTrue(message.IsFrozen); + } + + [Test] + public void Freeze_PreventsMutation() + { + var map = new MapField(); + map.Freeze(); + Assert.IsTrue(map.IsFrozen); + Assert.IsTrue(map.IsReadOnly); + ICollection> collection = map; + Assert.Throws(() => map["x"] = "y"); + Assert.Throws(() => map.Add("x", "y")); + Assert.Throws(() => map.Remove("x")); + Assert.Throws(() => map.Clear()); + Assert.Throws(() => collection.Add(NewKeyValuePair("x", "y"))); + Assert.Throws(() => collection.Remove(NewKeyValuePair("x", "y"))); + } + + [Test] + public void Clone_ReturnsNonFrozen() + { + var map = new MapField(); + map.Freeze(); + var clone = map.Clone(); + clone.Add("x", "y"); + } + + [Test] + public void Clone_ClonesMessages() + { + var message = new ForeignMessage { C = 20 }; + var map = new MapField { { "x", message } }; + var clone = map.Clone(); + map["x"].C = 30; + Assert.AreEqual(20, clone["x"].C); + } + + [Test] + public void NullValues() + { + TestNullValues(0); + TestNullValues(""); + TestNullValues(new TestAllTypes()); + } + + private void TestNullValues(T nonNullValue) + { + var map = new MapField(false); + var nullValue = (T) (object) null; + Assert.Throws(() => map.Add(0, nullValue)); + Assert.Throws(() => map[0] = nullValue); + map.Add(1, nonNullValue); + map[1] = nonNullValue; + + // Doesn't throw... + map = new MapField(true); + map.Add(0, nullValue); + map[0] = nullValue; + map.Add(1, nonNullValue); + map[1] = nonNullValue; + } + + [Test] + public void Add_ForbidsNullKeys() + { + var map = new MapField(); + Assert.Throws(() => map.Add(null, new ForeignMessage())); + } + + [Test] + public void Indexer_ForbidsNullKeys() + { + var map = new MapField(); + Assert.Throws(() => map[null] = new ForeignMessage()); + } + + [Test] + public void AddPreservesInsertionOrder() + { + var map = new MapField(); + map.Add("a", "v1"); + map.Add("b", "v2"); + map.Add("c", "v3"); + map.Remove("b"); + map.Add("d", "v4"); + CollectionAssert.AreEqual(new[] { "a", "c", "d" }, map.Keys); + CollectionAssert.AreEqual(new[] { "v1", "v3", "v4" }, map.Values); + } + + [Test] + public void EqualityIsOrderInsensitive() + { + var map1 = new MapField(); + map1.Add("a", "v1"); + map1.Add("b", "v2"); + + var map2 = new MapField(); + map2.Add("b", "v2"); + map2.Add("a", "v1"); + + EqualityTester.AssertEquality(map1, map2); + } + + [Test] + public void EqualityIsKeySensitive() + { + var map1 = new MapField(); + map1.Add("first key", "v1"); + map1.Add("second key", "v2"); + + var map2 = new MapField(); + map2.Add("third key", "v1"); + map2.Add("fourth key", "v2"); + + EqualityTester.AssertInequality(map1, map2); + } + + [Test] + public void Equality_Simple() + { + var map = new MapField(); + EqualityTester.AssertEquality(map, map); + EqualityTester.AssertInequality(map, null); + Assert.IsFalse(map.Equals(new object())); + } + + [Test] + 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(); + map1.Add("a", "first value"); + map1.Add("b", "second value"); + + var map2 = new MapField(); + map2.Add("a", "third value"); + map2.Add("b", "fourth value"); + + EqualityTester.AssertInequality(map1, map2); + } + + [Test] + public void EqualityHandlesNullValues() + { + var map1 = new MapField(); + map1.Add("a", new ForeignMessage { C = 10 }); + map1.Add("b", null); + + var map2 = new MapField(); + map2.Add("a", new ForeignMessage { C = 10 }); + map2.Add("b", null); + + EqualityTester.AssertEquality(map1, map2); + // Check the null value isn't ignored entirely... + Assert.IsTrue(map1.Remove("b")); + EqualityTester.AssertInequality(map1, map2); + map1.Add("b", new ForeignMessage()); + EqualityTester.AssertInequality(map1, map2); + map1["b"] = null; + EqualityTester.AssertEquality(map1, map2); + } + + [Test] + public void Add_Dictionary() + { + var map1 = new MapField + { + { "x", "y" }, + { "a", "b" } + }; + var map2 = new MapField + { + { "before", "" }, + map1, + { "after", "" } + }; + var expected = new MapField + { + { "before", "" }, + { "x", "y" }, + { "a", "b" }, + { "after", "" } + }; + Assert.AreEqual(expected, map2); + CollectionAssert.AreEqual(new[] { "before", "x", "a", "after" }, map2.Keys); + } + + // General IDictionary behavior tests + [Test] + public void Add_KeyAlreadyExists() + { + var map = new MapField(); + map.Add("foo", "bar"); + Assert.Throws(() => map.Add("foo", "baz")); + } + + [Test] + public void Add_Pair() + { + var map = new MapField(); + ICollection> collection = map; + collection.Add(NewKeyValuePair("x", "y")); + Assert.AreEqual("y", map["x"]); + Assert.Throws(() => collection.Add(NewKeyValuePair("x", "z"))); + } + + [Test] + public void Contains_Pair() + { + var map = new MapField { { "x", "y" } }; + ICollection> collection = map; + Assert.IsTrue(collection.Contains(NewKeyValuePair("x", "y"))); + Assert.IsFalse(collection.Contains(NewKeyValuePair("x", "z"))); + Assert.IsFalse(collection.Contains(NewKeyValuePair("z", "y"))); + } + + [Test] + public void Remove_Key() + { + var map = new MapField(); + map.Add("foo", "bar"); + Assert.AreEqual(1, map.Count); + Assert.IsFalse(map.Remove("missing")); + Assert.AreEqual(1, map.Count); + Assert.IsTrue(map.Remove("foo")); + Assert.AreEqual(0, map.Count); + Assert.Throws(() => map.Remove(null)); + } + + [Test] + public void Remove_Pair() + { + var map = new MapField(); + map.Add("foo", "bar"); + ICollection> 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(() => collection.Remove(new KeyValuePair(null, ""))); + } + + [Test] + public void CopyTo_Pair() + { + var map = new MapField(); + map.Add("foo", "bar"); + ICollection> collection = map; + KeyValuePair[] array = new KeyValuePair[3]; + collection.CopyTo(array, 1); + Assert.AreEqual(NewKeyValuePair("foo", "bar"), array[1]); + } + + [Test] + public void Clear() + { + var map = new MapField { { "x", "y" } }; + Assert.AreEqual(1, map.Count); + map.Clear(); + Assert.AreEqual(0, map.Count); + map.Add("x", "y"); + Assert.AreEqual(1, map.Count); + } + + [Test] + public void Indexer_Get() + { + var map = new MapField { { "x", "y" } }; + Assert.AreEqual("y", map["x"]); + Assert.Throws(() => { var ignored = map["z"]; }); + } + + [Test] + public void Indexer_Set() + { + var map = new MapField(); + map["x"] = "y"; + Assert.AreEqual("y", map["x"]); + map["x"] = "z"; // This won't throw, unlike Add. + Assert.AreEqual("z", map["x"]); + } + + [Test] + public void GetEnumerator_NonGeneric() + { + IEnumerable map = new MapField { { "x", "y" } }; + CollectionAssert.AreEqual(new[] { new KeyValuePair("x", "y") }, + map.Cast().ToList()); + } + + // Test for the explicitly-implemented non-generic IDictionary interface + [Test] + public void IDictionary_GetEnumerator() + { + IDictionary map = new MapField { { "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(() => 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(() => enumerator.Current.GetHashCode()); + enumerator.Reset(); + // Assert.Throws(() => enumerator.Current.GetHashCode()); + Assert.IsTrue(enumerator.MoveNext()); + Assert.AreEqual("x", enumerator.Key); // Assume the rest are okay + } + + [Test] + public void IDictionary_Add() + { + var map = new MapField { { "x", "y" } }; + IDictionary dictionary = map; + dictionary.Add("a", "b"); + Assert.AreEqual("b", map["a"]); + Assert.Throws(() => dictionary.Add("a", "duplicate")); + Assert.Throws(() => dictionary.Add(new object(), "key is bad")); + Assert.Throws(() => dictionary.Add("value is bad", new object())); + } + + [Test] + public void IDictionary_Contains() + { + var map = new MapField { { "x", "y" } }; + IDictionary dictionary = map; + + Assert.IsFalse(dictionary.Contains("a")); + Assert.IsFalse(dictionary.Contains(5)); + // Surprising, but IDictionary.Contains is only about keys. + Assert.IsFalse(dictionary.Contains(new DictionaryEntry("x", "y"))); + Assert.IsTrue(dictionary.Contains("x")); + } + + [Test] + public void IDictionary_Remove() + { + var map = new MapField { { "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(() => dictionary.Remove(null)); + + map.Freeze(); + // Call should fail even though it clearly doesn't contain 5 as a key. + Assert.Throws(() => dictionary.Remove(5)); + } + + [Test] + public void IDictionary_CopyTo() + { + var map = new MapField { { "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); + } + + [Test] + public void IDictionary_IsFixedSize() + { + var map = new MapField { { "x", "y" } }; + IDictionary dictionary = map; + Assert.IsFalse(dictionary.IsFixedSize); + map.Freeze(); + Assert.IsTrue(dictionary.IsFixedSize); + } + + [Test] + public void IDictionary_Keys() + { + IDictionary dictionary = new MapField { { "x", "y" } }; + CollectionAssert.AreEqual(new[] { "x" }, dictionary.Keys); + } + + [Test] + public void IDictionary_Values() + { + IDictionary dictionary = new MapField { { "x", "y" } }; + CollectionAssert.AreEqual(new[] { "y" }, dictionary.Values); + } + + [Test] + public void IDictionary_IsSynchronized() + { + IDictionary dictionary = new MapField { { "x", "y" } }; + Assert.IsFalse(dictionary.IsSynchronized); + } + + [Test] + public void IDictionary_SyncRoot() + { + IDictionary dictionary = new MapField { { "x", "y" } }; + Assert.AreSame(dictionary, dictionary.SyncRoot); + } + + [Test] + public void IDictionary_Indexer_Get() + { + IDictionary dictionary = new MapField { { "x", "y" } }; + Assert.AreEqual("y", dictionary["x"]); + Assert.IsNull(dictionary["a"]); + Assert.IsNull(dictionary[5]); + Assert.Throws(() => dictionary[null].GetHashCode()); + } + + [Test] + public void IDictionary_Indexer_Set() + { + var map = new MapField { { "x", "y" } }; + IDictionary dictionary = map; + map["a"] = "b"; + Assert.AreEqual("b", map["a"]); + map["a"] = "c"; + Assert.AreEqual("c", map["a"]); + Assert.Throws(() => dictionary[5] = "x"); + Assert.Throws(() => dictionary["x"] = 5); + Assert.Throws(() => dictionary[null] = "z"); + Assert.Throws(() => dictionary["x"] = null); + map.Freeze(); + // Note: Not InvalidOperationException. + Assert.Throws(() => dictionary["a"] = "c"); + } + + [Test] + public void AllowNullValues_Property() + { + // Non-message reference type values are non-nullable by default, but can be overridden + Assert.IsFalse(new MapField().AllowsNullValues); + Assert.IsFalse(new MapField(false).AllowsNullValues); + Assert.IsTrue(new MapField(true).AllowsNullValues); + + // Non-nullable value type values are never nullable + Assert.IsFalse(new MapField().AllowsNullValues); + Assert.IsFalse(new MapField(false).AllowsNullValues); + Assert.Throws(() => new MapField(true)); + + // Message type values are nullable by default, but can be overridden + Assert.IsTrue(new MapField().AllowsNullValues); + Assert.IsFalse(new MapField(false).AllowsNullValues); + Assert.IsTrue(new MapField(true).AllowsNullValues); + + // Nullable value type values are nullable by default, but can be overridden + Assert.IsTrue(new MapField().AllowsNullValues); + Assert.IsFalse(new MapField(false).AllowsNullValues); + Assert.IsTrue(new MapField(true).AllowsNullValues); + } + + private static KeyValuePair NewKeyValuePair(TKey key, TValue value) + { + return new KeyValuePair(key, value); + } + } +} -- cgit v1.2.3