-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeInTask1_should.cs
67 lines (58 loc) · 1.93 KB
/
BinaryTreeInTask1_should.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BinaryTrees
{
[TestFixture]
public class BinaryTreeInTask1_should
{
private readonly Random rnd = new Random();
private void TestAddContains<T>(IEnumerable<T> values)
where T : IComparable
{
var shuffledValues = values.Shuffle();
var toAdd = shuffledValues.Where(z => rnd.NextDouble() > 0.7).ToList();
var tree = new BinaryTree<T>();
foreach (var e in toAdd)
tree.Add(e);
foreach (var e in shuffledValues)
Assert.AreEqual(toAdd.Contains(e), tree.Contains(e));
}
[Test]
public void EmptyTreeDoesNotContainDefaultValue()
{
var intTree = new BinaryTree<int>();
Assert.IsFalse(intTree.Contains(0));
var stringTree = new BinaryTree<string>();
Assert.IsFalse(stringTree.Contains(null));
}
[Test]
public void WorkWithIntegers()
{
TestAddContains(Enumerable.Range(0, 20));
}
[Test]
public void WorkWithManyIntegers()
{
RunWithTimeout(() => TestAddContains(Enumerable.Range(0, 2000)), 500, "Your tree is too slow");
}
[Test]
public void WorkWithStrings()
{
TestAddContains(Enumerable.Range(1, 20).Select(z => new string((char)('a' + z), z)));
}
[Test]
public void WorkWithTuples()
{
TestAddContains(Enumerable.Range(1, 20).Select(z => Tuple.Create(z / 3, new string((char)('a' + z), z))));
}
protected static void RunWithTimeout(Action work, int timeout, string message)
{
var task = Task.Run(work);
if (!task.Wait(timeout))
throw new AssertionException(message);
}
}
}