-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy paththreesum_test.go
50 lines (45 loc) · 1.29 KB
/
threesum_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
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.
package invariants
import (
"math/rand"
"testing"
)
func TestHasThreeSum(t *testing.T) {
for _, test := range []struct {
xs []int
k int
want bool
}{
{nil, 0, false},
{[]int{1}, 1, false},
{[]int{1, 2}, 2, false},
{[]int{1, 2}, 3, true},
{[]int{1, 2}, 4, true},
{[]int{1, 2}, 5, true},
{[]int{-1, 2}, 0, true},
{[]int{-1, -2}, -5, true},
{[]int{1, 1, 1}, 1, false},
{[]int{1, 1, 1}, 2, false},
{[]int{1, 1, 1}, 3, true},
{[]int{1, 1, 1}, 4, false},
{[]int{5, 7, 2, 3, 11}, 21, true},
{[]int{-3, 7, -5, 2, 9}, -1, true},
} {
if got := HasThreeSum(test.xs, test.k); got != test.want {
t.Errorf("HasThreeSum(%v, %d) = %t; want %t", test.xs, test.k, got, test.want)
}
}
}
func benchHasThreeSum(b *testing.B, size int) {
xs := rand.New(rand.NewSource(int64(size))).Perm(size)
k := xs[0] + xs[size/3] + xs[size-1]
b.ResetTimer()
for i := 0; i < b.N; i++ {
HasThreeSum(xs, k)
}
}
func BenchmarkHasThreeSum1e2(b *testing.B) { benchHasThreeSum(b, 1e2) }
func BenchmarkHasThreeSum1e4(b *testing.B) { benchHasThreeSum(b, 1e4) }
func BenchmarkHasThreeSum1e6(b *testing.B) { benchHasThreeSum(b, 1e6) }