blob: 75f8ff2ae671bec92694ee6c6793aab8a47c9d4f [file] [log] [blame]
Jon Skeetdf44ae42015-06-25 12:08:18 +01001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2015 Google Inc. All rights reserved.
4// https://developers.google.com/protocol-buffers/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#endregion
32
33using System;
34using System.Collections.Generic;
35using Google.Protobuf.TestProtos;
36using NUnit.Framework;
37
38namespace Google.Protobuf.Collections
39{
40 /// <summary>
41 /// Tests for MapField which aren't reliant on the encoded format -
42 /// tests for serialization/deserialization are part of GeneratedMessageTest.
43 /// </summary>
44 public class MapFieldTest
45 {
46 // Protobuf-specific tests
47 [Test]
48 public void Freeze_FreezesMessages()
49 {
50 var message = new ForeignMessage { C = 20 };
51 var map = new MapField<string, ForeignMessage> { { "x", message } };
52 map.Freeze();
53 Assert.IsTrue(message.IsFrozen);
54 }
55
56 [Test]
57 public void Freeze_PreventsMutation()
58 {
59 var map = new MapField<string, string>();
60 map.Freeze();
61 Assert.IsTrue(map.IsFrozen);
62 Assert.IsTrue(map.IsReadOnly);
63 ICollection<KeyValuePair<string, string>> collection = map;
64 Assert.Throws<InvalidOperationException>(() => map["x"] = "y");
65 Assert.Throws<InvalidOperationException>(() => map.Add("x", "y"));
66 Assert.Throws<InvalidOperationException>(() => map.Remove("x"));
67 Assert.Throws<InvalidOperationException>(() => map.Clear());
68 Assert.Throws<InvalidOperationException>(() => collection.Add(NewKeyValuePair("x", "y")));
69 Assert.Throws<InvalidOperationException>(() => collection.Remove(NewKeyValuePair("x", "y")));
70 }
71
72 [Test]
73 public void Clone_ReturnsNonFrozen()
74 {
75 var map = new MapField<string, string>();
76 map.Freeze();
77 var clone = map.Clone();
78 clone.Add("x", "y");
79 }
80
81 [Test]
82 public void Clone_ClonesMessages()
83 {
84 var message = new ForeignMessage { C = 20 };
85 var map = new MapField<string, ForeignMessage> { { "x", message } };
86 var clone = map.Clone();
87 map["x"].C = 30;
88 Assert.AreEqual(20, clone["x"].C);
89 }
90
91 [Test]
92 public void Add_ForbidsNullKeys()
93 {
94 var map = new MapField<string, ForeignMessage>();
95 Assert.Throws<ArgumentNullException>(() => map.Add(null, new ForeignMessage()));
96 }
97
98 [Test]
99 public void Add_AcceptsNullMessageValues()
100 {
101 var map = new MapField<string, ForeignMessage>();
102 map.Add("missing", null);
103 Assert.IsNull(map["missing"]);
104 }
105
106 [Test]
107 public void Add_ForbidsNullStringValues()
108 {
109 var map = new MapField<string, string>();
110 Assert.Throws<ArgumentNullException>(() => map.Add("missing", null));
111 }
112
113 [Test]
114 public void Add_ForbidsNullByteStringValues()
115 {
116 var map = new MapField<string, ByteString>();
117 Assert.Throws<ArgumentNullException>(() => map.Add("missing", null));
118 }
119
120 [Test]
121 public void Indexer_ForbidsNullKeys()
122 {
123 var map = new MapField<string, ForeignMessage>();
124 Assert.Throws<ArgumentNullException>(() => map[null] = new ForeignMessage());
125 }
126
127 [Test]
128 public void Indexer_AcceptsNullMessageValues()
129 {
130 var map = new MapField<string, ForeignMessage>();
131 map["missing"] = null;
132 Assert.IsNull(map["missing"]);
133 }
134
135 [Test]
136 public void Indexer_ForbidsNullStringValues()
137 {
138 var map = new MapField<string, string>();
139 Assert.Throws<ArgumentNullException>(() => map["missing"] = null);
140 }
141
142 [Test]
143 public void Indexer_ForbidsNullByteStringValues()
144 {
145 var map = new MapField<string, ByteString>();
146 Assert.Throws<ArgumentNullException>(() => map["missing"] = null);
147 }
148
149 [Test]
150 public void AddPreservesInsertionOrder()
151 {
152 var map = new MapField<string, string>();
153 map.Add("a", "v1");
154 map.Add("b", "v2");
155 map.Add("c", "v3");
156 map.Remove("b");
157 map.Add("d", "v4");
158 CollectionAssert.AreEqual(new[] { "a", "c", "d" }, map.Keys);
159 CollectionAssert.AreEqual(new[] { "v1", "v3", "v4" }, map.Values);
160 }
161
162 [Test]
163 public void EqualityIsOrderInsensitive()
164 {
165 var map1 = new MapField<string, string>();
166 map1.Add("a", "v1");
167 map1.Add("b", "v2");
168
169 var map2 = new MapField<string, string>();
170 map2.Add("b", "v2");
171 map2.Add("a", "v1");
172
173 EqualityTester.AssertEquality(map1, map2);
174 }
175
176 [Test]
177 public void EqualityIsKeySensitive()
178 {
179 var map1 = new MapField<string, string>();
Jon Skeetc0622632015-06-25 13:45:53 +0100180 map1.Add("first key", "v1");
181 map1.Add("second key", "v2");
Jon Skeetdf44ae42015-06-25 12:08:18 +0100182
183 var map2 = new MapField<string, string>();
Jon Skeetc0622632015-06-25 13:45:53 +0100184 map2.Add("third key", "v1");
185 map2.Add("fourth key", "v2");
Jon Skeetdf44ae42015-06-25 12:08:18 +0100186
187 EqualityTester.AssertInequality(map1, map2);
188 }
189
190 [Test]
191 public void EqualityIsValueSensitive()
192 {
193 // Note: Without some care, it's a little easier than one might
194 // hope to see hash collisions, but only in some environments...
195 var map1 = new MapField<string, string>();
Jon Skeetc0622632015-06-25 13:45:53 +0100196 map1.Add("a", "first value");
197 map1.Add("b", "second value");
Jon Skeetdf44ae42015-06-25 12:08:18 +0100198
199 var map2 = new MapField<string, string>();
Jon Skeetc0622632015-06-25 13:45:53 +0100200 map2.Add("a", "third value");
201 map2.Add("b", "fourth value");
Jon Skeetdf44ae42015-06-25 12:08:18 +0100202
203 EqualityTester.AssertInequality(map1, map2);
204 }
205
206 [Test]
207 public void EqualityHandlesNullValues()
208 {
209 var map1 = new MapField<string, ForeignMessage>();
210 map1.Add("a", new ForeignMessage { C = 10 });
211 map1.Add("b", null);
212
213 var map2 = new MapField<string, ForeignMessage>();
214 map2.Add("a", new ForeignMessage { C = 10 });
215 map2.Add("b", null);
216
217 EqualityTester.AssertEquality(map1, map2);
218 // Check the null value isn't ignored entirely...
219 Assert.IsTrue(map1.Remove("b"));
220 EqualityTester.AssertInequality(map1, map2);
221 map1.Add("b", new ForeignMessage());
222 EqualityTester.AssertInequality(map1, map2);
223 map1["b"] = null;
224 EqualityTester.AssertEquality(map1, map2);
225 }
226
227 [Test]
228 public void Add_Dictionary()
229 {
230 var map1 = new MapField<string, string>
231 {
232 { "x", "y" },
233 { "a", "b" }
234 };
235 var map2 = new MapField<string, string>
236 {
237 { "before", "" },
238 map1,
239 { "after", "" }
240 };
241 var expected = new MapField<string, string>
242 {
243 { "before", "" },
244 { "x", "y" },
245 { "a", "b" },
246 { "after", "" }
247 };
248 Assert.AreEqual(expected, map2);
249 CollectionAssert.AreEqual(new[] { "before", "x", "a", "after" }, map2.Keys);
250 }
251
252 // General IDictionary<TKey, TValue> behavior tests
253 [Test]
254 public void Add_KeyAlreadyExists()
255 {
256 var map = new MapField<string, string>();
257 map.Add("foo", "bar");
258 Assert.Throws<ArgumentException>(() => map.Add("foo", "baz"));
259 }
260
261 [Test]
262 public void Add_Pair()
263 {
264 var map = new MapField<string, string>();
265 ICollection<KeyValuePair<string, string>> collection = map;
266 collection.Add(NewKeyValuePair("x", "y"));
267 Assert.AreEqual("y", map["x"]);
268 Assert.Throws<ArgumentException>(() => collection.Add(NewKeyValuePair("x", "z")));
269 }
270
271 [Test]
272 public void Contains_Pair()
273 {
274 var map = new MapField<string, string> { { "x", "y" } };
275 ICollection<KeyValuePair<string, string>> collection = map;
276 Assert.IsTrue(collection.Contains(NewKeyValuePair("x", "y")));
277 Assert.IsFalse(collection.Contains(NewKeyValuePair("x", "z")));
278 Assert.IsFalse(collection.Contains(NewKeyValuePair("z", "y")));
279 }
280
281 [Test]
282 public void Remove_Key()
283 {
284 var map = new MapField<string, string>();
285 map.Add("foo", "bar");
286 Assert.AreEqual(1, map.Count);
287 Assert.IsFalse(map.Remove("missing"));
288 Assert.AreEqual(1, map.Count);
289 Assert.IsTrue(map.Remove("foo"));
290 Assert.AreEqual(0, map.Count);
291 }
292
293 [Test]
294 public void Remove_Pair()
295 {
296 var map = new MapField<string, string>();
297 map.Add("foo", "bar");
298 ICollection<KeyValuePair<string, string>> collection = map;
299 Assert.AreEqual(1, map.Count);
300 Assert.IsFalse(collection.Remove(NewKeyValuePair("wrong key", "bar")));
301 Assert.AreEqual(1, map.Count);
302 Assert.IsFalse(collection.Remove(NewKeyValuePair("foo", "wrong value")));
303 Assert.AreEqual(1, map.Count);
304 Assert.IsTrue(collection.Remove(NewKeyValuePair("foo", "bar")));
305 Assert.AreEqual(0, map.Count);
306 Assert.Throws<ArgumentException>(() => collection.Remove(new KeyValuePair<string, string>(null, "")));
307 }
308
309 [Test]
310 public void CopyTo_Pair()
311 {
312 var map = new MapField<string, string>();
313 map.Add("foo", "bar");
314 ICollection<KeyValuePair<string, string>> collection = map;
315 KeyValuePair<string, string>[] array = new KeyValuePair<string, string>[3];
316 collection.CopyTo(array, 1);
317 Assert.AreEqual(NewKeyValuePair("foo", "bar"), array[1]);
318 }
319
320 [Test]
321 public void Clear()
322 {
323 var map = new MapField<string, string> { { "x", "y" } };
324 Assert.AreEqual(1, map.Count);
325 map.Clear();
326 Assert.AreEqual(0, map.Count);
327 map.Add("x", "y");
328 Assert.AreEqual(1, map.Count);
329 }
330
331 [Test]
332 public void Indexer_Get()
333 {
334 var map = new MapField<string, string> { { "x", "y" } };
335 Assert.AreEqual("y", map["x"]);
336 Assert.Throws<KeyNotFoundException>(() => { var ignored = map["z"]; });
337 }
338
339 [Test]
340 public void Indexer_Set()
341 {
342 var map = new MapField<string, string>();
343 map["x"] = "y";
344 Assert.AreEqual("y", map["x"]);
345 map["x"] = "z"; // This won't throw, unlike Add.
346 Assert.AreEqual("z", map["x"]);
347 }
348
349 private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value)
350 {
351 return new KeyValuePair<TKey, TValue>(key, value);
352 }
353 }
354}