| OLD | NEW |
| (Empty) |
| 1 #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 | |
| 33 using System; | |
| 34 using System.Collections.Generic; | |
| 35 using Google.Protobuf.TestProtos; | |
| 36 using NUnit.Framework; | |
| 37 using System.Collections; | |
| 38 using System.Linq; | |
| 39 | |
| 40 namespace Google.Protobuf.Collections | |
| 41 { | |
| 42 /// <summary> | |
| 43 /// Tests for MapField which aren't reliant on the encoded format - | |
| 44 /// tests for serialization/deserialization are part of GeneratedMessageTest
. | |
| 45 /// </summary> | |
| 46 public class MapFieldTest | |
| 47 { | |
| 48 [Test] | |
| 49 public void Clone_ClonesMessages() | |
| 50 { | |
| 51 var message = new ForeignMessage { C = 20 }; | |
| 52 var map = new MapField<string, ForeignMessage> { { "x", message } }; | |
| 53 var clone = map.Clone(); | |
| 54 map["x"].C = 30; | |
| 55 Assert.AreEqual(20, clone["x"].C); | |
| 56 } | |
| 57 | |
| 58 [Test] | |
| 59 public void NullValues() | |
| 60 { | |
| 61 TestNullValues<int?>(0); | |
| 62 TestNullValues(""); | |
| 63 TestNullValues(new TestAllTypes()); | |
| 64 } | |
| 65 | |
| 66 private void TestNullValues<T>(T nonNullValue) | |
| 67 { | |
| 68 var map = new MapField<int, T>(false); | |
| 69 var nullValue = (T) (object) null; | |
| 70 Assert.Throws<ArgumentNullException>(() => map.Add(0, nullValue)); | |
| 71 Assert.Throws<ArgumentNullException>(() => map[0] = nullValue); | |
| 72 map.Add(1, nonNullValue); | |
| 73 map[1] = nonNullValue; | |
| 74 | |
| 75 // Doesn't throw... | |
| 76 map = new MapField<int, T>(true); | |
| 77 map.Add(0, nullValue); | |
| 78 map[0] = nullValue; | |
| 79 map.Add(1, nonNullValue); | |
| 80 map[1] = nonNullValue; | |
| 81 } | |
| 82 | |
| 83 [Test] | |
| 84 public void Add_ForbidsNullKeys() | |
| 85 { | |
| 86 var map = new MapField<string, ForeignMessage>(); | |
| 87 Assert.Throws<ArgumentNullException>(() => map.Add(null, new Foreign
Message())); | |
| 88 } | |
| 89 | |
| 90 [Test] | |
| 91 public void Indexer_ForbidsNullKeys() | |
| 92 { | |
| 93 var map = new MapField<string, ForeignMessage>(); | |
| 94 Assert.Throws<ArgumentNullException>(() => map[null] = new ForeignMe
ssage()); | |
| 95 } | |
| 96 | |
| 97 [Test] | |
| 98 public void AddPreservesInsertionOrder() | |
| 99 { | |
| 100 var map = new MapField<string, string>(); | |
| 101 map.Add("a", "v1"); | |
| 102 map.Add("b", "v2"); | |
| 103 map.Add("c", "v3"); | |
| 104 map.Remove("b"); | |
| 105 map.Add("d", "v4"); | |
| 106 CollectionAssert.AreEqual(new[] { "a", "c", "d" }, map.Keys); | |
| 107 CollectionAssert.AreEqual(new[] { "v1", "v3", "v4" }, map.Values); | |
| 108 } | |
| 109 | |
| 110 [Test] | |
| 111 public void EqualityIsOrderInsensitive() | |
| 112 { | |
| 113 var map1 = new MapField<string, string>(); | |
| 114 map1.Add("a", "v1"); | |
| 115 map1.Add("b", "v2"); | |
| 116 | |
| 117 var map2 = new MapField<string, string>(); | |
| 118 map2.Add("b", "v2"); | |
| 119 map2.Add("a", "v1"); | |
| 120 | |
| 121 EqualityTester.AssertEquality(map1, map2); | |
| 122 } | |
| 123 | |
| 124 [Test] | |
| 125 public void EqualityIsKeySensitive() | |
| 126 { | |
| 127 var map1 = new MapField<string, string>(); | |
| 128 map1.Add("first key", "v1"); | |
| 129 map1.Add("second key", "v2"); | |
| 130 | |
| 131 var map2 = new MapField<string, string>(); | |
| 132 map2.Add("third key", "v1"); | |
| 133 map2.Add("fourth key", "v2"); | |
| 134 | |
| 135 EqualityTester.AssertInequality(map1, map2); | |
| 136 } | |
| 137 | |
| 138 [Test] | |
| 139 public void Equality_Simple() | |
| 140 { | |
| 141 var map = new MapField<string, string>(); | |
| 142 EqualityTester.AssertEquality(map, map); | |
| 143 EqualityTester.AssertInequality(map, null); | |
| 144 Assert.IsFalse(map.Equals(new object())); | |
| 145 } | |
| 146 | |
| 147 [Test] | |
| 148 public void EqualityIsValueSensitive() | |
| 149 { | |
| 150 // Note: Without some care, it's a little easier than one might | |
| 151 // hope to see hash collisions, but only in some environments... | |
| 152 var map1 = new MapField<string, string>(); | |
| 153 map1.Add("a", "first value"); | |
| 154 map1.Add("b", "second value"); | |
| 155 | |
| 156 var map2 = new MapField<string, string>(); | |
| 157 map2.Add("a", "third value"); | |
| 158 map2.Add("b", "fourth value"); | |
| 159 | |
| 160 EqualityTester.AssertInequality(map1, map2); | |
| 161 } | |
| 162 | |
| 163 [Test] | |
| 164 public void EqualityHandlesNullValues() | |
| 165 { | |
| 166 var map1 = new MapField<string, ForeignMessage>(); | |
| 167 map1.Add("a", new ForeignMessage { C = 10 }); | |
| 168 map1.Add("b", null); | |
| 169 | |
| 170 var map2 = new MapField<string, ForeignMessage>(); | |
| 171 map2.Add("a", new ForeignMessage { C = 10 }); | |
| 172 map2.Add("b", null); | |
| 173 | |
| 174 EqualityTester.AssertEquality(map1, map2); | |
| 175 // Check the null value isn't ignored entirely... | |
| 176 Assert.IsTrue(map1.Remove("b")); | |
| 177 EqualityTester.AssertInequality(map1, map2); | |
| 178 map1.Add("b", new ForeignMessage()); | |
| 179 EqualityTester.AssertInequality(map1, map2); | |
| 180 map1["b"] = null; | |
| 181 EqualityTester.AssertEquality(map1, map2); | |
| 182 } | |
| 183 | |
| 184 [Test] | |
| 185 public void Add_Dictionary() | |
| 186 { | |
| 187 var map1 = new MapField<string, string> | |
| 188 { | |
| 189 { "x", "y" }, | |
| 190 { "a", "b" } | |
| 191 }; | |
| 192 var map2 = new MapField<string, string> | |
| 193 { | |
| 194 { "before", "" }, | |
| 195 map1, | |
| 196 { "after", "" } | |
| 197 }; | |
| 198 var expected = new MapField<string, string> | |
| 199 { | |
| 200 { "before", "" }, | |
| 201 { "x", "y" }, | |
| 202 { "a", "b" }, | |
| 203 { "after", "" } | |
| 204 }; | |
| 205 Assert.AreEqual(expected, map2); | |
| 206 CollectionAssert.AreEqual(new[] { "before", "x", "a", "after" }, map
2.Keys); | |
| 207 } | |
| 208 | |
| 209 // General IDictionary<TKey, TValue> behavior tests | |
| 210 [Test] | |
| 211 public void Add_KeyAlreadyExists() | |
| 212 { | |
| 213 var map = new MapField<string, string>(); | |
| 214 map.Add("foo", "bar"); | |
| 215 Assert.Throws<ArgumentException>(() => map.Add("foo", "baz")); | |
| 216 } | |
| 217 | |
| 218 [Test] | |
| 219 public void Add_Pair() | |
| 220 { | |
| 221 var map = new MapField<string, string>(); | |
| 222 ICollection<KeyValuePair<string, string>> collection = map; | |
| 223 collection.Add(NewKeyValuePair("x", "y")); | |
| 224 Assert.AreEqual("y", map["x"]); | |
| 225 Assert.Throws<ArgumentException>(() => collection.Add(NewKeyValuePai
r("x", "z"))); | |
| 226 } | |
| 227 | |
| 228 [Test] | |
| 229 public void Contains_Pair() | |
| 230 { | |
| 231 var map = new MapField<string, string> { { "x", "y" } }; | |
| 232 ICollection<KeyValuePair<string, string>> collection = map; | |
| 233 Assert.IsTrue(collection.Contains(NewKeyValuePair("x", "y"))); | |
| 234 Assert.IsFalse(collection.Contains(NewKeyValuePair("x", "z"))); | |
| 235 Assert.IsFalse(collection.Contains(NewKeyValuePair("z", "y"))); | |
| 236 } | |
| 237 | |
| 238 [Test] | |
| 239 public void Remove_Key() | |
| 240 { | |
| 241 var map = new MapField<string, string>(); | |
| 242 map.Add("foo", "bar"); | |
| 243 Assert.AreEqual(1, map.Count); | |
| 244 Assert.IsFalse(map.Remove("missing")); | |
| 245 Assert.AreEqual(1, map.Count); | |
| 246 Assert.IsTrue(map.Remove("foo")); | |
| 247 Assert.AreEqual(0, map.Count); | |
| 248 Assert.Throws<ArgumentNullException>(() => map.Remove(null)); | |
| 249 } | |
| 250 | |
| 251 [Test] | |
| 252 public void Remove_Pair() | |
| 253 { | |
| 254 var map = new MapField<string, string>(); | |
| 255 map.Add("foo", "bar"); | |
| 256 ICollection<KeyValuePair<string, string>> collection = map; | |
| 257 Assert.AreEqual(1, map.Count); | |
| 258 Assert.IsFalse(collection.Remove(NewKeyValuePair("wrong key", "bar")
)); | |
| 259 Assert.AreEqual(1, map.Count); | |
| 260 Assert.IsFalse(collection.Remove(NewKeyValuePair("foo", "wrong value
"))); | |
| 261 Assert.AreEqual(1, map.Count); | |
| 262 Assert.IsTrue(collection.Remove(NewKeyValuePair("foo", "bar"))); | |
| 263 Assert.AreEqual(0, map.Count); | |
| 264 Assert.Throws<ArgumentException>(() => collection.Remove(new KeyValu
ePair<string, string>(null, ""))); | |
| 265 } | |
| 266 | |
| 267 [Test] | |
| 268 public void CopyTo_Pair() | |
| 269 { | |
| 270 var map = new MapField<string, string>(); | |
| 271 map.Add("foo", "bar"); | |
| 272 ICollection<KeyValuePair<string, string>> collection = map; | |
| 273 KeyValuePair<string, string>[] array = new KeyValuePair<string, stri
ng>[3]; | |
| 274 collection.CopyTo(array, 1); | |
| 275 Assert.AreEqual(NewKeyValuePair("foo", "bar"), array[1]); | |
| 276 } | |
| 277 | |
| 278 [Test] | |
| 279 public void Clear() | |
| 280 { | |
| 281 var map = new MapField<string, string> { { "x", "y" } }; | |
| 282 Assert.AreEqual(1, map.Count); | |
| 283 map.Clear(); | |
| 284 Assert.AreEqual(0, map.Count); | |
| 285 map.Add("x", "y"); | |
| 286 Assert.AreEqual(1, map.Count); | |
| 287 } | |
| 288 | |
| 289 [Test] | |
| 290 public void Indexer_Get() | |
| 291 { | |
| 292 var map = new MapField<string, string> { { "x", "y" } }; | |
| 293 Assert.AreEqual("y", map["x"]); | |
| 294 Assert.Throws<KeyNotFoundException>(() => { var ignored = map["z"];
}); | |
| 295 } | |
| 296 | |
| 297 [Test] | |
| 298 public void Indexer_Set() | |
| 299 { | |
| 300 var map = new MapField<string, string>(); | |
| 301 map["x"] = "y"; | |
| 302 Assert.AreEqual("y", map["x"]); | |
| 303 map["x"] = "z"; // This won't throw, unlike Add. | |
| 304 Assert.AreEqual("z", map["x"]); | |
| 305 } | |
| 306 | |
| 307 [Test] | |
| 308 public void GetEnumerator_NonGeneric() | |
| 309 { | |
| 310 IEnumerable map = new MapField<string, string> { { "x", "y" } }; | |
| 311 CollectionAssert.AreEqual(new[] { new KeyValuePair<string, string>("
x", "y") }, | |
| 312 map.Cast<object>().ToList()); | |
| 313 } | |
| 314 | |
| 315 // Test for the explicitly-implemented non-generic IDictionary interface | |
| 316 [Test] | |
| 317 public void IDictionary_GetEnumerator() | |
| 318 { | |
| 319 IDictionary map = new MapField<string, string> { { "x", "y" } }; | |
| 320 var enumerator = map.GetEnumerator(); | |
| 321 | |
| 322 // Commented assertions show an ideal situation - it looks like | |
| 323 // the LinkedList enumerator doesn't throw when you ask for the curr
ent entry | |
| 324 // at an inappropriate time; fixing this would be more work than it'
s worth. | |
| 325 // Assert.Throws<InvalidOperationException>(() => enumerator.Current
.GetHashCode()); | |
| 326 Assert.IsTrue(enumerator.MoveNext()); | |
| 327 Assert.AreEqual("x", enumerator.Key); | |
| 328 Assert.AreEqual("y", enumerator.Value); | |
| 329 Assert.AreEqual(new DictionaryEntry("x", "y"), enumerator.Current); | |
| 330 Assert.AreEqual(new DictionaryEntry("x", "y"), enumerator.Entry); | |
| 331 Assert.IsFalse(enumerator.MoveNext()); | |
| 332 // Assert.Throws<InvalidOperationException>(() => enumerator.Current
.GetHashCode()); | |
| 333 enumerator.Reset(); | |
| 334 // Assert.Throws<InvalidOperationException>(() => enumerator.Current
.GetHashCode()); | |
| 335 Assert.IsTrue(enumerator.MoveNext()); | |
| 336 Assert.AreEqual("x", enumerator.Key); // Assume the rest are okay | |
| 337 } | |
| 338 | |
| 339 [Test] | |
| 340 public void IDictionary_Add() | |
| 341 { | |
| 342 var map = new MapField<string, string> { { "x", "y" } }; | |
| 343 IDictionary dictionary = map; | |
| 344 dictionary.Add("a", "b"); | |
| 345 Assert.AreEqual("b", map["a"]); | |
| 346 Assert.Throws<ArgumentException>(() => dictionary.Add("a", "duplicat
e")); | |
| 347 Assert.Throws<InvalidCastException>(() => dictionary.Add(new object(
), "key is bad")); | |
| 348 Assert.Throws<InvalidCastException>(() => dictionary.Add("value is b
ad", new object())); | |
| 349 } | |
| 350 | |
| 351 [Test] | |
| 352 public void IDictionary_Contains() | |
| 353 { | |
| 354 var map = new MapField<string, string> { { "x", "y" } }; | |
| 355 IDictionary dictionary = map; | |
| 356 | |
| 357 Assert.IsFalse(dictionary.Contains("a")); | |
| 358 Assert.IsFalse(dictionary.Contains(5)); | |
| 359 // Surprising, but IDictionary.Contains is only about keys. | |
| 360 Assert.IsFalse(dictionary.Contains(new DictionaryEntry("x", "y"))); | |
| 361 Assert.IsTrue(dictionary.Contains("x")); | |
| 362 } | |
| 363 | |
| 364 [Test] | |
| 365 public void IDictionary_Remove() | |
| 366 { | |
| 367 var map = new MapField<string, string> { { "x", "y" } }; | |
| 368 IDictionary dictionary = map; | |
| 369 dictionary.Remove("a"); | |
| 370 Assert.AreEqual(1, dictionary.Count); | |
| 371 dictionary.Remove(5); | |
| 372 Assert.AreEqual(1, dictionary.Count); | |
| 373 dictionary.Remove(new DictionaryEntry("x", "y")); | |
| 374 Assert.AreEqual(1, dictionary.Count); | |
| 375 dictionary.Remove("x"); | |
| 376 Assert.AreEqual(0, dictionary.Count); | |
| 377 Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null)); | |
| 378 } | |
| 379 | |
| 380 [Test] | |
| 381 public void IDictionary_CopyTo() | |
| 382 { | |
| 383 var map = new MapField<string, string> { { "x", "y" } }; | |
| 384 IDictionary dictionary = map; | |
| 385 var array = new DictionaryEntry[3]; | |
| 386 dictionary.CopyTo(array, 1); | |
| 387 CollectionAssert.AreEqual(new[] { default(DictionaryEntry), new Dict
ionaryEntry("x", "y"), default(DictionaryEntry) }, | |
| 388 array); | |
| 389 var objectArray = new object[3]; | |
| 390 dictionary.CopyTo(objectArray, 1); | |
| 391 CollectionAssert.AreEqual(new object[] { null, new DictionaryEntry("
x", "y"), null }, | |
| 392 objectArray); | |
| 393 } | |
| 394 | |
| 395 [Test] | |
| 396 public void IDictionary_IsFixedSize() | |
| 397 { | |
| 398 var map = new MapField<string, string> { { "x", "y" } }; | |
| 399 IDictionary dictionary = map; | |
| 400 Assert.IsFalse(dictionary.IsFixedSize); | |
| 401 } | |
| 402 | |
| 403 [Test] | |
| 404 public void IDictionary_Keys() | |
| 405 { | |
| 406 IDictionary dictionary = new MapField<string, string> { { "x", "y" }
}; | |
| 407 CollectionAssert.AreEqual(new[] { "x" }, dictionary.Keys); | |
| 408 } | |
| 409 | |
| 410 [Test] | |
| 411 public void IDictionary_Values() | |
| 412 { | |
| 413 IDictionary dictionary = new MapField<string, string> { { "x", "y" }
}; | |
| 414 CollectionAssert.AreEqual(new[] { "y" }, dictionary.Values); | |
| 415 } | |
| 416 | |
| 417 [Test] | |
| 418 public void IDictionary_IsSynchronized() | |
| 419 { | |
| 420 IDictionary dictionary = new MapField<string, string> { { "x", "y" }
}; | |
| 421 Assert.IsFalse(dictionary.IsSynchronized); | |
| 422 } | |
| 423 | |
| 424 [Test] | |
| 425 public void IDictionary_SyncRoot() | |
| 426 { | |
| 427 IDictionary dictionary = new MapField<string, string> { { "x", "y" }
}; | |
| 428 Assert.AreSame(dictionary, dictionary.SyncRoot); | |
| 429 } | |
| 430 | |
| 431 [Test] | |
| 432 public void IDictionary_Indexer_Get() | |
| 433 { | |
| 434 IDictionary dictionary = new MapField<string, string> { { "x", "y" }
}; | |
| 435 Assert.AreEqual("y", dictionary["x"]); | |
| 436 Assert.IsNull(dictionary["a"]); | |
| 437 Assert.IsNull(dictionary[5]); | |
| 438 Assert.Throws<ArgumentNullException>(() => dictionary[null].GetHashC
ode()); | |
| 439 } | |
| 440 | |
| 441 [Test] | |
| 442 public void IDictionary_Indexer_Set() | |
| 443 { | |
| 444 var map = new MapField<string, string> { { "x", "y" } }; | |
| 445 IDictionary dictionary = map; | |
| 446 map["a"] = "b"; | |
| 447 Assert.AreEqual("b", map["a"]); | |
| 448 map["a"] = "c"; | |
| 449 Assert.AreEqual("c", map["a"]); | |
| 450 Assert.Throws<InvalidCastException>(() => dictionary[5] = "x"); | |
| 451 Assert.Throws<InvalidCastException>(() => dictionary["x"] = 5); | |
| 452 Assert.Throws<ArgumentNullException>(() => dictionary[null] = "z"); | |
| 453 Assert.Throws<ArgumentNullException>(() => dictionary["x"] = null); | |
| 454 } | |
| 455 | |
| 456 [Test] | |
| 457 public void AllowNullValues_Property() | |
| 458 { | |
| 459 // Non-message reference type values are non-nullable by default, bu
t can be overridden | |
| 460 Assert.IsFalse(new MapField<int, string>().AllowsNullValues); | |
| 461 Assert.IsFalse(new MapField<int, string>(false).AllowsNullValues); | |
| 462 Assert.IsTrue(new MapField<int, string>(true).AllowsNullValues); | |
| 463 | |
| 464 // Non-nullable value type values are never nullable | |
| 465 Assert.IsFalse(new MapField<int, int>().AllowsNullValues); | |
| 466 Assert.IsFalse(new MapField<int, int>(false).AllowsNullValues); | |
| 467 Assert.Throws<ArgumentException>(() => new MapField<int, int>(true))
; | |
| 468 | |
| 469 // Message type values are nullable by default, but can be overridde
n | |
| 470 Assert.IsTrue(new MapField<int, TestAllTypes>().AllowsNullValues); | |
| 471 Assert.IsFalse(new MapField<int, TestAllTypes>(false).AllowsNullValu
es); | |
| 472 Assert.IsTrue(new MapField<int, TestAllTypes>(true).AllowsNullValues
); | |
| 473 | |
| 474 // Nullable value type values are nullable by default, but can be ov
erridden | |
| 475 Assert.IsTrue(new MapField<int, int?>().AllowsNullValues); | |
| 476 Assert.IsFalse(new MapField<int, int?>(false).AllowsNullValues); | |
| 477 Assert.IsTrue(new MapField<int, int?>(true).AllowsNullValues); | |
| 478 } | |
| 479 | |
| 480 [Test] | |
| 481 public void KeysReturnsLiveView() | |
| 482 { | |
| 483 var map = new MapField<string, string>(); | |
| 484 var keys = map.Keys; | |
| 485 CollectionAssert.AreEqual(new string[0], keys); | |
| 486 map["foo"] = "bar"; | |
| 487 map["x"] = "y"; | |
| 488 CollectionAssert.AreEqual(new[] { "foo", "x" }, keys); | |
| 489 } | |
| 490 | |
| 491 [Test] | |
| 492 public void ValuesReturnsLiveView() | |
| 493 { | |
| 494 var map = new MapField<string, string>(); | |
| 495 var values = map.Values; | |
| 496 CollectionAssert.AreEqual(new string[0], values); | |
| 497 map["foo"] = "bar"; | |
| 498 map["x"] = "y"; | |
| 499 CollectionAssert.AreEqual(new[] { "bar", "y" }, values); | |
| 500 } | |
| 501 | |
| 502 // Just test keys - we know the implementation is the same for values | |
| 503 [Test] | |
| 504 public void ViewsAreReadOnly() | |
| 505 { | |
| 506 var map = new MapField<string, string>(); | |
| 507 var keys = map.Keys; | |
| 508 Assert.IsTrue(keys.IsReadOnly); | |
| 509 Assert.Throws<NotSupportedException>(() => keys.Clear()); | |
| 510 Assert.Throws<NotSupportedException>(() => keys.Remove("a")); | |
| 511 Assert.Throws<NotSupportedException>(() => keys.Add("a")); | |
| 512 } | |
| 513 | |
| 514 // Just test keys - we know the implementation is the same for values | |
| 515 [Test] | |
| 516 public void ViewCopyTo() | |
| 517 { | |
| 518 var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y
" } }; | |
| 519 var keys = map.Keys; | |
| 520 var array = new string[4]; | |
| 521 Assert.Throws<ArgumentException>(() => keys.CopyTo(array, 3)); | |
| 522 Assert.Throws<ArgumentOutOfRangeException>(() => keys.CopyTo(array,
-1)); | |
| 523 keys.CopyTo(array, 1); | |
| 524 CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); | |
| 525 } | |
| 526 | |
| 527 // Just test keys - we know the implementation is the same for values | |
| 528 [Test] | |
| 529 public void NonGenericViewCopyTo() | |
| 530 { | |
| 531 IDictionary map = new MapField<string, string> { { "foo", "bar" }, {
"x", "y" } }; | |
| 532 ICollection keys = map.Keys; | |
| 533 // Note the use of the Array type here rather than string[] | |
| 534 Array array = new string[4]; | |
| 535 Assert.Throws<ArgumentException>(() => keys.CopyTo(array, 3)); | |
| 536 Assert.Throws<ArgumentOutOfRangeException>(() => keys.CopyTo(array,
-1)); | |
| 537 keys.CopyTo(array, 1); | |
| 538 CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); | |
| 539 } | |
| 540 | |
| 541 [Test] | |
| 542 public void KeysContains() | |
| 543 { | |
| 544 var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y
" } }; | |
| 545 var keys = map.Keys; | |
| 546 Assert.IsTrue(keys.Contains("foo")); | |
| 547 Assert.IsFalse(keys.Contains("bar")); // It's a value! | |
| 548 Assert.IsFalse(keys.Contains("1")); | |
| 549 // Keys can't be null, so we should prevent contains check | |
| 550 Assert.Throws<ArgumentNullException>(() => keys.Contains(null)); | |
| 551 } | |
| 552 | |
| 553 [Test] | |
| 554 public void ValuesContains() | |
| 555 { | |
| 556 var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y
" } }; | |
| 557 var values = map.Values; | |
| 558 Assert.IsTrue(values.Contains("bar")); | |
| 559 Assert.IsFalse(values.Contains("foo")); // It's a key! | |
| 560 Assert.IsFalse(values.Contains("1")); | |
| 561 // Values can be null, so this makes sense | |
| 562 Assert.IsFalse(values.Contains(null)); | |
| 563 } | |
| 564 | |
| 565 private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(
TKey key, TValue value) | |
| 566 { | |
| 567 return new KeyValuePair<TKey, TValue>(key, value); | |
| 568 } | |
| 569 } | |
| 570 } | |
| OLD | NEW |