summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/valid_encoding/utf_8_spec.rb
blob: a14c3af83003b7e1d6603b12258bc5719e446187 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- encoding: utf-8 -*-
require_relative '../../../spec_helper'

describe "String#valid_encoding? and UTF-8" do
  def utf8(bytes)
    bytes.pack("C*").force_encoding("UTF-8")
  end

  describe "1-byte character" do
    it "is valid if is in format 0xxxxxxx" do
      utf8([0b00000000]).valid_encoding?.should == true
      utf8([0b01111111]).valid_encoding?.should == true
    end

    it "is not valid if is not in format 0xxxxxxx" do
      utf8([0b10000000]).valid_encoding?.should == false
      utf8([0b11111111]).valid_encoding?.should == false
    end
  end

  describe "2-bytes character" do
    it "is valid if in format [110xxxxx 10xxxxx]" do
      utf8([0b11000010, 0b10000000]).valid_encoding?.should == true
      utf8([0b11000010, 0b10111111]).valid_encoding?.should == true

      utf8([0b11011111, 0b10000000]).valid_encoding?.should == true
      utf8([0b11011111, 0b10111111]).valid_encoding?.should == true
    end

    it "is not valid if the first byte is not in format 110xxxxx" do
      utf8([0b00000010, 0b10000000]).valid_encoding?.should == false
      utf8([0b00100010, 0b10000000]).valid_encoding?.should == false
      utf8([0b01000010, 0b10000000]).valid_encoding?.should == false
      utf8([0b01100010, 0b10000000]).valid_encoding?.should == false
      utf8([0b10000010, 0b10000000]).valid_encoding?.should == false
      utf8([0b10100010, 0b10000000]).valid_encoding?.should == false
      utf8([0b11000010, 0b10000000]).valid_encoding?.should == true # correct bytes
      utf8([0b11100010, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if the second byte is not in format 10xxxxxx" do
      utf8([0b11000010, 0b00000000]).valid_encoding?.should == false
      utf8([0b11000010, 0b01000000]).valid_encoding?.should == false
      utf8([0b11000010, 0b11000000]).valid_encoding?.should == false
    end

    it "is not valid if is smaller than [xxxxxx10 xx000000] (codepoints < U+007F, that are encoded with the 1-byte format)" do
      utf8([0b11000000, 0b10111111]).valid_encoding?.should == false
      utf8([0b11000001, 0b10111111]).valid_encoding?.should == false
    end

    it "is not valid if the first byte is missing" do
      bytes = [0b11000010, 0b10000000]
      utf8(bytes[1..1]).valid_encoding?.should == false
    end

    it "is not valid if the second byte is missing" do
      bytes = [0b11000010, 0b10000000]
      utf8(bytes[0..0]).valid_encoding?.should == false
    end
  end

  describe "3-bytes character" do
    it "is valid if in format [1110xxxx 10xxxxxx 10xxxxxx]" do
      utf8([0b11100000, 0b10100000, 0b10000000]).valid_encoding?.should == true
      utf8([0b11100000, 0b10100000, 0b10111111]).valid_encoding?.should == true
      utf8([0b11100000, 0b10111111, 0b10111111]).valid_encoding?.should == true
      utf8([0b11101111, 0b10111111, 0b10111111]).valid_encoding?.should == true
    end

    it "is not valid if the first byte is not in format 1110xxxx" do
      utf8([0b00000000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b00010000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b00100000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b00110000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b01000000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b01010000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b01100000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b01110000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b10000000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b10010000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b10100000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b10110000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11000000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11010000, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10100000, 0b10000000]).valid_encoding?.should == true # correct bytes
      utf8([0b11110000, 0b10100000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if the second byte is not in format 10xxxxxx" do
      utf8([0b11100000, 0b00100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b01100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b11100000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if the third byte is not in format 10xxxxxx" do
      utf8([0b11100000, 0b10100000, 0b00000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10100000, 0b01000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10100000, 0b01000000]).valid_encoding?.should == false
    end

    it "is not valid if is smaller than [xxxx0000 xx100000 xx000000] (codepoints < U+07FF that are encoded with the 2-byte format)" do
      utf8([0b11100000, 0b10010000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10001000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10000100, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10000010, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10000001, 0b10000000]).valid_encoding?.should == false
      utf8([0b11100000, 0b10000000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if in range [xxxx1101 xx100000 xx000000] - [xxxx1101 xx111111 xx111111] (codepoints U+D800 - U+DFFF)" do
      utf8([0b11101101, 0b10100000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11101101, 0b10100000, 0b10000001]).valid_encoding?.should == false
      utf8([0b11101101, 0b10111111, 0b10111111]).valid_encoding?.should == false

      utf8([0b11101101, 0b10011111, 0b10111111]).valid_encoding?.should == true # lower boundary - 1
      utf8([0b11101110, 0b10000000, 0b10000000]).valid_encoding?.should == true # upper boundary + 1
    end

    it "is not valid if the first byte is missing" do
      bytes = [0b11100000, 0b10100000, 0b10000000]
      utf8(bytes[2..3]).valid_encoding?.should == false
    end

    it "is not valid if the second byte is missing" do
      bytes = [0b11100000, 0b10100000, 0b10000000]
      utf8([bytes[0], bytes[2]]).valid_encoding?.should == false
    end

    it "is not valid if the second and the third bytes are missing" do
      bytes = [0b11100000, 0b10100000, 0b10000000]
      utf8(bytes[0..0]).valid_encoding?.should == false
    end
  end

  describe "4-bytes character" do
    it "is valid if in format [11110xxx 10xxxxxx 10xxxxxx 10xxxxxx]" do
      utf8([0b11110000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == true
      utf8([0b11110000, 0b10010000, 0b10000000, 0b10111111]).valid_encoding?.should == true
      utf8([0b11110000, 0b10010000, 0b10111111, 0b10111111]).valid_encoding?.should == true
      utf8([0b11110000, 0b10111111, 0b10111111, 0b10111111]).valid_encoding?.should == true
      utf8([0b11110100, 0b10001111, 0b10111111, 0b10111111]).valid_encoding?.should == true
    end

    it "is not valid if the first byte is not in format 11110xxx" do
      utf8([0b11100000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11010000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b10110000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b01110000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if the second byte is not in format 10xxxxxx" do
      utf8([0b11110000, 0b00010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b01010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == true # correct bytes
      utf8([0b11110000, 0b11010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if the third byte is not in format 10xxxxxx" do
      utf8([0b11110000, 0b10010000, 0b00000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10010000, 0b01000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == true # correct bytes
      utf8([0b11110000, 0b10010000, 0b11000000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if the forth byte is not in format 10xxxxxx" do
      utf8([0b11110000, 0b10010000, 0b10000000, 0b00000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10010000, 0b10000000, 0b01000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == true # correct bytes
      utf8([0b11110000, 0b10010000, 0b10000000, 0b11000000]).valid_encoding?.should == false
    end

    it "is not valid if is smaller than [xxxxx000 xx001000 xx000000 xx000000] (codepoint < U+10000)" do
      utf8([0b11110000, 0b10000111, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000110, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000101, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000100, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000011, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000010, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000001, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110000, 0b10000000, 0b10000000, 0b10000000]).valid_encoding?.should == false
    end

    it "is not valid if is greater than [xxxxx100 xx001111 xx111111 xx111111] (codepoint > U+10FFFF)" do
      utf8([0b11110100, 0b10010000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110100, 0b10100000, 0b10000000, 0b10000000]).valid_encoding?.should == false
      utf8([0b11110100, 0b10110000, 0b10000000, 0b10000000]).valid_encoding?.should == false

      utf8([0b11110101, 0b10001111, 0b10111111, 0b10111111]).valid_encoding?.should == false
      utf8([0b11110110, 0b10001111, 0b10111111, 0b10111111]).valid_encoding?.should == false
      utf8([0b11110111, 0b10001111, 0b10111111, 0b10111111]).valid_encoding?.should == false
    end

    it "is not valid if the first byte is missing" do
      bytes = [0b11110000, 0b10010000, 0b10000000, 0b10000000]
      utf8(bytes[1..3]).valid_encoding?.should == false
    end

    it "is not valid if the second byte is missing" do
      bytes = [0b11110000, 0b10010000, 0b10000000, 0b10000000]
      utf8([bytes[0], bytes[2], bytes[3]]).valid_encoding?.should == false
    end

    it "is not valid if the second and the third bytes are missing" do
      bytes = [0b11110000, 0b10010000, 0b10000000, 0b10000000]
      utf8([bytes[0], bytes[3]]).valid_encoding?.should == false
    end

    it "is not valid if the second, the third and the fourth bytes are missing" do
      bytes = [0b11110000, 0b10010000, 0b10000000, 0b10000000]
      utf8(bytes[0..0]).valid_encoding?.should == false
    end
  end
end