github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/sort/search_test.gno (about) 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_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 // XXX keys not yet supported in composite literals. 19 var data = []int{-10, -5, 0, 1, 2, 3, 5, 7, 11, 100, 100, 100, 1000, 10000} 20 21 // 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} 22 23 var tests = []struct { 24 name string 25 n int 26 f func(int) bool 27 i int 28 }{ 29 {"empty", 0, nil, 0}, 30 {"1 1", 1, func(i int) bool { return i >= 1 }, 1}, 31 {"1 true", 1, func(i int) bool { return true }, 0}, 32 {"1 false", 1, func(i int) bool { return false }, 1}, 33 {"1e9 991", 1e9, func(i int) bool { return i >= 991 }, 991}, 34 {"1e9 true", 1e9, func(i int) bool { return true }, 0}, 35 {"1e9 false", 1e9, func(i int) bool { return false }, 1e9}, 36 {"data -20", len(data), f(data, -20), 0}, 37 {"data -10", len(data), f(data, -10), 0}, 38 {"data -9", len(data), f(data, -9), 1}, 39 {"data -6", len(data), f(data, -6), 1}, 40 {"data -5", len(data), f(data, -5), 1}, 41 {"data 3", len(data), f(data, 3), 5}, 42 {"data 11", len(data), f(data, 11), 8}, 43 {"data 99", len(data), f(data, 99), 9}, 44 {"data 100", len(data), f(data, 100), 9}, 45 {"data 101", len(data), f(data, 101), 12}, 46 {"data 10000", len(data), f(data, 10000), 13}, 47 {"data 10001", len(data), f(data, 10001), 14}, 48 {"descending a", 7, func(i int) bool { return []int{99, 99, 59, 42, 7, 0, -1, -1}[i] <= 7 }, 4}, 49 {"descending 7", 1e9, func(i int) bool { return 1e9-i <= 7 }, 1e9 - 7}, 50 {"overflow", 2e9, func(i int) bool { return false }, 2e9}, 51 } 52 53 func TestSearch(t *testing.T) { 54 for _, e := range tests { 55 i := sort.Search(e.n, e.f) 56 if i != e.i { 57 t.Errorf("%s: expected index %d; got %d", e.name, e.i, i) 58 } 59 } 60 } 61 62 // log2 computes the binary logarithm of x, rounded up to the next integer. 63 // (log2(0) == 0, log2(1) == 0, log2(2) == 1, log2(3) == 2, etc.) 64 func log2(x int) int { 65 n := 0 66 for p := 1; p < x; p += p { 67 // p == 2**n 68 n++ 69 } 70 // p/2 < x <= p == 2**n 71 return n 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 83 i := sort.Search(n, func(i int) bool { count++; return i >= x }) 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 // Smoke tests for convenience wrappers - not comprehensive. 97 98 // var fdata = []float64{0: -3.14, 1: 0, 2: 1, 3: 2, 4: 1000.7} 99 // XXX 100 // var sdata = []string{0: "f", 1: "foo", 2: "foobar", 3: "x"} 101 var sdata = []string{"f", "foo", "foobar", "x"} 102 103 var wrappertests = []struct { 104 name string 105 result int 106 i int 107 }{ 108 {"SearchInts", sort.SearchInts(data, 11), 8}, 109 //{"SearchFloat64s", sort.SearchFloat64s(fdata, 2.1), 4}, 110 {"SearchStrings", sort.SearchStrings(sdata, ""), 0}, 111 {"IntSlice.Search", sort.IntSlice(data).Search(0), 2}, 112 //{"Float64Slice.Search", sort.Float64Slice(fdata).Search(2.0), 3}, 113 {"StringSlice.Search", sort.StringSlice(sdata).Search("x"), 3}, 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 } 123 124 func runSearchWrappers() { 125 sort.SearchInts(data, 11) 126 // sort.SearchFloat64s(fdata, 2.1) 127 sort.SearchStrings(sdata, "") 128 sort.IntSlice(data).Search(0) 129 // sort.Float64Slice(fdata).Search(2.0) 130 sort.StringSlice(sdata).Search("x") 131 } 132 133 /* XXX t.Skip not yet implemented; and runtime. 134 func TestSearchWrappersDontAlloc(t *testing.T) { 135 if testing.Short() { 136 t.Skip("skipping malloc count in short mode") 137 } 138 if runtime.GOMAXPROCS(0) > 1 { 139 t.Skip("skipping; GOMAXPROCS>1") 140 } 141 allocs := testing.AllocsPerRun2(100, runSearchWrappers) 142 if allocs != 0 { 143 t.Errorf("expected no allocs for runSearchWrappers, got %v", allocs) 144 } 145 } 146 */ 147 148 func BenchmarkSearchWrappers(b *testing.B) { 149 for i := 0; i < b.N; i++ { 150 runSearchWrappers() 151 } 152 } 153 154 // Abstract exhaustive test: all sizes up to 100, 155 // all possible return values. If there are any small 156 // corner cases, this test exercises them. 157 func TestSearchExhaustive(t *testing.T) { 158 for size := 0; size <= 100; size++ { 159 for targ := 0; targ <= size; targ++ { 160 i := sort.Search(size, func(i int) bool { return i >= targ }) 161 if i != targ { 162 t.Errorf("Search(%d, %d) = %d", size, targ, i) 163 } 164 } 165 } 166 }