interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / utf8.lua

292 lines · 8273 bytes Raw Blame History
1-- $Id: testes/utf8.lua $
2-- See Copyright Notice in file lua.h
3
4-- UTF-8 file
5
6global <const> *
7
8print "testing UTF-8 library"
9
10local utf8 = require'utf8'
11
12
13local function checkerror (msg, f, ...)
14 local s, err = pcall(f, ...)
15 assert(not s and string.find(err, msg))
16end
17
18
19local function len (s)
20 return #string.gsub(s, "[\x80-\xBF]", "")
21end
22
23
24local justone = "^" .. utf8.charpattern .. "$"
25
26-- 't' is the list of codepoints of 's'
27local function checksyntax (s, t)
28 -- creates a string "return '\u{t[1]}...\u{t[n]}'"
29 local ts = {"return '"}
30 for i = 1, #t do ts[i + 1] = string.format("\\u{%x}", t[i]) end
31 ts[#t + 2] = "'"
32 ts = table.concat(ts)
33 -- its execution should result in 's'
34 assert(assert(load(ts))() == s)
35end
36
37assert(not utf8.offset("alo", 5))
38assert(not utf8.offset("alo", -4))
39
40-- 'check' makes several tests over the validity of string 's'.
41-- 't' is the list of codepoints of 's'.
42local function check (s, t, nonstrict)
43 local l = utf8.len(s, 1, -1, nonstrict)
44 assert(#t == l and len(s) == l)
45 assert(utf8.char(table.unpack(t)) == s) -- 't' and 's' are equivalent
46
47 assert(utf8.offset(s, 0) == 1)
48
49 checksyntax(s, t)
50
51 -- creates new table with all codepoints of 's'
52 local t1 = {utf8.codepoint(s, 1, -1, nonstrict)}
53 assert(#t == #t1)
54 for i = 1, #t do assert(t[i] == t1[i]) end -- 't' is equal to 't1'
55
56 for i = 1, l do -- for all codepoints
57 local pi, pie = utf8.offset(s, i) -- position of i-th char
58 local pi1 = utf8.offset(s, 2, pi) -- position of next char
59 assert(pi1 == pie + 1)
60 assert(string.find(string.sub(s, pi, pi1 - 1), justone))
61 assert(utf8.offset(s, -1, pi1) == pi)
62 assert(utf8.offset(s, i - l - 1) == pi)
63 assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict)))
64 for j = pi, pi1 - 1 do
65 local off1, off2 = utf8.offset(s, 0, j)
66 assert(off1 == pi and off2 == pi1 - 1)
67 end
68 for j = pi + 1, pi1 - 1 do
69 assert(not utf8.len(s, j))
70 end
71 assert(utf8.len(s, pi, pi, nonstrict) == 1)
72 assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1)
73 assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1)
74 assert(utf8.len(s, pi1, -1, nonstrict) == l - i)
75 assert(utf8.len(s, 1, pi, nonstrict) == i)
76 end
77
78 local expected = 1 -- expected position of "current" character
79 for i = 1, l + 1 do
80 local p, e = utf8.offset(s, i)
81 assert(p == expected)
82 expected = e + 1
83 end
84 assert(expected - 1 == #s + 1)
85
86 local i = 0
87 for p, c in utf8.codes(s, nonstrict) do
88 i = i + 1
89 assert(c == t[i] and p == utf8.offset(s, i))
90 assert(utf8.codepoint(s, p, p, nonstrict) == c)
91 end
92 assert(i == #t)
93
94 i = 0
95 for c in string.gmatch(s, utf8.charpattern) do
96 i = i + 1
97 assert(c == utf8.char(t[i]))
98 end
99 assert(i == #t)
100
101 for i = 1, l do
102 assert(utf8.offset(s, i) == utf8.offset(s, i - l - 1, #s + 1))
103 end
104
105end
106
107
108do -- error indication in utf8.len
109 local function checklen (s, p)
110 local a, b = utf8.len(s)
111 assert(not a and b == p)
112 end
113 checklen("abc\xE3def", 4)
114 checklen("\xF4\x9F\xBF", 1)
115 checklen("\xF4\x9F\xBF\xBF", 1)
116 -- spurious continuation bytes
117 checklen("汉字\x80", #("汉字") + 1)
118 checklen("\x80hello", 1)
119 checklen("hel\x80lo", 4)
120 checklen("汉字\xBF", #("汉字") + 1)
121 checklen("\xBFhello", 1)
122 checklen("hel\xBFlo", 4)
123end
124
125-- errors in utf8.codes
126do
127 local function errorcodes (s)
128 checkerror("invalid UTF%-8 code",
129 function ()
130 for c in utf8.codes(s) do assert(c) end
131 end)
132 end
133 errorcodes("ab\xff")
134 errorcodes("\u{110000}")
135 errorcodes("in\x80valid")
136 errorcodes("\xbfinvalid")
137 errorcodes("αλφ\xBFα")
138
139 -- calling iteration function with invalid arguments
140 local f = utf8.codes("")
141 assert(f("", 2) == nil)
142 assert(f("", -1) == nil)
143 assert(f("", math.mininteger) == nil)
144
145end
146
147-- error in initial position for offset
148checkerror("position out of bounds", utf8.offset, "abc", 1, 5)
149checkerror("position out of bounds", utf8.offset, "abc", 1, -4)
150checkerror("position out of bounds", utf8.offset, "", 1, 2)
151checkerror("position out of bounds", utf8.offset, "", 1, -1)
152checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
153checkerror("continuation byte", utf8.offset, "𦧺", 1, 2)
154checkerror("continuation byte", utf8.offset, "\x80", 1)
155checkerror("continuation byte", utf8.offset, "\x9c", -1)
156
157-- error in indices for len
158checkerror("out of bounds", utf8.len, "abc", 0, 2)
159checkerror("out of bounds", utf8.len, "abc", 1, 4)
160
161do -- missing continuation bytes
162 -- get what is available
163 local p, e = utf8.offset("\xE0", 1)
164 assert(p == 1 and e == 1)
165 local p, e = utf8.offset("\xE0\x9e", -1)
166 assert(p == 1 and e == 2)
167end
168
169
170local s = "hello World"
171local t = {string.byte(s, 1, -1)}
172for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end
173check(s, t)
174
175check("汉字/漢字", {27721, 23383, 47, 28450, 23383,})
176
177do
178 local s = "áéí\128"
179 local t = {utf8.codepoint(s,1,#s - 1)}
180 assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237)
181 checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s)
182 checkerror("out of bounds", utf8.codepoint, s, #s + 1)
183 t = {utf8.codepoint(s, 4, 3)}
184 assert(#t == 0)
185 checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1)
186 checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1)
187 -- surrogates
188 assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1)
189 assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1)
190 assert(utf8.codepoint("\u{D800}", 1, 1, true) == 0xD800)
191 assert(utf8.codepoint("\u{DFFF}", 1, 1, true) == 0xDFFF)
192 assert(utf8.codepoint("\u{7FFFFFFF}", 1, 1, true) == 0x7FFFFFFF)
193end
194
195assert(utf8.char() == "")
196assert(utf8.char(0, 97, 98, 99, 1) == "\0abc\1")
197
198assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF)
199assert(utf8.codepoint(utf8.char(0x7FFFFFFF), 1, 1, true) == (1<<31) - 1)
200
201checkerror("value out of range", utf8.char, 0x7FFFFFFF + 1)
202checkerror("value out of range", utf8.char, -1)
203
204local function invalid (s)
205 checkerror("invalid UTF%-8 code", utf8.codepoint, s)
206 assert(not utf8.len(s))
207end
208
209-- UTF-8 representation for 0x11ffff (value out of valid range)
210invalid("\xF4\x9F\xBF\xBF")
211
212-- surrogates
213invalid("\u{D800}")
214invalid("\u{DFFF}")
215
216-- overlong sequences
217invalid("\xC0\x80") -- zero
218invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte)
219invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes)
220invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes)
221
222
223-- invalid bytes
224invalid("\x80") -- continuation byte
225invalid("\xBF") -- continuation byte
226invalid("\xFE") -- invalid byte
227invalid("\xFF") -- invalid byte
228
229
230-- empty string
231check("", {})
232
233-- minimum and maximum values for each sequence size
234s = "\0 \x7F\z
235 \xC2\x80 \xDF\xBF\z
236 \xE0\xA0\x80 \xEF\xBF\xBF\z
237 \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF"
238s = string.gsub(s, " ", "")
239check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF})
240
241
242-- again, without strictness
243s = "\xF0\x90\x80\x80 \xF7\xBF\xBF\xBF\z
244 \xF8\x88\x80\x80\x80 \xFB\xBF\xBF\xBF\xBF\z
245 \xFC\x84\x80\x80\x80\x80 \xFD\xBF\xBF\xBF\xBF\xBF"
246s = string.gsub(s, " ", "")
247check(s, {0x10000,0x1FFFFF, 0x200000,0x3FFFFFF, 0x4000000,0x7FFFFFFF}, true)
248
249do
250 -- original UTF-8 values
251 local s = "\u{4000000}\u{7FFFFFFF}"
252 assert(s == "\xFC\x84\x80\x80\x80\x80\xFD\xBF\xBF\xBF\xBF\xBF")
253 check(s, {0x4000000, 0x7FFFFFFF}, true)
254
255 s = "\u{200000}\u{3FFFFFF}"
256 assert(#s == 10)
257 check(s, {0x200000, 0x3FFFFFF}, true)
258
259 s = "\u{10000}\u{1fffff}"
260 assert(#s == 8)
261 check(s, {0x10000, 0x1FFFFF}, true)
262end
263
264local x = "日本語a-4\0éó"
265check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})
266
267
268-- more than 5 continuation bytes
269assert(not utf8.len("\xff\x8f\x8f\x8f\x8f\x8f\x8f\x8f"))
270
271
272-- Supplementary Characters
273check("𣲷𠜎𠱓𡁻𠵼ab𠺢",
274 {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,})
275
276check("𨳊𩶘𦧺𨳒𥄫𤓓\xF4\x8F\xBF\xBF",
277 {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff})
278
279
280local i = 0
281for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do
282 i = i + 1
283 assert(utf8.offset(x, i) == p)
284 assert(utf8.len(x, p) == utf8.len(x) - i + 1)
285 assert(utf8.len(c) == 1)
286 for j = 1, #c - 1 do
287 assert(utf8.offset(x, 0, p + j - 1) == p)
288 end
289end
290
291print'ok'
292