golang.org/x/tools@v0.21.0/go/ssa/interp/testdata/minmax.go (about) 1 // Copyright 2023 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 "fmt" 9 "math" 10 ) 11 12 func main() { 13 TestMinFloat() 14 TestMaxFloat() 15 TestMinMaxInt() 16 TestMinMaxUint8() 17 TestMinMaxString() 18 } 19 20 func errorf(format string, args ...any) { panic(fmt.Sprintf(format, args...)) } 21 func fatalf(format string, args ...any) { panic(fmt.Sprintf(format, args...)) } 22 23 // derived from $GOROOT/src/runtime/minmax_test.go 24 25 var ( 26 zero = math.Copysign(0, +1) 27 negZero = math.Copysign(0, -1) 28 inf = math.Inf(+1) 29 negInf = math.Inf(-1) 30 nan = math.NaN() 31 ) 32 33 var tests = []struct{ min, max float64 }{ 34 {1, 2}, 35 {-2, 1}, 36 {negZero, zero}, 37 {zero, inf}, 38 {negInf, zero}, 39 {negInf, inf}, 40 {1, inf}, 41 {negInf, 1}, 42 } 43 44 var all = []float64{1, 2, -1, -2, zero, negZero, inf, negInf, nan} 45 46 func eq(x, y float64) bool { 47 return x == y && math.Signbit(x) == math.Signbit(y) 48 } 49 50 func TestMinFloat() { 51 for _, tt := range tests { 52 if z := min(tt.min, tt.max); !eq(z, tt.min) { 53 errorf("min(%v, %v) = %v, want %v", tt.min, tt.max, z, tt.min) 54 } 55 if z := min(tt.max, tt.min); !eq(z, tt.min) { 56 errorf("min(%v, %v) = %v, want %v", tt.max, tt.min, z, tt.min) 57 } 58 } 59 for _, x := range all { 60 if z := min(nan, x); !math.IsNaN(z) { 61 errorf("min(%v, %v) = %v, want %v", nan, x, z, nan) 62 } 63 if z := min(x, nan); !math.IsNaN(z) { 64 errorf("min(%v, %v) = %v, want %v", nan, x, z, nan) 65 } 66 } 67 } 68 69 func TestMaxFloat() { 70 for _, tt := range tests { 71 if z := max(tt.min, tt.max); !eq(z, tt.max) { 72 errorf("max(%v, %v) = %v, want %v", tt.min, tt.max, z, tt.max) 73 } 74 if z := max(tt.max, tt.min); !eq(z, tt.max) { 75 errorf("max(%v, %v) = %v, want %v", tt.max, tt.min, z, tt.max) 76 } 77 } 78 for _, x := range all { 79 if z := max(nan, x); !math.IsNaN(z) { 80 errorf("min(%v, %v) = %v, want %v", nan, x, z, nan) 81 } 82 if z := max(x, nan); !math.IsNaN(z) { 83 errorf("min(%v, %v) = %v, want %v", nan, x, z, nan) 84 } 85 } 86 } 87 88 // testMinMax tests that min/max behave correctly on every pair of 89 // values in vals. 90 // 91 // vals should be a sequence of values in strictly ascending order. 92 func testMinMax[T int | uint8 | string](vals ...T) { 93 for i, x := range vals { 94 for _, y := range vals[i+1:] { 95 if !(x < y) { 96 fatalf("values out of order: !(%v < %v)", x, y) 97 } 98 99 if z := min(x, y); z != x { 100 errorf("min(%v, %v) = %v, want %v", x, y, z, x) 101 } 102 if z := min(y, x); z != x { 103 errorf("min(%v, %v) = %v, want %v", y, x, z, x) 104 } 105 106 if z := max(x, y); z != y { 107 errorf("max(%v, %v) = %v, want %v", x, y, z, y) 108 } 109 if z := max(y, x); z != y { 110 errorf("max(%v, %v) = %v, want %v", y, x, z, y) 111 } 112 } 113 } 114 } 115 116 func TestMinMaxInt() { testMinMax[int](-7, 0, 9) } 117 func TestMinMaxUint8() { testMinMax[uint8](0, 1, 2, 4, 7) } 118 func TestMinMaxString() { testMinMax[string]("a", "b", "c") }