annotate go/godc/godc_test.go @ 35:75fe1dec0eb4

- added support for signature-based calling convention switch
author Tassilo Philipp
date Mon, 13 Apr 2020 16:07:56 +0200
parents 0cfcc391201f
children 9943c30ee2aa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 /*
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 godc_test.go
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 Copyright (c) 2014 Tassilo Philipp <tphilipp@potion-studios.com>
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 Permission to use, copy, modify, and distribute this software for any
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 purpose with or without fee is hereby granted, provided that the above
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 copyright notice and this permission notice appear in all copies.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 */
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 package godc
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 import (
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 "testing"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 "fmt"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 "unsafe"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 "math"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 func TestGoDC(t *testing.T) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 lm := new(ExtLib)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 if err := lm.Load("/usr/lib/libm.so"); err != nil {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 t.FailNow()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 defer lm.Free()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 if err := lm.SymsInit("/usr/lib/libm.so"); err != nil {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 t.FailNow()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 defer lm.SymsCleanup()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 fmt.Printf("Testing dl:\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 fmt.Printf("-----------\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 fmt.Printf("Loaded library at address: %p\n", lm.lib)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 fmt.Printf("C sqrt function at address: %p\n", lm.FindSymbol("sqrt"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 fmt.Printf("C pow function at address: %p\n\n", lm.FindSymbol("pow"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 fmt.Printf("Testing dlSyms:\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 fmt.Printf("---------------\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 fmt.Printf("Symbols in libm: %d\n", lm.SymsCount())
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 fmt.Printf("Symbol name for address %p: %s\n", lm.FindSymbol("pow"), lm.SymsNameFromValue(lm.FindSymbol("pow")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 fmt.Printf("All symbol names in libm:\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 for i, n := 0, lm.SymsCount(); i<n; i++ {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 fmt.Printf(" %s\n", lm.SymsName(i))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 fmt.Printf("\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 // Another lib
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 lc := new(ExtLib)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 if err := lc.Load("/usr/lib/libc.so"); err != nil {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 t.FailNow()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 defer lc.Free()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 if err := lc.SymsInit("/usr/lib/libc.so"); err != nil {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 t.FailNow()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 defer lc.SymsCleanup()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 fmt.Printf("Symbols in libc: %d (not listing them here, too many)\n\n", lc.SymsCount())
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 // Call some functions
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 fmt.Printf("Testing dc:\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 fmt.Printf("-----------\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 vm := new(CallVM)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 if err := vm.InitCallVM(); err != nil {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 t.FailNow()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 defer vm.Free()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 vm.Mode(DC_CALL_C_DEFAULT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88 // Float
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 vm.ArgFloat(36)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 rf := vm.CallFloat(lm.FindSymbol("sqrtf"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 fmt.Printf("sqrtf(36) = %f\n", rf)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 if(rf != 6.0) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 vm.Reset() // Test reset, reusing VM
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 vm.ArgDouble(3.6)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 rd := vm.CallDouble(lm.FindSymbol("floor"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 fmt.Printf("floor(3.6) = %f\n", rd)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 if(rd != 3.0) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102 // Double
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 vm.ArgDouble(4.2373)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 rd = vm.CallDouble(lm.FindSymbol("sqrt"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106 fmt.Printf("sqrt(4.2373) = %f\n", rd)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 if(math.Abs(rd - 2.05847) > 0.00001) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 vm.ArgDouble(2.373)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 vm.ArgDouble(-1000) // 2 args
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 rd = vm.CallDouble(lm.FindSymbol("copysign"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 fmt.Printf("copysign(2.373, -1000) = %f\n", rd)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 if(rd != -2.373) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 // Strings
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 cs1 := vm.AllocCString("/return/only/this_here")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 defer vm.FreeCString(cs1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 vm.ArgPointer(cs1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 rs := vm.CallPointerToStr(lc.FindSymbol("basename"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 fmt.Printf("basename(\"/return/only/this_here\") = %s\n", rs)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 if(rs != "this_here") { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 // Reuse path
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 rs = vm.CallPointerToStr(lc.FindSymbol("dirname"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 fmt.Printf("dirname(\"/return/only/this_here\") = %s\n", rs)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 if(rs != "/return/only") { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 // Integer
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 vm.ArgInt('a')
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 ri := vm.CallInt(lc.FindSymbol("toupper"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136 fmt.Printf("toupper('a') = %c\n", ri)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 if(ri != 'A') { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 vm.ArgInt('a')
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 ri = vm.CallInt(lc.FindSymbol("tolower"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 fmt.Printf("tolower('a') = %c\n", ri)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 if(ri != 'a') { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146 vm.ArgInt('R')
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 ri = vm.CallInt(lc.FindSymbol("toupper"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 fmt.Printf("toupper('R') = %c\n", vm.CallInt(lc.FindSymbol("toupper")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 if(ri != 'R') { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 vm.ArgInt('R')
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153 ri = vm.CallInt(lc.FindSymbol("tolower"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154 fmt.Printf("tolower('R') = %c\n", vm.CallInt(lc.FindSymbol("tolower")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 if(ri != 'r') { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
158 // Integer return
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160 cs2 := vm.AllocCString("Tassilo")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 defer vm.FreeCString(cs2)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 fmt.Printf("rand() = %d\n", vm.CallInt(lc.FindSymbol("rand")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164 fmt.Printf("rand() = %d\n", vm.CallInt(lc.FindSymbol("rand")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
165 fmt.Printf("rand() = %d\n", vm.CallInt(lc.FindSymbol("rand")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 fmt.Printf("rand() = %d\n", vm.CallInt(lc.FindSymbol("rand")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 fmt.Printf("rand() = %d\n", vm.CallInt(lc.FindSymbol("rand")))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 vm.ArgPointer(cs2)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169 ri = vm.CallInt(lc.FindSymbol("strlen"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 fmt.Printf("strlen(\"Tassilo\") = %d\n", ri)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 if(ri != 7) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 // Formatted - with signature/conversion
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
175 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 vm.ArgF("dd)d", 3.14, -2000) // 2 args, second passed as int, but ArgF will convert
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177 rd = vm.CallDouble(lm.FindSymbol("copysign"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 fmt.Printf("dd)d: copysign(3.14, -2000) = %f\n", rd)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 if(rd != -3.14) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
181 // Formatted - without signature/conversion
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 vm.ArgF("dd)d", -31.4, 42.4) // 2 args, second passed as int, but ArgF will convert
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184 rd = vm.CallDouble(lm.FindSymbol("copysign"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185 fmt.Printf("dd)d: copysign(-31.4, 42.4) = %f\n", rd)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 if(rd != 31.4) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 // Formatted - use Go's types, pass unsupported type, should return an error
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190 err := vm.ArgF_Go(6.14, vm)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191 fmt.Printf("ArgF_Go: copysign(6.14, <unsupported type>) should return error: %t\n", err != nil)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 if(err == nil) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194 // Formatted - use Go's types
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
196 vm.ArgF_Go(-61.4, 42.4) // 2 args, both need to be correct or undefined behaviour
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197 rd = vm.CallDouble(lm.FindSymbol("copysign"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
198 fmt.Printf("copysign(-61.4, 42.4) = %f\n", rd)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
199 if(rd != 61.4) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
200
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
202 // Ellipse
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
203 vm.Mode(DC_CALL_C_ELLIPSIS)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
204 vm.Reset()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
205 buf := make([]byte, 1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
206 bufPtr := unsafe.Pointer(&buf[0])
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
207 cs3 := vm.AllocCString("Four:%d | \"Hello\":%s | Pi:%f")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
208 cs4 := vm.AllocCString("Hello")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
209 defer vm.FreeCString(cs4)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
210 defer vm.FreeCString(cs3)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
211
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
212 vm.ArgPointer(bufPtr)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
213 vm.ArgPointer(cs3)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
214 vm.Mode(DC_CALL_C_ELLIPSIS_VARARGS)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
215 vm.ArgInt(4)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
216 vm.ArgPointer(cs4)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217 vm.ArgDouble(3.14) // Double, b/c of ... promotion rules
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
218 ri = vm.CallInt(lc.FindSymbol("sprintf"))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219 fmt.Printf("sprintf(bufPtr, \"Four:%%d | \\\"Hello\\\":%%s | Pi:%%f\", 4, \"Hello\", 3.14) = %d:\n", ri)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
220 fmt.Printf(" bufPtr: %s\n", string(buf[/*slice printed bytes*/:ri]))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221 if(ri != 36) { t.FailNow() }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
222
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
223
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
224 //@@@ untested:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
225 // - ArgF and ArgF_Go with strings
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
226 // - ArgF and ArgF_Go with bools
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
227 // - ...
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
228 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
229