84ec87720d3680e2ff79180746435c4525db9be0
[chise/ruby.git] / test / test-rbchise.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       #qp f
35       assert_instance_of(String, f)
36     }
37
38     @ft.each {|k, v|
39       #qp k, v
40       assert_kind_of(Integer, k)
41       assert_instance_of(String, v)
42     }
43
44     ft = @ds.get_feature("numeric-value")
45     ft.each {|k, v|
46       #qp k, v
47       assert_kind_of(Integer, k)
48       assert_instance_of(String, v)
49     }
50   end
51
52   def test_error
53     @ds = CHISE::DataSource.new
54     @ft = @ds.get_feature("nosuchfeature")
55     v = @ft.get_value(20175)
56     assert_equal(nil, v)
57   end
58
59   def test_chisedb
60     @cd = CHISE::ChiseDB.instance
61
62     char_id = @cd.decode_char("=daikanwa", 364)
63     assert_equal(20175, char_id)
64
65     value = @cd.load_feature("ideographic-structure", char_id)
66     assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value)
67
68     value = @cd.load_feature("=ucs", char_id)
69     assert_equal(20175, value)
70
71     @cd.each_feature {|f|
72       assert_instance_of(String, f)
73     }
74
75     ft = @cd.get_feature("numeric-value")
76     ft.each {|k, v|
77       assert_kind_of(Integer, k)
78       assert_instance_of(String, v)
79     }
80   end
81
82   def test_ascii
83     @cd = CHISE::ChiseDB.instance
84     ct = @cd.get_ccs("ascii")
85     char_id = ct.decode(65)
86     assert_equal(65, char_id)
87     assert_equal("A", CHISE::Character.get(char_id).to_s)
88 #    assert_equal("A", char.to_s)
89   end
90
91
92   def test_parse_c_string
93     u8 = "字"
94     assert_equal(23383, u8.u8to_i)
95     assert_equal(23383, parse_c_string("?"+u8))
96     assert_equal(0,     parse_c_string("?\\^@"))
97     assert_equal(9,     parse_c_string("?\t"))
98     assert_equal(10,    parse_c_string("?\n"))
99     assert_equal(13,    parse_c_string("?\r"))
100     assert_equal(94,    parse_c_string("?^\\"))
101     assert_equal(31,    parse_c_string("?\\^_"))
102     assert_equal(32,    parse_c_string("?\\ "))
103     assert_equal(34,    parse_c_string("?\\\""))
104     assert_equal(126,   parse_c_string("?~"))
105     assert_equal(127,   parse_c_string("?\\^?\000"))
106     assert_equal(131,   parse_c_string("?\\^\303\237"))
107     assert_equal(0x7FF, parse_c_string("?\337\277"))
108     assert_equal(0xFFFF,        parse_c_string("?\357\277\277"))
109     assert_equal(0x1FFFFF,      parse_c_string("?\367\277\277\277"))
110     assert_equal(0x3FFFFFF,     parse_c_string("?\373\277\277\277\277"))
111     assert_equal(0xFFFFFFF,     parse_c_string("?\374\217\277\277\277\277"))
112     assert_raise(RuntimeError) { parse_c_string("nosuch") }
113   end
114
115   def test_format_char_id
116     u8 = "字"
117     assert_equal(u8, CHISE.i_tou8(23383))
118     assert_equal("?\345\255\227",       format_char_id(23383))
119     assert_equal("?"+u8,                format_char_id(23383))
120     assert_equal("?\\^@",       format_char_id(0))
121     assert_equal("?\t",         format_char_id(?\t))
122     assert_equal("?\n",         format_char_id(?\n))
123     assert_equal("?\r",         format_char_id(?\r))
124     assert_equal("?^\\",        format_char_id(0x1C))
125     assert_equal("?\\^_",       format_char_id(0x1F))
126     assert_equal("?\\ ",        format_char_id(?\s))
127     assert_equal("?\\\"",       format_char_id(?\"))
128     assert_equal("?~",          format_char_id(0x7E))
129     assert_equal("?\\^?\000",   format_char_id(0x7F))
130     assert_equal("?\\^\303\237",        format_char_id(0x9F))
131     assert_equal("?\337\277",   format_char_id(0x7FF))
132     assert_equal("?\357\277\277",       format_char_id(0xFFFF))
133     assert_equal("?\367\277\277\277",   format_char_id(0x1FFFFF))
134     assert_equal("?\373\277\277\277\277",       format_char_id(0x3FFFFFF))
135     assert_equal("?\374\217\277\277\277\277",   format_char_id(0xFFFFFFF))
136   end
137 end