aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-09-09 00:49:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-09-09 00:49:28 +0200
commit79189d25ecd077cc308d24c8c0919d2094cf8be3 (patch)
treec118a3ded21a835686eb0349dab6f8a05f465b14
parent648413b4644130d419397505ec28e005ea0e543d (diff)
Tests
-rw-r--r--main.go7
-rw-r--r--ops.go14
-rw-r--r--ops_test.go100
-rw-r--r--verify.go83
4 files changed, 66 insertions, 138 deletions
diff --git a/main.go b/main.go
deleted file mode 100644
index bbaeff0..0000000
--- a/main.go
+++ /dev/null
@@ -1,7 +0,0 @@
-package main
-
-const maxValue = 1000
-
-func main() {
- verifyAll()
-}
diff --git a/ops.go b/ops.go
index 7d35e0d..d73b1c1 100644
--- a/ops.go
+++ b/ops.go
@@ -1,4 +1,4 @@
-package main
+package add
import (
"container/list"
@@ -6,19 +6,19 @@ import (
"log"
)
-type (
- // Set is ordered set of alphabet
- Set struct{ *list.List }
+// Set is ordered set of alphabet
+type Set struct{ *list.List }
- // Element of ordered set
- Element struct{ *list.Element }
-)
+// Element of ordered set
+type Element struct{ *list.Element }
var (
alphabet Set
zero, one Element
)
+const maxValue = 1000
+
func init() {
alphabet = Set{list.New()}
for i := 0; i <= maxValue; i++ {
diff --git a/ops_test.go b/ops_test.go
index c89c461..a20ea3f 100644
--- a/ops_test.go
+++ b/ops_test.go
@@ -1,51 +1,69 @@
-package main
+package add
-import "testing"
+import (
+ "math"
+ "math/rand"
+ "testing"
+ "time"
+)
-func TestAdd(t *testing.T) {
- if _, ok := verify("+"); !ok {
- t.Error("add failed")
- }
-}
+func TestOp(t *testing.T) {
+ rand.Seed(time.Now().UnixNano())
-func BenchmarkAdd(b *testing.B) {
- for i := 0; i < b.N; i++ {
- verify("+")
+ testCases := map[string]struct {
+ function func(Element, Element) Element
+ expect func(int, int) int
+ maxm, maxn int
+ order bool
+ }{
+ "+": {
+ function: Add,
+ expect: func(m, n int) int { return m + n },
+ maxm: 100,
+ maxn: 100,
+ order: false,
+ },
+ "*": {
+ function: Mul,
+ expect: func(m, n int) int { return m * n },
+ maxm: 10,
+ maxn: 10,
+ order: false,
+ },
+ "^": {
+ function: Pow,
+ expect: func(m, n int) int { return int(math.Pow(float64(m), float64(n))) },
+ maxm: 10,
+ maxn: 3,
+ order: false,
+ },
+ "-": {
+ function: Sub,
+ expect: func(m, n int) int { return m - n },
+ maxm: 100,
+ maxn: 100,
+ order: true,
+ },
}
-}
-func TestSub(t *testing.T) {
- if _, ok := verify("-"); !ok {
- t.Error("sub failed")
- }
-}
+ for op, tc := range testCases {
+ t.Run(op, func(t *testing.T) {
-func BenchmarkSub(b *testing.B) {
- for i := 0; i < b.N; i++ {
- verify("-")
- }
-}
+ m := rand.Intn(tc.maxm)
+ n := rand.Intn(tc.maxn)
+ if tc.order && n > m {
+ m, n = n, m
+ }
+ e := tc.expect(m, n)
-func TestMul(t *testing.T) {
- if _, ok := verify("*"); !ok {
- t.Error("mul failed")
- }
-}
-
-func BenchmarkMul(b *testing.B) {
- for i := 0; i < b.N; i++ {
- verify("*")
- }
-}
-
-func TestPow(t *testing.T) {
- if _, ok := verify("^"); !ok {
- t.Error("pot failed")
- }
-}
+ me := Index(m)
+ ne := Index(n)
+ ee := Index(e)
+ re := tc.function(me, ne)
-func BenchmarkPow(b *testing.B) {
- for i := 0; i < b.N; i++ {
- verify("^")
+ if !re.Equals(ee) {
+ t.Errorf("%v %v %v: got %v, want %v", me, op, ne, re, ee)
+ }
+ })
}
}
diff --git a/verify.go b/verify.go
deleted file mode 100644
index 03cd09a..0000000
--- a/verify.go
+++ /dev/null
@@ -1,83 +0,0 @@
-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))
- }
-}