-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathbenchmark_test.go
45 lines (41 loc) · 942 Bytes
/
benchmark_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
package main
import (
"math/rand"
"reflect"
"testing"
"time"
)
// only a great number
const size = 100000
func TestSortInts(t *testing.T) {
// Create some cases to test sort function
for _, test := range []struct {
input []int
expected []int
}{
{[]int{}, []int{}},
{[]int{1, 2, 3, 4}, []int{1, 2, 3, 4}},
{[]int{4, 3, 2, 1}, []int{1, 2, 3, 4}},
{[]int{5, 2, 1, 3}, []int{1, 2, 3, 5}},
} {
SortInts(test.input)
// DeepEqual to compare all values in a list
if !reflect.DeepEqual(test.input, test.expected) {
t.Errorf("Expected %v but found %v", test.expected, test.input)
}
}
}
func BenchmarkSortInts(b *testing.B) {
// randomize
rand.Seed(time.Now().UTC().UnixNano())
// initialize big array of ints
bigArray := make([]int, size)
// with random numbers
for i := 0; i < size; i++ {
bigArray[i] = rand.Intn(size)
}
// benchmark sort function
for i := 0; i < b.N; i++ {
SortInts(bigArray)
}
}