-
Notifications
You must be signed in to change notification settings - Fork 97
/
maxdiff_test.go
54 lines (49 loc) · 1.46 KB
/
maxdiff_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
// 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 arrays
import (
"math/rand"
"testing"
)
func TestMinBatteryCap(t *testing.T) {
for _, test := range []struct {
in []int
want int
ok bool
}{
{[]int{}, 0, true},
{[]int{0}, 0, true},
{[]int{1}, 0, true},
{[]int{-1}, 0, true},
{[]int{0, 0}, 0, true},
{[]int{0, 1}, 1, true},
{[]int{1, 0}, 0, true},
{[]int{3, 3, 10}, 7, true},
{[]int{-3, 3, 10}, 13, true},
{[]int{3, -3, 10}, 13, true},
{[]int{3, 3, -10}, 0, true},
{[]int{1, maxInt}, maxInt - 1, true},
{[]int{-1, maxInt}, 0, false},
{[]int{1, -maxInt}, 0, true},
{[]int{-1, -maxInt}, 0, true},
{[]int{1, minInt}, 0, false},
{[]int{-1, minInt}, 0, true},
{[]int{minInt, maxInt}, 0, false},
{[]int{maxInt, minInt}, 0, false},
} {
if got, ok := MinBatteryCap(test.in); ok != test.ok || got != test.want {
t.Errorf("MinBatteryCap(%d) = %d, %t; want %d, %t", test.in, got, ok, test.want, test.ok)
}
}
}
func benchMinBatteryCap(b *testing.B, size int) {
data := rand.New(rand.NewSource(int64(size))).Perm(size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
MinBatteryCap(data)
}
}
func BenchmarkMinBatteryCap1e2(b *testing.B) { benchMinBatteryCap(b, 1e2) }
func BenchmarkMinBatteryCap1e4(b *testing.B) { benchMinBatteryCap(b, 1e4) }
func BenchmarkMinBatteryCap1e6(b *testing.B) { benchMinBatteryCap(b, 1e6) }