Debian Codegraph @

exp/ordered/ordered_test.go

raw
1 package ordered
2
3 import (
4 "cmp"
5 "fmt"
6 "testing"
7 )
8
9 func assertEqual[T cmp.Ordered](tb testing.TB, result, expect T) {
10 tb.Helper()
11 if result != expect {
12 tb.Errorf("expected %v, got %v", expect, result)
13 }
14 }
15
16 func TestMin(t *testing.T) {
17 name := func(x, y, expect any) string {
18 return fmt.Sprintf("min(%v, %v) = %v", x, y, expect)
19 }
20
21 for expect, args := range map[int][2]int{
22 1: {1, 2},
23 0: {1, 0},
24 -10: {1, -10},
25 } {
26 t.Run(name(args[0], args[1], expect), func(t *testing.T) {
27 assertEqual(t, Min(args[0], args[1]), expect)
28 })
29 }
30 for expect, args := range map[float64][2]float64{
31 0.1: {0.1, 2},
32 0.0: {1, 0},
33 -1.0: {1, -1.0},
34 } {
35 t.Run(name(args[0], args[1], expect), func(t *testing.T) {
36 assertEqual(t, Min(args[0], args[1]), expect)
37 })
38 }
39 for expect, args := range map[string][2]string{
40 "": {"", "a"},
41 "a": {"aa", "a"},
42 "aa": {"aa", "aaaa"},
43 } {
44 t.Run(name(args[0], args[1], expect), func(t *testing.T) {
45 assertEqual(t, Min(args[0], args[1]), expect)
46 })
47 }
48 }
49
50 func TestMax(t *testing.T) {
51 name := func(x, y, expect any) string {
52 return fmt.Sprintf("max(%v, %v) = %v", x, y, expect)
53 }
54
55 for expect, args := range map[int][2]int{
56 2: {1, 2},
57 1: {1, 0},
58 0: {0, -10},
59 } {
60 t.Run(name(args[0], args[1], expect), func(t *testing.T) {
61 assertEqual(t, Max(args[0], args[1]), expect)
62 })
63 }
64 for expect, args := range map[float64][2]float64{
65 0.1: {0.1, 0.02},
66 1.0: {1, 0},
67 -1.0: {-1.1, -1.0},
68 } {
69 t.Run(name(args[0], args[1], expect), func(t *testing.T) {
70 assertEqual(t, Max(args[0], args[1]), expect)
71 })
72 }
73 for expect, args := range map[string][2]string{
74 "a": {"", "a"},
75 "aa": {"aa", "a"},
76 "aaaa": {"aa", "aaaa"},
77 } {
78 t.Run(name(args[0], args[1], expect), func(t *testing.T) {
79 assertEqual(t, Max(args[0], args[1]), expect)
80 })
81 }
82 }
83
84 func TestClamp(t *testing.T) {
85 name := func(n, low, high, expect any) string {
86 return fmt.Sprintf("clamp(%v, %v, %v) = %v", n, low, high, expect)
87 }
88
89 for expect, input := range map[int]struct {
90 n, low, high int
91 }{
92 10: {10, 1, 10},
93 4: {2, 4, 30},
94 32: {45, 20, 32},
95 15: {15, 33, 11},
96 } {
97 t.Run(name(input.n, input.low, input.high, expect), func(t *testing.T) {
98 assertEqual(t, Clamp(input.n, input.low, input.high), expect)
99 })
100 }
101
102 for expect, input := range map[float64]struct {
103 n, low, high float64
104 }{
105 1.0: {1.0, 1.0, 10.3},
106 0.4: {0.2, 0.4, 30},
107 3.2: {4.5, 2.0, 3.2},
108 } {
109 t.Run(name(input.n, input.low, input.high, expect), func(t *testing.T) {
110 assertEqual(t, Clamp(input.n, input.low, input.high), expect)
111 })
112 }
113
114 for expect, input := range map[string]struct {
115 n, low, high string
116 }{
117 "a": {"a", "a", "aaaa"},
118 "aaa": {"aaa", "aa", "aaaa"},
119 "aaaa": {"aaaaaa", "aa", "aaaa"},
120 } {
121 t.Run(name(input.n, input.low, input.high, expect), func(t *testing.T) {
122 assertEqual(t, Clamp(input.n, input.low, input.high), expect)
123 })
124 }
125 }
126
127 func TestFirst(t *testing.T) {
128 name := func(args []any, expect any) string {
129 return fmt.Sprintf("first(%v) = %v", args, expect)
130 }
131 for expect, args := range map[string]struct {
132 x string
133 y []string
134 }{
135 "a": {"", []string{"", "a", "b", ""}},
136 "c": {"c", []string{"", "a", "b", ""}},
137 "": {"", nil},
138 } {
139 fnargs := []any{args.x}
140 for _, y := range args.y {
141 fnargs = append(fnargs, y)
142 }
143
144 t.Run(name(fnargs, expect), func(t *testing.T) {
145 assertEqual(t, First(args.x, args.y...), expect)
146 })
147 }
148
149 for expect, args := range map[int]struct {
150 x int
151 y []int
152 }{
153 1: {0, []int{0, 1, 2}},
154 0: {0, []int{0, 0, 0, 0}},
155 100: {100, []int{0}},
156 } {
157 fnargs := []any{args.x}
158 for _, y := range args.y {
159 fnargs = append(fnargs, y)
160 }
161
162 t.Run(name(fnargs, expect), func(t *testing.T) {
163 assertEqual(t, First(args.x, args.y...), expect)
164 })
165 }
166 }