-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtensionsTests.cs
110 lines (98 loc) · 3.18 KB
/
ExtensionsTests.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Linq;
using NUnit.Framework;
namespace linq_slideviews
{
[TestFixture]
public class ExtensionsTests
{
[Test]
public void MedianOfEmptyList_Fails()
{
Assert.Throws<InvalidOperationException>(
() => Array.Empty<double>().Median());
}
[Test]
public void MedianOfSingleItemList()
{
var actual = new[] { 42.0 }.Median();
Assert.That(actual, Is.EqualTo(42.0));
}
[Test]
public void MedianOfTwoItemsList()
{
var actual = new[] { 1.0, 2.0 }.Median();
Assert.That(actual, Is.EqualTo(1.5).Within(1e-7));
}
[TestCase(new[] { 1.0, 10, 9, 2, 3 }, 3.0)]
[TestCase(new[] { 10, 9, 2, 3.0 }, 6.0)]
public void MedianOfUnsortedList(double[] list, double expectedMedian)
{
var actual = list.Median();
Assert.That(actual, Is.EqualTo(expectedMedian));
}
[Test]
public void MedianOfLongOddList()
{
var list = Enumerable.Range(10, 1001).Select(i => (double)i).ToList();
var actual = list.Median();
Assert.That(actual, Is.EqualTo(list[1001 / 2]));
}
[Test]
public void MedianOfLongEvenList()
{
var list = Enumerable.Range(10, 1000).Select(i => (double)i).ToList();
var actual = list.Median();
Assert.That(actual, Is.EqualTo((list[500] + list[499]) / 2).Within(1e-7));
}
[Test]
public void MedianOfThreeItemsList()
{
var actual = new[] { 1.0, 2.0, 3000.0 }.Median();
Assert.That(actual, Is.EqualTo(2.0));
}
[Test]
public void MedianOfSequenceWithRepetitions()
{
var actual = new[] { 1.0, 1.0, 1.0, 2.0, 3.0 }.Median();
Assert.That(actual, Is.EqualTo(1.0));
}
[Test]
public void BigramsOfSingleItemList()
{
var actual = new[] { 42.0 }.Bigrams();
Assert.That(actual, Is.Empty);
}
[Test]
public void BigramsOfTwoItemList()
{
var actual = new[] { 1, 2 }.Bigrams();
Assert.That(actual, Is.EqualTo(new[] { Tuple.Create(1, 2) }));
}
[Test]
public void BigramsOfLongList()
{
var count = 1000;
var actual = Enumerable.Range(100, count).Bigrams().ToList();
Assert.That(actual, Has.Count.EqualTo(count - 1));
for (var index = 0; index < count - 1; index++)
{
var tuple = actual[index];
Assert.That(tuple.Item1, Is.EqualTo(100 + index));
Assert.That(tuple.Item2, Is.EqualTo(101 + index));
}
}
[Test]
public void BigramsOfReferenceTypeSequence()
{
var actual = new[] { "1", null, "2" }.Bigrams();
Assert.That(actual,
Is.EqualTo(
new[]
{
Tuple.Create("1", (string)null),
Tuple.Create((string)null, "2"),
}));
}
}
}