Skip to content

Commit 05be5cf

Browse files
committed
Add Property Based Tests
1 parent 57d8a63 commit 05be5cf

File tree

5 files changed

+147
-18
lines changed

5 files changed

+147
-18
lines changed

demo_05/fib_test.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@ import (
77

88
func TestFibRecursive(t *testing.T) {
99
tests := map[int]int{
10-
0: 0,
11-
1: 1,
12-
2: 1,
13-
3: 2,
14-
4: 3,
15-
5: 5,
10+
0: 0,
11+
1: 1,
12+
2: 1,
13+
3: 2,
14+
4: 3,
15+
5: 5,
16+
10: 55,
1617
}
1718
for i, v := range tests {
18-
if f := FibRecursive(i); f != v {
19-
t.Fatalf("FibRecursive(%v) == %v : expected %v", i, f, v)
20-
}
19+
t.Run(strconv.Itoa(i), func(t *testing.T) {
20+
if f := FibRecursive(i); f != v {
21+
t.Fatalf("FibRecursive(%v) == %v : expected %v", i, f, v)
22+
}
23+
})
2124
}
2225
}
2326

2427
func TestFibCache(t *testing.T) {
2528
tests := map[int]int{
26-
0: 0,
27-
1: 1,
28-
2: 1,
29-
3: 2,
30-
4: 3,
31-
5: 5,
29+
0: 0,
30+
1: 1,
31+
2: 1,
32+
3: 2,
33+
4: 3,
34+
5: 5,
35+
10: 55,
3236
}
3337
for i, v := range tests {
34-
if f := FibCache(i); f != v {
35-
t.Fatalf("FibCache(%v) == %v : expected %v", i, f, v)
36-
}
38+
t.Run(strconv.Itoa(i), func(t *testing.T) {
39+
if f := FibCache(i); f != v {
40+
t.Fatalf("FibCache(%v) == %v : expected %v", i, f, v)
41+
}
42+
})
3743
}
3844
}
3945

demo_06/mystrings.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package mystrings
2+
3+
const (
4+
LowerCaseA = 97
5+
UpperCaseZ = 90
6+
)
7+
8+
func Lower(s string) string {
9+
input := []rune(s)
10+
out := make([]rune, len(input))
11+
for i, r := range input {
12+
if r <= UpperCaseZ {
13+
out[i] = r + 32
14+
} else {
15+
out[i] = r
16+
}
17+
}
18+
return string(out)
19+
}
20+
21+
func Upper(s string) string {
22+
input := []rune(s)
23+
out := make([]rune, len(input))
24+
for i, r := range input {
25+
if r >= LowerCaseA {
26+
out[i] = r - 32
27+
} else {
28+
out[i] = r
29+
}
30+
}
31+
return string(out)
32+
}

demo_06/mystrings_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package mystrings
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/leanovate/gopter"
8+
"github.com/leanovate/gopter/gen"
9+
"github.com/leanovate/gopter/prop"
10+
)
11+
12+
func TestLower(t *testing.T) {
13+
ts := map[string]string{
14+
"HELLO": "hello",
15+
"WORLD[]": "world[]",
16+
"": "",
17+
}
18+
19+
for s, v := range ts {
20+
t.Run(s, func(t *testing.T) {
21+
if r := Lower(s); r != v {
22+
t.Fatalf("%v != %v", r, v)
23+
}
24+
})
25+
}
26+
}
27+
28+
func TestUpper(t *testing.T) {
29+
ts := map[string]string{
30+
"hello": "HELLO",
31+
"first-name": "FIRST-NAME",
32+
"": "",
33+
}
34+
35+
for s, v := range ts {
36+
t.Run(s, func(t *testing.T) {
37+
if r := Upper(s); r != v {
38+
t.Fatalf("%v != %v", r, v)
39+
}
40+
})
41+
}
42+
}
43+
44+
func TestLowerMatchesStrings(t *testing.T) {
45+
properties := gopter.NewProperties(nil)
46+
properties.Property("my implimentation matches strings.Lower", prop.ForAll(
47+
func(s string) bool {
48+
if r := Lower(s); r != strings.ToLower(s) {
49+
t.Logf("Lower(%+v) == %+v", s, r)
50+
return false
51+
}
52+
53+
return true
54+
},
55+
genStringRange('A', 'z'),
56+
))
57+
properties.TestingRun(t)
58+
}
59+
60+
func TestUppoerMatchesStrings(t *testing.T) {
61+
properties := gopter.NewProperties(nil)
62+
properties.Property("my implimentation matches strings.Upper", prop.ForAll(
63+
func(s string) bool {
64+
if r := Upper(s); r != strings.ToUpper(s) {
65+
t.Logf("Upper(%+v) == %+v", s, r)
66+
return false
67+
}
68+
69+
return true
70+
},
71+
genStringRange('A', 'z'),
72+
//genStringRange(' ', '~'),
73+
))
74+
properties.TestingRun(t)
75+
}
76+
77+
func genStringRange(min, max rune) gopter.Gen {
78+
return gopter.DeriveGen(
79+
func(rs []rune) string {
80+
return string(rs)
81+
},
82+
func(s string) []rune {
83+
return []rune(s)
84+
},
85+
gen.SliceOf(gen.RuneRange(min, max)),
86+
)
87+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/gsinghkular/go_test_demo
22

33
go 1.17
4+
5+
require github.com/leanovate/gopter v0.2.9

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
2+
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=

0 commit comments

Comments
 (0)