n.c.
[chise/ruby.git] / test / test-libchise.rb
1 #!/usr/bin/env ruby
2 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
3
4 require "common"
5
6 class TestRbChise < Test::Unit::TestCase
7   include CHISE::ChiseValue
8
9   def test_rbchise
10     @ds = CHISE::DataSource.new
11     assert_instance_of(CHISE::DataSource, @ds)
12     assert_match(/chise-db\Z/, @ds.location.to_s)
13
14     @ct = @ds.get_ccs("=daikanwa")
15     assert_instance_of(CHISE::CCSTable, @ct)
16     char_id = @ct.decode(364) # get a character by Daikanwa number 364.
17     assert_equal(20175, char_id)
18     str = format_char_id(20175)
19     assert_equal("?\344\273\217", str)
20
21     char_id = @ds.decode_char("=daikanwa", 364)
22     assert_equal(20175, char_id)
23
24     @ft = @ds.get_feature("ideographic-structure")
25     assert_instance_of(CHISE::FeatureTable, @ft)
26     value = @ft.get_value(char_id)
27     assert_instance_of(String, value)
28     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
29
30     value = @ds.load_feature("ideographic-structure", char_id)
31     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
32
33     @ds.each_feature {|f|
34       assert_instance_of(String, f)
35     }
36
37     @ft.each {|k, v|
38       assert_kind_of(Integer, k)
39       assert_instance_of(String, v)
40     }
41
42     ft = @ds.get_feature("numeric-value")
43     ft.each {|k, v|
44       assert_kind_of(Integer, k)
45       assert_instance_of(String, v)
46     }
47   end
48
49   def test_each_ccs
50     @ds = CHISE::DataSource.new
51     @ds.each_ccs {|ccs|
52       assert_instance_of(String, ccs)
53       ct = @ds.get_ccs(ccs)
54       assert_instance_of(CHISE::CCSTable, ct)
55     }
56
57     ct = @ds.get_ccs("=ucs")
58     ct.each {|k, v|
59       assert_kind_of(Integer, k)
60       assert_kind_of(Integer, v)
61     }
62     ct.close
63   end
64
65   def test_error
66     @ds = CHISE::DataSource.new
67     @ft = @ds.get_feature("nosuchfeature")
68     v = @ft.get_value(20175)
69     assert_equal(nil, v)
70   end
71
72   def test_chisedb
73     @cd = CHISE::ChiseDB.instance
74
75     char_id = @cd.decode_char("=daikanwa", 364)
76     assert_equal(20175, char_id)
77
78     value = @cd.load_feature("ideographic-structure", char_id)
79     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
80
81     value = @cd.load_feature("=ucs", char_id)
82     assert_equal(20175, value)
83
84     @cd.each_feature {|f|
85       assert_instance_of(String, f)
86     }
87
88     ft = @cd.get_feature("numeric-value")
89     ft.each {|k, v|
90       assert_kind_of(Integer, k)
91       assert_instance_of(String, v)
92     }
93   end
94
95   def test_ascii
96     @cd = CHISE::ChiseDB.instance
97     ct = @cd.get_ccs("ascii")
98     char_id = ct.decode(65)
99     assert_equal(65, char_id)
100     assert_equal("A", CHISE::Character.get(char_id).to_s)
101 #   assert_equal("A", char.to_s)
102   end
103
104   def test_put
105     @cd = CHISE::ChiseDB.instance
106     char_id = "字".char.char_id
107     ft = @cd.get_feature("test")
108     #assert_equal(nil, ft.get_value(char_id))
109     ft.set_value(char_id, "test1")
110     assert_equal("test1", ft.get_value(char_id))
111     ft.sync
112
113     ds = @cd.instance_eval { @ds }
114     path = ds.location+"character/feature/test"
115     assert_equal(true, path.exist?)
116   end
117
118   def test_parse_c_string
119     u8 = "字"
120 #    assert_equal(23383, u8.u8to_i)
121     assert_equal(23383, parse_c_string("?"+u8))
122     assert_equal(0,     parse_c_string("?\\^@"))
123     assert_equal(9,     parse_c_string("?\t"))
124     assert_equal(10,    parse_c_string("?\n"))
125     assert_equal(13,    parse_c_string("?\r"))
126     assert_equal(94,    parse_c_string("?^\\"))
127     assert_equal(31,    parse_c_string("?\\^_"))
128     assert_equal(32,    parse_c_string("?\\ "))
129     assert_equal(34,    parse_c_string("?\\\""))
130     assert_equal(126,   parse_c_string("?~"))
131     assert_equal(127,   parse_c_string("?\\^?\000"))
132     assert_equal(131,   parse_c_string("?\\^\303\237"))
133     assert_equal(0x7FF, parse_c_string("?\337\277"))
134     assert_equal(0xFFFF,        parse_c_string("?\357\277\277"))
135     assert_equal(0x1FFFFF,      parse_c_string("?\367\277\277\277"))
136     assert_equal(0x3FFFFFF,     parse_c_string("?\373\277\277\277\277"))
137     assert_equal(0xFFFFFFF,     parse_c_string("?\374\217\277\277\277\277"))
138     assert_raise(RuntimeError) { parse_c_string("nosuch") }
139   end
140
141   def test_format_char_id
142     u8 = "字"
143 #    assert_equal(u8, CHISE.i_tou8(23383))
144     assert_equal("?\345\255\227",       format_char_id(23383))
145     assert_equal("?"+u8,                format_char_id(23383))
146     assert_equal("?\\^@",       format_char_id(0))
147     assert_equal("?\t",         format_char_id(?\t))
148     assert_equal("?\n",         format_char_id(?\n))
149     assert_equal("?\r",         format_char_id(?\r))
150     assert_equal("?^\\",        format_char_id(0x1C))
151     assert_equal("?\\^_",       format_char_id(0x1F))
152     assert_equal("?\\ ",        format_char_id(?\s))
153     assert_equal("?\\\"",       format_char_id(?\"))
154     assert_equal("?~",          format_char_id(0x7E))
155     assert_equal("?\\^?\000",   format_char_id(0x7F))
156     assert_equal("?\\^\303\237",        format_char_id(0x9F))
157     assert_equal("?\337\277",   format_char_id(0x7FF))
158     assert_equal("?\357\277\277",       format_char_id(0xFFFF))
159     assert_equal("?\367\277\277\277",   format_char_id(0x1FFFFF))
160     assert_equal("?\373\277\277\277\277",       format_char_id(0x3FFFFFF))
161     assert_equal("?\374\217\277\277\277\277",   format_char_id(0xFFFFFFF))
162   end
163 end