Check key for null in ConcurrentDictionary
[mono.git] / mcs / class / corlib / Test / System.Collections.Concurrent / ConcurrentDictionaryTests.cs
index 70888e8637bca4080e656eabee40bd46d0675195..5978f6b219fe335253770e7bb0947cc784ff9377 100644 (file)
@@ -314,6 +314,33 @@ namespace MonoTests.System.Collections.Concurrent
                        foreach (var id in ids)
                                Assert.IsFalse (dict.TryGetValue (id, out result), id.ToString () + " (second)");
                }
+
+               [Test]
+               public void NullArgumentsTest ()
+               {
+                       AssertThrowsArgumentNullException (() => { var x = map[null]; });
+                       AssertThrowsArgumentNullException (() => map[null] = 0);
+                       AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, k => 0, (k, v) => v));
+                       AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", null, (k, v) => v));
+                       AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", k => 0, null));
+                       AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, 0, (k, v) => v));
+                       AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", 0, null));
+                       AssertThrowsArgumentNullException (() => map.ContainsKey (null));
+                       AssertThrowsArgumentNullException (() => map.GetOrAdd (null, 0));
+                       int value;
+                       AssertThrowsArgumentNullException (() => map.TryGetValue (null, out value));
+                       AssertThrowsArgumentNullException (() => map.TryRemove (null, out value));
+                       AssertThrowsArgumentNullException (() => map.TryUpdate (null, 0, 0));
+               } 
+
+               void AssertThrowsArgumentNullException (Action action)
+               {
+                       try {
+                               action ();
+                               Assert.Fail ("Expected ArgumentNullException.");
+                       } catch (ArgumentNullException ex) {
+                       }
+               }
        }
 }
 #endif