aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-10-13 13:39:50 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-10-13 13:39:50 +0200
commit0b9501345b0685d31b355f2e85d3121f398b28c8 (patch)
treecbd5f2326a42168386cb9ffa1519c967554f9033
parent5cfc3f44d3aa5a30a6d590913a4e1eff10db0d77 (diff)
Cleanup
-rw-r--r--ops.go24
-rw-r--r--verify.go45
2 files changed, 36 insertions, 33 deletions
diff --git a/ops.go b/ops.go
index 73a2d38..bca0acd 100644
--- a/ops.go
+++ b/ops.go
@@ -2,6 +2,7 @@ package main
import (
"container/list"
+ "fmt"
"log"
)
@@ -81,3 +82,26 @@ func (m Element) sub(n Element) Element {
}
return m.sub(n.prev()).prev()
}
+
+// Equals compares two elements
+func (m Element) Equals(n Element) bool {
+ return m.Value == n.Value
+}
+
+// String pretty-prints value
+func (m Element) String() string {
+ return fmt.Sprint(m.Value)
+}
+
+// Scan returns n-th Element
+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}
+}
diff --git a/verify.go b/verify.go
index 7c2a4c8..ffb4c54 100644
--- a/verify.go
+++ b/verify.go
@@ -22,28 +22,28 @@ func init() {
var cases = map[string]testCase{
"+": testCase{
function: Add,
- expect: testAdd,
+ expect: expectAdd,
maxm: 100,
maxn: 100,
order: false,
},
"*": testCase{
function: Mul,
- expect: testMul,
+ expect: expectMul,
maxm: 10,
maxn: 10,
order: false,
},
"^": testCase{
function: Pot,
- expect: testPot,
+ expect: expectPot,
maxm: 10,
maxn: 3,
order: false,
},
"-": testCase{
function: Sub,
- expect: testSub,
+ expect: expectSub,
maxm: 100,
maxn: 100,
order: true,
@@ -63,42 +63,21 @@ func verify(op string) (string, bool) {
}
e := c.expect(m, n)
- me := scan(m)
- ne := scan(n)
- ee := scan(e)
+ me := Scan(m)
+ ne := Scan(n)
+ ee := Scan(e)
re := c.function(me, ne)
- return fmt.Sprint(me, op, ne, "=", re), re.equals(ee)
+ 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 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))
}
}
-
-// 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
-}