forked from ray-g/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexpr_test.go
217 lines (204 loc) · 4.95 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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)
// 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")
}
// Pretty-print it:
data, err = MarshalIndent(strangelove)
if err != nil {
t.Fatal(err)
}
t.Logf("MarshalIdent() = %s\n", data)
}
func TestFloat(t *testing.T) {
var tests = []struct {
num32 float32
num64 float64
}{
{1.0, 3.21},
{0.0, 10000},
}
for _, test := range tests {
actual32, err := Marshal(test.num32)
if err != nil {
t.Fatalf("return err %v", err.Error())
}
actual64, err := Marshal(test.num64)
if err != nil {
t.Fatalf("return err %v", err.Error())
}
if string(actual32) != fmt.Sprintf("%g", test.num32) {
t.Errorf("Result = %v, Expected %v", actual32, test.num32)
}
if string(actual64) != fmt.Sprintf("%g", test.num64) {
t.Errorf("Result = %v, Expected %v", actual64, test.num64)
}
}
}
func TestComplex(t *testing.T) {
var tests = []struct {
num64 complex64
num128 complex128
}{
{12.0 - 3i, 3.2 + 1i},
{0.0 - 0i, 10000 + 0i},
}
for _, test := range tests {
actual64, err := Marshal(test.num64)
if err != nil {
t.Fatalf("return err %v", err.Error())
}
actual128, err := Marshal(test.num128)
if err != nil {
t.Fatalf("return err %v", err.Error())
}
if string(actual64) != fmt.Sprintf("#C(%g %g)", real(test.num64), imag(test.num64)) {
t.Errorf("Result = %s, Expected %v", actual64, test.num64)
}
if string(actual128) != fmt.Sprintf("#C(%g %g)", real(test.num128),
imag(test.num128)) {
t.Errorf("Result = %s, Expected %v", actual128, test.num128)
}
}
}
func TestBool(t *testing.T) {
var tests = []struct {
b bool
want string
}{
{true, "t"},
{false, "nil"},
}
for _, test := range tests {
actual, err := Marshal(test.b)
if err != nil {
t.Fatalf("return err %v", err.Error())
}
if string(actual) != test.want {
t.Errorf("Result = %s, Expected %v", actual, test.want)
}
}
}
func TestMarshal(t *testing.T) {
type Interface interface{}
type Record struct {
B bool `sexpr:"bee"`
F32 float32
F64 float64
C64 complex64
C128 complex128
I Interface
}
tests := []struct {
r Record
want string
}{
{
Record{true, 2.5, 0, 1 + 2i, 2 + 3i, Interface(5)},
`((bee t) (F32 2.5) (F64 0) (C64 #C(1 2)) (C128 #C(2 3)) (I ("sexpr.Interface" 5)))`,
},
{
Record{false, 0, 1.5, 0, 1i, Interface(0)},
`((bee nil) (F32 0) (F64 1.5) (C64 #C(0 0)) (C128 #C(0 1)) (I ("sexpr.Interface" 0)))`,
},
}
for _, test := range tests {
data, err := Marshal(test.r)
s := string(data)
if err != nil {
t.Errorf("Marshal(%s): %s", s, err)
}
if s != test.want {
t.Errorf("Marshal(%#v) got %s, wanted %s", test.r, s, test.want)
}
}
}
func TestUnmarshal(t *testing.T) {
type Interface interface{}
type Record struct {
B bool
F32 float32
F64 float64
C64 complex64
C128 complex128
I Interface `sexpr:"face"`
}
Interfaces["sexpr.Interface"] = reflect.TypeOf(int(0))
tests := []struct {
s string
want Record
}{
{
`((B t) (F32 2.5) (F64 0) (I ("sexpr.Interface" 5)))`,
Record{true, 2.5, 0, 0, 0, Interface(5)},
},
{
`((B nil) (F32 0) (F64 1.5) (face ("sexpr.Interface" 0)))`,
Record{false, 0, 1.5, 0, 0, Interface(0)},
},
}
for _, test := range tests {
var r Record
err := Unmarshal([]byte(test.s), &r)
if err != nil {
t.Errorf("Unmarshal(%q): %s", test.s, err)
}
if !reflect.DeepEqual(r, test.want) {
t.Errorf("Unmarshal(%q) got %#v, wanted %#v", test.s, r, test.want)
}
}
}