-
Notifications
You must be signed in to change notification settings - Fork 176
/
sexpr_test.go
168 lines (157 loc) · 3.64 KB
/
sexpr_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package sexpr
import (
"fmt"
"reflect"
"testing"
)
// Test verifies that encoding and decoding a complex data value
// produces an equal result.
//
// The test does not make direct assertions about the encoded output
// because the output depends on map iteration order, which is
// nondeterministic. The output of the t.Log statements can be
// inspected by running the test with the -v flag:
//
// $ go test -v gopl.io/ch12/sexpr
//
func Test(t *testing.T) {
type Movie struct {
Title, Subtitle string
Year int
Actor map[string]string
Oscars []string
Sequel *string
}
strangelove := Movie{
Title: "Dr. Strangelove",
Subtitle: "How I Learned to Stop Worrying and Love the Bomb",
Year: 1964,
Actor: map[string]string{
"Dr. Strangelove": "Peter Sellers",
"Grp. Capt. Lionel Mandrake": "Peter Sellers",
"Pres. Merkin Muffley": "Peter Sellers",
"Gen. Buck Turgidson": "George C. Scott",
"Brig. Gen. Jack D. Ripper": "Sterling Hayden",
`Maj. T.J. "King" Kong`: "Slim Pickens",
},
Oscars: []string{
"Best Actor (Nomin.)",
"Best Adapted Screenplay (Nomin.)",
"Best Director (Nomin.)",
"Best Picture (Nomin.)",
},
}
// Encode it
data, err := Marshal(strangelove)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
t.Logf("Marshal() = %s\n", data)
fmt.Println(string(data))
// Decode it
var movie Movie
if err := Unmarshal(data, &movie); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshal() = %+v\n", movie)
// Check equality.
if !reflect.DeepEqual(movie, strangelove) {
t.Fatal("not equal")
}
}
func TestBool(t *testing.T) {
tests := []struct {
v bool
want string
}{
{true, "t"},
{false, "nil"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}
func TestFloat32(t *testing.T) {
tests := []struct {
v float32
want string
}{
{3.2e9, "3.2e+09"},
{1.0, "1"},
{0, "0"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}
func TestFloat64(t *testing.T) {
tests := []struct {
v float64
want string
}{
{3.2e9, "3.2e+09"},
{1.0, "1"},
{0, "0"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}
func TestComplex64(t *testing.T) {
tests := []struct {
v complex64
want string
}{
{0 + 0i, "#C(0 0)"},
{3 - 2i, "#C(3 -2)"},
{-1e9 + -2.2e9i, "#C(-1e+09 -2.2e+09)"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}
func TestComplex128(t *testing.T) {
tests := []struct {
v complex128
want string
}{
{0 + 0i, "#C(0 0)"},
{3 - 2i, "#C(3 -2)"},
{-1e9 + -2.2e9i, "#C(-1e+09 -2.2e+09)"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}