| 1 |
// Copyright 2021 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 main |
| 6 |
|
| 7 |
import ( |
| 8 |
"./a" |
| 9 |
"fmt" |
| 10 |
"math" |
| 11 |
"strings" |
| 12 |
) |
| 13 |
|
| 14 |
type Integer interface { |
| 15 |
~int | ~int8 | ~int16 | ~int32 | ~int64 | |
| 16 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
| 17 |
} |
| 18 |
|
| 19 |
func TestEqual() { |
| 20 |
s1 := []int{1, 2, 3} |
| 21 |
if !a.Equal(s1, s1) { |
| 22 |
panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", s1, s1)) |
| 23 |
} |
| 24 |
s2 := []int{1, 2, 3} |
| 25 |
if !a.Equal(s1, s2) { |
| 26 |
panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", s1, s2)) |
| 27 |
} |
| 28 |
s2 = append(s2, 4) |
| 29 |
if a.Equal(s1, s2) { |
| 30 |
panic(fmt.Sprintf("a.Equal(%v, %v) = true, want false", s1, s2)) |
| 31 |
} |
| 32 |
|
| 33 |
s3 := []float64{1, 2, math.NaN()} |
| 34 |
if !a.Equal(s3, s3) { |
| 35 |
panic(fmt.Sprintf("a.Equal(%v, %v) = false, want true", s3, s3)) |
| 36 |
} |
| 37 |
|
| 38 |
if a.Equal(s1, nil) { |
| 39 |
panic(fmt.Sprintf("a.Equal(%v, nil) = true, want false", s1)) |
| 40 |
} |
| 41 |
if a.Equal(nil, s1) { |
| 42 |
panic(fmt.Sprintf("a.Equal(nil, %v) = true, want false", s1)) |
| 43 |
} |
| 44 |
if !a.Equal(s1[:0], nil) { |
| 45 |
panic(fmt.Sprintf("a.Equal(%v, nil = false, want true", s1[:0])) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func offByOne[Elem Integer](a, b Elem) bool { |
| 50 |
return a == b+1 || a == b-1 |
| 51 |
} |
| 52 |
|
| 53 |
func TestEqualFn() { |
| 54 |
s1 := []int{1, 2, 3} |
| 55 |
s2 := []int{2, 3, 4} |
| 56 |
if a.EqualFn(s1, s1, offByOne[int]) { |
| 57 |
panic(fmt.Sprintf("a.EqualFn(%v, %v, offByOne) = true, want false", s1, s1)) |
| 58 |
} |
| 59 |
if !a.EqualFn(s1, s2, offByOne[int]) { |
| 60 |
panic(fmt.Sprintf("a.EqualFn(%v, %v, offByOne) = false, want true", s1, s2)) |
| 61 |
} |
| 62 |
|
| 63 |
if !a.EqualFn(s1[:0], nil, offByOne[int]) { |
| 64 |
panic(fmt.Sprintf("a.EqualFn(%v, nil, offByOne) = false, want true", s1[:0])) |
| 65 |
} |
| 66 |
|
| 67 |
s3 := []string{"a", "b", "c"} |
| 68 |
s4 := []string{"A", "B", "C"} |
| 69 |
if !a.EqualFn(s3, s4, strings.EqualFold) { |
| 70 |
panic(fmt.Sprintf("a.EqualFn(%v, %v, strings.EqualFold) = false, want true", s3, s4)) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestMap() { |
| 75 |
s1 := []int{1, 2, 3} |
| 76 |
s2 := a.Map(s1, func(i int) float64 { return float64(i) * 2.5 }) |
| 77 |
if want := []float64{2.5, 5, 7.5}; !a.Equal(s2, want) { |
| 78 |
panic(fmt.Sprintf("a.Map(%v, ...) = %v, want %v", s1, s2, want)) |
| 79 |
} |
| 80 |
|
| 81 |
s3 := []string{"Hello", "World"} |
| 82 |
s4 := a.Map(s3, strings.ToLower) |
| 83 |
if want := []string{"hello", "world"}; !a.Equal(s4, want) { |
| 84 |
panic(fmt.Sprintf("a.Map(%v, strings.ToLower) = %v, want %v", s3, s4, want)) |
| 85 |
} |
| 86 |
|
| 87 |
s5 := a.Map(nil, func(i int) int { return i }) |
| 88 |
if len(s5) != 0 { |
| 89 |
panic(fmt.Sprintf("a.Map(nil, identity) = %v, want empty slice", s5)) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestReduce() { |
| 94 |
s1 := []int{1, 2, 3} |
| 95 |
r := a.Reduce(s1, 0, func(f float64, i int) float64 { return float64(i)*2.5 + f }) |
| 96 |
if want := 15.0; r != want { |
| 97 |
panic(fmt.Sprintf("a.Reduce(%v, 0, ...) = %v, want %v", s1, r, want)) |
| 98 |
} |
| 99 |
|
| 100 |
if got := a.Reduce(nil, 0, func(i, j int) int { return i + j }); got != 0 { |
| 101 |
panic(fmt.Sprintf("a.Reduce(nil, 0, add) = %v, want 0", got)) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestFilter() { |
| 106 |
s1 := []int{1, 2, 3} |
| 107 |
s2 := a.Filter(s1, func(i int) bool { return i%2 == 0 }) |
| 108 |
if want := []int{2}; !a.Equal(s2, want) { |
| 109 |
panic(fmt.Sprintf("a.Filter(%v, even) = %v, want %v", s1, s2, want)) |
| 110 |
} |
| 111 |
|
| 112 |
if s3 := a.Filter(s1[:0], func(i int) bool { return true }); len(s3) > 0 { |
| 113 |
panic(fmt.Sprintf("a.Filter(%v, identity) = %v, want empty slice", s1[:0], s3)) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestMax() { |
| 118 |
s1 := []int{1, 2, 3, -5} |
| 119 |
if got, want := a.SliceMax(s1), 3; got != want { |
| 120 |
panic(fmt.Sprintf("a.Max(%v) = %d, want %d", s1, got, want)) |
| 121 |
} |
| 122 |
|
| 123 |
s2 := []string{"aaa", "a", "aa", "aaaa"} |
| 124 |
if got, want := a.SliceMax(s2), "aaaa"; got != want { |
| 125 |
panic(fmt.Sprintf("a.Max(%v) = %q, want %q", s2, got, want)) |
| 126 |
} |
| 127 |
|
| 128 |
if got, want := a.SliceMax(s2[:0]), ""; got != want { |
| 129 |
panic(fmt.Sprintf("a.Max(%v) = %q, want %q", s2[:0], got, want)) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestMin() { |
| 134 |
s1 := []int{1, 2, 3, -5} |
| 135 |
if got, want := a.SliceMin(s1), -5; got != want { |
| 136 |
panic(fmt.Sprintf("a.Min(%v) = %d, want %d", s1, got, want)) |
| 137 |
} |
| 138 |
|
| 139 |
s2 := []string{"aaa", "a", "aa", "aaaa"} |
| 140 |
if got, want := a.SliceMin(s2), "a"; got != want { |
| 141 |
panic(fmt.Sprintf("a.Min(%v) = %q, want %q", s2, got, want)) |
| 142 |
} |
| 143 |
|
| 144 |
if got, want := a.SliceMin(s2[:0]), ""; got != want { |
| 145 |
panic(fmt.Sprintf("a.Min(%v) = %q, want %q", s2[:0], got, want)) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestAppend() { |
| 150 |
s := []int{1, 2, 3} |
| 151 |
s = a.Append(s, 4, 5, 6) |
| 152 |
want := []int{1, 2, 3, 4, 5, 6} |
| 153 |
if !a.Equal(s, want) { |
| 154 |
panic(fmt.Sprintf("after a.Append got %v, want %v", s, want)) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func TestCopy() { |
| 159 |
s1 := []int{1, 2, 3} |
| 160 |
s2 := []int{4, 5} |
| 161 |
if got := a.Copy(s1, s2); got != 2 { |
| 162 |
panic(fmt.Sprintf("a.Copy returned %d, want 2", got)) |
| 163 |
} |
| 164 |
want := []int{4, 5, 3} |
| 165 |
if !a.Equal(s1, want) { |
| 166 |
panic(fmt.Sprintf("after a.Copy got %v, want %v", s1, want)) |
| 167 |
} |
| 168 |
} |
| 169 |
func main() { |
| 170 |
TestEqual() |
| 171 |
TestEqualFn() |
| 172 |
TestMap() |
| 173 |
TestReduce() |
| 174 |
TestFilter() |
| 175 |
TestMax() |
| 176 |
TestMin() |
| 177 |
TestAppend() |
| 178 |
TestCopy() |
| 179 |
} |