comparison erlang/erldc/test/call_SUITE.erl @ 0:0cfcc391201f

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:26:28 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0cfcc391201f
1 %% Copyright (c) 2014 Erik Mackdanz <erikmack@gmail.com>
2
3 %% Permission to use, copy, modify, and distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(call_SUITE).
16 -compile(export_all).
17
18 -define(VMSZ, 1024).
19
20 all() ->
21 [
22 ret_double_arg_double,
23 arg_double_bad_vm,
24 arg_double_bad_vm_2,
25 arg_double_bad_vm_3,
26 arg_double_bad_arg,
27 call_double_bad_vm,
28 call_double_bad_sym,
29
30 ret_float_arg_float,
31
32 ret_int,
33 arg_int,
34
35 ret_char_arg_char,
36 ret_bool_arg_bool,
37 ret_bool_arg_bool_2,
38 bad_bool_arg,
39 ret_short_arg_short,
40 ret_long_arg_long,
41 ret_longlong_arg_longlong,
42
43 ret_ptr_arg_int__ret_void_arg_ptr,
44 ret_string_arg_string
45 ].
46
47 %% Tests on sqrt
48
49 ret_double_arg_double(_) ->
50 {ok,Libm} = dyncall:load_library("libm"),
51 {ok,Sqrt} = dyncall:find_symbol(Libm, "sqrt"),
52 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
53 ok = dyncall:arg_double(CallVm,49.0),
54 {ok,7.0} = dyncall:call_double(CallVm,Sqrt).
55
56 arg_double_bad_vm(_) ->
57 {error,invalid_vm} = dyncall:arg_double(1000,49.0).
58
59 arg_double_bad_vm_2(_) ->
60 {error,invalid_vm} = dyncall:arg_double("Not an int",49.0).
61
62 arg_double_bad_vm_3(_) ->
63 {error,invalid_vm} = dyncall:arg_double(-4,49.0).
64
65 arg_double_bad_arg(_) ->
66 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
67 {error,invalid_arg} = dyncall:arg_double(CallVm,"Not a double").
68
69 call_double_bad_vm(_) ->
70 {ok,Libm} = dyncall:load_library("libm"),
71 {ok,Sqrt} = dyncall:find_symbol(Libm, "sqrt"),
72 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
73 ok = dyncall:arg_double(CallVm,49.0),
74 {error,invalid_vm} = dyncall:call_double(12,Sqrt). %% assuming 12 is bad after preceding tests
75
76 call_double_bad_sym(_) ->
77 {ok,Libm} = dyncall:load_library("libm"),
78 {ok,_Sqrt} = dyncall:find_symbol(Libm, "sqrt"),
79 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
80 ok = dyncall:arg_double(CallVm,49.0),
81 {error,invalid_arg} = dyncall:call_double(CallVm,12). %% assuming 12 is bad after preceding tests
82
83
84 %% Tests on sqrtf
85
86 ret_float_arg_float(_) ->
87 {ok,Libm} = dyncall:load_library("libm"),
88 {ok,Sqrt} = dyncall:find_symbol(Libm, "sqrtf"),
89 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
90 ok = dyncall:arg_float(CallVm,49.0),
91 {ok,7.0} = dyncall:call_float(CallVm,Sqrt).
92
93 %% Tests on ilogb
94 ret_int(_) ->
95 {ok,Libm} = dyncall:load_library("libm"),
96 {ok,Exp} = dyncall:find_symbol(Libm, "ilogb"),
97 {ok,CallVm} = dyncall:new_call_vm(4096),
98 ok = dyncall:arg_double(CallVm,1024.0),
99 {ok,10} = dyncall:call_int(CallVm,Exp).
100
101 %% Tests on ldexp
102 arg_int(_) ->
103 {ok,Libm} = dyncall:load_library("libm"),
104 {ok,Exp} = dyncall:find_symbol(Libm, "ldexp"),
105 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
106 ok = dyncall:arg_double(CallVm,3.0),
107 ok = dyncall:arg_int(CallVm,2),
108 {ok,12.0} = dyncall:call_double(CallVm,Exp).
109
110 %% Tests on get_next_char
111 ret_char_arg_char(_) ->
112 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
113 {ok,Sym} = dyncall:find_symbol(Libm, "get_next_char"),
114 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
115 ok = dyncall:arg_char(CallVm,$e),
116 {ok,$f} = dyncall:call_char(CallVm,Sym).
117
118 %% Tests on is_false
119 ret_bool_arg_bool(_) ->
120 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
121 {ok,Sym} = dyncall:find_symbol(Libm, "is_false"),
122 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
123 ok = dyncall:arg_bool(CallVm,true),
124 {ok,false} = dyncall:call_bool(CallVm,Sym).
125
126 ret_bool_arg_bool_2(_) ->
127 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
128 {ok,Sym} = dyncall:find_symbol(Libm, "is_false"),
129 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
130 ok = dyncall:arg_bool(CallVm,false),
131 {ok,true} = dyncall:call_bool(CallVm,Sym).
132
133 bad_bool_arg(_) ->
134 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
135 {ok,_Sym} = dyncall:find_symbol(Libm, "is_false"),
136 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
137 {error,invalid_arg} = dyncall:arg_bool(CallVm,foobar).
138
139 %% Tests on times_three
140 ret_short_arg_short(_) ->
141 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
142 {ok,Sym} = dyncall:find_symbol(Libm, "times_three"),
143 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
144 ok = dyncall:arg_short(CallVm,7),
145 {ok,21} = dyncall:call_short(CallVm,Sym).
146
147 %% Tests on add_nineteen
148 ret_long_arg_long(_) ->
149 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
150 {ok,Sym} = dyncall:find_symbol(Libm, "add_nineteen"),
151 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
152 ok = dyncall:arg_long(CallVm,12),
153 {ok,31} = dyncall:call_long(CallVm,Sym).
154
155 %% Tests on subtract_four
156 ret_longlong_arg_longlong(_) ->
157 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
158 {ok,Sym} = dyncall:find_symbol(Libm, "subtract_four"),
159 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
160 ok = dyncall:arg_longlong(CallVm,15),
161 {ok,11} = dyncall:call_longlong(CallVm,Sym).
162
163 %% Tests on coolmalloc/coolfree
164 ret_ptr_arg_int__ret_void_arg_ptr(_) ->
165 {ok,Lib} = dyncall:load_library("erldc_testtargets"),
166 {ok,Sym} = dyncall:find_symbol(Lib, "coolmalloc"),
167 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
168 ok = dyncall:arg_int(CallVm,100),
169 {ok,Ptr} = dyncall:call_ptr(CallVm,Sym),
170
171 %% Comes out as <<>> (opaque)
172 %% io:format("Pointer in erl is ~p~n",[Ptr]),
173
174 {ok,Sym2} = dyncall:find_symbol(Lib, "coolsetstr"),
175 {ok,CallVm2} = dyncall:new_call_vm(?VMSZ),
176 ok = dyncall:arg_ptr(CallVm2,Ptr),
177 ok = dyncall:arg_string(CallVm2,"Barbaz"),
178 ok = dyncall:call_void(CallVm2,Sym2),
179
180 {ok,Sym3} = dyncall:find_symbol(Lib, "coolfree"),
181 {ok,CallVm3} = dyncall:new_call_vm(?VMSZ),
182 ok = dyncall:arg_ptr(CallVm3,Ptr),
183 ok = dyncall:call_void(CallVm3,Sym3).
184
185 %% Tests on interested_reply
186 ret_string_arg_string(_) ->
187 {ok,Libm} = dyncall:load_library("erldc_testtargets"),
188 {ok,Sym} = dyncall:find_symbol(Libm, "interested_reply"),
189 {ok,CallVm} = dyncall:new_call_vm(?VMSZ),
190 ok = dyncall:arg_string(CallVm,"Georg"),
191 {ok,"Really, Georg? My name is Erik."} = dyncall:call_string(CallVm,Sym).
192
193