aboutsummaryrefslogtreecommitdiff
path: root/ops_test.go
blob: a20ea3f507bc7518e22fee1bb0ebf9ef3fc45a9d (plain)
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
package add

import (
	"math"
	"math/rand"
	"testing"
	"time"
)

func TestOp(t *testing.T) {
	rand.Seed(time.Now().UnixNano())

	testCases := map[string]struct {
		function   func(Element, Element) Element
		expect     func(int, int) int
		maxm, maxn int
		order      bool
	}{
		"+": {
			function: Add,
			expect:   func(m, n int) int { return m + n },
			maxm:     100,
			maxn:     100,
			order:    false,
		},
		"*": {
			function: Mul,
			expect:   func(m, n int) int { return m * n },
			maxm:     10,
			maxn:     10,
			order:    false,
		},
		"^": {
			function: Pow,
			expect:   func(m, n int) int { return int(math.Pow(float64(m), float64(n))) },
			maxm:     10,
			maxn:     3,
			order:    false,
		},
		"-": {
			function: Sub,
			expect:   func(m, n int) int { return m - n },
			maxm:     100,
			maxn:     100,
			order:    true,
		},
	}

	for op, tc := range testCases {
		t.Run(op, func(t *testing.T) {

			m := rand.Intn(tc.maxm)
			n := rand.Intn(tc.maxn)
			if tc.order && n > m {
				m, n = n, m
			}
			e := tc.expect(m, n)

			me := Index(m)
			ne := Index(n)
			ee := Index(e)
			re := tc.function(me, ne)

			if !re.Equals(ee) {
				t.Errorf("%v %v %v: got %v, want %v", me, op, ne, re, ee)
			}
		})
	}
}