File: test-utf.rb

package info (click to toggle)
libuconv-ruby 0.4.9-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,752 kB
  • ctags: 135
  • sloc: ansic: 160,433; xml: 13,060; ruby: 396; makefile: 51
file content (179 lines) | stat: -rw-r--r-- 5,515 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require 'runit/testcase'
require 'runit/cui/testrunner'

class UconvTest < RUNIT::TestCase

  require 'uconv'

  def to_hex(str)
    ret = ""
    str.each_byte do |c|
      ret << format("%02X", c)
    end
    ret
  end

  def test_00
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xc0\x80")
    }
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xc1\x80")
    }
    assert_equal(Uconv.u8tou16("\xc2\x80"), "\x80\x00")
  end

  def test_01
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xe0\x80\x80")
    }
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xe0\x9f\x80")
    }
    assert_equal(Uconv.u8tou16("\xe0\xa0\x80"), "\x00\x08")
    assert_equal(Uconv.u8tou16("\xe1\x80\x80"), "\x00\x10")
  end

  def test_02
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xf0\x8f\x80\x80")
    }
    assert_equal(Uconv.u8tou16("\xf0\x90\x80\x80"), "\x00\xd8\x00\xdc")
    assert_equal(Uconv.u8tou16("\xf1\x80\x80\x80"), "\xc0\xd8\x00\xdc")
    assert_equal(Uconv.u8tou16("\xf4\x8f\xbf\xbf"), "\xff\xdb\xff\xdf")
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xf4\x90\x80\x80")
    }
  end


  def test_10
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xc0\x80")
    }
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xc1\x80")
    }
    assert_equal(Uconv.u8tou4("\xc2\x80"), "\x80\x00\x00\x00")
  end

  def test_11
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xe0\x80\x80")
    }
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xe0\x9f\x80")
    }
    assert_equal(Uconv.u8tou4("\xe0\xa0\x80"), "\x00\x08\x00\x00")
    assert_equal(Uconv.u8tou4("\xe1\x80\x80"), "\x00\x10\x00\x00")
  end

  def test_12
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xf0\x8f\x80\x80")
    }
    assert_equal(Uconv.u8tou4("\xf0\x90\x80\x80"), "\x00\x00\x01\x00")
    assert_equal(Uconv.u8tou4("\xf1\x80\x80\x80"), "\x00\x00\x04\x00")
    assert_equal(Uconv.u8tou4("\xf4\x8f\xbf\xbf"), "\xff\xff\x10\x00")
  end

  def test_13
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xf8\x87\x80\x80\x80")
    }
    assert_equal(Uconv.u8tou4("\xf8\x88\x80\x80\x80"), "\x00\x00\x20\x00")
    assert_equal(Uconv.u8tou4("\xf9\x80\x80\x80\x80"), "\x00\x00\x00\x01")
  end

  def test_14
    Uconv::shortest = true
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xfc\x83\x80\x80\x80\x80")
    }
    assert_equal(Uconv.u8tou4("\xfc\x84\x80\x80\x80\x80"), "\x00\x00\x00\x04")
    assert_equal(Uconv.u8tou4("\xfd\x80\x80\x80\x80\x80"), "\x00\x00\x00\x40")
    assert_equal(Uconv.u8tou4("\xfd\xbf\xbf\xbf\xbf\xbf"), "\xff\xff\xff\x7f")
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xfe\x80\x80\x80\x80\x80\x80")
    }
  end



  def test_20
    Uconv::shortest = false
    assert_equal(Uconv.u8tou16("\xc0\x80"), "\x00\x00")
    assert_equal(Uconv.u8tou16("\xc1\x80"), "\x40\x00")
    assert_equal(Uconv.u8tou16("\xc2\x80"), "\x80\x00")
  end

  def test_21
    Uconv::shortest = false
    assert_equal(Uconv.u8tou16("\xe0\x80\x80"), "\x00\x00")
    assert_equal(Uconv.u8tou16("\xe0\x9f\x80"), "\xc0\x07")
    assert_equal(Uconv.u8tou16("\xe0\xa0\x80"), "\x00\x08")
    assert_equal(Uconv.u8tou16("\xe1\x80\x80"), "\x00\x10")
  end

  def test_22
    Uconv::shortest = false
    assert_equal(Uconv.u8tou16("\xf0\x8f\x80\x80"), "\x00\xf0")
    assert_equal(Uconv.u8tou16("\xf0\x90\x80\x80"), "\x00\xd8\x00\xdc")
    assert_equal(Uconv.u8tou16("\xf1\x80\x80\x80"), "\xc0\xd8\x00\xdc")
    assert_equal(Uconv.u8tou16("\xf4\x8f\xbf\xbf"), "\xff\xdb\xff\xdf")
    assert_exception(Uconv::Error) {
      Uconv.u8tou16("\xf4\x90\x80\x80")
    }
  end


  def test_30
    Uconv::shortest = false
    assert_equal(Uconv.u8tou4("\xc0\x80"), "\x00\x00\x00\x00")
    assert_equal(Uconv.u8tou4("\xc1\x80"), "\x40\x00\x00\x00")
    assert_equal(Uconv.u8tou4("\xc2\x80"), "\x80\x00\x00\x00")
  end

  def test_31
    Uconv::shortest = false
    assert_equal(Uconv.u8tou4("\xe0\x80\x80"), "\x00\x00\x00\x00")
    assert_equal(Uconv.u8tou4("\xe0\x9f\x80"), "\xc0\x07\x00\x00")
    assert_equal(Uconv.u8tou4("\xe0\xa0\x80"), "\x00\x08\x00\x00")
    assert_equal(Uconv.u8tou4("\xe1\x80\x80"), "\x00\x10\x00\x00")
  end

  def test_32
    Uconv::shortest = false
    assert_equal(Uconv.u8tou4("\xf0\x8f\x80\x80"), "\x00\xf0\x00\x00")
    assert_equal(Uconv.u8tou4("\xf0\x90\x80\x80"), "\x00\x00\x01\x00")
    assert_equal(Uconv.u8tou4("\xf1\x80\x80\x80"), "\x00\x00\x04\x00")
    assert_equal(Uconv.u8tou4("\xf4\x8f\xbf\xbf"), "\xff\xff\x10\x00")
  end

  def test_33
    Uconv::shortest = false
    assert_equal(Uconv.u8tou4("\xf8\x87\x80\x80\x80"), "\x00\x00\x1c\x00")
    assert_equal(Uconv.u8tou4("\xf8\x88\x80\x80\x80"), "\x00\x00\x20\x00")
    assert_equal(Uconv.u8tou4("\xf9\x80\x80\x80\x80"), "\x00\x00\x00\x01")
  end

  def test_34
    Uconv::shortest = false
    assert_equal(Uconv.u8tou4("\xfc\x83\x80\x80\x80\x80"), "\x00\x00\x00\x03")
    assert_equal(Uconv.u8tou4("\xfc\x84\x80\x80\x80\x80"), "\x00\x00\x00\x04")
    assert_equal(Uconv.u8tou4("\xfd\x80\x80\x80\x80\x80"), "\x00\x00\x00\x40")
    assert_equal(Uconv.u8tou4("\xfd\xbf\xbf\xbf\xbf\xbf"), "\xff\xff\xff\x7f")
    assert_exception(Uconv::Error) {
      Uconv.u8tou4("\xfe\x80\x80\x80\x80\x80\x80")
    }
  end
end

RUNIT::CUI::TestRunner.run(UconvTest.suite)