aboutsummaryrefslogtreecommitdiff
path: root/verify.go
blob: 7c2a4c81b2fa74ccd87995e359132d2d3eb8cfbf (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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:   testAdd,
		maxm:     100,
		maxn:     100,
		order:    false,
	},
	"*": testCase{
		function: Mul,
		expect:   testMul,
		maxm:     10,
		maxn:     10,
		order:    false,
	},
	"^": testCase{
		function: Pot,
		expect:   testPot,
		maxm:     10,
		maxn:     3,
		order:    false,
	},
	"-": testCase{
		function: Sub,
		expect:   testSub,
		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 scan(n int) Element {
	if n > alphabet.Len() {
		log.Fatal("out of range ", n)
		return Element{}
	}
	e := alphabet.Front()
	for i := 0; i < n; i++ {
		e = e.Next()
	}
	return Element{e}
}

func testAdd(m, n int) int { return m + n }
func testMul(m, n int) int { return m * n }
func testPot(m, n int) int { return int(math.Pow(float64(m), float64(n))) }
func testSub(m, n int) int { return m - n }

func verifyAll() {
	for op := range cases {
		fmt.Println(verify(op))
	}
}

// String pretty-prints value
func (m Element) String() string {
	return fmt.Sprint(m.Value)
}

func (m Element) equals(n Element) bool {
	return m.Value == n.Value
}