Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
2010-06-03 Jérémie Laval <jeremie.laval@gmail.com>
* CollectionStressTestHelper.cs: Bump remove test repeat to 1000.
Use a local variable for tracking status and only update the global one
at the end.
2010-03-24 Jérémie Laval <jeremie.laval@gmail.com>
* ConcurrentDictionaryTests.cs: Renaming in Assert
2010-03-24 Jérémie Laval <jeremie.laval@gmail.com>
* ConcurrentDictionaryTests.cs: Update behavior of TryAddDuplicateTest
The method returns false and doesn't throw exception anymore
2010-02-02 Jérémie Laval <jeremie.laval@gmail.com>
* CollectionStressTestHelper.cs:
* ConcurrentBagTests.cs:
* ConcurrentDictionaryTests.cs:
* ConcurrentQueueTests.cs:
* ConcurrentSkipListTests.cs:
* ConcurrentStackTests.cs:
* ParallelConcurrentQueueTests.cs:
* ParallelConcurrentStackTests.cs: Update namespaces and tested methods
2009-08-11 Jérémie Laval <jeremie.laval@gmail.com>
* BlockingCollectionTests.cs: Moved file.
2009-08-05 Jérémie Laval <jeremie.laval@gmail.com>
* ConcurrentDictionaryTests.cs: Re-enable ConcurrentDictionary unit
tests.
2009-08-04 Raja R Harinath <harinath@hurrynot.org>
* ParallelConcurrentQueueTests.cs (CountTestCase): Remove call to
internal method.
2009-07-31 Jérémie Laval <jeremie.laval@gmail.com>
* ConcurrentDictionary: Ignore tests until runtime/compiler
is fixed
2009-07-31 Jérémie Laval <jeremie.laval@gmail.com>
* ConcurrentDictionary.cs:
* ConcurrentQueue.cs: Adapt test to new API
2009-07-27 Jérémie Laval <jeremie.laval@gmail.com>
* BlockingCollectionTests.cs:
* CollectionStressTestHelper.cs:
* ConcurrentBagTests.cs:
* ConcurrentDictionaryTests.cs:
* ConcurrentQueueTests.cs:
* ConcurrentSkipListTests.cs:
* ConcurrentStackTests.cs:
* ParallelConcurrentQueueTests.cs:
* ParallelConcurrentStackTests.cs: ParallelFx unit tests for
System.Collections.Concurrent namespace.

View File

@@ -0,0 +1,129 @@
#if NET_4_0
//
// CollectionStressTestHelper.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Linq;
using MonoTests.System.Threading.Tasks;
using NUnit;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace MonoTests.System.Collections.Concurrent
{
public enum CheckOrderingType {
InOrder,
Reversed,
DontCare
}
public static class CollectionStressTestHelper
{
public static void AddStressTest (IProducerConsumerCollection<int> coll)
{
ParallelTestHelper.Repeat (delegate {
int amount = -1;
const int count = 10;
const int threads = 5;
ParallelTestHelper.ParallelStressTest (coll, (q) => {
int t = Interlocked.Increment (ref amount);
for (int i = 0; i < count; i++)
coll.TryAdd (t);
}, threads);
Assert.AreEqual (threads * count, coll.Count, "#-1");
int[] values = new int[threads];
int temp;
while (coll.TryTake (out temp)) {
values[temp]++;
}
for (int i = 0; i < threads; i++)
Assert.AreEqual (count, values[i], "#" + i);
});
}
public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
{
ParallelTestHelper.Repeat (delegate {
const int count = 10;
const int threads = 5;
const int delta = 5;
for (int i = 0; i < (count + delta) * threads; i++)
while (!coll.TryAdd (i));
bool state = true;
Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
ParallelTestHelper.ParallelStressTest (coll, (q) => {
bool s = true;
int t;
for (int i = 0; i < count; i++) {
s &= coll.TryTake (out t);
// try again in case it was a transient failure
if (!s && coll.TryTake (out t))
s = true;
}
if (!s)
state = false;
}, threads);
Assert.IsTrue (state, "#1");
Assert.AreEqual (delta * threads, coll.Count, "#2");
string actual = string.Empty;
int temp;
while (coll.TryTake (out temp)) {
actual += temp.ToString ();;
}
IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
if (order == CheckOrderingType.Reversed)
range = range.Reverse ();
string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
if (order == CheckOrderingType.DontCare)
Assert.That (actual, new CollectionEquivalentConstraint (expected), "#3");
else
Assert.AreEqual (expected, actual, "#3");
}, 10);
}
}
}
#endif

View File

@@ -0,0 +1,358 @@
#if NET_4_0
// ConcurrentDictionaryTests.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Linq;
using System.Threading;
using MonoTests.System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using NUnit;
using NUnit.Framework;
#if !MOBILE
using NUnit.Framework.SyntaxHelpers;
#endif
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture]
public class ConcurrentDictionaryTests
{
ConcurrentDictionary<string, int> map;
[SetUp]
public void Setup ()
{
map = new ConcurrentDictionary<string, int> ();
AddStuff();
}
void AddStuff ()
{
map.TryAdd ("foo", 1);
map.TryAdd ("bar", 2);
map["foobar"] = 3;
}
[Test]
public void AddWithoutDuplicateTest ()
{
map.TryAdd("baz", 2);
int val;
Assert.IsTrue (map.TryGetValue("baz", out val));
Assert.AreEqual(2, val);
Assert.AreEqual(2, map["baz"]);
Assert.AreEqual(4, map.Count);
}
[Test]
public void AddParallelWithoutDuplicateTest ()
{
ParallelTestHelper.Repeat (delegate {
Setup ();
int index = 0;
ParallelTestHelper.ParallelStressTest (map, delegate {
int own = Interlocked.Increment (ref index);
while (!map.TryAdd ("monkey" + own.ToString (), own));
}, 4);
Assert.AreEqual (7, map.Count);
int value;
Assert.IsTrue (map.TryGetValue ("monkey1", out value), "#1");
Assert.AreEqual (1, value, "#1b");
Assert.IsTrue (map.TryGetValue ("monkey2", out value), "#2");
Assert.AreEqual (2, value, "#2b");
Assert.IsTrue (map.TryGetValue ("monkey3", out value), "#3");
Assert.AreEqual (3, value, "#3b");
Assert.IsTrue (map.TryGetValue ("monkey4", out value), "#4");
Assert.AreEqual (4, value, "#4b");
});
}
[Test]
public void RemoveParallelTest ()
{
ParallelTestHelper.Repeat (delegate {
Setup ();
int index = 0;
bool r1 = false, r2 = false, r3 = false;
int val;
ParallelTestHelper.ParallelStressTest (map, delegate {
int own = Interlocked.Increment (ref index);
switch (own) {
case 1:
r1 = map.TryRemove ("foo", out val);
break;
case 2:
r2 =map.TryRemove ("bar", out val);
break;
case 3:
r3 = map.TryRemove ("foobar", out val);
break;
}
}, 3);
Assert.AreEqual (0, map.Count);
int value;
Assert.IsTrue (r1, "1");
Assert.IsTrue (r2, "2");
Assert.IsTrue (r3, "3");
Assert.IsFalse (map.TryGetValue ("foo", out value), "#1b " + value.ToString ());
Assert.IsFalse (map.TryGetValue ("bar", out value), "#2b");
Assert.IsFalse (map.TryGetValue ("foobar", out value), "#3b");
});
}
[Test]
public void AddWithDuplicate()
{
Assert.IsFalse (map.TryAdd("foo", 6));
}
[Test]
public void GetValueTest()
{
Assert.AreEqual(1, map["foo"], "#1");
Assert.AreEqual(2, map["bar"], "#2");
Assert.AreEqual(3, map.Count, "#3");
}
[Test, ExpectedException(typeof(KeyNotFoundException))]
public void GetValueUnknownTest()
{
int val;
Assert.IsFalse(map.TryGetValue("barfoo", out val));
val = map["barfoo"];
}
[Test]
public void ModificationTest()
{
map["foo"] = 9;
int val;
Assert.AreEqual(9, map["foo"], "#1");
Assert.IsTrue(map.TryGetValue("foo", out val), "#3");
Assert.AreEqual(9, val, "#4");
}
[Test]
public void IterateTest ()
{
string[] keys = { "foo", "bar", "foobar" };
int[] occurence = new int[3];
foreach (var kvp in map) {
int index = Array.IndexOf (keys, kvp.Key);
Assert.AreNotEqual (-1, index, "#a");
Assert.AreEqual (index + 1, kvp.Value, "#b");
Assert.That (++occurence[index], Is.LessThan (2), "#c");
}
}
[Test]
public void GetOrAddTest ()
{
Assert.AreEqual (1, map.GetOrAdd ("foo", (_) => 12));
Assert.AreEqual (13, map.GetOrAdd ("baz", (_) => 13));
}
[Test]
public void TryUpdateTest ()
{
Assert.IsFalse (map.TryUpdate ("foo", 12, 11));
Assert.AreEqual (1, map["foo"]);
Assert.IsTrue (map.TryUpdate ("foo", 11, 1));
Assert.AreEqual (11, map["foo"]);
}
[Test]
public void AddOrUpdateTest ()
{
Assert.AreEqual (11, map.AddOrUpdate ("bar", (_) => 12, (_, __) => 11));
Assert.AreEqual (12, map.AddOrUpdate ("baz", (_) => 12, (_, __) => 11));
}
[Test]
public void ContainsTest ()
{
Assert.IsTrue (map.ContainsKey ("foo"));
Assert.IsTrue (map.ContainsKey ("bar"));
Assert.IsTrue (map.ContainsKey ("foobar"));
Assert.IsFalse (map.ContainsKey ("baz"));
Assert.IsFalse (map.ContainsKey ("oof"));
}
class DumbClass : IEquatable<DumbClass>
{
int foo;
public DumbClass (int foo)
{
this.foo = foo;
}
public int Foo {
get {
return foo;
}
}
public override bool Equals (object rhs)
{
DumbClass temp = rhs as DumbClass;
return temp == null ? false : Equals (temp);
}
public bool Equals (DumbClass rhs)
{
return this.foo == rhs.foo;
}
public override int GetHashCode ()
{
return 5;
}
}
[Test]
public void SameHashCodeInsertTest ()
{
var classMap = new ConcurrentDictionary<DumbClass, string> ();
var class1 = new DumbClass (1);
var class2 = new DumbClass (2);
Assert.IsTrue (classMap.TryAdd (class1, "class1"), "class 1");
Console.WriteLine ();
Assert.IsTrue (classMap.TryAdd (class2, "class2"), "class 2");
Assert.AreEqual ("class1", classMap[class1], "class 1 check");
Assert.AreEqual ("class2", classMap[class2], "class 2 check");
}
[Test]
public void InitWithEnumerableTest ()
{
int[] data = {1,2,3,4,5,6,7,8,9,10};
var ndic = data.ToDictionary (x => x);
var cdic = new ConcurrentDictionary<int, int> (ndic);
foreach (var index in data) {
Assert.IsTrue (cdic.ContainsKey (index));
int val;
Assert.IsTrue (cdic.TryGetValue (index, out val));
Assert.AreEqual (index, val);
}
}
[Test]
public void QueryWithSameHashCodeTest ()
{
var ids = new long[] {
34359738370,
34359738371,
34359738372,
34359738373,
34359738374,
34359738375,
34359738376,
34359738377,
34359738420
};
var dict = new ConcurrentDictionary<long, long>();
long result;
for (var i = 0; i < 20; i++)
dict[-i] = -i * 1000;
foreach (var id in ids)
Assert.IsFalse (dict.TryGetValue (id, out result), id.ToString ());
foreach (var id in ids) {
Assert.IsTrue (dict.TryAdd (id, id));
Assert.AreEqual (id, dict[id]);
}
foreach (var id in ids) {
Assert.IsTrue (dict.TryRemove (id, out result));
Assert.AreEqual (id, result);
}
foreach (var id in ids)
Assert.IsFalse (dict.TryGetValue (id, out result), id.ToString () + " (second)");
}
[Test]
public void NullArgumentsTest ()
{
AssertThrowsArgumentNullException (() => { var x = map[null]; });
AssertThrowsArgumentNullException (() => map[null] = 0);
AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, k => 0, (k, v) => v));
AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", null, (k, v) => v));
AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", k => 0, null));
AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, 0, (k, v) => v));
AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", 0, null));
AssertThrowsArgumentNullException (() => map.ContainsKey (null));
AssertThrowsArgumentNullException (() => map.GetOrAdd (null, 0));
int value;
AssertThrowsArgumentNullException (() => map.TryGetValue (null, out value));
AssertThrowsArgumentNullException (() => map.TryRemove (null, out value));
AssertThrowsArgumentNullException (() => map.TryUpdate (null, 0, 0));
}
[Test]
public void IDictionaryNullOnNonExistingKey ()
{
IDictionary dict = new ConcurrentDictionary<long, string> ();
object val = dict [1234L];
Assert.IsNull (val);
}
void AssertThrowsArgumentNullException (Action action)
{
try {
action ();
Assert.Fail ("Expected ArgumentNullException.");
} catch (ArgumentNullException ex) {
}
}
}
}
#endif

View File

@@ -0,0 +1,220 @@
#if NET_4_0
// ConcurrentQueueTest.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using System.Collections.Concurrent;
using NUnit.Framework;
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture()]
public class ConcurrentQueueTests
{
ConcurrentQueue<int> queue;
[SetUpAttribute]
public void Setup()
{
queue = new ConcurrentQueue<int>();
for (int i = 0; i < 10; i++) {
queue.Enqueue(i);
}
}
[Test]
public void StressEnqueueTestCase ()
{
/*ParallelTestHelper.Repeat (delegate {
queue = new ConcurrentQueue<int> ();
int amount = -1;
const int count = 10;
const int threads = 5;
ParallelTestHelper.ParallelStressTest (queue, (q) => {
int t = Interlocked.Increment (ref amount);
for (int i = 0; i < count; i++)
queue.Enqueue (t);
}, threads);
Assert.AreEqual (threads * count, queue.Count, "#-1");
int[] values = new int[threads];
int temp;
while (queue.TryDequeue (out temp)) {
values[temp]++;
}
for (int i = 0; i < threads; i++)
Assert.AreEqual (count, values[i], "#" + i);
});*/
CollectionStressTestHelper.AddStressTest (new ConcurrentQueue<int> ());
}
[Test]
public void StressDequeueTestCase ()
{
/*ParallelTestHelper.Repeat (delegate {
queue = new ConcurrentQueue<int> ();
const int count = 10;
const int threads = 5;
const int delta = 5;
for (int i = 0; i < (count + delta) * threads; i++)
queue.Enqueue (i);
bool state = true;
ParallelTestHelper.ParallelStressTest (queue, (q) => {
int t;
for (int i = 0; i < count; i++)
state &= queue.TryDequeue (out t);
}, threads);
Assert.IsTrue (state, "#1");
Assert.AreEqual (delta * threads, queue.Count, "#2");
string actual = string.Empty;
int temp;
while (queue.TryDequeue (out temp)) {
actual += temp;
}
string expected = Enumerable.Range (count * threads, delta * threads)
.Aggregate (string.Empty, (acc, v) => acc + v);
Assert.AreEqual (expected, actual, "#3");
});*/
CollectionStressTestHelper.RemoveStressTest (new ConcurrentQueue<int> (), CheckOrderingType.InOrder);
}
[Test]
public void CountTestCase()
{
Assert.AreEqual(10, queue.Count, "#1");
int value;
queue.TryPeek(out value);
queue.TryDequeue(out value);
queue.TryDequeue(out value);
Assert.AreEqual(8, queue.Count, "#2");
}
//[Ignore]
[Test]
public void EnumerateTestCase()
{
string s = string.Empty;
foreach (int i in queue) {
s += i;
}
Assert.AreEqual("0123456789", s, "#1 : " + s);
}
[Test()]
public void TryPeekTestCase()
{
int value;
queue.TryPeek(out value);
Assert.AreEqual(0, value, "#1 : " + value);
queue.TryDequeue(out value);
Assert.AreEqual(0, value, "#2 : " + value);
queue.TryDequeue(out value);
Assert.AreEqual(1, value, "#3 : " + value);
queue.TryPeek(out value);
Assert.AreEqual(2, value, "#4 : " + value);
queue.TryPeek(out value);
Assert.AreEqual(2, value, "#5 : " + value);
}
[Test()]
public void TryDequeueTestCase()
{
int value;
queue.TryPeek(out value);
Assert.AreEqual(0, value, "#1");
Assert.IsTrue(queue.TryDequeue(out value), "#2");
Assert.IsTrue(queue.TryDequeue(out value), "#3");
Assert.AreEqual(1, value, "#4");
}
[Test()]
public void TryDequeueEmptyTestCase()
{
int value;
queue = new ConcurrentQueue<int> ();
queue.Enqueue(1);
Assert.IsTrue(queue.TryDequeue(out value), "#1");
Assert.IsFalse(queue.TryDequeue(out value), "#2");
Assert.IsTrue(queue.IsEmpty, "#3");
}
[Test]
public void ToArrayTest()
{
int[] array = queue.ToArray();
string s = string.Empty;
foreach (int i in array) {
s += i;
}
Assert.AreEqual("0123456789", s, "#1 : " + s);
queue.CopyTo(array, 0);
s = string.Empty;
foreach (int i in array) {
s += i;
}
Assert.AreEqual("0123456789", s, "#2 : " + s);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void ToExistingArray_Null ()
{
queue.CopyTo (null, 0);
}
[Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
public void ToExistingArray_OutOfRange ()
{
queue.CopyTo (new int[3], -1);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void ToExistingArray_IndexOverflow ()
{
queue.CopyTo (new int[3], 4);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void ToExistingArray_Overflow ()
{
queue.CopyTo (new int[3], 0);
}
}
}
#endif

View File

@@ -0,0 +1,306 @@
#if NET_4_0
// ConcurrentStackTests.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Threading;
using System.Linq;
using System.Collections.Concurrent;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture()]
public class ConcurrentStackTests
{
ConcurrentStack<int> stack;
[SetUpAttribute]
public void Setup()
{
stack = new ConcurrentStack<int>();
for (int i = 0; i < 10; i++) {
stack.Push(i);
}
}
[Test]
public void StressPushTestCase ()
{
/*ParallelTestHelper.Repeat (delegate {
stack = new ConcurrentStack<int> ();
int amount = -1;
const int count = 10;
const int threads = 5;
ParallelTestHelper.ParallelStressTest (stack, (q) => {
int t = Interlocked.Increment (ref amount);
for (int i = 0; i < count; i++)
stack.Push (t);
}, threads);
Assert.AreEqual (threads * count, stack.Count, "#-1");
int[] values = new int[threads];
int temp;
while (stack.TryPop (out temp)) {
values[temp]++;
}
for (int i = 0; i < threads; i++)
Assert.AreEqual (count, values[i], "#" + i);
});*/
CollectionStressTestHelper.AddStressTest (new ConcurrentStack<int> ());
}
[Test]
public void StressPopTestCase ()
{
/*ParallelTestHelper.Repeat (delegate {
stack = new ConcurrentStack<int> ();
const int count = 10;
const int threads = 5;
const int delta = 5;
for (int i = 0; i < (count + delta) * threads; i++)
stack.Push (i);
bool state = true;
ParallelTestHelper.ParallelStressTest (stack, (q) => {
int t;
for (int i = 0; i < count; i++)
state &= stack.TryPop (out t);
}, threads);
Assert.IsTrue (state, "#1");
Assert.AreEqual (delta * threads, stack.Count, "#2");
string actual = string.Empty;
int temp;
while (stack.TryPop (out temp)) {
actual += temp;
}
string expected = Enumerable.Range (0, delta * threads).Reverse()
.Aggregate (string.Empty, (acc, v) => acc + v);
Assert.AreEqual (expected, actual, "#3");
});*/
CollectionStressTestHelper.RemoveStressTest (new ConcurrentStack<int> (), CheckOrderingType.Reversed);
}
[Test]
public void CountTestCase()
{
Assert.IsTrue(stack.Count == 10, "#1");
int value;
stack.TryPeek(out value);
stack.TryPop(out value);
stack.TryPop(out value);
Assert.IsTrue(stack.Count == 8, "#2");
stack.Clear();
Assert.IsTrue(stack.Count == 0, "#3");
Assert.IsTrue(stack.IsEmpty, "#4");
}
[Test()]
public void EnumerateTestCase()
{
string s = string.Empty;
foreach (int i in stack) {
s += i;
}
Assert.IsTrue(s == "9876543210", "#1 : " + s);
}
[Test()]
public void TryPeekTestCase()
{
int value;
stack.TryPeek(out value);
Assert.IsTrue(value == 9, "#1 : " + value);
stack.TryPop(out value);
Assert.IsTrue(value == 9, "#2 : " + value);
stack.TryPop(out value);
Assert.IsTrue(value == 8, "#3 : " + value);
stack.TryPeek(out value);
Assert.IsTrue(value == 7, "#4 : " + value);
stack.TryPeek(out value);
Assert.IsTrue(value == 7, "#5 : " + value);
}
[Test()]
public void TryPopTestCase()
{
int value;
stack.TryPeek(out value);
Assert.IsTrue(value == 9, "#1");
stack.TryPop(out value);
stack.TryPop(out value);
Assert.IsTrue(value == 8, "#2 : " + value);
}
[Test()]
public void TryPopEmptyTestCase()
{
int value;
stack.Clear();
stack.Push(1);
Assert.IsTrue(stack.TryPop(out value), "#1");
Assert.IsFalse(stack.TryPop(out value), "#2");
Assert.IsTrue(stack.IsEmpty, "#3");
}
[Test]
public void ToArrayTest()
{
int[] array = stack.ToArray();
string s = string.Empty;
foreach (int i in array) {
s += i;
}
Assert.IsTrue(s == "9876543210", "#1 : " + s);
stack.CopyTo(array, 0);
s = string.Empty;
foreach (int i in array) {
s += i;
}
Assert.IsTrue(s == "9876543210", "#1 : " + s);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void ToExistingArray_Null ()
{
stack.CopyTo (null, 0);
}
[Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
public void ToExistingArray_OutOfRange ()
{
stack.CopyTo (new int[3], -1);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void ToExistingArray_IndexOverflow ()
{
stack.CopyTo (new int[3], 4);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void ToExistingArray_Overflow ()
{
stack.CopyTo (new int[3], 0);
}
[Test]
public void TryPopRangeTest ()
{
int[] values = new int[3];
Assert.AreEqual (3, stack.TryPopRange (values));
Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9, 8, 7 }));
Assert.AreEqual (10 - values.Length, stack.Count);
for (int i = 9 - values.Length; i >= 0; i--) {
int outValue;
Assert.IsTrue (stack.TryPop (out outValue));
Assert.AreEqual (i, outValue);
}
}
[Test]
public void TryPopRangeEmpty ()
{
stack = new ConcurrentStack<int>();
Assert.AreEqual (0, stack.TryPopRange (new int [1]));
}
[Test]
public void TryPopRangeTestWithOneElement ()
{
int[] values = new int[1];
Assert.AreEqual (1, stack.TryPopRange (values));
Assert.That (values, new CollectionEquivalentConstraint (new int[] { 9 }));
Assert.AreEqual (10 - values.Length, stack.Count);
for (int i = 9 - values.Length; i >= 0; i--) {
int outValue;
Assert.IsTrue (stack.TryPop (out outValue));
Assert.AreEqual (i, outValue);
}
}
[Test]
public void TryPopRangeFullTest ()
{
int[] values = new int[10];
Assert.AreEqual (10, stack.TryPopRange (values));
Assert.That (values, new CollectionEquivalentConstraint (Enumerable.Range (0, 10).Reverse ()));
Assert.AreEqual (0, stack.Count);
}
[Test]
public void TryPopRangePartialFillTest ()
{
int[] values = new int[5];
Assert.AreEqual (2, stack.TryPopRange (values, 3, 2));
Assert.That (values, new CollectionEquivalentConstraint (new int[] { 0, 0, 0, 9, 8 }));
Assert.AreEqual (8, stack.Count);
}
[Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TryPopRange_NegativeIndex ()
{
stack.TryPopRange (new int[3], -2, 3);
}
[Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TryPopRange_LargeIndex ()
{
stack.TryPopRange (new int[3], 200, 3);
}
[Test, ExpectedException (typeof (ArgumentException))]
public void TryPopRange_LargeCount ()
{
stack.TryPopRange (new int[3], 2, 5);
}
[Test, ExpectedException (typeof (ArgumentNullException))]
public void TryPopRange_NullArray ()
{
stack.TryPopRange (null);
}
[Test]
public void PushRangeTestCase()
{
var testStack = new ConcurrentStack<int>();
var testData = new int[] { 1, 2, 3, 4, 5 };
testStack.PushRange (testData);
Assert.AreEqual (testData.Length, testStack.Count);
}
}
}
#endif

View File

@@ -0,0 +1,59 @@
#if NET_4_0
// ParallelConcurrentQueueTests.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Collections.Concurrent;
using MonoTests.System.Threading.Tasks;
using NUnit.Framework;
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture()]
public class ParallelConcurrentQueueTests
{
ConcurrentQueue<int> queue;
[SetUpAttribute]
public void Setup()
{
queue = new ConcurrentQueue<int>();
}
[Test]
public void CountTestCase()
{
const int numThread = 5;
ParallelTestHelper.ParallelAdder(queue, numThread);
Assert.AreEqual(10 * numThread, queue.Count, "#1");
int value;
queue.TryPeek(out value);
ParallelTestHelper.ParallelRemover(queue, numThread, 3);
Assert.AreEqual(10 * numThread - 3, queue.Count, "#2");
}
}
}
#endif

View File

@@ -0,0 +1,62 @@
#if NET_4_0
// ParallelConcurrentStackTests.cs
//
// Copyright (c) 2008 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using MonoTests.System.Threading.Tasks;
using System.Collections.Concurrent;
using NUnit.Framework;
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture()]
public class ParallelConcurrentStackTests
{
ConcurrentStack<int> stack;
[SetUpAttribute]
public void Setup()
{
stack = new ConcurrentStack<int>();
}
[Test]
public void CountTestCase()
{
const int numThread = 5;
ParallelTestHelper.ParallelAdder(stack, numThread);
Assert.AreEqual(10 * numThread, stack.Count, "#1");
int value;
stack.TryPeek(out value);
ParallelTestHelper.ParallelRemover(stack, numThread, 3);
Assert.AreEqual(10 * numThread - 3, stack.Count, "#2");
stack.Clear();
Assert.AreEqual(0, stack.Count, "#3");
Assert.IsTrue(stack.IsEmpty, "#4");
}
}
}
#endif

View File

@@ -0,0 +1,96 @@
//
// PartitionerTests.cs
//
// Author:
// Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
//
// Copyright (c) 2009 Jérémie "Garuma" Laval
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if NET_4_0
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using NUnit.Framework;
#if !MOBILE
using NUnit.Framework.SyntaxHelpers;
#endif
namespace MonoTests.System.Collections.Concurrent
{
[TestFixture]
public class PartitionerTests
{
[Test]
public void PartitionerCreateIntegerWithExplicitRange ()
{
OrderablePartitioner<Tuple<int, int>> partitioner = Partitioner.Create (1, 20, 5);
var partitions = partitioner.GetOrderablePartitions (3);
Assert.AreEqual (3, partitions.Count);
Assert.That (partitions, Is.All.Not.Null);
var iterator = partitions[0];
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (Create (0, 1, 6)));
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (Create (1, 6, 11)));
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (Create (2, 11, 16)));
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (Create (3, 16, 20)));
Assert.IsFalse (partitions[1].MoveNext ());
Assert.IsFalse (partitions[2].MoveNext ());
}
[Test]
public void PartitionerCreateLongWithExplicitRange ()
{
OrderablePartitioner<Tuple<long, long>> partitioner = Partitioner.Create ((long)1, (long)20, (long)5);
var partitions = partitioner.GetOrderablePartitions (3);
Assert.AreEqual (3, partitions.Count);
Assert.That (partitions, Is.All.Not.Null);
var iterator = partitions[0];
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (CreateL (0, 1, 6)));
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (CreateL (1, 6, 11)));
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (CreateL (2, 11, 16)));
Assert.IsTrue (iterator.MoveNext ());
Assert.IsTrue (iterator.Current.Equals (CreateL (3, 16, 20)));
Assert.IsFalse (partitions[1].MoveNext ());
Assert.IsFalse (partitions[2].MoveNext ());
}
static KeyValuePair<long, Tuple<int, int>> Create (long ind, int i1, int i2)
{
return new KeyValuePair<long, Tuple<int, int>> (ind, Tuple.Create (i1, i2));
}
static KeyValuePair<long, Tuple<long, long>> CreateL (long ind, long i1, long i2)
{
return new KeyValuePair<long, Tuple<long, long>> (ind, Tuple.Create (i1, i2));
}
}
}
#endif