-
Notifications
You must be signed in to change notification settings - Fork 1
/
PriorityQueueTests.cs
104 lines (87 loc) · 3.25 KB
/
PriorityQueueTests.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
using System;
using Xunit;
using FsCheck.Xunit;
using System.Collections.Generic;
using System.Linq;
namespace PriorityQueue.Tests
{
public class PriorityQueueTests
{
[Fact]
public static void Simple_Priority_Queue()
{
var pq = new PriorityQueue<string, int>();
pq.Enqueue("John", 1940);
pq.Enqueue("Paul", 1942);
pq.Enqueue("George", 1943);
pq.Enqueue("Ringo", 1940);
Assert.Equal("John", pq.Dequeue());
Assert.Equal("Ringo", pq.Dequeue());
Assert.Equal("Paul", pq.Dequeue());
Assert.Equal("George", pq.Dequeue());
}
[Fact]
public static void Simple_Priority_Queue_Heapify()
{
var pq = new PriorityQueue<string, int>(new (string, int)[] { ("John", 1940), ("Paul", 1942), ("George", 1943), ("Ringo", 1940) });
Assert.Equal("John", pq.Dequeue());
Assert.Equal("Ringo", pq.Dequeue());
Assert.Equal("Paul", pq.Dequeue());
Assert.Equal("George", pq.Dequeue());
}
[Fact]
public static void Simple_Priority_Queue_Enumeration()
{
var pq = new PriorityQueue<string, int>(new (string, int)[] { ("John", 1940), ("Paul", 1942), ("George", 1943), ("Ringo", 1940) });
(string, int)[] expected = new[] { ("John", 1940), ("Paul", 1942), ("George", 1943), ("Ringo", 1940) };
Assert.Equal(expected, pq.UnorderedItems.ToArray());
}
[Property(MaxTest = 10_000)]
public static void HeapSort_Should_Work(string[] inputs)
{
string[] expected = inputs.OrderBy(inp => inp).ToArray();
string[] actual = HeapSort(inputs).ToArray();
Assert.Equal(expected, actual);
static IEnumerable<string> HeapSort(string[] inputs)
{
var pq = new PriorityQueue<string, string>();
ValidateState(pq);
foreach (string input in inputs)
{
pq.Enqueue(input, input);
ValidateState(pq);
}
Assert.Equal(inputs.Length, pq.Count);
while (pq.Count > 0)
{
yield return pq.Dequeue();
ValidateState(pq);
}
}
}
[Property(MaxTest = 10_000)]
public static void HeapSort_Ctor_Should_Work(string[] inputs)
{
string[] expected = inputs.OrderBy(inp => inp).ToArray();
string[] actual = HeapSort(inputs).ToArray();
Assert.Equal(expected, actual);
static IEnumerable<string> HeapSort(string[] inputs)
{
var pq = new PriorityQueue<string, string>(inputs.Select(x => (x,x)));
ValidateState(pq);
Assert.Equal(inputs.Length, pq.Count);
while (pq.Count > 0)
{
yield return pq.Dequeue();
ValidateState(pq);
}
}
}
private static void ValidateState<TElement, TPriority>(PriorityQueue<TElement, TPriority> pq)
{
#if DEBUG
pq.ValidateInternalState();
#endif
}
}
}