ext/dl/test/test.rb


DEFINITIONS

This source file includes following functions.


   1  # -*- ruby -*-
   2  
   3  require 'dl'
   4  require 'dl/import'
   5  
   6  $FAIL = 0
   7  $TOTAL = 0
   8  
   9  def assert(label, ty, *conds)
  10    $TOTAL += 1
  11    cond = !conds.include?(false)
  12    if( cond )
  13      printf("succeed in `#{label}'\n")
  14    else
  15      $FAIL += 1
  16      case ty
  17      when :may
  18        printf("fail in `#{label}' ... expected\n")
  19      when :must
  20        printf("fail in `#{label}' ... unexpected\n")
  21      when :raise
  22        raise(RuntimeError, "fail in `#{label}'")
  23      end
  24    end
  25  end
  26  
  27  def debug(*xs)
  28    if( $DEBUG )
  29      xs.each{|x|
  30        p x
  31      }
  32    end
  33  end
  34  
  35  print("VERSION       = #{DL::VERSION}\n")
  36  print("MAJOR_VERSION = #{DL::MAJOR_VERSION}\n")
  37  print("MINOR_VERSION = #{DL::MINOR_VERSION}\n")
  38  print("\n")
  39  print("DLSTACK   = #{DL::DLSTACK}\n")
  40  print("MAX_ARG   = #{DL::MAX_ARG}\n")
  41  print("\n")
  42  print("DL::FREE = #{DL::FREE.inspect}\n")
  43  print("\n")
  44  
  45  $LIB = nil
  46  if( !$LIB && File.exist?("libtest.so") )
  47    $LIB = "./libtest.so"
  48  end
  49  if( !$LIB && File.exist?("test/libtest.so") )
  50    $LIB = "./test/libtest.so"
  51  end
  52  
  53  module LIBTest
  54    extend DL::Importable
  55  
  56    dlload($LIB)
  57    extern "int test_c2i(char)"
  58    extern "char test_i2c(int)"
  59    extern "long test_lcc(char, char)"
  60    extern "double test_f2d(float)"
  61    extern "float test_d2f(double)"
  62    extern "int test_strlen(char*)"
  63    extern "int test_isucc(int)"
  64    extern "long test_lsucc(long)"
  65    extern "void test_succ(long *)"
  66    extern "int test_arylen(int [])"
  67    extern "void test_append(char*[], int, char *)"
  68  end
  69  
  70  DL.dlopen($LIB){|h|
  71    c2i = h["test_c2i","IC"]
  72    debug c2i
  73    r,rs = c2i[?a]
  74    debug r,rs
  75    assert("c2i", :may, r == ?a)
  76    assert("extern c2i", :must, r == LIBTest.test_c2i(?a))
  77  
  78    i2c = h["test_i2c","CI"]
  79    debug i2c
  80    r,rs = i2c[?a]
  81    debug r,rs
  82    assert("i2c", :may, r == ?a)
  83    assert("exern i2c", :must, r == LIBTest.test_i2c(?a))
  84  
  85    lcc = h["test_lcc","LCC"]
  86    debug lcc
  87    r,rs = lcc[1,2]
  88    assert("lcc", :may, r == 3)
  89    assert("extern lcc", :must, r == LIBTest.test_lcc(1,2))
  90  
  91    f2d = h["test_f2d","DF"]
  92    debug f2d
  93    r,rs = f2d[20.001]
  94    debug r,rs
  95    assert("f2d", :may, r.to_i == 20)
  96    assert("extern f2d", :must, r = LIBTest.test_f2d(20.001))
  97  
  98    d2f = h["test_d2f","FD"]
  99    debug d2f
 100    r,rs = d2f[20.001]
 101    debug r,rs
 102    assert("d2f", :may, r.to_i == 20)
 103    assert("extern d2f", :must, r == LIBTest.test_d2f(20.001))
 104  
 105    strlen = h["test_strlen","IS"]
 106    debug strlen
 107    r,rs = strlen["0123456789"]
 108    debug r,rs
 109    assert("strlen", :must, r == 10)
 110    assert("extern strlen", :must, r == LIBTest.test_strlen("0123456789"))
 111  
 112    isucc = h["test_isucc","II"]
 113    debug isucc
 114    r,rs = isucc[2]
 115    debug r,rs
 116    assert("isucc", :must, r == 3)
 117    assert("extern isucc", :must, r == LIBTest.test_isucc(2))
 118  
 119    lsucc = h["test_lsucc","LL"]
 120    debug lsucc
 121    r,rs = lsucc[10000000]
 122    debug r,rs
 123    assert("lsucc", :must, r == 10000001)
 124    assert("extern lsucc", :must, r == LIBTest.test_lsucc(10000000))
 125  
 126    succ = h["test_succ","0l"]
 127    debug succ
 128    r,rs = succ[0]
 129    debug r,rs
 130    assert("succ", :must, rs[0] == 1)
 131    l = DL.malloc(DL.sizeof("L"))
 132    l.struct!("L",:lval)
 133    LIBTest.test_succ(l)
 134    assert("extern succ", :must, rs[0] == l[:lval])
 135  
 136    arylen = h["test_arylen","IA"]
 137    debug arylen
 138    r,rs = arylen[["a","b","c","d",nil]]
 139    debug r,rs
 140    assert("arylen", :must, r == 4)
 141  
 142    arylen = h["test_arylen","IP"]
 143    debug arylen
 144    r,rs = arylen[["a","b","c","d",nil]]
 145    debug r,rs
 146    assert("arylen", :must, r == 4)
 147    assert("extern arylen", :must, r == LIBTest.test_arylen(["a","b","c","d",nil]))
 148  
 149    append = h["test_append","0aIS"]
 150    debug append
 151    r,rs = append[["a","b","c"],3,"x"]
 152    debug r,rs
 153    assert("append", :must, rs[0].to_a('S',3) == ["ax","bx","cx"])
 154  
 155    LIBTest.test_append(["a","b","c"],3,"x")
 156    assert("extern append", :must, rs[0].to_a('S',3) == LIBTest._args_[0].to_a('S',3))
 157  
 158    strcat = h["test_strcat","SsS"]
 159    debug strcat
 160    r,rs = strcat["abc\0","x"]
 161    debug r,rs
 162    assert("strcat", :must, rs[0].to_s == "abcx")
 163  
 164    init = h["test_init","IiP"]
 165    debug init
 166    argc = 3
 167    argv = ["arg0","arg1","arg2"].to_ptr
 168    r,rs = init[argc, argv.ref]
 169    assert("init", :must, r == 0)
 170  }
 171  
 172  
 173  h = DL.dlopen($LIB)
 174  
 175  sym_open = h["test_open", "PSS"]
 176  sym_gets = h["test_gets", "SsIP"]
 177  sym_close = h["test_close", "0P"]
 178  debug sym_open,sym_gets,sym_close
 179  
 180  line = "Hello world!\n"
 181  File.open("tmp.txt", "w"){|f|
 182    f.print(line)
 183  }
 184  
 185  fp,rs = sym_open["tmp.txt", "r"]
 186  if( fp )
 187    fp.free = sym_close
 188    r,rs = sym_gets[" " * 256, 256, fp]
 189    debug r,rs
 190    assert("open,gets", :must, rs[0] == line)
 191    ObjectSpace.define_finalizer(fp) {File.unlink("tmp.txt")}
 192    fp = nil
 193  else
 194    assert("open,gets", :must, line == nil)
 195    File.unlink("tmp.txt")
 196  end
 197  
 198  
 199  callback1 = h["test_callback1"]
 200  debug callback1
 201  r,rs = h["test_call_func1", "IP"][callback1]
 202  debug r,rs
 203  assert("callback1", :must, r == 1)
 204  
 205  
 206  callback2 = DL.callback("LLP"){|num,ptr|
 207    msg = ptr.to_s
 208    if( msg == "callback message" )
 209      2
 210    else
 211      0
 212    end
 213  }
 214  debug callback2
 215  r,rs = h["test_call_func1", "IP"][callback2]
 216  debug r,rs
 217  assert("callback2", :must, r == 2)
 218  DL.remove_callback(callback2)
 219  
 220  ptr = DL.malloc(DL.sizeof('CL'))
 221  ptr.struct!("CL", :c, :l)
 222  ptr["c"] = 0
 223  ptr["l"] = 0
 224  r,rs = h["test_fill_test_struct","0PIL"][ptr,100,1000]
 225  debug r,rs
 226  assert("fill_test_struct", :must, ptr["c"] == 100, ptr["l"] == 1000)
 227  assert("fill_test_struct", :must, ptr[:c] == 100, ptr[:l] == 1000) unless (Fixnum === :-)
 228  
 229  
 230  r,rs = h["test_alloc_test_struct", "PIL"][100,200]
 231  r.free = DL::FREE
 232  r.struct!("CL", :c, :l)
 233  assert("alloc_test_struct", :must, r["c"] == 100, r["l"] == 200)
 234  assert("alloc_test_struct", :must, r[:c] == 100, r[:l] == 200) unless (Fixnum === :-)
 235  
 236  ptr = h["test_strlen"]
 237  sym1 = DL::Symbol.new(ptr,"foo","0")
 238  sym2 = h["test_strlen","LS"]
 239  assert("Symbol.new", :must, ptr == sym1.to_ptr, sym1.to_ptr == sym2.to_ptr)
 240  
 241  set_val = h["test_set_long_value","0"]
 242  get_val = h["test_get_long_value","L"]
 243  lval = get_val[][0]
 244  ptr = h["internal_long_value"]
 245  ptr.struct!("L", :l)
 246  assert("get value", :must, ptr["l"] == lval)
 247  assert("get value", :must, ptr[:l] == lval) unless (Fixnum === :-)
 248  ptr["l"] = 200
 249  lval = get_val[][0]
 250  assert("set value", :must, ptr["l"] == lval)
 251  assert("set value", :must, ptr[:l] == lval) unless (Fixnum === :-)
 252  
 253  
 254  data_init = h["test_data_init", "P"]
 255  data_add  = h["test_data_add", "0PS"]
 256  data_aref = h["test_data_aref", "PPI"]
 257  r,rs = data_init[]
 258  ptr = r
 259  data_add[ptr, "name1"]
 260  data_add[ptr, "name2"]
 261  data_add[ptr, "name3"]
 262  
 263  r,rs = data_aref[ptr, 1]
 264  ptr = r
 265  ptr.struct!("C1024P", :name, :next)
 266  assert("data_aref", :must,
 267         ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name2")
 268  assert("data_aref", :must,
 269         ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name2") unless (Fixnum === :-)
 270  
 271  ptr = ptr["next"]
 272  ptr.struct!("C1024P", :name, :next)
 273  assert("data_aref", :must,
 274         ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name1")
 275  assert("data_aref", :must,
 276         ptr["name"].collect{|c| c.chr}.join.split("\0")[0] == "name1") unless (Fixnum === :-)
 277  
 278  GC.start
 279  
 280  ptr = DL::malloc(1024)
 281  ptr.struct!("CHIL", "c", "h", "i", "l")
 282  ptr["c"] = 1
 283  ptr["h"] = 2
 284  ptr["i"] = 3
 285  ptr["l"] = 4
 286  assert("struct!", :must,
 287         ptr["c"] == 1 &&
 288         ptr["h"] == 2 &&
 289         ptr["i"] == 3 &&
 290         ptr["l"] == 4)
 291  
 292  GC.start
 293  
 294  printf("fail/total = #{$FAIL}/#{$TOTAL}\n")