Skip to content

Commit f75f8e0

Browse files
committed
Update examples/tests to use slices/maps for Equal()
1 parent ed54bbc commit f75f8e0

File tree

19 files changed

+52
-94
lines changed

19 files changed

+52
-94
lines changed

exercises/practice/all-your-base/all_your_base_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package allyourbase
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -15,7 +15,7 @@ func TestConvertToBase(t *testing.T) {
1515
} else if tc.expectedError != err.Error() {
1616
t.Errorf("ConvertToBase(%d, %#v, %d)\nwant error: %q\ngot: %q", tc.inputBase, tc.inputDigits, tc.outputBase, tc.expectedError, err.Error())
1717
}
18-
} else if !reflect.DeepEqual(tc.expected, actual) {
18+
} else if !slices.Equal(tc.expected, actual) {
1919
t.Errorf("ConvertToBase(%d, %#v, %d) = %#v, want: %#v", tc.inputBase, tc.inputDigits, tc.outputBase, actual, tc.expected)
2020
}
2121
})

exercises/practice/alphametics/alphametics_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package alphametics
22

33
import (
4-
"reflect"
4+
"maps"
55
"testing"
66
)
77

@@ -16,7 +16,7 @@ func TestSolve(t *testing.T) {
1616
}
1717
case err != nil:
1818
t.Fatalf("Solve(%q)\nwant: %#v\ngot error: %q", tc.input, tc.expected, err)
19-
case !reflect.DeepEqual(s, tc.expected):
19+
case !maps.Equal(s, tc.expected):
2020
t.Fatalf("Solve(%q)\ngot: %#v\nwant: %#v", tc.input, s, tc.expected)
2121
}
2222
})

exercises/practice/anagram/anagram_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
package anagram
22

33
import (
4-
"fmt"
5-
"sort"
4+
"slices"
65
"testing"
76
)
87

98
func TestDetectAnagrams(t *testing.T) {
109
for _, tc := range testCases {
1110
t.Run(tc.description, func(t *testing.T) {
1211
actual := Detect(tc.subject, tc.candidates)
13-
if !equal(tc.expected, actual) {
12+
slices.Sort(actual)
13+
slices.Sort(tc.expected)
14+
if !slices.Equal(tc.expected, actual) {
1415
t.Errorf("Detect(%q, %#v) = %#v, want: %#v", tc.subject, tc.candidates, actual, tc.expected)
1516
}
1617
})
1718
}
1819
}
1920

20-
func equal(a, b []string) bool {
21-
if len(b) != len(a) {
22-
return false
23-
}
24-
25-
sort.Strings(a)
26-
sort.Strings(b)
27-
return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
28-
}
29-
3021
func BenchmarkDetectAnagrams(b *testing.B) {
3122
for range b.N {
3223
for _, tt := range testCases {

exercises/practice/bottle-song/bottle_song_test.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package bottlesong
22

33
import (
4-
"fmt"
4+
"slices"
55
"strings"
66
"testing"
77
"unicode"
@@ -11,31 +11,18 @@ func TestRecite(t *testing.T) {
1111
for _, tc := range testCases {
1212
t.Run(tc.description, func(t *testing.T) {
1313
actual := Recite(tc.input.startBottles, tc.input.takeDown)
14-
if !equal(actual, tc.expected) {
14+
if !slices.Equal(actual, tc.expected) {
1515
t.Errorf("Recite(%d, %d) = %q, want: %q", tc.input.startBottles, tc.input.takeDown, actual, tc.expected)
1616
}
1717
})
1818
}
1919
}
2020

21-
func equal(a, b []string) bool {
22-
if len(b) != len(a) {
23-
return false
24-
}
25-
26-
if len(a) == 0 && len(b) == 0 {
27-
return true
28-
}
29-
30-
return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
31-
}
32-
3321
// Title is a copy of strings.Title function of the stdlib.
3422
// The copy is here because strings.Title is deprecated but we still
3523
// want to use this function as the alternative would require us to support
3624
// external dependencies which we don't yet (tracking issue https://github.com/exercism/go/issues/2379).
3725
// Students should still be able to use strings.Title if they want.
38-
// Since this exercise is currently deprecated, this shouldn't matter too much.
3926
func Title(s string) string {
4027
// Use a closure here to remember state.
4128
// Hackish but effective. Depends on Map scanning in order and calling

exercises/practice/change/change_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package change
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -12,7 +12,7 @@ func TestChange(t *testing.T) {
1212
if tc.valid {
1313
if err != nil {
1414
t.Fatalf("Change(%v, %d): want: %v, got error: %v", tc.coins, tc.target, tc.expectedChange, err)
15-
} else if !reflect.DeepEqual(actual, tc.expectedChange) {
15+
} else if !slices.Equal(actual, tc.expectedChange) {
1616
t.Fatalf("Change(%v, %d): want: %#v, got: %#v", tc.coins, tc.target, tc.expectedChange, actual)
1717
}
1818
} else {

exercises/practice/etl/etl_test.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package etl
22

3-
import "testing"
3+
import (
4+
"maps"
5+
"testing"
6+
)
47

58
var transformTests = []struct {
69
description string
@@ -53,26 +56,10 @@ var transformTests = []struct {
5356
},
5457
}
5558

56-
func equal(actual, expectation map[string]int) bool {
57-
if len(actual) != len(expectation) {
58-
return false
59-
}
60-
61-
for k, actualVal := range actual {
62-
expectationVal, present := expectation[k]
63-
64-
if !present || actualVal != expectationVal {
65-
return false
66-
}
67-
}
68-
69-
return true
70-
}
71-
7259
func TestTransform(t *testing.T) {
7360
for _, tt := range transformTests {
7461
t.Run(tt.description, func(t *testing.T) {
75-
if actual := Transform(tt.input); !equal(actual, tt.expect) {
62+
if actual := Transform(tt.input); !maps.Equal(actual, tt.expect) {
7663
t.Fatalf("Transform(%v)\ngot: %v\nwant: %v", tt.input, actual, tt.expect)
7764
}
7865
})

exercises/practice/flatten-array/flatten_array_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package flattenarray
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -15,7 +15,7 @@ func TestFlatten(t *testing.T) {
1515
t.Fatalf("Flatten(%v) = %v (nil slice), want: %v (empty slice)", tc.input, actual, tc.expected)
1616
}
1717

18-
if !reflect.DeepEqual(actual, tc.expected) {
18+
if !slices.Equal(actual, tc.expected) {
1919
t.Fatalf("Flatten(%v) = %v, want: %v", tc.input, &actual, tc.expected)
2020
}
2121
})

exercises/practice/forth/forth_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package forth
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -12,7 +12,7 @@ func TestForth(t *testing.T) {
1212
if err == nil {
1313
if tc.expected == nil {
1414
t.Fatalf("Forth(%#v) expected an error, got %v", tc.input, v)
15-
} else if !reflect.DeepEqual(v, tc.expected) {
15+
} else if !slices.Equal(v, tc.expected) {
1616
t.Fatalf("Forth(%#v) expected %v, got %v", tc.input, tc.expected, v)
1717
}
1818
} else if tc.expected != nil {

exercises/practice/grep/grep_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package grep
22

33
import (
44
"os"
5-
"reflect"
5+
"slices"
66
"strings"
77
"testing"
88
)
@@ -70,7 +70,7 @@ func TestSearch(t *testing.T) {
7070
return
7171
}
7272

73-
if !reflect.DeepEqual(actual, tc.expected) {
73+
if !slices.Equal(actual, tc.expected) {
7474
t.Errorf("Search(%q, %q, %q)\ngot: %q\nwant: %q", tc.pattern, tc.flags, tc.files, actual, tc.expected)
7575
}
7676
})

exercises/practice/ocr-numbers/ocr_numbers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ocr
22

33
import (
4-
"reflect"
4+
"slices"
55
"testing"
66
)
77

@@ -168,7 +168,7 @@ var _ = recognizeDigit // step 1.
168168
func TestRecognize(t *testing.T) {
169169
for _, tc := range testCases {
170170
t.Run(tc.description, func(t *testing.T) {
171-
if got := Recognize(tc.in); !reflect.DeepEqual(got, tc.out) {
171+
if got := Recognize(tc.in); !slices.Equal(got, tc.out) {
172172
t.Fatalf("Recognize(%q) = %q, want: %q", tc.in, got, tc.out)
173173
}
174174
})

0 commit comments

Comments
 (0)