New test.
[mono.git] / mcs / class / corlib / Test / System.Collections.Concurrent / ConcurrentDictionaryTests.cs
1 #if NET_4_0
2 // ConcurrentDictionaryTests.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27 using System.Threading;
28 using MonoTests.System.Threading.Tasks;
29 using System.Collections.Concurrent;
30
31 using NUnit;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Collections.Concurrent
35 {
36         [TestFixture]
37         public class ConcurrentDictionaryTests
38         {
39                 ConcurrentDictionary<string, int> map;
40                 
41                 [SetUp]
42                 public void Setup ()
43                 {
44                         map = new ConcurrentDictionary<string, int> ();
45                         AddStuff();
46                 }
47                 
48                 void AddStuff ()
49                 {
50                         map.TryAdd ("foo", 1);
51                         map.TryAdd ("bar", 2);
52                         map.TryAdd ("foobar", 3);
53                 }
54                 
55                 [Test]
56                 public void AddWithoutDuplicateTest ()
57                 {
58                         map.TryAdd("baz", 2);
59                         int val;
60                         
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);
65                 }
66                 
67                 [Test]
68                 public void AddParallelWithoutDuplicateTest ()
69                 {
70                         ParallelTestHelper.Repeat (delegate {
71                                 Setup ();
72                                 int index = 0;
73                                 
74                                 ParallelTestHelper.ParallelStressTest (map, delegate {
75                                         int own = Interlocked.Increment (ref index);
76                                         
77                                         while (!map.TryAdd ("monkey" + own.ToString (), 3));
78                                         
79                                 }, 4);
80                                 
81                                 Assert.AreEqual (7, map.Count);
82                                 int value;
83                                 
84                                 Assert.IsTrue (map.TryGetValue ("monkey1", out value), "#1");
85                                 Assert.AreEqual (3, value, "#1b");
86                                 
87                                 Assert.IsTrue (map.TryGetValue ("monkey2", out value), "#2");
88                                 Assert.AreEqual (3, value, "#2b");
89                                 
90                                 Assert.IsTrue (map.TryGetValue ("monkey3", out value), "#3");
91                                 Assert.AreEqual (3, value, "#3b");
92                                 
93                                 Assert.IsTrue (map.TryGetValue ("monkey4", out value), "#4");
94                                 Assert.AreEqual (3, value, "#4b");
95                         });
96                 }
97                 
98                 [Test]
99                 public void RemoveParallelTest ()
100                 {
101                         ParallelTestHelper.Repeat (delegate {
102                                 Setup ();
103                                 int index = 0;
104                                 bool r1 = false, r2 = false, r3 = false;
105                                 int val;
106                                 
107                                 ParallelTestHelper.ParallelStressTest (map, delegate {
108                                         int own = Interlocked.Increment (ref index);
109                                         switch (own) {
110                                         case 1:
111                                                 r1 = map.TryRemove ("foo", out val);
112                                                 break;
113                                         case 2:
114                                           r2 =map.TryRemove ("bar", out val);
115                                                 break;
116                                         case 3:
117                                           r3 = map.TryRemove ("foobar", out val);
118                                                 break;
119                                         }
120                                 }, 3);
121                                 
122                                 Assert.AreEqual (0, map.Count);
123                                 int value;
124         
125                                 Assert.IsTrue (r1, "1");
126                                 Assert.IsTrue (r2, "2");
127                                 Assert.IsTrue (r3, "3");
128                                 
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");
132                         });
133                 }
134                 
135                 [Test]
136                 public void AddWithDuplicate()
137                 {
138                         Assert.IsFalse (map.TryAdd("foo", 6));
139                 }
140                 
141                 [Test]
142                 public void GetValueTest()
143                 {
144                         Assert.AreEqual(1, map["foo"], "#1");
145                         Assert.AreEqual(2, map["bar"], "#2");
146                         Assert.AreEqual(3, map.Count, "#3");
147                 }
148                 
149                 [Test, ExpectedException(typeof(ArgumentException))]
150                 public void GetValueUnknownTest()
151                 {
152                         int val;
153                         Assert.IsFalse(map.TryGetValue("barfoo", out val));
154                         val = map["barfoo"];
155                 }
156                 
157                 [Test]
158                 public void ModificationTest()
159                 {
160                         map["foo"] = 9;
161                         int val;
162                         
163                         Assert.AreEqual(9, map["foo"], "#1");
164                         Assert.IsTrue(map.TryGetValue("foo", out val), "#3");
165                         Assert.AreEqual(9, val, "#4");
166                 }
167         }
168 }
169 #endif