-
Notifications
You must be signed in to change notification settings - Fork 292
/
PairingHeap_Tests.cs
101 lines (77 loc) · 2.99 KB
/
PairingHeap_Tests.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
using System;
using System.Linq;
using Advanced.Algorithms.DataStructures;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Advanced.Algorithms.Tests.DataStructures
{
[TestClass]
public class PairingHeapTests
{
[TestMethod]
public void Min_PairingHeap_Test()
{
var nodeCount = 1000 * 10;
var minHeap = new PairingHeap<int>();
for (var i = 0; i <= nodeCount; i++) minHeap.Insert(i);
for (var i = 0; i <= nodeCount; i++) minHeap.UpdateKey(i, i - 1);
var min = 0;
for (var i = 0; i <= nodeCount; i++)
{
min = minHeap.Extract();
Assert.AreEqual(min, i - 1);
}
//IEnumerable tests.
Assert.AreEqual(minHeap.Count, minHeap.Count());
var rnd = new Random();
var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList();
foreach (var item in testSeries) minHeap.Insert(item);
for (var i = 0; i < testSeries.Count; i++)
{
var decremented = testSeries[i] - rnd.Next(0, 1000);
minHeap.UpdateKey(testSeries[i], decremented);
testSeries[i] = decremented;
}
testSeries.Sort();
for (var i = 0; i < nodeCount - 2; i++)
{
min = minHeap.Extract();
Assert.AreEqual(testSeries[i], min);
}
//IEnumerable tests.
Assert.AreEqual(minHeap.Count, minHeap.Count());
}
[TestMethod]
public void Max_PairingHeap_Test()
{
var nodeCount = 1000 * 10;
var maxHeap = new PairingHeap<int>(SortDirection.Descending);
for (var i = 0; i <= nodeCount; i++) maxHeap.Insert(i);
for (var i = 0; i <= nodeCount; i++) maxHeap.UpdateKey(i, i + 1);
//IEnumerable tests.
Assert.AreEqual(maxHeap.Count, maxHeap.Count());
var max = 0;
for (var i = nodeCount; i >= 0; i--)
{
max = maxHeap.Extract();
Assert.AreEqual(max, i + 1);
}
var rnd = new Random();
var testSeries = Enumerable.Range(0, nodeCount - 1).OrderBy(x => rnd.Next()).ToList();
foreach (var item in testSeries) maxHeap.Insert(item);
for (var i = 0; i < testSeries.Count; i++)
{
var incremented = testSeries[i] + rnd.Next(0, 1000);
maxHeap.UpdateKey(testSeries[i], incremented);
testSeries[i] = incremented;
}
testSeries = testSeries.OrderByDescending(x => x).ToList();
for (var i = 0; i < nodeCount - 2; i++)
{
max = maxHeap.Extract();
Assert.AreEqual(testSeries[i], max);
}
//IEnumerable tests.
Assert.AreEqual(maxHeap.Count, maxHeap.Count());
}
}
}