| 1 |
747 |
jeremybenn |
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
package sort_test
|
| 6 |
|
|
|
| 7 |
|
|
import (
|
| 8 |
|
|
. "sort"
|
| 9 |
|
|
"testing"
|
| 10 |
|
|
)
|
| 11 |
|
|
|
| 12 |
|
|
func f(a []int, x int) func(int) bool {
|
| 13 |
|
|
return func(i int) bool {
|
| 14 |
|
|
return a[i] >= x
|
| 15 |
|
|
}
|
| 16 |
|
|
}
|
| 17 |
|
|
|
| 18 |
|
|
var data = []int{0: -10, 1: -5, 2: 0, 3: 1, 4: 2, 5: 3, 6: 5, 7: 7, 8: 11, 9: 100, 10: 100, 11: 100, 12: 1000, 13: 10000}
|
| 19 |
|
|
|
| 20 |
|
|
var tests = []struct {
|
| 21 |
|
|
name string
|
| 22 |
|
|
n int
|
| 23 |
|
|
f func(int) bool
|
| 24 |
|
|
i int
|
| 25 |
|
|
}{
|
| 26 |
|
|
{"empty", 0, nil, 0},
|
| 27 |
|
|
{"1 1", 1, func(i int) bool { return i >= 1 }, 1},
|
| 28 |
|
|
{"1 true", 1, func(i int) bool { return true }, 0},
|
| 29 |
|
|
{"1 false", 1, func(i int) bool { return false }, 1},
|
| 30 |
|
|
{"1e9 991", 1e9, func(i int) bool { return i >= 991 }, 991},
|
| 31 |
|
|
{"1e9 true", 1e9, func(i int) bool { return true }, 0},
|
| 32 |
|
|
{"1e9 false", 1e9, func(i int) bool { return false }, 1e9},
|
| 33 |
|
|
{"data -20", len(data), f(data, -20), 0},
|
| 34 |
|
|
{"data -10", len(data), f(data, -10), 0},
|
| 35 |
|
|
{"data -9", len(data), f(data, -9), 1},
|
| 36 |
|
|
{"data -6", len(data), f(data, -6), 1},
|
| 37 |
|
|
{"data -5", len(data), f(data, -5), 1},
|
| 38 |
|
|
{"data 3", len(data), f(data, 3), 5},
|
| 39 |
|
|
{"data 11", len(data), f(data, 11), 8},
|
| 40 |
|
|
{"data 99", len(data), f(data, 99), 9},
|
| 41 |
|
|
{"data 100", len(data), f(data, 100), 9},
|
| 42 |
|
|
{"data 101", len(data), f(data, 101), 12},
|
| 43 |
|
|
{"data 10000", len(data), f(data, 10000), 13},
|
| 44 |
|
|
{"data 10001", len(data), f(data, 10001), 14},
|
| 45 |
|
|
{"descending a", 7, func(i int) bool { return []int{99, 99, 59, 42, 7, 0, -1, -1}[i] <= 7 }, 4},
|
| 46 |
|
|
{"descending 7", 1e9, func(i int) bool { return 1e9-i <= 7 }, 1e9 - 7},
|
| 47 |
|
|
{"overflow", 2e9, func(i int) bool { return false }, 2e9},
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
func TestSearch(t *testing.T) {
|
| 51 |
|
|
for _, e := range tests {
|
| 52 |
|
|
i := Search(e.n, e.f)
|
| 53 |
|
|
if i != e.i {
|
| 54 |
|
|
t.Errorf("%s: expected index %d; got %d", e.name, e.i, i)
|
| 55 |
|
|
}
|
| 56 |
|
|
}
|
| 57 |
|
|
}
|
| 58 |
|
|
|
| 59 |
|
|
// log2 computes the binary logarithm of x, rounded up to the next integer.
|
| 60 |
|
|
// (log2(0) == 0, log2(1) == 0, log2(2) == 1, log2(3) == 2, etc.)
|
| 61 |
|
|
//
|
| 62 |
|
|
func log2(x int) int {
|
| 63 |
|
|
n := 0
|
| 64 |
|
|
for p := 1; p < x; p += p {
|
| 65 |
|
|
// p == 2**n
|
| 66 |
|
|
n++
|
| 67 |
|
|
}
|
| 68 |
|
|
// p/2 < x <= p == 2**n
|
| 69 |
|
|
return n
|
| 70 |
|
|
}
|
| 71 |
|
|
|
| 72 |
|
|
func TestSearchEfficiency(t *testing.T) {
|
| 73 |
|
|
n := 100
|
| 74 |
|
|
step := 1
|
| 75 |
|
|
for exp := 2; exp < 10; exp++ {
|
| 76 |
|
|
// n == 10**exp
|
| 77 |
|
|
// step == 10**(exp-2)
|
| 78 |
|
|
max := log2(n)
|
| 79 |
|
|
for x := 0; x < n; x += step {
|
| 80 |
|
|
count := 0
|
| 81 |
|
|
i := Search(n, func(i int) bool { count++; return i >= x })
|
| 82 |
|
|
if i != x {
|
| 83 |
|
|
t.Errorf("n = %d: expected index %d; got %d", n, x, i)
|
| 84 |
|
|
}
|
| 85 |
|
|
if count > max {
|
| 86 |
|
|
t.Errorf("n = %d, x = %d: expected <= %d calls; got %d", n, x, max, count)
|
| 87 |
|
|
}
|
| 88 |
|
|
}
|
| 89 |
|
|
n *= 10
|
| 90 |
|
|
step *= 10
|
| 91 |
|
|
}
|
| 92 |
|
|
}
|
| 93 |
|
|
|
| 94 |
|
|
// Smoke tests for convenience wrappers - not comprehensive.
|
| 95 |
|
|
|
| 96 |
|
|
var fdata = []float64{0: -3.14, 1: 0, 2: 1, 3: 2, 4: 1000.7}
|
| 97 |
|
|
var sdata = []string{0: "f", 1: "foo", 2: "foobar", 3: "x"}
|
| 98 |
|
|
|
| 99 |
|
|
var wrappertests = []struct {
|
| 100 |
|
|
name string
|
| 101 |
|
|
result int
|
| 102 |
|
|
i int
|
| 103 |
|
|
}{
|
| 104 |
|
|
{"SearchInts", SearchInts(data, 11), 8},
|
| 105 |
|
|
{"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4},
|
| 106 |
|
|
{"SearchStrings", SearchStrings(sdata, ""), 0},
|
| 107 |
|
|
{"IntSlice.Search", IntSlice(data).Search(0), 2},
|
| 108 |
|
|
{"Float64Slice.Search", Float64Slice(fdata).Search(2.0), 3},
|
| 109 |
|
|
{"StringSlice.Search", StringSlice(sdata).Search("x"), 3},
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
func TestSearchWrappers(t *testing.T) {
|
| 113 |
|
|
for _, e := range wrappertests {
|
| 114 |
|
|
if e.result != e.i {
|
| 115 |
|
|
t.Errorf("%s: expected index %d; got %d", e.name, e.i, e.result)
|
| 116 |
|
|
}
|
| 117 |
|
|
}
|
| 118 |
|
|
}
|
| 119 |
|
|
|
| 120 |
|
|
// Abstract exhaustive test: all sizes up to 100,
|
| 121 |
|
|
// all possible return values. If there are any small
|
| 122 |
|
|
// corner cases, this test exercises them.
|
| 123 |
|
|
func TestSearchExhaustive(t *testing.T) {
|
| 124 |
|
|
for size := 0; size <= 100; size++ {
|
| 125 |
|
|
for targ := 0; targ <= size; targ++ {
|
| 126 |
|
|
i := Search(size, func(i int) bool { return i >= targ })
|
| 127 |
|
|
if i != targ {
|
| 128 |
|
|
t.Errorf("Search(%d, %d) = %d", size, targ, i)
|
| 129 |
|
|
}
|
| 130 |
|
|
}
|
| 131 |
|
|
}
|
| 132 |
|
|
}
|