-
Notifications
You must be signed in to change notification settings - Fork 0
/
eytzinger_test.go
48 lines (43 loc) · 1.16 KB
/
eytzinger_test.go
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
package eytzinger
import (
"math/rand"
"testing"
"time"
)
func TestEytzinger(t *testing.T) {
rand.Seed(time.Now().UnixNano())
// Generate 100 different length arrays filled with random values, to sort and search.
for count := 15; count < 100; count++ {
nums := make([]int, count)
for n := 0; n < len(nums)/2; n++ {
nums[n] = rand.Intn(count / 2) // ensure duplicates
}
for n := len(nums) / 2; n < len(nums); n++ {
nums[n] = rand.Intn(count * 5) // ensure gaps
}
ref := make([]int, len(nums))
copy(ref, nums)
Sort(ref)
// Sort 100 different permutations of the array and make sure they all
// sort to the same result.
for j := 0; j < 100; j++ {
rand.Shuffle(len(nums), func(i, j int) {
nums[i], nums[j] = nums[j], nums[i]
})
Sort(nums)
for i := range nums {
if nums[i] != ref[i] {
t.Fatal("Sort is not consistent")
}
}
}
// Search for each number in array and check that the number is at the
// returned index.
for i, find := range ref {
index := Search(ref, find)
if ref[index] != ref[i] {
t.Fatalf("Search did not return correct index for %d, expected %d got %d", find, i, index)
}
}
}
}