aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-04-08 13:33:01 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-04-08 13:33:01 +0200
commita3a8f67b718768867bf579c79bc2ce30a0bde45b (patch)
tree6d3bd2499bcf1e3ff19d36ac3c96e168009de015
parent4ad8cc3d0c36cb784fea32398afbf0cd0e138165 (diff)
Clean up
-rw-r--r--ops.go55
-rw-r--r--ops_test.go16
-rw-r--r--verify.go24
3 files changed, 55 insertions, 40 deletions
diff --git a/ops.go b/ops.go
index 71b62f5..a6c533f 100644
--- a/ops.go
+++ b/ops.go
@@ -2,81 +2,92 @@ package main
import (
"container/list"
+ "fmt"
"log"
)
+type (
+ List struct{ *list.List }
+ Element struct{ *list.Element }
+)
+
var (
- alphabet *list.List
- zero, one *list.Element
+ alphabet List
+ zero, one Element
)
func init() {
- alphabet = list.New()
+ alphabet = List{list.New()}
for i := 0; i <= maxValue; i++ {
alphabet.PushBack(i)
}
- zero = alphabet.Front()
- one = zero.Next()
+ zero = Element{alphabet.Front()}
+ one = Element{zero.Next()}
}
// next returns next char in alphabet
-func next(n *list.Element) *list.Element {
+func (n Element) next() Element {
if nxt := n.Next(); nxt != nil {
- return nxt
+ return Element{nxt}
}
log.Fatal("out of range", n.Value)
- return nil
+ return Element{}
}
// prev returns previous char in alphabet
-func prev(n *list.Element) *list.Element {
+func (n Element) prev() Element {
if prv := n.Prev(); prv != nil {
- return prv
+ return Element{prv}
}
return zero
}
// add defines addition
-func add(m, n *list.Element) *list.Element {
+func add(m, n Element) Element {
if m == zero {
return n
}
- return next(add(prev(m), n))
+ return add(m.prev(), n).next()
}
// times defines mutiplication
-func times(m, n *list.Element) *list.Element {
+func times(m, n Element) Element {
if m == zero {
return zero
}
- return add(times(prev(m), n), n)
+ return add(times(m.prev(), n), n)
}
// pot defines power function
-func pot(m, n *list.Element) *list.Element {
+func pot(m, n Element) Element {
if n == zero {
return one
}
- return times(pot(m, prev(n)), m)
+ return times(pot(m, n.prev()), m)
}
// sub defines substraction
-func sub(m, n *list.Element) *list.Element {
+func sub(m, n Element) Element {
if n == zero {
return m
}
- return prev(sub(m, prev(n)))
+ return sub(m, n.prev()).prev()
}
-// scan lookups element representation
-func scan(n int) *list.Element {
+// scan lookups n-th element representation
+func scan(n int) Element {
if n > alphabet.Len() {
log.Fatal("out of range")
- return nil
+ return Element{}
}
e := alphabet.Front()
for i := 0; i < n; i++ {
e = e.Next()
}
- return e
+ return Element{e}
+}
+
+// String pretty-prints value
+func (n Element) String() string {
+ return fmt.Sprint(n.Value)
}
diff --git a/ops_test.go b/ops_test.go
index 2945447..fd0cadb 100644
--- a/ops_test.go
+++ b/ops_test.go
@@ -3,49 +3,49 @@ package main
import "testing"
func TestAdd(t *testing.T) {
- if _, ok := verify("+", add); !ok {
+ if _, ok := verify("+"); !ok {
t.Error("add failed")
}
}
func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
- verify("+", add)
+ verify("+")
}
}
func TestSub(t *testing.T) {
- if _, ok := verify("-", sub); !ok {
+ if _, ok := verify("-"); !ok {
t.Error("sub failed")
}
}
func BenchmarkSub(b *testing.B) {
for i := 0; i < b.N; i++ {
- verify("-", sub)
+ verify("-")
}
}
func TestTimes(t *testing.T) {
- if _, ok := verify("*", times); !ok {
+ if _, ok := verify("*"); !ok {
t.Error("times failed")
}
}
func BenchmarkTimes(b *testing.B) {
for i := 0; i < b.N; i++ {
- verify("*", times)
+ verify("*")
}
}
func TestPot(t *testing.T) {
- if _, ok := verify("^", pot); !ok {
+ if _, ok := verify("^"); !ok {
t.Error("pot failed")
}
}
func BenchmarkPot(b *testing.B) {
for i := 0; i < b.N; i++ {
- verify("^", pot)
+ verify("^")
}
}
diff --git a/verify.go b/verify.go
index db616ca..ffdc816 100644
--- a/verify.go
+++ b/verify.go
@@ -1,21 +1,25 @@
package main
import (
- "container/list"
"fmt"
"math"
"math/rand"
"time"
)
-type function func(*list.Element, *list.Element) *list.Element
-
func init() {
rand.Seed(time.Now().Unix())
}
+var f = map[string]func(Element, Element) Element{
+ "+": add,
+ "-": sub,
+ "*": times,
+ "^": pot,
+}
+
// verify perfoms tests
-func verify(op string, f function) (string, bool) {
+func verify(op string) (string, bool) {
var mm, nn, rr int
switch op {
case "+":
@@ -40,15 +44,15 @@ func verify(op string, f function) (string, bool) {
}
m := scan(mm)
n := scan(nn)
- r := f(m, n)
+ r := f[op](m, n)
expected := scan(rr)
ok := r.Value == expected.Value
- return fmt.Sprint(m.Value, op, n.Value, "=", r.Value), ok
+ return fmt.Sprint(m, op, n, "=", r), ok
}
func verifyAll() {
- fmt.Println(verify("+", add))
- fmt.Println(verify("-", sub))
- fmt.Println(verify("*", times))
- fmt.Println(verify("^", pot))
+ fmt.Println(verify("+"))
+ fmt.Println(verify("-"))
+ fmt.Println(verify("*"))
+ fmt.Println(verify("^"))
}