Skip to content

Commit 91341c6

Browse files
Updated examples for the chapter.
1 parent 765cce7 commit 91341c6

8 files changed

Lines changed: 66 additions & 79 deletions

File tree

‎chapter9/listing01/listing01_test.go‎

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

9-
const succeed = "\u2713"
10-
const failed = "\u2717"
9+
const checkMark = "\u2713"
10+
const ballotX = "\u2717"
1111

1212
// TestDownload validates the http Get function can download content.
1313
func TestDownload(t *testing.T) {
@@ -22,19 +22,19 @@ func TestDownload(t *testing.T) {
2222
resp, err := http.Get(url)
2323
if err != nil {
2424
t.Fatal("\t\tShould be able to make the Get call.",
25-
failed, err)
25+
ballotX, err)
2626
}
2727
t.Log("\t\tShould be able to make the Get call.",
28-
succeed)
28+
checkMark)
2929

3030
defer resp.Body.Close()
3131

3232
if resp.StatusCode == statusCode {
3333
t.Logf("\t\tShould receive a \"%d\" status code. %v",
34-
statusCode, succeed)
34+
statusCode, checkMark)
3535
} else {
3636
t.Errorf("\t\tShould receive a \"%d\" status code. %v %v",
37-
statusCode, failed, resp.StatusCode)
37+
statusCode, ballotX, resp.StatusCode)
3838
}
3939
}
4040
}

‎chapter9/listing02/listing02_test.go‎

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

9-
const succeed = "\u2713"
10-
const failed = "\u2717"
9+
const checkMark = "\u2713"
10+
const ballotX = "\u2717"
1111

1212
// TestDownload validates the http Get function can download content and
1313
// handles different status conditions properly.
@@ -29,19 +29,19 @@ func TestDownload(t *testing.T) {
2929
resp, err := http.Get(u.url)
3030
if err != nil {
3131
t.Fatal("\t\tShould be able to make the Get call.",
32-
failed, err)
32+
ballotX, err)
3333
}
3434
t.Log("\t\tShould be able to make the Get call.",
35-
succeed)
35+
checkMark)
3636

3737
defer resp.Body.Close()
3838

3939
if resp.StatusCode == u.statusCode {
4040
t.Logf("\t\tShould receive a \"%d\" status code. %v",
41-
u.statusCode, succeed)
41+
u.statusCode, checkMark)
4242
} else {
4343
t.Errorf("\t\tShould receive a \"%d\" status code. %v %v",
44-
u.statusCode, failed, resp.StatusCode)
44+
u.statusCode, ballotX, resp.StatusCode)
4545
}
4646
}
4747
}

‎chapter9/listing03/listing03_test.go‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import (
77
"net/http"
88
"net/http/httptest"
99
"testing"
10-
11-
ex2 "github.com/ArdanStudios/gotraining/09-testing/01-testing/example2"
1210
)
1311

14-
const succeed = "\u2713"
15-
const failed = "\u2717"
12+
const checkMark = "\u2713"
13+
const ballotX = "\u2717"
1614

1715
// feed is mocking the XML document we except to receive.
1816
var feed = `<?xml version="1.0" encoding="UTF-8"?>
@@ -84,36 +82,36 @@ func TestDownload(t *testing.T) {
8482
resp, err := http.Get(server.URL)
8583
if err != nil {
8684
t.Fatal("\t\tShould be able to make the Get call.",
87-
failed, err)
85+
ballotX, err)
8886
}
8987
t.Log("\t\tShould be able to make the Get call.",
90-
succeed)
88+
checkMark)
9189

9290
defer resp.Body.Close()
9391

9492
if resp.StatusCode == statusCode {
9593
t.Logf("\t\tShould receive a \"%d\" status code. %v",
96-
statusCode, succeed)
94+
statusCode, checkMark)
9795
} else {
9896
t.Errorf("\t\tShould receive a \"%d\" status code. %v %v",
99-
statusCode, failed, resp.StatusCode)
97+
statusCode, ballotX, resp.StatusCode)
10098
}
10199

102-
var d ex2.Document
100+
var d Document
103101
if err := xml.NewDecoder(resp.Body).Decode(&d); err == nil {
104102
t.Log("\t\tShould be able to unmarshal the response.",
105-
succeed)
103+
checkMark)
106104
} else {
107105
t.Fatal("\t\tShould be able to unmarshal the response.",
108-
failed, err)
106+
ballotX, err)
109107
}
110108

111109
if len(d.Channel.Items) == 1 {
112110
t.Log("\t\tShould have \"1\" item in the feed.",
113-
succeed)
111+
checkMark)
114112
} else {
115113
t.Fatal("\t\tShould have \"1\" item in the feed.",
116-
failed, len(d.Channel.Items))
114+
ballotX, len(d.Channel.Items))
117115
}
118116
}
119117
}

‎chapter9/listing04/example04.go‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,4 @@ func SendJSON(rw http.ResponseWriter, r *http.Request) {
4646
h.Set("Content-Length", strconv.Itoa(datalen))
4747
rw.WriteHeader(200)
4848
fmt.Fprintf(rw, "%s\n", data)
49-
50-
LogResponse(&u)
51-
}
52-
53-
// LogResponse is used to write the response to the log.
54-
func LogResponse(v interface{}) {
55-
d, err := json.MarshalIndent(v, "", " ")
56-
if err != nil {
57-
log.Println("Unable to marshal response", err)
58-
return
59-
}
60-
61-
log.Println(string(d))
6249
}

‎chapter9/listing04/example04_example_test.go‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22
package main_test
33

44
import (
5+
"encoding/json"
6+
"fmt"
57
"log"
8+
"net/http"
9+
"net/http/httptest"
610
"os"
7-
8-
ex "github.com/goinaction/code/chapter9/listing04"
911
)
1012

11-
// ExampleLogResponse provides a basic example test example.
12-
func ExampleLogResponse() {
13+
// ExampleSendJSON provides a basic example test example.
14+
func ExampleSendJSON() {
1315
log.SetFlags(0)
1416
log.SetOutput(os.Stdout)
1517

16-
u := struct {
18+
r, _ := http.NewRequest("GET", "/sendjson", nil)
19+
w := httptest.NewRecorder()
20+
http.DefaultServeMux.ServeHTTP(w, r)
21+
22+
var u struct {
1723
Name string
1824
Email string
19-
}{
20-
Name: "Bill",
21-
Email: "bill@ardanstudios.com",
2225
}
2326

24-
ex.LogResponse(&u)
27+
if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
28+
log.Println("ERROR:", err)
29+
}
30+
31+
fmt.Println(u)
2532
// Output:
26-
// {
27-
// "Name": "Bill",
28-
// "Email": "bill@ardanstudios.com"
29-
// }
33+
// {Bill bill@ardanstudios.com}
3034
}

‎chapter9/listing04/example04_test.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
ex "github.com/goinaction/code/chapter9/listing04"
1111
)
1212

13-
const succeed = "\u2713"
14-
const failed = "\u2717"
13+
const checkMark = "\u2713"
14+
const ballotX = "\u2717"
1515

1616
func init() {
1717
ex.Routes()
@@ -26,29 +26,29 @@ func TestSendJSON(t *testing.T) {
2626
http.DefaultServeMux.ServeHTTP(w, r)
2727

2828
if w.Code != 200 {
29-
t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, failed)
29+
t.Fatalf("\tShould received a status code of 200 for the response. Received[%d] %s", w.Code, ballotX)
3030
}
31-
t.Log("\tShould received a status code of 200 for the response.", succeed)
31+
t.Log("\tShould received a status code of 200 for the response.", checkMark)
3232

3333
u := struct {
3434
Name string
3535
Email string
3636
}{}
3737
if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
38-
t.Fatal("\tShould be able to unmarshal the response.", failed)
38+
t.Fatal("\tShould be able to unmarshal the response.", ballotX)
3939
}
40-
t.Log("\tShould be able to unmarshal the response.", succeed)
40+
t.Log("\tShould be able to unmarshal the response.", checkMark)
4141

4242
if u.Name == "Bill" {
43-
t.Log("\tShould have \"Bill\" for Name in the response.", succeed)
43+
t.Log("\tShould have \"Bill\" for Name in the response.", checkMark)
4444
} else {
45-
t.Error("\tShould have \"Bill\" for Name in the response.", failed, u.Name)
45+
t.Error("\tShould have \"Bill\" for Name in the response.", ballotX, u.Name)
4646
}
4747

4848
if u.Email == "bill@ardanstudios.com" {
49-
t.Log("\tShould have \"bill@ardanstudios.com\" for Email in the response.", succeed)
49+
t.Log("\tShould have \"bill@ardanstudios.com\" for Email in the response.", checkMark)
5050
} else {
51-
t.Error("\tShould have \"bill@ardanstudios.com\" for Email in the response.", failed, u.Email)
51+
t.Error("\tShould have \"bill@ardanstudios.com\" for Email in the response.", ballotX, u.Email)
5252
}
5353
}
5454
}

‎chapter9/listing05/listing05.go‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,36 @@
22
// matrix in different ways.
33
package listing05
44

5-
import "fmt"
5+
const cols = 64
66

7-
const rows = 64
8-
9-
// Set the size of each row to 64k
10-
const cols = 64 * 1024
7+
// Set the size of each row to be 64k.
8+
const rows = 64 * 1024
119

1210
// matrix represents a set of columns that each exist on
1311
// their own cache line.
14-
var matrix [rows][cols]byte
12+
var matrix [cols][rows]byte
1513

1614
// init sets ~13% of the matrix to 0XFF.
1715
func init() {
1816
var ctr int
19-
for row := 0; row < rows; row++ {
20-
for col := 0; col < cols; col++ {
21-
if col%8 == 0 {
22-
matrix[row][col] = 0xFF
17+
for col := 0; col < cols; col++ {
18+
for row := 0; row < rows; row++ {
19+
if row%8 == 0 {
20+
matrix[col][row] = 0xFF
2321
ctr++
2422
}
2523
}
2624
}
27-
fmt.Println(ctr, "Elements set out of", cols*rows)
25+
println(ctr, "Elements set out of", cols*rows)
2826
}
2927

3028
// rowTraverse traverses the matrix linearly by each column for each row.
3129
func rowTraverse() int {
3230
var ctr int
3331

34-
for row := 0; row < rows; row++ {
35-
for col := 0; col < cols; col++ {
36-
if matrix[row][col] == 0xFF {
32+
for col := 0; col < cols; col++ {
33+
for row := 0; row < rows; row++ {
34+
if matrix[col][row] == 0xFF {
3735
ctr++
3836
}
3937
}
@@ -46,9 +44,9 @@ func rowTraverse() int {
4644
func colTraverse() int {
4745
var ctr int
4846

49-
for col := 0; col < cols; col++ {
50-
for row := 0; row < rows; row++ {
51-
if matrix[row][col] == 0xFF {
47+
for row := 0; row < rows; row++ {
48+
for col := 0; col < cols; col++ {
49+
if matrix[col][row] == 0xFF {
5250
ctr++
5351
}
5452
}

‎chapter9/listing05/listing05_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// go test -run=XXX -bench=.
1+
// go test -run=none -bench=.
22

33
// Sample test to show how to write benchmark tests.
44
package listing05

0 commit comments

Comments
 (0)