-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza_test.go
164 lines (145 loc) · 4.01 KB
/
pizza_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package pizza
import "testing"
type PopTestCase[T any] struct {
name string
valuesBefore Slice[T]
valuesAfter Slice[T]
expectedPopped *T
expectedError error
}
func toPtr[T any](v T) *T {
return &v
}
func TestPop(t *testing.T) {
testCases := []PopTestCase[int]{
{
name: "Pop from non-empty slice",
valuesBefore: Slice[int]{1, 2, 3},
valuesAfter: Slice[int]{1, 2},
expectedPopped: toPtr(3),
expectedError: nil,
},
{
name: "Pop from one-element slice",
valuesBefore: Slice[int]{42},
valuesAfter: Slice[int]{},
expectedPopped: toPtr(42),
expectedError: nil,
},
{
name: "Pop from empty slice",
valuesBefore: Slice[int]{},
valuesAfter: Slice[int]{},
expectedPopped: nil,
expectedError: ErrEmptySlice,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
popped, err := tc.valuesBefore.Pop()
// Check popped
if popped != nil && (tc.expectedPopped == nil || *popped != *tc.expectedPopped) {
t.Errorf("Test case '%s': Expected popped value %v, got %v", tc.name, tc.expectedPopped, *popped)
} else if popped == nil && tc.expectedPopped != nil {
t.Errorf("Test case '%s': Expected popped value %v, got nil", tc.name, tc.expectedPopped)
}
// Check valuesAfter
before, after := tc.valuesBefore, tc.valuesAfter
if !before.Equals(after) {
t.Errorf("Test case '%s': Before and after values mismatch. Expected %v, got %v", tc.name, after, before)
}
// Check error
if err == nil && tc.expectedError != nil {
t.Errorf("Test case '%s': Expected error '%v', got nil", tc.name, tc.expectedError)
} else if err != nil && tc.expectedError == nil {
t.Errorf("Test case '%s': Expected no error, got '%v'", tc.name, err)
} else if err != nil && err.Error() != tc.expectedError.Error() {
t.Errorf("Test case '%s': Expected error '%v', got '%v'", tc.name, tc.expectedError, err)
}
})
}
}
type ForEachTestCase[T any] struct {
name string
input Slice[T]
expected []T
}
func TestForEach(t *testing.T) {
testCases := []ForEachTestCase[int]{
{
name: "ForEach over a non-empty slice",
input: Slice[int]{1, 2, 3, 4, 5},
expected: []int{1, 2, 3, 4, 5},
},
{
name: "ForEach over an empty slice",
input: Slice[int]{},
expected: []int{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var values []int
tc.input.ForEach(func(e int, _ int) {
values = append(values, e)
})
// Check lengths of Slices
if len(values) != len(tc.expected) {
t.Fatalf("Test case '%s': Expected %d number of values, got %d", tc.name, len(tc.expected), len(values))
}
// Check values of Slices
for i := range values {
if values[i] != tc.expected[i] {
t.Errorf("Test case '%s': Expected value %d at index %d, got %d", tc.name, tc.expected[i], i, values[i])
}
}
})
}
}
type SomeTestCase[T any] struct {
name string
input Slice[T]
predicate func(e T, i int) bool
expected bool
expectedIndex int
}
func TestSome(t *testing.T) {
testCases := []SomeTestCase[int]{
{
name: "Check for even integers",
input: []int{1, 2, 3},
predicate: func(e int, i int) bool {
return e%2 == 0
},
expected: true,
expectedIndex: 1,
},
{
name: "Check for odd integers",
input: []int{2, 2, 2},
predicate: func(e int, i int) bool {
return e%2 != 0
},
expected: false,
expectedIndex: -1,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
meetsPredicate := tc.input.Some(tc.predicate)
if meetsPredicate != tc.expected {
t.Errorf("Test case '%s': Expected result to equal %t, got %t", tc.name, tc.expected, meetsPredicate)
}
truthyIndex := -1
for i, e := range tc.input {
if tc.predicate(e, i) {
truthyIndex = i
break
}
}
if truthyIndex != tc.expectedIndex {
t.Errorf("Test case '%s': Expected truthy at index %d, got %d", tc.name, tc.expectedIndex, truthyIndex)
}
})
}
}