2 // ConcurrentDictionaryTests.cs
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 using System.Threading;
29 using MonoTests.System.Threading.Tasks;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
34 using NUnit.Framework;
36 namespace MonoTests.System.Collections.Concurrent
39 public class ConcurrentDictionaryTests
41 ConcurrentDictionary<string, int> map;
46 map = new ConcurrentDictionary<string, int> ();
52 map.TryAdd ("foo", 1);
53 map.TryAdd ("bar", 2);
58 public void AddWithoutDuplicateTest ()
63 Assert.IsTrue (map.TryGetValue("baz", out val));
64 Assert.AreEqual(2, val);
65 Assert.AreEqual(2, map["baz"]);
66 Assert.AreEqual(4, map.Count);
70 public void AddParallelWithoutDuplicateTest ()
72 ParallelTestHelper.Repeat (delegate {
76 ParallelTestHelper.ParallelStressTest (map, delegate {
77 int own = Interlocked.Increment (ref index);
79 while (!map.TryAdd ("monkey" + own.ToString (), own));
83 Assert.AreEqual (7, map.Count);
86 Assert.IsTrue (map.TryGetValue ("monkey1", out value), "#1");
87 Assert.AreEqual (1, value, "#1b");
89 Assert.IsTrue (map.TryGetValue ("monkey2", out value), "#2");
90 Assert.AreEqual (2, value, "#2b");
92 Assert.IsTrue (map.TryGetValue ("monkey3", out value), "#3");
93 Assert.AreEqual (3, value, "#3b");
95 Assert.IsTrue (map.TryGetValue ("monkey4", out value), "#4");
96 Assert.AreEqual (4, value, "#4b");
101 public void RemoveParallelTest ()
103 ParallelTestHelper.Repeat (delegate {
106 bool r1 = false, r2 = false, r3 = false;
109 ParallelTestHelper.ParallelStressTest (map, delegate {
110 int own = Interlocked.Increment (ref index);
113 r1 = map.TryRemove ("foo", out val);
116 r2 =map.TryRemove ("bar", out val);
119 r3 = map.TryRemove ("foobar", out val);
124 Assert.AreEqual (0, map.Count);
127 Assert.IsTrue (r1, "1");
128 Assert.IsTrue (r2, "2");
129 Assert.IsTrue (r3, "3");
131 Assert.IsFalse (map.TryGetValue ("foo", out value), "#1b " + value.ToString ());
132 Assert.IsFalse (map.TryGetValue ("bar", out value), "#2b");
133 Assert.IsFalse (map.TryGetValue ("foobar", out value), "#3b");
138 public void AddWithDuplicate()
140 Assert.IsFalse (map.TryAdd("foo", 6));
144 public void GetValueTest()
146 Assert.AreEqual(1, map["foo"], "#1");
147 Assert.AreEqual(2, map["bar"], "#2");
148 Assert.AreEqual(3, map.Count, "#3");
151 [Test, ExpectedException(typeof(KeyNotFoundException))]
152 public void GetValueUnknownTest()
155 Assert.IsFalse(map.TryGetValue("barfoo", out val));
160 public void ModificationTest()
165 Assert.AreEqual(9, map["foo"], "#1");
166 Assert.IsTrue(map.TryGetValue("foo", out val), "#3");
167 Assert.AreEqual(9, val, "#4");
171 public void IterateTest ()
173 string[] keys = { "foo", "bar", "foobar" };
174 int[] occurence = new int[3];
176 foreach (var kvp in map) {
177 int index = Array.IndexOf (keys, kvp.Key);
178 Assert.AreNotEqual (-1, index, "#a");
179 Assert.AreEqual (index + 1, kvp.Value, "#b");
180 Assert.Less (++occurence[index], 2, "#c");
185 public void GetOrAddTest ()
187 Assert.AreEqual (1, map.GetOrAdd ("foo", (_) => 12));
188 Assert.AreEqual (13, map.GetOrAdd ("baz", (_) => 13));
192 public void TryUpdateTest ()
194 Assert.IsFalse (map.TryUpdate ("foo", 12, 11));
195 Assert.AreEqual (1, map["foo"]);
196 Assert.IsTrue (map.TryUpdate ("foo", 11, 1));
197 Assert.AreEqual (11, map["foo"]);
201 public void AddOrUpdateTest ()
203 Assert.AreEqual (11, map.AddOrUpdate ("bar", (_) => 12, (_, __) => 11));
204 Assert.AreEqual (12, map.AddOrUpdate ("baz", (_) => 12, (_, __) => 11));
208 public void ContainsTest ()
210 Assert.IsTrue (map.ContainsKey ("foo"));
211 Assert.IsTrue (map.ContainsKey ("bar"));
212 Assert.IsTrue (map.ContainsKey ("foobar"));
213 Assert.IsFalse (map.ContainsKey ("baz"));
214 Assert.IsFalse (map.ContainsKey ("oof"));
217 class DumbClass : IEquatable<DumbClass>
221 public DumbClass (int foo)
232 public override bool Equals (object rhs)
234 DumbClass temp = rhs as DumbClass;
235 return temp == null ? false : Equals (temp);
238 public bool Equals (DumbClass rhs)
240 return this.foo == rhs.foo;
243 public override int GetHashCode ()
250 public void SameHashCodeInsertTest ()
252 var classMap = new ConcurrentDictionary<DumbClass, string> ();
254 var class1 = new DumbClass (1);
255 var class2 = new DumbClass (2);
257 Assert.IsTrue (classMap.TryAdd (class1, "class1"), "class 1");
258 Console.WriteLine ();
259 Assert.IsTrue (classMap.TryAdd (class2, "class2"), "class 2");
261 Assert.AreEqual ("class1", classMap[class1], "class 1 check");
262 Assert.AreEqual ("class2", classMap[class2], "class 2 check");
266 public void InitWithEnumerableTest ()
268 int[] data = {1,2,3,4,5,6,7,8,9,10};
269 var ndic = data.ToDictionary (x => x);
270 var cdic = new ConcurrentDictionary<int, int> (ndic);
272 foreach (var index in data) {
273 Assert.IsTrue (cdic.ContainsKey (index));
275 Assert.IsTrue (cdic.TryGetValue (index, out val));
276 Assert.AreEqual (index, val);
281 public void QueryWithSameHashCodeTest ()
283 var ids = new long[] {
295 var dict = new ConcurrentDictionary<long, long>();
298 for (var i = 0; i < 20; i++)
299 dict[-i] = -i * 1000;
301 foreach (var id in ids)
302 Assert.IsFalse (dict.TryGetValue (id, out result), id.ToString ());
304 foreach (var id in ids) {
305 Assert.IsTrue (dict.TryAdd (id, id));
306 Assert.AreEqual (id, dict[id]);
309 foreach (var id in ids) {
310 Assert.IsTrue (dict.TryRemove (id, out result));
311 Assert.AreEqual (id, result);
314 foreach (var id in ids)
315 Assert.IsFalse (dict.TryGetValue (id, out result), id.ToString () + " (second)");
319 public void NullArgumentsTest ()
321 AssertThrowsArgumentNullException (() => { var x = map[null]; });
322 AssertThrowsArgumentNullException (() => map[null] = 0);
323 AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, k => 0, (k, v) => v));
324 AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", null, (k, v) => v));
325 AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", k => 0, null));
326 AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, 0, (k, v) => v));
327 AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", 0, null));
328 AssertThrowsArgumentNullException (() => map.ContainsKey (null));
329 AssertThrowsArgumentNullException (() => map.GetOrAdd (null, 0));
331 AssertThrowsArgumentNullException (() => map.TryGetValue (null, out value));
332 AssertThrowsArgumentNullException (() => map.TryRemove (null, out value));
333 AssertThrowsArgumentNullException (() => map.TryUpdate (null, 0, 0));
336 void AssertThrowsArgumentNullException (Action action)
340 Assert.Fail ("Expected ArgumentNullException.");
341 } catch (ArgumentNullException ex) {