From 79189d25ecd077cc308d24c8c0919d2094cf8be3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 9 Sep 2017 00:49:28 +0200 Subject: Tests --- main.go | 7 ----- ops.go | 14 ++++----- ops_test.go | 100 +++++++++++++++++++++++++++++++++++------------------------- verify.go | 83 ------------------------------------------------- 4 files changed, 66 insertions(+), 138 deletions(-) delete mode 100644 main.go delete mode 100644 verify.go 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)) - } -} -- cgit v1.2.3