Skip to content

Commit 8a54422

Browse files
test: Use error.Is(errorType, errorInstance) to check for error equality (TheAlgorithms#668)
Co-authored-by: Rak Laptudirm <[email protected]>
1 parent c129115 commit 8a54422

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

search/binary_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package search
22

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

58
func TestBinary(t *testing.T) {
69
for _, test := range searchTests {
710
actualValue, actualError := Binary(test.data, test.key, 0, len(test.data)-1)
811
if actualValue != test.expected {
912
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
1013
}
11-
if actualError != test.expectedError {
14+
if !errors.Is(test.expectedError, actualError) {
1215
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
1316
}
1417
}
@@ -20,7 +23,7 @@ func TestBinaryIterative(t *testing.T) {
2023
if actualValue != test.expected {
2124
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
2225
}
23-
if actualError != test.expectedError {
26+
if !errors.Is(test.expectedError, actualError) {
2427
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
2528
}
2629
}
@@ -32,7 +35,7 @@ func TestLowerBound(t *testing.T) {
3235
if actualValue != test.expected {
3336
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
3437
}
35-
if actualError != test.expectedError {
38+
if !errors.Is(test.expectedError, actualError) {
3639
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
3740
}
3841
}
@@ -44,7 +47,7 @@ func TestUpperBound(t *testing.T) {
4447
if actualValue != test.expected {
4548
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
4649
}
47-
if actualError != test.expectedError {
50+
if !errors.Is(test.expectedError, actualError) {
4851
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
4952
}
5053
}

0 commit comments

Comments
 (0)