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) } }) } }