-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_abs_test.go
76 lines (69 loc) · 1.79 KB
/
max_abs_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
package maths
import (
"errors"
"fmt"
"math"
"testing"
)
func TestMax(t *testing.T) {
testCases := []struct {
input []int
expectedResult int
}{
{[]int{}, 0},
{[]int{0}, 0},
{[]int{-10}, -10},
{[]int{10}, 10},
{[]int{-10, -10}, -10},
{[]int{-10, -9}, -9},
{[]int{-1, 0}, 0},
{[]int{0, -1}, 0},
{[]int{0, 0}, 0},
{[]int{0, 1}, 1},
{[]int{-1, 1}, 1},
{[]int{1, 10}, 10},
{[]int{-100, -100, -100}, -100},
{[]int{-100, -100, -99}, -99},
{[]int{-100, -1, 0}, 0},
{[]int{1, 100, 10}, 100},
{[]int{10, 5, 7, 2, 3, 6, 8, 1, 4, 9}, 10},
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %v", tC.input)
t.Run(testName, func(t *testing.T) {
// Check if the actual result matches the expected result.
if actualResult := Max(tC.input...); actualResult != tC.expectedResult {
t.Errorf("Expected result: %d, got result: %d", tC.expectedResult, actualResult)
}
})
}
}
func TestAbs(t *testing.T) {
testCases := []struct {
input, expectedResult int
expectedError error
}{
{math.MinInt + 1, math.MaxInt, nil},
{-100, 100, nil},
{-1, 1, nil},
{0, 0, nil},
{1, 1, nil},
{100, 100, nil},
{math.MaxInt, math.MaxInt, nil},
{math.MinInt, 0, ErrAbsoluteValueOfMinInt},
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %d", tC.input)
t.Run(testName, func(t *testing.T) {
actualResult, actualError := Abs(tC.input)
// Check if the actual error matches the expected error.
if !errors.Is(actualError, tC.expectedError) {
t.Errorf("Expected error: %v, got error: %v", tC.expectedError, actualError)
}
// Check if the actual result matches the expected result.
if actualResult != tC.expectedResult {
t.Errorf("Expected result: %d, got result: %d", tC.expectedResult, actualResult)
}
})
}
}