-
Notifications
You must be signed in to change notification settings - Fork 1
/
InterpolationSearchTests.cs
178 lines (136 loc) · 5.59 KB
/
InterpolationSearchTests.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
namespace Sortable_Collection.Tests
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
[TestClass]
public class InterpolationSearchTests
{
private static readonly Random Random = new Random();
[TestMethod]
public void TestWithEmptyCollectionShouldReturnMissingElement()
{
var collection = new SortableCollection<int>();
this.ApplyDelegates(ref collection);
var result = collection.InterpolationSearch(0);
var expected = Array.BinarySearch(collection.ToArray(), 0);
Assert.AreEqual(expected, result, "No elements are present in an empty collection; method should return -1.");
}
[TestMethod]
public void TestWithMissingElement()
{
var collection = new SortableCollection<int>(-1, 1, 5, 12, 50);
this.ApplyDelegates(ref collection);
var result = collection.InterpolationSearch(0);
var expected = -1;
Assert.AreEqual(expected, result, "Missing element should return -1.");
}
[TestMethod]
public void TestWithItemAtMidpoint()
{
var collection = new SortableCollection<int>(1, 2, 3, 4, 5);
ApplyDelegates(ref collection);
var result = collection.InterpolationSearch(3);
var expected = Array.BinarySearch(collection.ToArray(), 3);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void TestWithItemToTheLeftOfMidpoint()
{
var collection = new SortableCollection<int>(1, 2, 3, 4, 5);
this.ApplyDelegates(ref collection);
var result = collection.InterpolationSearch(2);
var expected = Array.BinarySearch(collection.ToArray(), 2);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void TestWithItemToTheRightOfMidpoint()
{
var collection = new SortableCollection<int>(1, 2, 3, 4, 5);
this.ApplyDelegates(ref collection);
var result = collection.InterpolationSearch(4);
var expected = Array.BinarySearch(collection.ToArray(), 4);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void TestWithMultipleMissingKeysSmallerThanMinimum()
{
const int NumberOfChecks = 10000;
const int NumberOfElements = 1000;
var elements = new int[NumberOfElements];
for (int i = 0; i < NumberOfElements; i++)
{
elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
}
Array.Sort(elements);
var collection = new SortableCollection<int>(elements);
this.ApplyDelegates(ref collection);
for (int i = 0; i < NumberOfChecks; i++)
{
var item = Random.Next(int.MinValue, collection.Items[0]);
int result = collection.InterpolationSearch(item);
Assert.AreEqual(-1, result);
}
}
[TestMethod]
public void TestWithMultipleMissingKeysLargerThanMaximum()
{
const int NumberOfChecks = 10000;
const int NumberOfElements = 1000;
var elements = new int[NumberOfElements];
for (int i = 0; i < NumberOfElements; i++)
{
elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
}
Array.Sort(elements);
var collection = new SortableCollection<int>(elements);
this.ApplyDelegates(ref collection);
for (int i = 0; i < NumberOfChecks; i++)
{
var item = Random.Next(collection.Items[collection.Count - 1], int.MaxValue);
int result = collection.InterpolationSearch(item);
Assert.AreEqual(-1, result);
}
}
// Below test are unappliable because we can not predict the first index where the InterpolationSearch will check and find the searched item
//[TestMethod]
//public void TestWithMultipleKeys()
//{
// const int NumberOfElements = 10000;
// var elements = new int[NumberOfElements];
// for (int i = 0; i < NumberOfElements; i++)
// {
// elements[i] = Random.Next(-100, 100);
// }
// Array.Sort(elements);
// var collection = new SortableCollection<int>(elements);
// this.ApplyDelegates(ref collection);
// foreach (var element in elements)
// {
// int expected = Array.IndexOf(elements, element);
// int result = collection.InterpolationSearch(element);
// Assert.AreEqual(expected, result);
// }
//}
//[TestMethod]
//public void TestWithRepeatingItemShouldReturnFirstDiscoveredIndex()
//{
// var collection = new SortableCollection<int>(0, 3, 3, 3, 3, 7, 7, 7, 7, 7, 7);
// this.ApplyDelegates(ref collection);
// var result = collection.InterpolationSearch(3);
// Assert.AreEqual(1, result);
//}
private void ApplyDelegates(ref SortableCollection<int> collection)
{
collection = new SortableCollection<int>(collection.Items, Subtract, Multiply);
}
private int Multiply(int a, int b)
{
return a * b;
}
private int Subtract(int a, int b)
{
return a - b;
}
}
}