1
1
package method
2
2
3
3
import (
4
+ "sort"
4
5
"strings"
6
+ "testing"
5
7
_ "unsafe"
6
8
)
7
9
@@ -29,12 +31,12 @@ type point struct {
29
31
Y int
30
32
}
31
33
32
- func testStruct () {
34
+ func testStruct (t * testing. T ) {
33
35
var pt point
34
36
struct_Set (& pt , 1 , 2 )
35
37
x , y := struct_Get (pt )
36
38
if x != 1 || y != 2 {
37
- panic ( pt )
39
+ t . Fatalf ( "Got: struct_Get(pt) = (%v,%v). Want: (1,2)." , x , y )
38
40
}
39
41
}
40
42
@@ -56,13 +58,14 @@ func slice_Append(*list, ...string)
56
58
//go:linkname slice_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.List.Get
57
59
func slice_Get (list ) string
58
60
59
- func testSlice () {
61
+ func testSlice (t * testing. T ) {
60
62
var v list
61
63
v = append (v , "one" )
62
64
slice_Append (& v , "two" , "three" )
63
- s := slice_Get (v )
64
- if s != "one,two,three" {
65
- panic (s )
65
+ got := slice_Get (v )
66
+ want := "one,two,three"
67
+ if got != want {
68
+ t .Fatalf ("Got: slice_Get(v) = %q. Want: %q." , got , want )
66
69
}
67
70
}
68
71
@@ -84,14 +87,15 @@ func array_Set(*array, int, string)
84
87
//go:linkname array_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Array.Get
85
88
func array_Get (array ) string
86
89
87
- func testArray () {
90
+ func testArray (t * testing. T ) {
88
91
var a array
89
92
a [0 ] = "one"
90
93
array_Set (& a , 1 , "two" )
91
94
array_Set (& a , 4 , "five" )
92
- r := array_Get (a )
93
- if r != "one,two,,,five" {
94
- panic (r )
95
+ got := array_Get (a )
96
+ want := "one,two,,,five"
97
+ if got != want {
98
+ t .Fatalf ("Got: array_Get(a) = %q. Want: %q." , got , want )
95
99
}
96
100
}
97
101
@@ -110,6 +114,7 @@ func (m Map) Get() string {
110
114
for _ , v := range m {
111
115
list = append (list , v )
112
116
}
117
+ sort .Strings (list )
113
118
return strings .Join (list , "," )
114
119
}
115
120
@@ -124,13 +129,14 @@ func map_SetPtr(*_map, int, string)
124
129
//go:linkname map_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Map.Get
125
130
func map_Get (_map ) string
126
131
127
- func testMap () {
132
+ func testMap (t * testing. T ) {
128
133
m := make (_map )
129
134
map_Set (m , 1 , "one" )
130
135
map_SetPtr (& m , 2 , "two" )
131
- r := map_Get (m )
132
- if r != "one,two" {
133
- panic (r )
136
+ got := map_Get (m )
137
+ want := "one,two"
138
+ if got != want {
139
+ t .Fatalf ("Got: map_Get(m) = %q. Want: %q." , got , want )
134
140
}
135
141
}
136
142
@@ -152,17 +158,17 @@ func func_Call(_func, int, int) int
152
158
//go:linkname func_CallPtr github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Func).CallPtr
153
159
func func_CallPtr (* _func , int , int ) int
154
160
155
- func testFunc () {
161
+ func testFunc (t * testing. T ) {
156
162
var fn _func = func (a , b int ) int {
157
163
return a + b
158
164
}
159
165
r := func_Call (fn , 100 , 200 )
160
166
if r != 300 {
161
- panic ( r )
167
+ t . Fatalf ( "Got: func_Call(fn,100,200) = %v. Want: 300." , r )
162
168
}
163
169
r2 := func_CallPtr (& fn , 100 , 200 )
164
170
if r2 != 300 {
165
- panic ( r2 )
171
+ t . Fatalf ( "Got: func_CallPtr(fn,100,200) = %v. Want: 300." , r2 )
166
172
}
167
173
}
168
174
@@ -191,21 +197,21 @@ func chan_SendPtr(*_chan, int)
191
197
//go:linkname chan_Recv github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Chan.Recv
192
198
func chan_Recv (_chan ) int
193
199
194
- func testChan () {
200
+ func testChan (t * testing. T ) {
195
201
c := make (_chan )
196
202
go func () {
197
203
chan_Send (c , 100 )
198
204
}()
199
205
r := chan_Recv (c )
200
206
if r != 100 {
201
- panic ( r )
207
+ t . Fatalf ( "Got: chan_Recv(c) = %v. Want: 100." , r )
202
208
}
203
209
go func () {
204
210
chan_SendPtr (& c , 200 )
205
211
}()
206
212
r = chan_Recv (c )
207
213
if r != 200 {
208
- panic ( r )
214
+ t . Fatalf ( "Got: chan_Recv(c) = %v. Want: 200." , r )
209
215
}
210
216
}
211
217
@@ -247,12 +253,12 @@ func int_Set(*_int, int) int
247
253
//go:linkname int_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Int.Get
248
254
func int_Get (_int ) int
249
255
250
- func testInt () {
256
+ func testInt (t * testing. T ) {
251
257
var i _int
252
258
int_Set (& i , 100 )
253
259
r := int_Get (i )
254
260
if r != 100 {
255
- panic ( r )
261
+ t . Fatalf ( "Got: int_Get(i) = %v. Want: 100." , r )
256
262
}
257
263
}
258
264
@@ -274,12 +280,12 @@ func uint_Set(*_uint, uint) uint
274
280
//go:linkname uint_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Uint.Get
275
281
func uint_Get (_uint ) uint
276
282
277
- func testUint () {
283
+ func testUint (t * testing. T ) {
278
284
var i _uint
279
285
uint_Set (& i , 100 )
280
286
r := uint_Get (i )
281
287
if r != 100 {
282
- panic ( r )
288
+ t . Fatalf ( "Got: uint_Get(i) = %v. Want: 100." , r )
283
289
}
284
290
}
285
291
@@ -301,12 +307,12 @@ func float64_Set(*_float64, float64) float64
301
307
//go:linkname float64_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Float64.Get
302
308
func float64_Get (_float64 ) float64
303
309
304
- func testFloat64 () {
310
+ func testFloat64 (t * testing. T ) {
305
311
var i _float64
306
312
float64_Set (& i , 3.14 )
307
313
r := float64_Get (i )
308
314
if r != 3.14 {
309
- panic ( r )
315
+ t . Fatalf ( "Got: float64_Get(i) = %v. Want: 3.14." , r )
310
316
}
311
317
}
312
318
@@ -328,12 +334,13 @@ func complex128_Set(*_complex128, complex128) complex128
328
334
//go:linkname complex128_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Complex128.Get
329
335
func complex128_Get (_complex128 ) complex128
330
336
331
- func testComplex128 () {
337
+ func testComplex128 (t * testing. T ) {
332
338
var i _complex128
333
- complex128_Set (& i , 1 + 2i )
334
- r := complex128_Get (i )
335
- if r != 1 + 2i {
336
- panic (r )
339
+ want := 1 + 2i
340
+ complex128_Set (& i , want )
341
+ got := complex128_Get (i )
342
+ if got != want {
343
+ t .Fatalf ("Got: complex128_Get(i) = %v. Want: %v." , got , want )
337
344
}
338
345
}
339
346
@@ -355,12 +362,12 @@ func uintptr_Set(*_uintptr, uintptr) uintptr
355
362
//go:linkname uintptr_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Uintptr.Get
356
363
func uintptr_Get (_uintptr ) uintptr
357
364
358
- func testUintptr () {
365
+ func testUintptr (t * testing. T ) {
359
366
var i _uintptr
360
367
uintptr_Set (& i , 0x1234 )
361
368
r := uintptr_Get (i )
362
369
if r != 0x1234 {
363
- panic ( r )
370
+ t . Fatalf ( "Got: uintptr_Get(i) = %v. Want: 0x1234." , r )
364
371
}
365
372
}
366
373
@@ -382,12 +389,12 @@ func bool_Set(*_bool, bool) bool
382
389
//go:linkname bool_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Bool.Get
383
390
func bool_Get (_bool ) bool
384
391
385
- func testBool () {
392
+ func testBool (t * testing. T ) {
386
393
var i _bool
387
394
bool_Set (& i , true )
388
395
r := bool_Get (i )
389
396
if r != true {
390
- panic ( r )
397
+ t . Fatalf ( "Got: bool_Get(i) = %v. Want: true." , r )
391
398
}
392
399
}
393
400
@@ -409,12 +416,12 @@ func byte_Set(*_byte, byte) byte
409
416
//go:linkname byte_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Byte.Get
410
417
func byte_Get (_byte ) byte
411
418
412
- func testByte () {
419
+ func testByte (t * testing. T ) {
413
420
var i _byte
414
421
byte_Set (& i , 0x7f )
415
422
r := byte_Get (i )
416
423
if r != 0x7f {
417
- panic ( r )
424
+ t . Fatalf ( "Got: byte_Get(i) = %v. Want: 0x7f." , r )
418
425
}
419
426
}
420
427
@@ -436,27 +443,28 @@ func string_Set(*_string, string) string
436
443
//go:linkname string_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.String.Get
437
444
func string_Get (_string ) string
438
445
439
- func testString () {
446
+ func testString (t * testing. T ) {
440
447
var i _string
441
- string_Set (& i , "hello world" )
442
- r := string_Get (i )
443
- if r != "hello world" {
444
- panic (r )
448
+ want := "hello world"
449
+ string_Set (& i , want )
450
+ got := string_Get (i )
451
+ if got != want {
452
+ t .Fatalf ("Got: string_Get(i) = %q. Want: %q." , got , want )
445
453
}
446
454
}
447
455
448
- func TestLinkname () {
449
- testStruct ()
450
- testSlice ()
451
- testArray ()
452
- testMap ()
453
- testFunc ()
454
- testChan ()
455
- testBool ()
456
- testByte ()
457
- testInt ()
458
- testUint ()
459
- testFloat64 ()
460
- testComplex128 ()
461
- testString ()
456
+ func TestLinkname (t * testing. T ) {
457
+ testStruct (t )
458
+ testSlice (t )
459
+ testArray (t )
460
+ testMap (t )
461
+ testFunc (t )
462
+ testChan (t )
463
+ testBool (t )
464
+ testByte (t )
465
+ testInt (t )
466
+ testUint (t )
467
+ testFloat64 (t )
468
+ testComplex128 (t )
469
+ testString (t )
462
470
}
0 commit comments