[System] UriKind.RelativeOrAbsolute workaround.
[mono.git] / mcs / class / Mono.Parallel / Test / Mono.Collections.Concurrent / CollectionStressTestHelper.cs
1 #if NET_4_0
2 // 
3 // CollectionStressTestHelper.cs
4 //  
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // 
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // 
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
32
33 using System.Threading;
34 using System.Linq;
35 using System.Threading.Tasks;
36
37 using NUnit;
38 using NUnit.Framework;
39 using MonoTests.Mono.Threading.Tasks;
40
41 namespace MonoTests.Mono.Collections.Concurrent
42 {
43         public enum CheckOrderingType {
44                 InOrder,
45                 Reversed,
46                 DontCare
47         }
48         
49         public static class CollectionStressTestHelper
50         {
51                 public static void AddStressTest (IProducerConsumerCollection<int> coll)
52                 {
53                         ParallelTestHelper.Repeat (delegate {
54                                 int amount = -1;
55                                 const int count = 10;
56                                 const int threads = 5;
57                                 
58                                 ParallelTestHelper.ParallelStressTest (coll, (q) => {
59                                         int t = Interlocked.Increment (ref amount);
60                                         for (int i = 0; i < count; i++)
61                                                 coll.TryAdd (t);
62                                 }, threads);
63                                 
64                                 Assert.AreEqual (threads * count, coll.Count, "#-1");
65                                 int[] values = new int[threads];
66                                 int temp;
67                                 while (coll.TryTake (out temp)) {
68                                         values[temp]++;
69                                 }
70                                 
71                                 for (int i = 0; i < threads; i++)
72                                         Assert.AreEqual (count, values[i], "#" + i);
73                         });
74                 }
75                 
76                 public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
77                 {
78                         ParallelTestHelper.Repeat (delegate {
79                                 
80                                 const int count = 10;
81                                 const int threads = 5;
82                                 const int delta = 5;
83                                 
84                                 for (int i = 0; i < (count + delta) * threads; i++)
85                                         while (!coll.TryAdd (i));
86                                 
87                                 bool state = true;
88                                 
89                                 Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
90                                 
91                                 ParallelTestHelper.ParallelStressTest (coll, (q) => {
92                                         bool s = true;
93                                         int t;
94                                         
95                                         for (int i = 0; i < count; i++) {
96                                                 s &= coll.TryTake (out t);
97                                                 // try again in case it was a transient failure
98                                                 if (!s && coll.TryTake (out t))
99                                                         s = true;
100                                         }
101                                         
102                                         if (!s)
103                                                 state = false;
104                                 }, threads);
105                                 
106                                 Assert.IsTrue (state, "#1");
107                                 Assert.AreEqual (delta * threads, coll.Count, "#2");
108                                 
109                                 string actual = string.Empty;
110                                 int temp;
111                                 while (coll.TryTake (out temp)) {
112                                         actual += temp.ToString ();;
113                                 }
114                                 
115                                 IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
116                                 if (order == CheckOrderingType.Reversed)
117                                         range = range.Reverse ();
118                                 
119                                 string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
120                                 
121                                 if (order == CheckOrderingType.DontCare)
122                                         CollectionAssert.AreEquivalent (expected, actual, "#3");
123                                 else 
124                                         Assert.AreEqual (expected, actual, "#3");
125                         }, 1000);
126                 }
127         }
128 }
129 #endif