local device = file.create_memory_device() local function path(name) return device .. ":" .. name end local function eq_list(actual, expected, msg) asserts.equals(#actual, #expected, (msg or "list") .. " length") for i = 1, #expected do asserts.equals(actual[i], expected[i], (msg or "list") .. "[" .. i .. "]") end end file.write(path("test.txt"), "Hello\nWorld") file.write_bytes(path("test.bin"), Bytearray({20, 30, 100, 200, 255})) do local stream = file.open(path("test.txt"), 'r') asserts.equals(stream:read_line(), "Hello") asserts.equals(stream:read_line(), "World") stream:close() end do local stream = file.open(path("test.bin"), 'rb') local data = stream:read(5) asserts.equals(data[1], 20); asserts.equals(data[2], 30); asserts.equals(data[3], 100); asserts.equals(data[4], 200); asserts .equals(data[5], 255) stream:close() end do local stream = file.open(path("test.txt"), 'r') assert(stream:is_alive()) assert(not stream:is_closed()) stream:close() assert(not stream:is_alive()) assert(stream:is_closed()) end do local stream = file.open(path("test.txt"), 'r') stream:seek('b', 6) asserts.equals(stream:read_line(), "World") stream:close() end do local stream = file.open(path("test.txt"), 'r') stream:seek('e', 0) asserts.equals(stream:tell(), 11) stream:close() end ------------------------------------------------------------------------------- -- text: line endings and end of stream ------------------------------------------------------------------------------- do local stream = file.open(path("test.txt"), 'r') stream:read_line(); stream:read_line() asserts.equals(stream:read_line(), nil, "after last line") asserts.equals(stream:read_line(), nil, "after last line, again") stream:close() end do file.write(path("empty.txt"), "") local stream = file.open(path("empty.txt"), 'r') asserts.equals(stream:read_line(), nil, "empty file") stream:close() end do file.write(path("crlf.txt"), "a\r\nb\rc\nd\r") local stream = file.open(path("crlf.txt"), 'r') asserts.equals(stream:read_line(), "a", "CRLF") asserts.equals(stream:read_line(), "b\rc", "lone CR is kept") asserts.equals(stream:read_line(), "d\r", "CR at end of stream") asserts.equals(stream:read_line(), nil) stream:close() end ------------------------------------------------------------------------------- -- text: read(count) and read_fully ------------------------------------------------------------------------------- do file.write(path("blank.txt"), "\n\nA\nB\n\n") local stream = file.open(path("blank.txt"), 'r') eq_list(stream:read(10), {"A", "B"}, "trimmed") stream:close() end do local stream = file.open(path("blank.txt"), 'r') eq_list(stream:read(10, false), {"", "", "A", "B", ""}, "untrimmed") stream:close() end do file.write(path("only_blank.txt"), "\n\n\n") local stream = file.open(path("only_blank.txt"), 'r') eq_list(stream:read(5), {}, "only empty lines") stream:close() end do local stream = file.open(path("test.txt"), 'r') eq_list(stream:read(5), {"Hello", "World"}, "past end of stream") stream:close() end do local stream = file.open(path("test.txt"), 'r') assert(not pcall(stream.read, stream, -1), "negative count must fail") stream:close() end do local stream = file.open(path("test.txt"), 'r') asserts.equals(stream:read(), "Hello") stream:close() end do local stream = file.open(path("test.txt"), 'r') asserts.equals(stream:read_fully(), "Hello\nWorld", "read_fully as string") stream:close() end do local stream = file.open(path("test.txt"), 'r') eq_list(stream:read_fully(true), {"Hello", "World"}, "read_fully as lines") stream:close() end ------------------------------------------------------------------------------- -- text: writing ------------------------------------------------------------------------------- do local stream = file.open(path("out.txt"), 'w') stream:write("one") stream:write({"two", "three"}) stream:write_line("four") stream:close() asserts.equals(file.read(path("out.txt")), "one\ntwo\nthree\nfour\n", "text write") end do local stream = file.open(path("out.txt"), 'w') assert(not pcall(stream.write, stream, 42), "writing a number in text mode must fail") stream:close() end ------------------------------------------------------------------------------- -- binary ------------------------------------------------------------------------------- do local stream = file.open(path("test.bin"), 'rb') eq_list(stream:read(5, true), {20, 30, 100, 200, 255}, "read as table") stream:close() end do local stream = file.open(path("test.bin"), 'rb') stream:read(3) asserts.equals(#stream:read(10), 2, "short read") asserts.equals(#stream:read(10), 0, "read at end of stream") stream:close() end do local stream = file.open(path("test.bin"), 'rb') local data = stream:read_fully() asserts.equals(#data, 5); asserts.equals(data[1], 20); asserts.equals(data[5], 255) stream:close() end do local stream = file.open(path("test.bin"), 'rb') eq_list(stream:read_fully(true), {20, 30, 100, 200, 255}, "read_fully as table") stream:close() end do local stream = file.open(path("out.bin"), 'wb') stream:write({1, 2, 3}) stream:write(Bytearray({4, 5})) stream:close() local data = file.read_bytes(path("out.bin")) asserts.equals(#data, 5, "binary write length") for i = 1, 5 do asserts.equals(data[i], i, "binary write byte " .. i) end end do local stream = file.open(path("fmt.bin"), 'wb') stream:write("