Skip to content

Commit a921b3c

Browse files
committed
add samples
1 parent 67e3a4e commit a921b3c

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

hw/week3/.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch test package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "test",
12+
"program": "${workspaceFolder}"
13+
},
14+
]
15+
}

hw/week3/hello_test.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,74 @@ import (
66
"testing"
77
)
88

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+
977
// HelloWorld returns the famous 'Hello, World!' string, substituting 'World'
1078
// with the name provided by the caller. If name is an empty string, or a
1179
// string that contains only white space, an empty string is returned.
@@ -22,6 +90,29 @@ func HelloWorld(name string) string {
2290
// more clear, but not necessary. Make sure to read the comment to understand
2391
// what the function *should* do.
2492
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+
}
27118
}

0 commit comments

Comments
 (0)