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
27 using System.Threading;
28 using MonoTests.System.Threading.Tasks;
29 using System.Collections.Concurrent;
32 using NUnit.Framework;
34 namespace MonoTests.System.Collections.Concurrent
37 public class ConcurrentDictionaryTests
39 ConcurrentDictionary<string, int> map;
44 map = new ConcurrentDictionary<string, int> ();
50 map.TryAdd ("foo", 1);
51 map.TryAdd ("bar", 2);
52 map.TryAdd ("foobar", 3);
56 public void AddWithoutDuplicateTest ()
61 Assert.IsTrue (map.TryGetValue("baz", out val));
62 Assert.AreEqual(2, val);
63 Assert.AreEqual(2, map["baz"]);
64 Assert.AreEqual(4, map.Count);
68 public void AddParallelWithoutDuplicateTest ()
70 ParallelTestHelper.Repeat (delegate {
74 ParallelTestHelper.ParallelStressTest (map, delegate {
75 int own = Interlocked.Increment (ref index);
77 while (!map.TryAdd ("monkey" + own.ToString (), 3));
81 Assert.AreEqual (7, map.Count);
84 Assert.IsTrue (map.TryGetValue ("monkey1", out value), "#1");
85 Assert.AreEqual (3, value, "#1b");
87 Assert.IsTrue (map.TryGetValue ("monkey2", out value), "#2");
88 Assert.AreEqual (3, value, "#2b");
90 Assert.IsTrue (map.TryGetValue ("monkey3", out value), "#3");
91 Assert.AreEqual (3, value, "#3b");
93 Assert.IsTrue (map.TryGetValue ("monkey4", out value), "#4");
94 Assert.AreEqual (3, value, "#4b");
99 public void RemoveParallelTest ()
101 ParallelTestHelper.Repeat (delegate {
104 bool r1 = false, r2 = false, r3 = false;
107 ParallelTestHelper.ParallelStressTest (map, delegate {
108 int own = Interlocked.Increment (ref index);
111 r1 = map.TryRemove ("foo", out val);
114 r2 =map.TryRemove ("bar", out val);
117 r3 = map.TryRemove ("foobar", out val);
122 Assert.AreEqual (0, map.Count);
125 Assert.IsTrue (r1, "1");
126 Assert.IsTrue (r2, "2");
127 Assert.IsTrue (r3, "3");
129 Assert.IsFalse (map.TryGetValue ("foo", out value), "#1");
130 Assert.IsFalse (map.TryGetValue ("bar", out value), "#2");
131 Assert.IsFalse (map.TryGetValue ("foobar", out value), "#3");
136 public void AddWithDuplicate()
138 Assert.IsFalse (map.TryAdd("foo", 6));
142 public void GetValueTest()
144 Assert.AreEqual(1, map["foo"], "#1");
145 Assert.AreEqual(2, map["bar"], "#2");
146 Assert.AreEqual(3, map.Count, "#3");
149 [Test, ExpectedException(typeof(ArgumentException))]
150 public void GetValueUnknownTest()
153 Assert.IsFalse(map.TryGetValue("barfoo", out val));
158 public void ModificationTest()
163 Assert.AreEqual(9, map["foo"], "#1");
164 Assert.IsTrue(map.TryGetValue("foo", out val), "#3");
165 Assert.AreEqual(9, val, "#4");