package main import ( "fmt" "log" "math" "math/rand" "time" ) type testCase struct { function func(Element, Element) Element expect func(int, int) int maxm, maxn int order bool } func init() { rand.Seed(time.Now().UnixNano()) } var cases = map[string]testCase{ "+": { function: Add, expect: expectAdd, maxm: 100, maxn: 100, order: false, }, "*": { function: Mul, expect: expectMul, maxm: 10, maxn: 10, order: false, }, "^": { function: Pow, expect: expectPow, maxm: 10, maxn: 3, order: false, }, "-": { function: Sub, expect: expectSub, maxm: 100, maxn: 100, order: true, }, } func verify(op string) (string, bool) { c, ok := cases[op] if !ok { log.Fatal("unknown case") } m := rand.Intn(c.maxm) n := rand.Intn(c.maxn) if c.order && n > m { m, n = n, m } e := c.expect(m, n) me := Index(m) ne := Index(n) ee := Index(e) re := c.function(me, ne) return fmt.Sprint(me, op, ne, "=", re), re.Equals(ee) } func expectAdd(m, n int) int { return m + n } func expectMul(m, n int) int { return m * n } func expectPow(m, n int) int { return int(math.Pow(float64(m), float64(n))) } func expectSub(m, n int) int { return m - n } func verifyAll() { for op := range cases { fmt.Println(verify(op)) } }