9 def assert(label, ty, *conds)
11 cond = !conds.include?(false)
13 printf("succeed in `#{label}'\n")
18 printf("fail in `#{label}' ... expected\n")
20 printf("fail in `#{label}' ... unexpected\n")
22 raise(RuntimeError, "fail in `#{label}'")
35 print("DLSTACK = #{DL::DLSTACK}\n")
36 print("MAX_ARG = #{DL::MAX_ARG}\n")
38 print("DL::FREE = #{DL::FREE.inspect}\n")
42 if( !$LIB && File.exist?("libtest.so") )
45 if( !$LIB && File.exist?("test/libtest.so") )
46 $LIB = "./test/libtest.so"
53 extern "int test_c2i(char)"
54 extern "char test_i2c(int)"
55 extern "long test_lcc(char, char)"
56 extern "double test_f2d(float)"
57 extern "float test_d2f(double)"
58 extern "int test_strlen(char*)"
59 extern "int test_isucc(int)"
60 extern "long test_lsucc(long)"
61 extern "void test_succ(long *)"
62 extern "int test_arylen(int [])"
63 extern "void test_append(char*[], int, char *)"
67 c2i = h["test_c2i","IC"]
71 assert("c2i", :may, r == ?a)
72 assert("extern c2i", :must, r == LIBTest.test_c2i(?a))
74 i2c = h["test_i2c","CI"]
78 assert("i2c", :may, r == ?a)
79 assert("exern i2c", :must, r == LIBTest.test_i2c(?a))
81 lcc = h["test_lcc","LCC"]
84 assert("lcc", :may, r == 3)
85 assert("extern lcc", :must, r == LIBTest.test_lcc(1,2))
87 f2d = h["test_f2d","DF"]
91 assert("f2d", :may, r.to_i == 20)
92 assert("extern f2d", :must, r = LIBTest.test_f2d(20.001))
94 d2f = h["test_d2f","FD"]
98 assert("d2f", :may, r.to_i == 20)
99 assert("extern d2f", :must, r == LIBTest.test_d2f(20.001))
101 strlen = h["test_strlen","IS"]
103 r,rs = strlen["0123456789"]
105 assert("strlen", :must, r == 10)
106 assert("extern strlen", :must, r == LIBTest.test_strlen("0123456789"))
108 isucc = h["test_isucc","II"]
112 assert("isucc", :must, r == 3)
113 assert("extern isucc", :must, r == LIBTest.test_isucc(2))
115 lsucc = h["test_lsucc","LL"]
117 r,rs = lsucc[10000000]
119 assert("lsucc", :must, r == 10000001)
120 assert("extern lsucc", :must, r == LIBTest.test_lsucc(10000000))
122 succ = h["test_succ","0l"]
126 assert("succ", :must, rs[0] == 1)
127 l = DL.malloc(DL.sizeof("L"))
130 assert("extern succ", :must, rs[0] == l[:lval])
132 arylen = h["test_arylen","IA"]
134 r,rs = arylen[["a","b","c","d",nil]]
136 assert("arylen", :must, r == 4)
138 arylen = h["test_arylen","IP"]
140 r,rs = arylen[["a","b","c","d",nil]]
142 assert("arylen", :must, r == 4)
143 assert("extern arylen", :must, r == LIBTest.test_arylen(["a","b","c","d",nil]))
145 append = h["test_append","0aIS"]
147 r,rs = append[["a","b","c"],3,"x"]
149 assert("append", :must, rs[0].to_a('S',3) == ["ax","bx","cx"])
151 LIBTest.test_append(["a","b","c"],3,"x")
152 assert("extern append", :must, rs[0].to_a('S',3) == LIBTest._args_[0].to_a('S',3))
154 strcat = h["test_strcat","SsS"]
156 r,rs = strcat["abc\0","x"]
158 assert("strcat", :must, rs[0].to_s == "abcx")
160 init = h["test_init","IiP"]
163 argv = ["arg0","arg1","arg2"].to_ptr
164 r,rs = init[argc, argv.ref]
165 assert("init", :must, r == 0)
171 sym_open = h["test_open", "PSS"]
172 sym_gets = h["test_gets", "SsIP"]
173 sym_close = h["test_close", "0P"]
174 debug sym_open,sym_gets,sym_close
176 line = "Hello world!\n"
177 File.open("tmp.txt", "w"){|f|
181 fp,rs = sym_open["tmp.txt", "r"]
184 r,rs = sym_gets[" " * 256, 256, fp]
186 assert("open,gets", :must, rs[0] == line)
187 ObjectSpace.define_finalizer(fp) {File.unlink("tmp.txt")}
190 assert("open,gets", :must, line == nil)
191 File.unlink("tmp.txt")
195 callback1 = h["test_callback1"]
197 r,rs = h["test_call_func1", "IP"][callback1]
199 assert("callback1", :must, r == 1)
202 callback2 = DL.callback("LLP"){|num,ptr|
204 if( msg == "callback message" )
211 r,rs = h["test_call_func1", "IP"][callback2]
213 assert("callback2", :must, r == 2)
214 DL.remove_callback(callback2)
216 ptr = DL.malloc(DL.sizeof('CL'))
217 ptr.struct!("CL", :c, :l)
220 r,rs = h["test_fill_test_struct","0PIL"][ptr,100,1000]
222 assert("fill_test_struct", :must, ptr["c"] == 100, ptr["l"] == 1000)
223 assert("fill_test_struct", :must, ptr[:c] == 100, ptr[:l] == 1000) unless (Fixnum === :-)
226 r,rs = h["test_alloc_test_struct", "PIL"][100,200]
228 r.struct!("CL", :c, :l)
229 assert("alloc_test_struct", :must, r["c"] == 100, r["l"] == 200)
230 assert("alloc_test_struct", :must, r[:c] == 100, r[:l] == 200) unless (Fixnum === :-)
232 ptr = h["test_strlen"]
233 sym1 = DL::Symbol.new(ptr,"foo","0")
234 sym2 = h["test_strlen","LS"]
235 assert("Symbol.new", :must, ptr == sym1.to_ptr, sym1.to_ptr == sym2.to_ptr)
237 set_val = h["test_set_long_value","0"]
238 get_val = h["test_get_long_value","L"]
240 ptr = h["internal_long_value"]
242 assert("get value", :must, ptr["l"] == lval)
243 assert("get value", :must, ptr[:l] == lval) unless (Fixnum === :-)
246 assert("set value", :must, ptr["l"] == lval)
247 assert("set value", :must, ptr[:l] == lval) unless (Fixnum === :-)
250 data_init = h["test_data_init", "P"]
251 data_add = h["test_data_add", "0PS"]
252 data_aref = h["test_data_aref", "PPI"]
255 data_add[ptr, "name1"]
256 data_add[ptr, "name2"]
257 data_add[ptr, "name3"]
259 r,rs = data_aref[ptr, 1]
261 ptr.struct!("C1024P", :name, :next)
262 assert("data_aref", :must,
263 ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name2")
264 assert("data_aref", :must,
265 ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name2") unless (Fixnum === :-)
268 ptr.struct!("C1024P", :name, :next)
269 assert("data_aref", :must,
270 ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name1")
271 assert("data_aref", :must,
272 ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name1") unless (Fixnum === :-)
277 ptr.struct!("CHIL", "c", "h", "i", "l")
282 assert("struct!", :must,
288 ptr = DL::malloc(DL::sizeof("IP"))
289 ptr.struct!("IP", "n", "ptr")
292 assert("struct!", :must, ptr["n"] == 10 && ptr["ptr"] == nil)
295 ptr.struct!("CICI", "c1", "i1", "c2", "i2")
298 c1 = [ptr["c1"]].pack("c").unpack("C")[0]
299 c2 = [ptr["c2"]].pack("c").unpack("C")[0]
300 assert("struct!", :must,
306 printf("fail/total = #{$FAIL}/#{$TOTAL}\n")