1require 'test/unit'
2
3class TestISO8859 < Test::Unit::TestCase
4  ASSERTS = %q(
5    assert_match(/^(\xdf)\1$/i, "\xdf\xdf")
6    assert_match(/^(\xdf)\1$/i, "ssss")
7    # assert_match(/^(\xdf)\1$/i, "\xdfss") # this must be bug...
8    assert_match(/^[\xdfz]+$/i, "sszzsszz")
9    assert_match(/^SS$/i, "\xdf")
10    assert_match(/^Ss$/i, "\xdf")
11    ((0xc0..0xde).to_a - [0xd7]).each do |c|
12      c1 = c.chr("ENCODING")
13      c2 = (c + 0x20).chr("ENCODING")
14      assert_match(/^(#{ c1 })\1$/i, c2 + c1)
15      assert_match(/^(#{ c2 })\1$/i, c1 + c2)
16      assert_match(/^[#{ c1 }]+$/i, c2 + c1)
17      assert_match(/^[#{ c2 }]+$/i, c1 + c2)
18    end
19    assert_match(/^\xff$/i, "\xff")
20  )
21
22  def test_iso_8859_1
23    eval("# encoding: iso8859-1\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-1"))
24  end
25
26  def test_iso_8859_2
27    eval("# encoding: iso8859-2\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-2"))
28  end
29
30  def test_iso_8859_3
31    eval(%q(# encoding: iso8859-3
32      assert_match(/^(\xdf)\1$/i, "\xdf\xdf")
33      assert_match(/^(\xdf)\1$/i, "ssss")
34      assert_match(/^[\xdfz]+$/i, "sszzsszz")
35      assert_match(/^SS$/i, "\xdf")
36      assert_match(/^Ss$/i, "\xdf")
37      [0xa1, 0xa6, *(0xa9..0xac), 0xaf].each do |c|
38        c1 = c.chr("iso8859-3")
39        c2 = (c + 0x10).chr("iso8859-3")
40        assert_match(/^(#{ c1 })\1$/i, c2 + c1)
41        assert_match(/^(#{ c2 })\1$/i, c1 + c2)
42        assert_match(/^[#{ c1 }]+$/i, c2 + c1)
43        assert_match(/^[#{ c2 }]+$/i, c1 + c2)
44      end
45      ([*(0xc0..0xde)] - [0xc3, 0xd0, 0xd7]).each do |c|
46        c1 = c.chr("iso8859-3")
47        c2 = (c + 0x20).chr("iso8859-3")
48        assert_match(/^(#{ c1 })\1$/i, c2 + c1)
49        assert_match(/^(#{ c2 })\1$/i, c1 + c2)
50        assert_match(/^[#{ c1 }]+$/i, c2 + c1)
51        assert_match(/^[#{ c2 }]+$/i, c1 + c2)
52      end
53    ))
54  end
55
56  def test_iso_8859_4
57    eval("# encoding: iso8859-4\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-4"))
58  end
59
60  def test_iso_8859_5
61    eval(%q(# encoding: iso8859-5
62      (0xb0..0xcf).each do |c|
63        c1 = c.chr("iso8859-5")
64        c2 = (c + 0x20).chr("iso8859-5")
65        assert_match(/^(#{ c1 })\1$/i, c2 + c1)
66        assert_match(/^(#{ c2 })\1$/i, c1 + c2)
67        assert_match(/^[#{ c1 }]+$/i, c2 + c1)
68        assert_match(/^[#{ c2 }]+$/i, c1 + c2)
69      end
70      ((0xa1..0xaf).to_a - [0xad]).each do |c|
71        c1 = c.chr("iso8859-5")
72        c2 = (c + 0x50).chr("iso8859-5")
73        assert_match(/^(#{ c1 })\1$/i, c2 + c1)
74        assert_match(/^(#{ c2 })\1$/i, c1 + c2)
75        assert_match(/^[#{ c1 }]+$/i, c2 + c1)
76        assert_match(/^[#{ c2 }]+$/i, c1 + c2)
77      end
78    ))
79  end
80
81  def test_iso_8859_6
82    eval(%q(# encoding: iso8859-6
83      [0xa4, 0xac, 0xbb, 0xbf, *(0xc1..0xda), *(0xe0..0xf2)].each do |c|
84        c1 = c.chr("iso8859-6")
85        assert_match(/^(#{ c1 })\1$/i, c1 * 2)
86      end
87    ))
88  end
89
90  def test_iso_8859_7
91    eval(%q(# encoding: iso8859-7
92      ((0xa0..0xfe).to_a - [0xae, 0xd2]).each do |c|
93        c1 = c.chr("iso8859-7")
94        assert_match(/^(#{ c1 })\1$/i, c1 * 2)
95      end
96      ((0xc1..0xd9).to_a - [0xd2]).each do |c|
97        c1 = c.chr("iso8859-7")
98        c2 = (c + 0x20).chr("iso8859-7")
99        assert_match(/^(#{ c1 })\1$/i, c2 + c1)
100        assert_match(/^(#{ c2 })\1$/i, c1 + c2)
101        assert_match(/^[#{ c1 }]+$/i, c2 + c1)
102        assert_match(/^[#{ c2 }]+$/i, c1 + c2)
103      end
104    ))
105  end
106
107  def test_iso_8859_8
108    eval(%q(# encoding: iso8859-8
109      [0xa0, *(0xa2..0xbe), *(0xdf..0xfa), 0xfc, 0xfd].each do |c|
110        c1 = c.chr("iso8859-8")
111        assert_match(/^(#{ c1 })\1$/i, c1 * 2)
112      end
113    ))
114  end
115
116  def test_iso_8859_9
117    eval(%q(# encoding: iso8859-9
118      assert_match(/^(\xdf)\1$/i, "\xdf\xdf")
119      assert_match(/^(\xdf)\1$/i, "ssss")
120      assert_match(/^[\xdfz]+$/i, "sszzsszz")
121      assert_match(/^SS$/i, "\xdf")
122      assert_match(/^Ss$/i, "\xdf")
123      ([*(0xc0..0xdc)] - [0xd7]).each do |c|
124        c1 = c.chr("iso8859-9")
125        c2 = (c + 0x20).chr("iso8859-9")
126        assert_match(/^(#{ c1 })\1$/i, c2 + c1)
127        assert_match(/^(#{ c2 })\1$/i, c1 + c2)
128        assert_match(/^[#{ c1 }]+$/i, c2 + c1)
129        assert_match(/^[#{ c2 }]+$/i, c1 + c2)
130      end
131    ))
132  end
133
134  def test_iso_8859_10
135    eval("# encoding: iso8859-10\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-10"))
136  end
137
138  def test_iso_8859_11
139    eval(%q(# encoding: iso8859-11
140      [*(0xa0..0xda), *(0xdf..0xfb)].each do |c|
141        c1 = c.chr("iso8859-11")
142        assert_match(/^(#{ c1 })\1$/i, c1 * 2)
143      end
144    ))
145  end
146
147  def test_iso_8859_13
148    eval("# encoding: iso8859-13\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-13"))
149  end
150
151  def test_iso_8859_14
152    eval("# encoding: iso8859-14\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-14"))
153  end
154
155  def test_iso_8859_15
156    eval("# encoding: iso8859-15\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-15"))
157  end
158
159  def test_iso_8859_16
160    eval("# encoding: iso8859-16\n" + ASSERTS.gsub(/ENCODING/m, "iso8859-16"))
161  end
162end
163
164