Skip to content

Commit d6ca10e

Browse files
committed
xxx
1 parent be12012 commit d6ca10e

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

tests/lapi/io_torture_test.lua

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ file:setvbuf(mode [, size])
1919
]]
2020

2121
local luzer = require("luzer")
22+
local test_lib = require("lib")
23+
2224
-- The maximum file size is 1Mb (1000 * 1000).
2325
local MAX_N = 1e3
2426

2527
local function io_seek(self)
26-
local SEEK_MODE = { "set", "cur", "end" }
28+
local SEEK_MODE = {
29+
"set", -- Base is position 0 (beginning of the file).
30+
"cur", -- Base is current position.
31+
"end", -- Base is end of file.
32+
}
2733
local mode = self.fdp:oneof(SEEK_MODE)
2834
local offset = self.fdp:consume_integer(0, self.MAX_N)
2935
self.fh:seek(mode, offset)
@@ -34,7 +40,11 @@ local function io_flush(self)
3440
end
3541

3642
local function io_setvbuf(self)
37-
local VBUF_MODE = { "no", "full", "line" }
43+
local VBUF_MODE = {
44+
"no", -- no buffering.
45+
"full", -- full buffering.
46+
"line", -- line buffering.
47+
}
3848
local mode = self.fdp:oneof(VBUF_MODE)
3949
local size = self.fdp:consume_integer(0, self.MAX_N)
4050
self.fh:setvbuf(mode, size)
@@ -46,13 +56,39 @@ local function io_write(self)
4656
end
4757

4858
local function io_read(self)
49-
-- As a special case, `io.read(0)` works as a test for end of
50-
-- file: It returns an empty string if there is more to be
51-
-- read or `nil` otherwise.
52-
local size = self.fdp:consume_integer(0, self.MAX_N)
53-
local READ_MODE = { "*number", "*all", "*line", size }
54-
local mode = self.fdp:oneof(READ_MODE)
55-
local _ = self.fh:read(mode)
59+
local n_formats = self.fdp:consume_integer(1, self.MAX_N)
60+
local READ_FORMAT = {
61+
"*n", -- Reads a number.
62+
"*a", -- Reads the whole file, starting at the current
63+
-- position.
64+
"*l", -- Reads the next line (skipping the end of line),
65+
-- returning nil on end of file.
66+
}
67+
if test_lib.lua_current_version_ge_than(5, 2) then
68+
-- "*L" reads the next line keeping the end of line
69+
-- (if present), returning nil on end of file.
70+
table.insert(READ_FORMAT, "*L")
71+
end
72+
-- Build a table with formats, which specify what to read. For
73+
-- each format, the function returns a string (or a number)
74+
-- with the characters read, or `nil` if it cannot read data
75+
-- with the specified format. When called without formats, it
76+
-- uses a default format that reads the entire next line.
77+
local read_formats = {}
78+
for _ = 1, n_formats do
79+
local format_is_size = self.fdp:consume_boolean()
80+
if format_is_size then
81+
-- As a special case, `io.read(0)` works as a test for
82+
-- end of file: It returns an empty string if there is
83+
-- more to be read or `nil` otherwise.
84+
local size = self.fdp:consume_integer(0, test_lib.MAX_INT64)
85+
table.insert(read_formats, size)
86+
else
87+
local format = self.fdp:oneof(READ_FORMAT)
88+
-- table.insert(read_formats, format)
89+
end
90+
end
91+
local _ = self.fh:read(unpack(read_formats))
5692
end
5793

5894
local function io_close(self)

0 commit comments

Comments
 (0)