-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathquicktest_test.go
64 lines (58 loc) · 1.53 KB
/
quicktest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"math/rand"
"reflect"
"testing"
"testing/quick"
)
// Generate is the needed method to implements generate interface
// needed to generate random data
func (r Retangle) Generate(rand *rand.Rand, size int) reflect.Value {
randomRetangle := Retangle{
height: rand.Int(),
width: rand.Int(),
}
return reflect.ValueOf(randomRetangle)
}
// Test table like style
func TestIsOddTable(t *testing.T) {
// tipical golang test
// anonymous struct to iterate over cases
for _, value := range []struct {
expected bool
input int
}{
{true, 3},
{false, 2},
{true, -5},
{false, -4},
} {
if obtained := IsOdd(value.input); value.expected != obtained {
t.Errorf("Expected %t but found %t", value.expected, obtained)
}
}
}
// Tests uding testing/quick package
func TestNumberPlusOne(t *testing.T) {
// assertion: if n is odd -> n+1 is even and opposite is truth too
assertion := func(x int) bool {
// you will see this log many times
t.Log("Random value: ", x)
return IsOdd(x) != IsOdd(x+1)
}
// check the assertion for many random cases(default 100)
if err := quick.Check(assertion, nil); err != nil {
t.Error(err)
}
}
// As Retangle implents generate, we can use your own function to generate random data
func TestRetangle(t *testing.T) {
assertion := func(x Retangle) bool {
// again we see many iterations
t.Log("Random Retangle:", x)
return (x.height == x.width) && (x.area() == x.height*x.height) || x.height != x.width
}
if err := quick.Check(assertion, nil); err != nil {
t.Error(err)
}
}