@@ -6,6 +6,74 @@ import (
6
6
"testing"
7
7
)
8
8
9
+ type Point struct {
10
+ X float64
11
+ Y float64
12
+ }
13
+
14
+ type Rect struct {
15
+ MinX float64
16
+ MaxX float64
17
+ MinY float64
18
+ MaxY float64
19
+ }
20
+
21
+ func PointInRect (p Point , r Rect ) bool {
22
+ if p .X <= r .MaxX && p .X >= r .MinX &&
23
+ p .Y <= r .MaxY && p .Y >= r .MinY {
24
+ return true
25
+ }
26
+ return false
27
+ }
28
+
29
+ var sink bool
30
+
31
+ func BenchmarkPointInRect (b * testing.B ) {
32
+ var r Rect
33
+ var p Point
34
+ r .MinX = 0
35
+ r .MaxX = 10
36
+ r .MinY = 0
37
+ r .MaxY = 10
38
+ p .X = 5
39
+ p .Y = 5
40
+ for i := 0 ; i < b .N ; i ++ {
41
+ sink = PointInRect (p , r )
42
+ }
43
+ }
44
+
45
+ func TestPointRect (t * testing.T ) {
46
+ var r Rect
47
+ var p Point
48
+ r .MinX = 0
49
+ r .MaxX = 10
50
+ r .MinY = 0
51
+ r .MaxY = 10
52
+ p .X = 5
53
+ p .Y = 5
54
+ var got bool
55
+ got = PointInRect (p , r )
56
+ if got != true {
57
+ t .Errorf ("got: %t, want: %t, %+v, %+v" , got , true , p , r )
58
+ }
59
+ p .X = 15
60
+ p .Y = 15
61
+ got = PointInRect (p , r )
62
+ if got != false {
63
+ t .Errorf ("got: %t, want: %t, %+v, %+v" , got , false , p , r )
64
+ }
65
+ p .X = 10
66
+ p .Y = 10
67
+ got = PointInRect (p , r )
68
+ if got != true {
69
+ t .Errorf ("got: %t, want: %t, %+v, %+v" , got , true , p , r )
70
+ }
71
+ }
72
+
73
+ func HelloKyle () string {
74
+ return "Hello, Kyle!"
75
+ }
76
+
9
77
// HelloWorld returns the famous 'Hello, World!' string, substituting 'World'
10
78
// with the name provided by the caller. If name is an empty string, or a
11
79
// string that contains only white space, an empty string is returned.
@@ -22,6 +90,29 @@ func HelloWorld(name string) string {
22
90
// more clear, but not necessary. Make sure to read the comment to understand
23
91
// what the function *should* do.
24
92
func TestHelloWorld (t * testing.T ) {
25
- // Technically, if you delete this line, the test passes. That won't fly.
26
- t .Fatal ("not implemented" )
93
+ type test struct {
94
+ in string
95
+ want string
96
+ }
97
+ var tests = []test {
98
+ test {in : "Kyle" , want : "Hello, Kyle!" },
99
+ test {in : "" , want : "" },
100
+ test {in : " " , want : "" },
101
+ }
102
+ var got string
103
+ for i , helloTest := range tests {
104
+ got = HelloWorld (helloTest .in )
105
+ if got != helloTest .want {
106
+ t .Errorf ("at %d, got: %s, want: %s" , i , got , helloTest .want )
107
+ }
108
+ }
109
+ }
110
+
111
+ func TestHelloKyle (t * testing.T ) {
112
+ var got , want string
113
+ want = "Hello, Kyle!"
114
+ got = HelloKyle ()
115
+ if got != want {
116
+ t .Errorf ("got: %s, want: %s" , got , want )
117
+ }
27
118
}
0 commit comments