update.
[chise/ruby.git] / test / test-libchise.rb
index 9d4d88c..ae9776d 100755 (executable)
 
 require "common"
 
-class TestRbChise < Test::Unit::TestCase
-  include CHISE::ChiseValue
+class TestLibChise < Test::Unit::TestCase
+  def test_libchise
+    db_dir = CHISE::DataSource::DB_DIR
+    assert_match(/chise-db\Z/, db_dir)
 
-  def test_rbchise
-    @ds = CHISE::DataSource.new
+    @ds = CHISE::DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755)
     assert_instance_of(CHISE::DataSource, @ds)
-    assert_match(/chise-db\Z/, @ds.location.to_s)
 
-    @ct = @ds.get_ccs("=daikanwa")
-    assert_instance_of(CHISE::CCSTable, @ct)
-    char_id = @ct.decode(364) # get a character by Daikanwa number 364.
-    assert_equal(20175, char_id)
-    str = format_char_id(20175)
-    assert_equal("?\344\273\217", str)
-
-    char_id = @ds.decode_char("=daikanwa", 364)
-    assert_equal(20175, char_id)
-
-    @ft = @ds.get_feature("ideographic-structure")
-    assert_instance_of(CHISE::FeatureTable, @ft)
-    value = @ft.get_value(char_id)
-    assert_instance_of(String, value)
-    assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
-
-    value = @ds.load_feature("ideographic-structure", char_id)
-    assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
-
-    @ds.each_feature {|f|
-      assert_instance_of(String, f)
-      ft = @ds.get_feature(f)
-      v = ft.get_value(23383)
-      ft.close
-    }
-
-    @ft.each {|k, v|
-      assert_kind_of(Integer, k)
-      assert_instance_of(String, v)
-    }
-
-    ft = @ds.get_feature("numeric-value")
-    ft.each {|k, v|
-      assert_kind_of(Integer, k)
-      assert_instance_of(String, v)
-    }
-  end
-
-  def test_each_ccs
-    @ds = CHISE::DataSource.new
-    @ds.each_ccs {|ccs|
-      assert_instance_of(String, ccs)
-      ct = @ds.get_ccs(ccs)
-      assert_instance_of(CHISE::CCSTable, ct)
-    }
-
-    ct = @ds.get_ccs("=ucs")
-    ct.each {|k, v|
-      assert_kind_of(Integer, k)
-      assert_kind_of(Integer, v)
+    @ds.each_feature_name {|name|
+      assert_instance_of(String, name)
     }
-    ct.close
-  end
-
-  def test_error
-    @ds = CHISE::DataSource.new
-    @ft = @ds.get_feature("nosuchfeature")
-    v = @ft.get_value(20175)
-    assert_equal(nil, v)
-  end
 
-  def test_chisedb
-    @cd = CHISE::ChiseDB.instance
-
-    char_id = @cd.decode_char("=daikanwa", 364)
-    assert_equal(20175, char_id)
-
-    value = @cd.load_feature("ideographic-structure", char_id)
+    # get a feature value
+    char_id = 0x4ECF
+    feature = @ds.get_feature("ideographic-structure")
+    assert_instance_of(CHISE::Feature, feature)
+    assert_equal(true, feature.setup_db(0))
+    assert_equal(true, feature.sync)
+    value = feature.get_value(char_id)
     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
 
-    value = @cd.load_feature("=ucs", char_id)
-    assert_equal(20175, value)
-
-    @cd.each_feature {|f|
-      assert_instance_of(String, f)
-    }
+    # shortcut
+    value = @ds.load_feature(char_id, "ideographic-structure")
+    assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
 
-    ft = @cd.get_feature("numeric-value")
-    ft.each {|k, v|
-      assert_kind_of(Integer, k)
-      assert_instance_of(String, v)
+    # set a value
+    feature = @ds.get_feature("test-libchise")
+    assert_instance_of(CHISE::Feature, feature)
+    v = "testvalue"+$$.to_s
+    assert_equal(true, feature.set_value(char_id, v))
+    assert_equal(v, feature.get_value(char_id))
+
+    # each char
+    feature = @ds.get_feature("numeric-value")
+    feature.each_char {|cid, valdatum|
+      assert_kind_of(Numeric, cid)
+      assert_instance_of(String, valdatum)
+
+      ucs = @ds.get_feature("=ucs").get_value(cid)
+      if ucs
+       assert_instance_of(String, ucs)
+      else
+       ucs = @ds.get_feature("=>ucs").get_value(cid)
+       if ucs
+         assert_instance_of(String, ucs)
+       end
+      end
+
+      name = @ds.get_feature("name").get_value(cid)
+      if name
+       assert_instance_of(String, name)
+      end
     }
-  end
-
-  def test_ascii
-    @cd = CHISE::ChiseDB.instance
-    ct = @cd.get_ccs("ascii")
-    char_id = ct.decode(65)
-    assert_equal(65, char_id)
-    assert_equal("A", CHISE::Character.get(char_id).to_s)
-#   assert_equal("A", char.to_s)
-  end
 
-  def test_put
-    @cd = CHISE::ChiseDB.instance
-    char_id = "字".char.char_id
-    ft = @cd.get_feature("test")
-    #assert_equal(nil, ft.get_value(char_id))
-    ft.set_value(char_id, "test1")
-    assert_equal("test1", ft.get_value(char_id))
-    ft.sync
+    # get a character by Daikanwa number 364.
+    ccs = @ds.get_ccs("=daikanwa")
+    assert_instance_of(CHISE::CCS, ccs)
+    assert_equal(true, ccs.setup_db(0))
+    assert_equal(true, ccs.sync)
+    char_id = ccs.decode(364)
+    assert_equal(0x4ECF, char_id)
 
-    ds = @cd.instance_eval { @ds }
-    path = ds.location+"character/feature/test"
-    assert_equal(true, path.exist?)
-  end
+    # shortcut
+    char_id = @ds.decode_char("=daikanwa", 364)
+    assert_equal(0x4ECF, char_id)
 
-  def test_parse_c_string
-    u8 = "字"
-#    assert_equal(23383, u8.u8to_i)
-    assert_equal(23383,        parse_c_string("?"+u8))
-    assert_equal(0,    parse_c_string("?\\^@"))
-    assert_equal(9,    parse_c_string("?\t"))
-    assert_equal(10,   parse_c_string("?\n"))
-    assert_equal(13,   parse_c_string("?\r"))
-    assert_equal(94,   parse_c_string("?^\\"))
-    assert_equal(31,   parse_c_string("?\\^_"))
-    assert_equal(32,   parse_c_string("?\\ "))
-    assert_equal(34,   parse_c_string("?\\\""))
-    assert_equal(126,  parse_c_string("?~"))
-    assert_equal(127,  parse_c_string("?\\^?\000"))
-    assert_equal(131,  parse_c_string("?\\^\303\237"))
-    assert_equal(0x7FF,        parse_c_string("?\337\277"))
-    assert_equal(0xFFFF,       parse_c_string("?\357\277\277"))
-    assert_equal(0x1FFFFF,     parse_c_string("?\367\277\277\277"))
-    assert_equal(0x3FFFFFF,    parse_c_string("?\373\277\277\277\277"))
-    assert_equal(0xFFFFFFF,    parse_c_string("?\374\217\277\277\277\277"))
-    assert_raise(RuntimeError) { parse_c_string("nosuch") }
-  end
+    # set a code_point
+    ccs = @ds.get_ccs("test-ccs")
+    assert_instance_of(CHISE::CCS, ccs)
+    assert_equal(true, ccs.set($$, 0x4ECF))
+    assert_equal(0x4ECF, ccs.decode($$))
 
-  def test_format_char_id
-    u8 = "字"
-#    assert_equal(u8, CHISE.i_tou8(23383))
-    assert_equal("?\345\255\227",      format_char_id(23383))
-    assert_equal("?"+u8,               format_char_id(23383))
-    assert_equal("?\\^@",      format_char_id(0))
-    assert_equal("?\t",                format_char_id(?\t))
-    assert_equal("?\n",                format_char_id(?\n))
-    assert_equal("?\r",                format_char_id(?\r))
-    assert_equal("?^\\",       format_char_id(0x1C))
-    assert_equal("?\\^_",      format_char_id(0x1F))
-    assert_equal("?\\ ",       format_char_id(?\s))
-    assert_equal("?\\\"",      format_char_id(?\"))
-    assert_equal("?~",         format_char_id(0x7E))
-    assert_equal("?\\^?\000",  format_char_id(0x7F))
-    assert_equal("?\\^\303\237",       format_char_id(0x9F))
-    assert_equal("?\337\277",  format_char_id(0x7FF))
-    assert_equal("?\357\277\277",      format_char_id(0xFFFF))
-    assert_equal("?\367\277\277\277",  format_char_id(0x1FFFFF))
-    assert_equal("?\373\277\277\277\277",      format_char_id(0x3FFFFFF))
-    assert_equal("?\374\217\277\277\277\277",  format_char_id(0xFFFFFFF))
+    @ds.close
   end
 end