diff --git a/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs b/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs
new file mode 100644
index 00000000..498e4718
--- /dev/null
+++ b/csharp/src/ProtocolBuffers.Test/Collections/MapFieldTest.cs
@@ -0,0 +1,354 @@
+#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;
+
+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_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 Add_ForbidsNullKeys()
+ {
+ var map = new MapField();
+ Assert.Throws(() => map.Add(null, new ForeignMessage()));
+ }
+
+ [Test]
+ public void Add_AcceptsNullMessageValues()
+ {
+ var map = new MapField();
+ map.Add("missing", null);
+ Assert.IsNull(map["missing"]);
+ }
+
+ [Test]
+ public void Add_ForbidsNullStringValues()
+ {
+ var map = new MapField();
+ Assert.Throws(() => map.Add("missing", null));
+ }
+
+ [Test]
+ public void Add_ForbidsNullByteStringValues()
+ {
+ var map = new MapField();
+ Assert.Throws(() => map.Add("missing", null));
+ }
+
+ [Test]
+ public void Indexer_ForbidsNullKeys()
+ {
+ var map = new MapField();
+ Assert.Throws(() => map[null] = new ForeignMessage());
+ }
+
+ [Test]
+ public void Indexer_AcceptsNullMessageValues()
+ {
+ var map = new MapField();
+ map["missing"] = null;
+ Assert.IsNull(map["missing"]);
+ }
+
+ [Test]
+ public void Indexer_ForbidsNullStringValues()
+ {
+ var map = new MapField();
+ Assert.Throws(() => map["missing"] = null);
+ }
+
+ [Test]
+ public void Indexer_ForbidsNullByteStringValues()
+ {
+ var map = new MapField();
+ Assert.Throws(() => map["missing"] = null);
+ }
+
+ [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("a1", "v1");
+ map1.Add("b1", "v2");
+
+ var map2 = new MapField();
+ map2.Add("a2", "v1");
+ map2.Add("b2", "v2");
+
+ EqualityTester.AssertInequality(map1, map2);
+ }
+
+ [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", "a1");
+ map1.Add("b", "a2");
+
+ var map2 = new MapField();
+ map2.Add("a", "b1");
+ map2.Add("b", "b2");
+
+ 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);
+ }
+
+ [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"]);
+ }
+
+ private static KeyValuePair NewKeyValuePair(TKey key, TValue value)
+ {
+ return new KeyValuePair(key, value);
+ }
+ }
+}
diff --git a/csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs b/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs
similarity index 98%
rename from csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs
rename to csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs
index c01cd8ff..29945c36 100644
--- a/csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs
+++ b/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
-using Google.Protobuf.Collections;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
-namespace Google.Protobuf
+namespace Google.Protobuf.Collections
{
public class RepeatedFieldTest
{
diff --git a/csharp/src/ProtocolBuffers.Test/EqualityTester.cs b/csharp/src/ProtocolBuffers.Test/EqualityTester.cs
new file mode 100644
index 00000000..816f2be1
--- /dev/null
+++ b/csharp/src/ProtocolBuffers.Test/EqualityTester.cs
@@ -0,0 +1,63 @@
+#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 System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using NUnit.Framework;
+
+namespace Google.Protobuf
+{
+ ///
+ /// Helper methods when testing equality. NUnit's Assert.AreEqual and
+ /// Assert.AreNotEqual methods try to be clever with collections, which can
+ /// be annoying...
+ ///
+ internal static class EqualityTester
+ {
+ public static void AssertEquality(T first, T second) where T : IEquatable
+ {
+ Assert.IsTrue(first.Equals(second));
+ Assert.AreEqual(first.GetHashCode(), second.GetHashCode());
+ }
+
+ public static void AssertInequality(T first, T second) where T : IEquatable
+ {
+ Assert.IsFalse(first.Equals(second));
+ // While this isn't a requirement, the chances of this test failing due to
+ // coincidence rather than a bug are very small.
+ Assert.AreNotEqual(first.GetHashCode(), second.GetHashCode());
+ }
+ }
+}
diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs
index 81e35940..efeb7a97 100644
--- a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs
+++ b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs
@@ -11,6 +11,15 @@ namespace Google.Protobuf
///
public class GeneratedMessageTest
{
+ [Test]
+ public void EmptyMessageFieldDistinctFromMissingMessageField()
+ {
+ // This demonstrates what we're really interested in...
+ var message1 = new TestAllTypes { SingleForeignMessage = new ForeignMessage() };
+ var message2 = new TestAllTypes(); // SingleForeignMessage is null
+ EqualityTester.AssertInequality(message1, message2);
+ }
+
[Test]
public void DefaultValues()
{
diff --git a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj
index 31095a5b..d11e2596 100644
--- a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj
+++ b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj
@@ -74,8 +74,10 @@
+
-
+
+
@@ -99,9 +101,7 @@
-
-
-
+