forked from mkrautz/variadic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variadic_386_test.go
111 lines (103 loc) · 2.51 KB
/
variadic_386_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
// Copyright 2013 Mikkel Krautz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package variadic
import (
"testing"
"unsafe"
"reflect"
"math"
)
func TestIntArg0(t *testing.T) {
fc := new(FunctionCall)
fc.addr = arg0fn()
fc.Words[0] = 0xcafebabe
fc.NumArgs = 1
ret := fc.Call()
if ret != fc.Words[0] {
t.Fatalf("got 0x%x; expected 0x%x", ret, fc.Words[0])
}
}
func TestArgN(t *testing.T) {
for n := 1; n < 10; n++ {
fc := new(FunctionCall)
fc.addr = argNfn()
for i := 0; i < len(fc.Words); i++ {
fc.Words[i] = 0
}
fc.Words[0] = uintptr(n)
fc.Words[n] = uintptr(0xdead + n)
fc.NumArgs = n+1
ret := fc.Call()
if ret != fc.Words[n] {
t.Fatalf("got 0x%x; expected 0x%x", ret, fc.Words[n])
}
}
}
func TestFloat32Ret(t *testing.T) {
fc := new(FunctionCall)
fc.addr = floatret()
fc.Words[0] = 0xf00
fc.NumArgs = 1
ret := fc.CallFloat32()
expected := float32(3.141592)
if ret != expected {
t.Fatalf("got %v; expected %v", ret, expected)
}
}
func TestFloat64Ret(t *testing.T) {
fc := new(FunctionCall)
fc.addr = doubleret()
fc.Words[0] = 0xf00
fc.NumArgs = 1
ret := fc.CallFloat64()
expected := float64(3.141592)
if ret != expected {
t.Fatalf("got %v; expected %v", ret, expected)
}
}
func TestPrintf(t *testing.T) {
buf := make([]byte, 256)
fmt := "%i 0x%x %u\n\x00"
fc := NewFunctionCall("sprintf")
slicehdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
strhdr := (*reflect.StringHeader)(unsafe.Pointer(&fmt))
fc.Words[0] = slicehdr.Data
fc.Words[1] = strhdr.Data
fc.Words[2] = 1
fc.Words[3] = 0xcafebabe
fc.Words[4] = 0xff
fc.NumArgs = 5
n := fc.Call()
if n > 256 {
t.Fatal("bad return value from sprintf")
}
val := string(buf[0:int(n)])
expected := "1 0xcafebabe 255\n"
if val != expected {
t.Fatalf("got %v; expected %v", val, expected)
}
}
func TestFloatPrintf(t *testing.T) {
buf := make([]byte, 256)
fmt := "%.6f %i\n\x00"
fc := NewFunctionCall("sprintf")
slicehdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf))
strhdr := (*reflect.StringHeader)(unsafe.Pointer(&fmt))
fc.Words[0] = slicehdr.Data
fc.Words[1] = strhdr.Data
u64 := math.Float64bits(3.141592)
fc.Words[2] = uintptr(u64 & 0xffffffff)
fc.Words[3] = uintptr((u64 >> 32) & 0xffffffff)
fc.Words[4] = 2
fc.NumArgs = 5
n := fc.Call()
if n > 256 {
t.Fatal("bad return value from sprintf")
}
val := string(buf[0:int(n)])
expected := "3.141592 2\n"
if val != expected {
t.Fatalf("got %v; expected %v", val, expected)
}
}