[corlib] Use ManualResetEvent.WaitOne instead of Thread.Sleep to test Thread.Interrupt
[mono.git] / mcs / class / Mono.Parallel / Test / Mono.Collections.Concurrent / ConcurrentSkipListTests.cs
1 #if NET_4_0
2 // ConcurrentSkipListTests.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 System.Collections.Concurrent;
29 using Mono.Collections.Concurrent;
30
31 using NUnit;
32 using NUnit.Framework;
33
34 namespace MonoTests.Mono.Collections.Concurrent
35 {
36         [TestFixtureAttribute]
37         public class ConcurrentSkipListTests
38         {
39                 ConcurrentSkipList<int> skiplist;
40
41                 [SetUpAttribute]
42                 public void Setup()
43                 {
44                         skiplist = new ConcurrentSkipList<int>();
45                 }
46
47                 void AddStuff()
48                 {
49                         skiplist.TryAdd(1);
50                         skiplist.TryAdd(2);
51                         skiplist.TryAdd(3);
52                         skiplist.TryAdd(4);
53                 }
54
55                 [TestAttribute]
56                 public void AddTestCase()
57                 {
58                         Assert.IsTrue(skiplist.TryAdd(1), "#1");
59                         Assert.AreEqual(1, skiplist.Count, "#2");
60                 }
61
62                 [TestAttribute]
63                 public void RemoveTestCase()
64                 {
65                         Assert.IsFalse(skiplist.Remove(2), "#1");
66                         Assert.IsFalse(skiplist.Remove(3), "#2");
67                         
68                         AddStuff();
69                         int count = skiplist.Count;
70                         Assert.IsTrue(skiplist.Remove(1), "#3");
71                         Assert.IsFalse(skiplist.Remove(1), "#4");
72                         Assert.IsTrue(skiplist.Remove(4), "#5");
73                         Assert.AreEqual(count - 2, skiplist.Count, "#6");
74                 }
75                 
76                 [Test]
77                 public void ContainsTestCase()
78                 {
79                         AddStuff();
80                         Assert.IsTrue(skiplist.Contains(1), "#1");
81                         Assert.IsTrue(skiplist.Contains(2), "#2");
82                         Assert.IsTrue(skiplist.Contains(3), "#3");
83                         Assert.IsTrue(skiplist.Contains(4), "#4");
84                 }
85
86                 [TestAttribute]
87                 public void EnumerateTestCase()
88                 {
89                         AddStuff();
90                         
91                         string s = string.Empty;
92                         foreach (int i in skiplist)
93                                 s += i.ToString();
94
95                         Assert.AreEqual("1234", s);
96                 }
97
98                 [TestAttribute]
99                 public void ToArrayTestCase()
100                 {
101                         int[] expected = new int[] { 1, 2, 3, 4 };
102                         AddStuff();
103                         int[] array = skiplist.ToArray();
104                         CollectionAssert.AreEqual(expected, array, "#1");
105
106                         Array.Clear(array, 0, array.Length);
107                         skiplist.CopyTo(array, 0);
108                         CollectionAssert.AreEqual(expected, array, "#2");
109                 }
110                  
111         }
112 }
113 #endif