n.c.
[chise/ruby.git] / test / test-libchise.rb
diff --git a/test/test-libchise.rb b/test/test-libchise.rb
new file mode 100755 (executable)
index 0000000..51c69a1
--- /dev/null
@@ -0,0 +1,163 @@
+#!/usr/bin/env ruby
+# Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
+
+require "common"
+
+class TestRbChise < Test::Unit::TestCase
+  include CHISE::ChiseValue
+
+  def test_rbchise
+    @ds = CHISE::DataSource.new
+    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.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)
+    }
+    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)
+    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)
+    }
+
+    ft = @cd.get_feature("numeric-value")
+    ft.each {|k, v|
+      assert_kind_of(Integer, k)
+      assert_instance_of(String, v)
+    }
+  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
+
+    ds = @cd.instance_eval { @ds }
+    path = ds.location+"character/feature/test"
+    assert_equal(true, path.exist?)
+  end
+
+  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
+
+  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))
+  end
+end