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
|
function test_read(t)
t:are_equal(io.readfile("files/utf8bom-lf-eleof"), "123\\\n456\n789\n")
t:are_equal(io.readfile("files/utf8-crlf-neleof"), "123\\\n456\n789")
t:are_equal(io.readfile("files/utf8-crlf-neleof", {encoding = "binary"}), "123\\\r\n456\r\n789")
t:are_equal(io.readfile("files/utf8-crlf-neleof", {continuation = "\\"}), "123456\n789")
t:are_equal(io.readfile("files/utf16be-lf-eleof"), "123\\\n456\n789\n")
t:are_equal(io.readfile("files/utf16le-crlf-neleof"), "123\\\n456\n789")
local data1 = io.readfile("files/utf8-longline-neleof")
t:are_equal(#data1, 10000)
t:require(data1:endswith("1234567890"))
local data2 = io.readfile("files/utf8-longline-eleof")
t:are_equal(#data2, 10001)
t:require(data2:endswith("1234567890\n"))
end
function test_lines(t)
t:are_equal(table.to_array(io.lines("files/utf8bom-lf-eleof")), {"123\\", "456", "789"})
t:are_equal(table.to_array(io.lines("files/utf8-crlf-neleof")), {"123\\", "456", "789"})
t:are_equal(table.to_array(io.lines("files/utf8-crlf-neleof", {encoding = "binary"})), {"123\\\r\n", "456\r\n", "789"})
t:are_equal(table.to_array(io.lines("files/utf8-crlf-neleof", {continuation = "\\"})), {"123456", "789"})
t:are_equal(table.to_array(io.lines("files/utf16be-lf-eleof")), {"123\\", "456", "789"})
t:are_equal(table.to_array(io.lines("files/utf16le-crlf-neleof")), {"123\\", "456", "789"})
end
function test_readlines(t)
function get_all_keep_crlf(file, opt)
local fp = io.open(file, "r", opt)
local r = {}
while true do
local l = fp:read("L", opt)
if l == nil then break end
table.insert(r, l)
end
t:require(fp:close())
return r
end
function get_all_without_crlf(file, opt)
local r = {}
local fp = io.open(file, "r", opt)
for l in fp:lines(opt) do
table.insert(r, l)
end
t:require(fp:close())
return r
end
t:are_equal(get_all_keep_crlf("files/utf8bom-lf-eleof"), {"123\\\n", "456\n", "789\n"})
t:are_equal(get_all_keep_crlf("files/utf8-crlf-neleof"), {"123\\\n", "456\n", "789"})
t:are_equal(get_all_keep_crlf("files/utf8-crlf-neleof", {encoding = "binary"}), {"123\\\r\n", "456\r\n", "789"})
t:are_equal(get_all_keep_crlf("files/utf8-crlf-neleof", {continuation = "\\"}), {"123456\n", "789"})
t:are_equal(get_all_keep_crlf("files/utf16be-lf-eleof"), {"123\\\n", "456\n", "789\n"})
t:are_equal(get_all_keep_crlf("files/utf16le-crlf-neleof"), {"123\\\n", "456\n", "789"})
t:are_equal(get_all_without_crlf("files/utf8bom-lf-eleof"), {"123\\", "456", "789"})
t:are_equal(get_all_without_crlf("files/utf8-crlf-neleof"), {"123\\", "456", "789"})
t:are_equal(get_all_without_crlf("files/utf8-crlf-neleof", {encoding = "binary"}), {"123\\\r\n", "456\r\n", "789"})
t:are_equal(get_all_without_crlf("files/utf8-crlf-neleof", {continuation = "\\"}), {"123456", "789"})
t:are_equal(get_all_without_crlf("files/utf16be-lf-eleof"), {"123\\", "456", "789"})
t:are_equal(get_all_without_crlf("files/utf16le-crlf-neleof"), {"123\\", "456", "789"})
end
function test_prop(t)
t:are_equal(io.open("files/utf8bom-lf-eleof"):size(), 16)
t:are_equal(io.open("files/utf8-crlf-neleof"):size(), 14)
t:are_equal(io.open("files/utf16be-lf-eleof"):size(), 28)
t:are_equal(io.open("files/utf16le-crlf-neleof"):size(), 30)
t:are_equal(io.open("files/utf8bom-lf-eleof"):path(), path.absolute("files/utf8bom-lf-eleof"))
t:are_equal(io.open("files/utf8-crlf-neleof"):path(), path.absolute("files/utf8-crlf-neleof"))
t:are_equal(io.open("files/utf16be-lf-eleof"):path(), path.absolute("files/utf16be-lf-eleof"))
t:are_equal(io.open("files/utf16le-crlf-neleof"):path(), path.absolute("files/utf16le-crlf-neleof"))
end
function test_write(t)
function write(fname, opt)
local f = io.open(fname, "w", opt)
f:write(123, "abc", 456, "def", "\n")
f:close()
-- give encoding info
t:are_equal(io.readfile(fname, opt), "123abc456def\n")
-- auto detect encoding
t:are_equal(io.readfile(fname), "123abc456def\n")
end
write("temp/path/not/exist/utf8", {encoding = "utf8"})
write("temp/path/not/exist/utf16", {encoding = "utf16"})
write("temp/path/not/exist/utf16le", {encoding = "utf16le"})
write("temp/path/not/exist/utf16be", {encoding = "utf16be"})
os.tryrm("temp")
end
function test_convert(t)
local src = "files/utf8bom-lf-eleof"
local dst = "temp/convert_test.txt"
os.mkdir("temp")
-- utf8 to gbk (strip bom)
io.convert(src, dst, {from = "utf8", to = "gbk"})
local content = io.readfile(dst, {encoding = "binary"})
t:are_equal(content, "123\\\n456\n789\n")
-- gbk to utf8
local src_gbk = dst
local dst_utf8 = "temp/convert_test_utf8.txt"
io.convert(src_gbk, dst_utf8, {from = "gbk", to = "utf8"})
content = io.readfile(dst_utf8, {encoding = "binary"})
t:are_equal(content, "123\\\n456\n789\n")
-- utf8 to utf8bom
local dst_utf8bom = "temp/convert_test_utf8bom.txt"
io.convert(src, dst_utf8bom, {from = "utf8", to = "utf8bom"})
content = io.readfile(dst_utf8bom, {encoding = "binary"})
t:are_equal(content:sub(1, 3), "\239\187\191")
t:are_equal(content:sub(4), "123\\\n456\n789\n")
-- utf16le to utf8
local src_utf16le = "files/utf16le-crlf-neleof"
local dst_utf16le_to_utf8 = "temp/convert_test_utf16le_to_utf8.txt"
io.convert(src_utf16le, dst_utf16le_to_utf8, {from = "utf16le", to = "utf8"})
content = io.readfile(dst_utf16le_to_utf8, {encoding = "binary"})
t:are_equal(content, "123\\\r\n456\r\n789")
-- utf16be to utf8
local src_utf16be = "files/utf16be-lf-eleof"
local dst_utf16be_to_utf8 = "temp/convert_test_utf16be_to_utf8.txt"
io.convert(src_utf16be, dst_utf16be_to_utf8, {from = "utf16be", to = "utf8"})
content = io.readfile(dst_utf16be_to_utf8, {encoding = "binary"})
t:are_equal(content, "123\\\n456\n789\n")
-- utf8 to utf16le
local dst_utf8_to_utf16le = "temp/convert_test_utf8_to_utf16le.txt"
io.convert(src, dst_utf8_to_utf16le, {from = "utf8", to = "utf16le"})
content = io.readfile(dst_utf8_to_utf16le, {encoding = "binary"})
t:are_equal(content:sub(1, 2), "1\0")
-- utf8 to utf16be
local dst_utf8_to_utf16be = "temp/convert_test_utf8_to_utf16be.txt"
io.convert(src, dst_utf8_to_utf16be, {from = "utf8", to = "utf16be"})
content = io.readfile(dst_utf8_to_utf16be, {encoding = "binary"})
t:are_equal(content:sub(1, 2), "\0001")
-- utf8 to utf16lebom
local dst_utf8_to_utf16lebom = "temp/convert_test_utf8_to_utf16lebom.txt"
io.convert(src, dst_utf8_to_utf16lebom, {from = "utf8", to = "utf16lebom"})
content = io.readfile(dst_utf8_to_utf16lebom, {encoding = "binary"})
t:are_equal(content:sub(1, 2), "\255\254")
-- utf8 to utf16bom
local dst_utf8_to_utf16bom = "temp/convert_test_utf8_to_utf16bom.txt"
io.convert(src, dst_utf8_to_utf16bom, {from = "utf8", to = "utf16bom"})
content = io.readfile(dst_utf8_to_utf16bom, {encoding = "binary"})
local bom = content:sub(1, 2)
t:require(bom == "\255\254" or bom == "\254\255")
os.tryrm("temp")
end
function test_read_proc_cpuinfo(t)
if not is_host("linux") then
return t:skip("wrong host platform")
end
if not os.isfile("/proc/cpuinfo") then
return t:skip("missing /proc/cpuinfo")
end
local data = io.readfile("/proc/cpuinfo", {encoding = "binary"})
t:require(data and #data > 0)
local data2 = io.readfile("/proc/cpuinfo")
t:require(data2 and #data2 > 0)
end
|