From b0fce68955378fa2126385fd3ebdde39ca1c8c32 Mon Sep 17 00:00:00 2001 From: Mihalis Tsoukalos Date: Tue, 23 Apr 2019 19:00:44 +0300 Subject: [PATCH] Using testing/quick --- ch11/quick.go | 10 +++++++++- ch11/quick_test.go | 27 ++++++++++++++++++++++++++- ch11/randomBuiltin.go | 4 ++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/ch11/quick.go b/ch11/quick.go index 2fd0fd2..7ed8762 100644 --- a/ch11/quick.go +++ b/ch11/quick.go @@ -4,6 +4,14 @@ import ( "fmt" ) +func Add(x, y uint16) uint16 { + var i uint16 + for i = 0; i < x; i++ { + y++ + } + return y +} + func main() { - + fmt.Println(Add(0, 0)) } diff --git a/ch11/quick_test.go b/ch11/quick_test.go index 8ca57dc..c586c4d 100644 --- a/ch11/quick_test.go +++ b/ch11/quick_test.go @@ -1,6 +1,31 @@ package main import ( - "fmt" + "testing" "testing/quick" ) + +var N = 1000000 + +func TestWithSystem(t *testing.T) { + condition := func(a, b uint16) bool { + return Add(a, b) == (b + a) + } + + err := quick.Check(condition, &quick.Config{MaxCount: N}) + if err != nil { + t.Errorf("Error: %v", err) + } +} + +func TestWithItself(t *testing.T) { + condition := func(a, b uint16) bool { + return Add(a, b) == Add(b, a) + } + + err := quick.Check(condition, &quick.Config{MaxCount: N}) + if err != nil { + t.Errorf("Error: %v", err) + } + +} diff --git a/ch11/randomBuiltin.go b/ch11/randomBuiltin.go index e5294c8..ad32b5d 100644 --- a/ch11/randomBuiltin.go +++ b/ch11/randomBuiltin.go @@ -13,9 +13,9 @@ func main() { X, Y, Z int8 S float32 } - aPoint := rand.New(rand.NewSource(time.Now().Unix())) + ran := rand.New(rand.NewSource(time.Now().Unix())) myValues := reflect.TypeOf(point3D{}) - x, _ := quick.Value(myValues, aPoint) + x, _ := quick.Value(myValues, ran) fmt.Println(x) }