aboutsummaryrefslogtreecommitdiff
path: root/ops_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'ops_test.go')
-rw-r--r--ops_test.go100
1 files changed, 59 insertions, 41 deletions
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)
+ }
+ })
}
}