aboutsummaryrefslogtreecommitdiff
path: root/verify.go
blob: ffb4c54ec9724e007edb249e594e99798fd46f65 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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().Unix())
}

var cases = map[string]testCase{
	"+": testCase{
		function: Add,
		expect:   expectAdd,
		maxm:     100,
		maxn:     100,
		order:    false,
	},
	"*": testCase{
		function: Mul,
		expect:   expectMul,
		maxm:     10,
		maxn:     10,
		order:    false,
	},
	"^": testCase{
		function: Pot,
		expect:   expectPot,
		maxm:     10,
		maxn:     3,
		order:    false,
	},
	"-": testCase{
		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 := Scan(m)
	ne := Scan(n)
	ee := Scan(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 expectPot(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))
	}
}