-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicates_test.go
61 lines (52 loc) · 1.83 KB
/
duplicates_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
49
50
51
52
53
54
55
56
57
58
59
60
61
package arrays
import (
"math"
"reflect"
"testing"
)
type deleteduplicatesFn func(s []int) []int
func testDeleteduplicatesFn(t *testing.T, fn deleteduplicatesFn) {
for _, test := range deleteDuplicatesTestData {
if got := fn(test.in); !reflect.DeepEqual(got, test.want) {
t.Errorf("%s(%v)=%v; want %v", fnName(fn), test.in, got, test.want)
}
}
}
func TestDeleteduplicatesSubarray(t *testing.T) { testDeleteduplicatesFn(t, DeleteDuplicatesSubarray) }
func TestDeleteDuplicatesShift(t *testing.T) { testDeleteduplicatesFn(t, DeleteDuplicatesShift) }
func benchDeleteDuplicatesFn(b *testing.B, fn deleteduplicatesFn) {
b.StopTimer()
s := make([]int, math.MaxInt16)
// populate slice with duplicated odd numbers
for i := 1; i < len(s); i++ {
if i&1 == 0 { // odd
s[i] = i
} else {
s[i] = i - 1
}
}
b.StartTimer()
for i := 0; i < b.N; i++ {
fn(s)
}
}
func BenchmarkDeleteDuplicatesSubarray(b *testing.B) {
benchDeleteDuplicatesFn(b, DeleteDuplicatesSubarray)
}
func BenchmarkDeleteDuplicatesShift(b *testing.B) { benchDeleteDuplicatesFn(b, DeleteDuplicatesShift) }
var deleteDuplicatesTestData = []struct {
in []int
want []int
}{
{[]int{}, []int{}},
{[]int{-1}, []int{-1}},
{[]int{0}, []int{0}},
{[]int{1, 1, 1}, []int{1}},
{[]int{-2, -1, 1}, []int{-2, -1, 1}},
{[]int{1, 1, 2, 2, 3, 3, 4}, []int{1, 2, 3, 4}},
{[]int{-2, -2, 0}, []int{-2, 0}},
{[]int{-11, -10, -8, -7, -6, -6, -6, -2, -1, -1, 0, 0, 0, 0, 3, 4, 4, 7, 7, 8, 9, 11}, []int{-11, -10, -8, -7, -6, -2, -1, 0, 3, 4, 7, 8, 9, 11}},
{[]int{-12, -12, -12, -10, -5, -5, -5, -3, -3, -2, -1, -1, 0, 1, 1, 4, 4, 6, 7, 7, 9, 10, 12, 12}, []int{-12, -10, -5, -3, -2, -1, 0, 1, 4, 6, 7, 9, 10, 12}},
{[]int{-3, -3, -2, -2, -2, -1, 1, 4}, []int{-3, -2, -1, 1, 4}},
{[]int{-8, -8, -7, -5, -5, -4, 1, 2, 2, 2, 2, 4, 5, 5, 7, 7}, []int{-8, -7, -5, -4, 1, 2, 4, 5, 7}},
}