Skip to content

Commit 2d77ae2

Browse files
committed
tests/testdata/linkname/method: TestLinkName using testing.T
1 parent f9cfa3a commit 2d77ae2

File tree

2 files changed

+65
-57
lines changed

2 files changed

+65
-57
lines changed

tests/linkname_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ func TestLinknameMethods(t *testing.T) {
3232
t.Fatalf("method.TestLinkname() paniced: %s", err)
3333
}
3434
}()
35-
method.TestLinkname()
35+
method.TestLinkname(t)
3636
}

tests/testdata/linkname/method/method.go

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package method
22

33
import (
4+
"sort"
45
"strings"
6+
"testing"
57
_ "unsafe"
68
)
79

@@ -29,12 +31,12 @@ type point struct {
2931
Y int
3032
}
3133

32-
func testStruct() {
34+
func testStruct(t *testing.T) {
3335
var pt point
3436
struct_Set(&pt, 1, 2)
3537
x, y := struct_Get(pt)
3638
if x != 1 || y != 2 {
37-
panic(pt)
39+
t.Fatalf("Got: struct_Get(pt) = (%v,%v). Want: (1,2).", x, y)
3840
}
3941
}
4042

@@ -56,13 +58,14 @@ func slice_Append(*list, ...string)
5658
//go:linkname slice_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.List.Get
5759
func slice_Get(list) string
5860

59-
func testSlice() {
61+
func testSlice(t *testing.T) {
6062
var v list
6163
v = append(v, "one")
6264
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)
6669
}
6770
}
6871

@@ -84,14 +87,15 @@ func array_Set(*array, int, string)
8487
//go:linkname array_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Array.Get
8588
func array_Get(array) string
8689

87-
func testArray() {
90+
func testArray(t *testing.T) {
8891
var a array
8992
a[0] = "one"
9093
array_Set(&a, 1, "two")
9194
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)
9599
}
96100
}
97101

@@ -110,6 +114,7 @@ func (m Map) Get() string {
110114
for _, v := range m {
111115
list = append(list, v)
112116
}
117+
sort.Strings(list)
113118
return strings.Join(list, ",")
114119
}
115120

@@ -124,13 +129,14 @@ func map_SetPtr(*_map, int, string)
124129
//go:linkname map_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Map.Get
125130
func map_Get(_map) string
126131

127-
func testMap() {
132+
func testMap(t *testing.T) {
128133
m := make(_map)
129134
map_Set(m, 1, "one")
130135
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)
134140
}
135141
}
136142

@@ -152,17 +158,17 @@ func func_Call(_func, int, int) int
152158
//go:linkname func_CallPtr github.com/gopherjs/gopherjs/tests/testdata/linkname/method.(*Func).CallPtr
153159
func func_CallPtr(*_func, int, int) int
154160

155-
func testFunc() {
161+
func testFunc(t *testing.T) {
156162
var fn _func = func(a, b int) int {
157163
return a + b
158164
}
159165
r := func_Call(fn, 100, 200)
160166
if r != 300 {
161-
panic(r)
167+
t.Fatalf("Got: func_Call(fn,100,200) = %v. Want: 300.", r)
162168
}
163169
r2 := func_CallPtr(&fn, 100, 200)
164170
if r2 != 300 {
165-
panic(r2)
171+
t.Fatalf("Got: func_CallPtr(fn,100,200) = %v. Want: 300.", r2)
166172
}
167173
}
168174

@@ -191,21 +197,21 @@ func chan_SendPtr(*_chan, int)
191197
//go:linkname chan_Recv github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Chan.Recv
192198
func chan_Recv(_chan) int
193199

194-
func testChan() {
200+
func testChan(t *testing.T) {
195201
c := make(_chan)
196202
go func() {
197203
chan_Send(c, 100)
198204
}()
199205
r := chan_Recv(c)
200206
if r != 100 {
201-
panic(r)
207+
t.Fatalf("Got: chan_Recv(c) = %v. Want: 100.", r)
202208
}
203209
go func() {
204210
chan_SendPtr(&c, 200)
205211
}()
206212
r = chan_Recv(c)
207213
if r != 200 {
208-
panic(r)
214+
t.Fatalf("Got: chan_Recv(c) = %v. Want: 200.", r)
209215
}
210216
}
211217

@@ -247,12 +253,12 @@ func int_Set(*_int, int) int
247253
//go:linkname int_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Int.Get
248254
func int_Get(_int) int
249255

250-
func testInt() {
256+
func testInt(t *testing.T) {
251257
var i _int
252258
int_Set(&i, 100)
253259
r := int_Get(i)
254260
if r != 100 {
255-
panic(r)
261+
t.Fatalf("Got: int_Get(i) = %v. Want: 100.", r)
256262
}
257263
}
258264

@@ -274,12 +280,12 @@ func uint_Set(*_uint, uint) uint
274280
//go:linkname uint_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Uint.Get
275281
func uint_Get(_uint) uint
276282

277-
func testUint() {
283+
func testUint(t *testing.T) {
278284
var i _uint
279285
uint_Set(&i, 100)
280286
r := uint_Get(i)
281287
if r != 100 {
282-
panic(r)
288+
t.Fatalf("Got: uint_Get(i) = %v. Want: 100.", r)
283289
}
284290
}
285291

@@ -301,12 +307,12 @@ func float64_Set(*_float64, float64) float64
301307
//go:linkname float64_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Float64.Get
302308
func float64_Get(_float64) float64
303309

304-
func testFloat64() {
310+
func testFloat64(t *testing.T) {
305311
var i _float64
306312
float64_Set(&i, 3.14)
307313
r := float64_Get(i)
308314
if r != 3.14 {
309-
panic(r)
315+
t.Fatalf("Got: float64_Get(i) = %v. Want: 3.14.", r)
310316
}
311317
}
312318

@@ -328,12 +334,13 @@ func complex128_Set(*_complex128, complex128) complex128
328334
//go:linkname complex128_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Complex128.Get
329335
func complex128_Get(_complex128) complex128
330336

331-
func testComplex128() {
337+
func testComplex128(t *testing.T) {
332338
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)
337344
}
338345
}
339346

@@ -355,12 +362,12 @@ func uintptr_Set(*_uintptr, uintptr) uintptr
355362
//go:linkname uintptr_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Uintptr.Get
356363
func uintptr_Get(_uintptr) uintptr
357364

358-
func testUintptr() {
365+
func testUintptr(t *testing.T) {
359366
var i _uintptr
360367
uintptr_Set(&i, 0x1234)
361368
r := uintptr_Get(i)
362369
if r != 0x1234 {
363-
panic(r)
370+
t.Fatalf("Got: uintptr_Get(i) = %v. Want: 0x1234.", r)
364371
}
365372
}
366373

@@ -382,12 +389,12 @@ func bool_Set(*_bool, bool) bool
382389
//go:linkname bool_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Bool.Get
383390
func bool_Get(_bool) bool
384391

385-
func testBool() {
392+
func testBool(t *testing.T) {
386393
var i _bool
387394
bool_Set(&i, true)
388395
r := bool_Get(i)
389396
if r != true {
390-
panic(r)
397+
t.Fatalf("Got: bool_Get(i) = %v. Want: true.", r)
391398
}
392399
}
393400

@@ -409,12 +416,12 @@ func byte_Set(*_byte, byte) byte
409416
//go:linkname byte_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.Byte.Get
410417
func byte_Get(_byte) byte
411418

412-
func testByte() {
419+
func testByte(t *testing.T) {
413420
var i _byte
414421
byte_Set(&i, 0x7f)
415422
r := byte_Get(i)
416423
if r != 0x7f {
417-
panic(r)
424+
t.Fatalf("Got: byte_Get(i) = %v. Want: 0x7f.", r)
418425
}
419426
}
420427

@@ -436,27 +443,28 @@ func string_Set(*_string, string) string
436443
//go:linkname string_Get github.com/gopherjs/gopherjs/tests/testdata/linkname/method.String.Get
437444
func string_Get(_string) string
438445

439-
func testString() {
446+
func testString(t *testing.T) {
440447
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)
445453
}
446454
}
447455

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)
462470
}

0 commit comments

Comments
 (0)