-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordered_test.go
53 lines (44 loc) · 977 Bytes
/
ordered_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package nstd
import (
"math"
"testing"
)
func TestClamp(t *testing.T) {
testClampOfType[int](t)
testClampOfType[uint32](t)
testClampOfType[int64](t)
testClampOfType[float32](t)
testClampOfType[float64](t)
testClamp(math.NaN(), 0, 1, math.NaN(), t)
testClamp("a", "c", "f", "c", t)
testClamp("x", "c", "f", "f", t)
defer func() {
if recover() == nil {
t.Fatalf("should panic when min > max")
}
}()
testClamp(math.NaN(), 1, 0, math.NaN(), t)
}
func testClamp[R Ordered](v, min, max, expected R, t *testing.T) {
if clamped := Clamp(v, min, max); clamped != expected {
if v == v || expected == expected {
t.Fatalf("Clamp(%v, %v, %v) != %v (but %v)", v, min, max, expected, clamped)
}
}
}
func testClampOfType[R Real](t *testing.T) {
var min R = 2
var max R = 8
var testCases = []struct {
v, clamped R
}{
{1, 2},
{2, 2},
{5, 5},
{8, 8},
{9, 8},
}
for _, tc := range testCases {
testClamp(tc.v, min, max, tc.clamped, t)
}
}