update.
[chise/ruby.git] / test / test-chisedb.rb
1 #!/usr/bin/env ruby
2 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
3
4 require "common"
5
6 class TestChiseDB < Test::Unit::TestCase
7   def test_chisedb
8     @cd = CHISE::ChiseDB.instance
9
10     @cd.each_feature_name {|f|
11       assert_instance_of(String, f)
12     }
13
14     # get a feature value
15     char_id = 0x4ECF
16     feature = @cd.get_feature("ideographic-structure")
17     assert_instance_of(CHISE::FeatureDB, feature)
18     #assert_equal(true, feature.setup_db(0))
19     #assert_equal(true, feature.sync)
20     value = feature.get_value(char_id)
21     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
22
23     # shortcut
24     value = @cd.load_feature(char_id, "ideographic-structure")
25     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
26
27     # set a value
28     feature = @cd.get_feature("test-chisedb")
29     assert_instance_of(CHISE::FeatureDB, feature)
30     v = "testvalue"+$$.to_s
31     assert_equal(true, feature.set_value(char_id, v))
32     assert_equal(v, feature.get_value(char_id))
33
34     # dump the feature
35     feature.dump
36     
37     # each char
38     feature = @cd.get_feature("numeric-value")
39     feature.each_char {|cid, valdatum|
40       assert_kind_of(Numeric, cid)
41       #assert_instance_of(String, valdatum)
42     }
43
44     # get a character by Daikanwa number 364.
45     ccs = @cd.get_ccs("=daikanwa")
46     assert_instance_of(CHISE::CCS_DB, ccs)
47     #assert_equal(true, ccs.setup_db(0))
48     #assert_equal(true, ccs.sync)
49     char_id = ccs.decode(364)
50     assert_equal(0x4ECF, char_id)
51
52     # shortcut
53     char_id = @cd.decode_char("=daikanwa", 364)
54     assert_equal(0x4ECF, char_id)
55
56     # set a code_point
57     ccs = @cd.get_ccs("test-ccs")
58     assert_instance_of(CHISE::CCS_DB, ccs)
59     assert_equal(true, ccs.set($$, 0x4ECF))
60     assert_equal(0x4ECF, ccs.decode($$))
61
62     @cd.close
63   end
64
65   def test_chisedb2
66     @cd = CHISE::ChiseDB.instance
67     char_id = 0x4ECF
68     value = @cd.load_feature(char_id, "=ucs")
69     assert_equal(20175, value)
70   end
71
72   def test_each_ccs
73     @cd = CHISE::ChiseDB.instance
74     @cd.each_ccs {|ccs|
75       assert_instance_of(String, ccs)
76       ccsdb = @cd.get_ccs(ccs)
77       assert_instance_of(CHISE::CCS_DB, ccsdb)
78     }
79   end
80
81   def test_ccs_each_char
82     @cd = CHISE::ChiseDB.instance
83     ccs = @cd.get_ccs("=ucs")
84     ccs.each_char {|code_point, cid|
85       assert_kind_of(Integer, code_point)
86       assert_kind_of(Integer, cid)
87     }
88   end
89
90   def test_set
91     @cd = CHISE::ChiseDB.instance
92     char_id = "字".char.char_id
93     feature = @cd.get_feature("test")
94     feature.setup_db(1) # writable
95     feature.set_value(char_id, "test1")
96     assert_equal("test1", feature.get_value(char_id))
97     #feature.sync
98
99     ds = @cd.instance_eval { @ds }
100     path = CHISE::DataSource::DB_DIR.path+"character/feature/test"
101     assert_equal(true, path.exist?)
102   end
103
104   def test_error
105     db_dir = CHISE::DataSource::DB_DIR
106     @ds = CHISE::DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755)
107     @feature = @ds.get_feature("nosuchfeature")
108     v = @feature.get_value(20175)
109     assert_equal(nil, v)
110   end
111
112   def test_ascii
113     @cd = CHISE::ChiseDB.instance
114     char_id = @cd.decode_char("ascii", 65)
115     assert_equal(65, char_id)
116     assert_equal("A", CHISE::Character.get(char_id).to_s)
117   end
118 end