Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 1 | // 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 |
| 6 | |
| 7 | import "testing" |
| 8 | |
| 9 | |
| 10 | func f(a []int, x int) func(int) bool { |
| 11 | return func(i int) bool { |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 12 | return a[i] >= x |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 13 | } |
| 14 | } |
| 15 | |
| 16 | |
| 17 | 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} |
| 18 | |
| 19 | var tests = []struct { |
| 20 | name string |
| 21 | n int |
| 22 | f func(int) bool |
| 23 | i int |
| 24 | }{ |
| 25 | {"empty", 0, nil, 0}, |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 26 | {"1 1", 1, func(i int) bool { return i >= 1 }, 1}, |
| 27 | {"1 true", 1, func(i int) bool { return true }, 0}, |
| 28 | {"1 false", 1, func(i int) bool { return false }, 1}, |
| 29 | {"1e9 991", 1e9, func(i int) bool { return i >= 991 }, 991}, |
| 30 | {"1e9 true", 1e9, func(i int) bool { return true }, 0}, |
| 31 | {"1e9 false", 1e9, func(i int) bool { return false }, 1e9}, |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 32 | {"data -20", len(data), f(data, -20), 0}, |
| 33 | {"data -10", len(data), f(data, -10), 0}, |
Roger Peppe | bac478d | 2010-11-12 15:57:33 -0800 | [diff] [blame] | 34 | {"data -9", len(data), f(data, -9), 1}, |
| 35 | {"data -6", len(data), f(data, -6), 1}, |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 36 | {"data -5", len(data), f(data, -5), 1}, |
| 37 | {"data 3", len(data), f(data, 3), 5}, |
Roger Peppe | bac478d | 2010-11-12 15:57:33 -0800 | [diff] [blame] | 38 | {"data 11", len(data), f(data, 11), 8}, |
| 39 | {"data 99", len(data), f(data, 99), 9}, |
| 40 | {"data 100", len(data), f(data, 100), 9}, |
| 41 | {"data 101", len(data), f(data, 101), 12}, |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 42 | {"data 10000", len(data), f(data, 10000), 13}, |
Roger Peppe | bac478d | 2010-11-12 15:57:33 -0800 | [diff] [blame] | 43 | {"data 10001", len(data), f(data, 10001), 14}, |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 44 | {"descending a", 7, func(i int) bool { return []int{99, 99, 59, 42, 7, 0, -1, -1}[i] <= 7 }, 4}, |
| 45 | {"descending 7", 1e9, func(i int) bool { return 1e9-i <= 7 }, 1e9 - 7}, |
| 46 | {"overflow", 2e9, func(i int) bool { return false }, 2e9}, |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 47 | } |
| 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 | |
Robert Griesemer | 6498c1d | 2010-11-11 14:52:37 -0800 | [diff] [blame] | 60 | // log2 computes the binary logarithm of x, rounded up to the next integer. |
| 61 | // (log2(0) == 0, log2(1) == 0, log2(2) == 1, log2(3) == 2, etc.) |
| 62 | // |
| 63 | func log2(x int) int { |
| 64 | n := 0 |
| 65 | for p := 1; p < x; p += p { |
| 66 | // p == 2**n |
| 67 | n++ |
| 68 | } |
| 69 | // p/2 < x <= p == 2**n |
| 70 | return n |
| 71 | } |
| 72 | |
| 73 | |
| 74 | func TestSearchEfficiency(t *testing.T) { |
| 75 | n := 100 |
| 76 | step := 1 |
| 77 | for exp := 2; exp < 10; exp++ { |
| 78 | // n == 10**exp |
| 79 | // step == 10**(exp-2) |
| 80 | max := log2(n) |
| 81 | for x := 0; x < n; x += step { |
| 82 | count := 0 |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 83 | i := Search(n, func(i int) bool { count++; return i >= x }) |
Robert Griesemer | 6498c1d | 2010-11-11 14:52:37 -0800 | [diff] [blame] | 84 | if i != x { |
| 85 | t.Errorf("n = %d: expected index %d; got %d", n, x, i) |
| 86 | } |
| 87 | if count > max { |
| 88 | t.Errorf("n = %d, x = %d: expected <= %d calls; got %d", n, x, max, count) |
| 89 | } |
| 90 | } |
| 91 | n *= 10 |
| 92 | step *= 10 |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 97 | // Smoke tests for convenience wrappers - not comprehensive. |
| 98 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame^] | 99 | var fdata = []float64{0: -3.14, 1: 0, 2: 1, 3: 2, 4: 1000.7} |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 100 | var sdata = []string{0: "f", 1: "foo", 2: "foobar", 3: "x"} |
| 101 | |
| 102 | var wrappertests = []struct { |
| 103 | name string |
| 104 | result int |
| 105 | i int |
| 106 | }{ |
| 107 | {"SearchInts", SearchInts(data, 11), 8}, |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame^] | 108 | {"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4}, |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 109 | {"SearchStrings", SearchStrings(sdata, ""), 0}, |
| 110 | {"IntArray.Search", IntArray(data).Search(0), 2}, |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame^] | 111 | {"Float64Array.Search", Float64Array(fdata).Search(2.0), 3}, |
Robert Griesemer | 194dde2 | 2010-11-10 13:19:28 -0800 | [diff] [blame] | 112 | {"StringArray.Search", StringArray(sdata).Search("x"), 3}, |
| 113 | } |
| 114 | |
| 115 | |
| 116 | func TestSearchWrappers(t *testing.T) { |
| 117 | for _, e := range wrappertests { |
| 118 | if e.result != e.i { |
| 119 | t.Errorf("%s: expected index %d; got %d", e.name, e.i, e.result) |
| 120 | } |
| 121 | } |
| 122 | } |
Russ Cox | 285298b | 2010-11-18 11:46:07 -0500 | [diff] [blame] | 123 | |
| 124 | |
| 125 | // Abstract exhaustive test: all sizes up to 100, |
| 126 | // all possible return values. If there are any small |
| 127 | // corner cases, this test exercises them. |
| 128 | func TestSearchExhaustive(t *testing.T) { |
| 129 | for size := 0; size <= 100; size++ { |
| 130 | for targ := 0; targ <= size; targ++ { |
| 131 | i := Search(size, func(i int) bool { return i >= targ }) |
| 132 | if i != targ { |
| 133 | t.Errorf("Search(%d, %d) = %d", size, targ, i) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |