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

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

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

	testCases := map[string]struct {
		f          func(Element, Element) Element
		ref        func(int, int) int
		maxm, maxn int
		ordered    bool
	}{
		"+": {
			f:       Add,
			ref:     func(m, n int) int { return m + n },
			maxm:    100,
			maxn:    100,
			ordered: false,
		},
		"*": {
			f:       Mul,
			ref:     func(m, n int) int { return m * n },
			maxm:    10,
			maxn:    10,
			ordered: false,
		},
		"^": {
			f:       Pow,
			ref:     func(m, n int) int { return int(math.Pow(float64(m), float64(n))) },
			maxm:    10,
			maxn:    3,
			ordered: false,
		},
		"-": {
			f:       Sub,
			ref:     func(m, n int) int { return m - n },
			maxm:    100,
			maxn:    100,
			ordered: 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.ordered && n > m {
				m, n = n, m
			}
			e := tc.ref(m, n)

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

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