2017-11-10 11:02:45 -08:00
|
|
|
const std = @import("index.zig");
|
|
|
|
const io = std.io;
|
2018-03-29 05:36:04 -07:00
|
|
|
const DefaultPrng = std.rand.DefaultPrng;
|
2017-11-10 11:02:45 -08:00
|
|
|
const assert = std.debug.assert;
|
2018-07-29 11:52:10 -07:00
|
|
|
const assertError = std.debug.assertError;
|
2017-11-10 11:02:45 -08:00
|
|
|
const mem = std.mem;
|
|
|
|
const os = std.os;
|
2017-11-10 14:08:11 -08:00
|
|
|
const builtin = @import("builtin");
|
2017-11-10 11:02:45 -08:00
|
|
|
|
|
|
|
test "write a file, read it, then delete it" {
|
2018-05-11 11:08:16 -07:00
|
|
|
var raw_bytes: [200 * 1024]u8 = undefined;
|
|
|
|
var allocator = &std.heap.FixedBufferAllocator.init(raw_bytes[0..]).allocator;
|
|
|
|
|
2017-11-10 11:02:45 -08:00
|
|
|
var data: [1024]u8 = undefined;
|
2018-03-29 05:36:04 -07:00
|
|
|
var prng = DefaultPrng.init(1234);
|
|
|
|
prng.random.bytes(data[0..]);
|
2017-11-10 11:02:45 -08:00
|
|
|
const tmp_file_name = "temp_test_file.txt";
|
|
|
|
{
|
2018-08-20 21:46:42 -07:00
|
|
|
var file = try os.File.openWrite(tmp_file_name);
|
2017-11-10 11:02:45 -08:00
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
var file_out_stream = io.FileOutStream.init(&file);
|
2018-02-05 15:09:13 -08:00
|
|
|
var buf_stream = io.BufferedOutStream(io.FileOutStream.Error).init(&file_out_stream.stream);
|
2017-11-10 11:02:45 -08:00
|
|
|
const st = &buf_stream.stream;
|
2018-01-08 21:07:01 -08:00
|
|
|
try st.print("begin");
|
|
|
|
try st.write(data[0..]);
|
|
|
|
try st.print("end");
|
|
|
|
try buf_stream.flush();
|
2017-11-10 11:02:45 -08:00
|
|
|
}
|
|
|
|
{
|
2018-08-19 21:40:14 -07:00
|
|
|
var file = try os.File.openRead(tmp_file_name);
|
2017-11-10 11:02:45 -08:00
|
|
|
defer file.close();
|
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
const file_size = try file.getEndPos();
|
2017-11-10 11:02:45 -08:00
|
|
|
const expected_file_size = "begin".len + data.len + "end".len;
|
|
|
|
assert(file_size == expected_file_size);
|
|
|
|
|
|
|
|
var file_in_stream = io.FileInStream.init(&file);
|
2018-02-05 15:09:13 -08:00
|
|
|
var buf_stream = io.BufferedInStream(io.FileInStream.Error).init(&file_in_stream.stream);
|
2017-11-10 11:02:45 -08:00
|
|
|
const st = &buf_stream.stream;
|
2018-01-08 21:07:01 -08:00
|
|
|
const contents = try st.readAllAlloc(allocator, 2 * 1024);
|
2017-11-10 11:02:45 -08:00
|
|
|
defer allocator.free(contents);
|
|
|
|
|
|
|
|
assert(mem.eql(u8, contents[0.."begin".len], "begin"));
|
2018-05-30 13:09:11 -07:00
|
|
|
assert(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data));
|
|
|
|
assert(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
|
2017-11-10 11:02:45 -08:00
|
|
|
}
|
2018-01-08 21:07:01 -08:00
|
|
|
try os.deleteFile(allocator, tmp_file_name);
|
2017-11-10 11:02:45 -08:00
|
|
|
}
|
2018-05-11 11:08:16 -07:00
|
|
|
|
|
|
|
test "BufferOutStream" {
|
|
|
|
var bytes: [100]u8 = undefined;
|
|
|
|
var allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator;
|
|
|
|
|
|
|
|
var buffer = try std.Buffer.initSize(allocator, 0);
|
|
|
|
var buf_stream = &std.io.BufferOutStream.init(&buffer).stream;
|
|
|
|
|
|
|
|
const x: i32 = 42;
|
|
|
|
const y: i32 = 1234;
|
|
|
|
try buf_stream.print("x: {}\ny: {}\n", x, y);
|
|
|
|
|
|
|
|
assert(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n"));
|
|
|
|
}
|
2018-07-23 23:24:53 -07:00
|
|
|
|
2018-07-29 11:52:10 -07:00
|
|
|
test "SliceInStream" {
|
2018-08-20 21:46:42 -07:00
|
|
|
const bytes = []const u8{ 1, 2, 3, 4, 5, 6, 7 };
|
2018-07-29 11:52:10 -07:00
|
|
|
var ss = io.SliceInStream.init(bytes);
|
2018-07-23 23:24:53 -07:00
|
|
|
|
|
|
|
var dest: [4]u8 = undefined;
|
|
|
|
|
|
|
|
var read = try ss.stream.read(dest[0..4]);
|
|
|
|
assert(read == 4);
|
|
|
|
assert(mem.eql(u8, dest[0..4], bytes[0..4]));
|
|
|
|
|
|
|
|
read = try ss.stream.read(dest[0..4]);
|
|
|
|
assert(read == 3);
|
|
|
|
assert(mem.eql(u8, dest[0..3], bytes[4..7]));
|
|
|
|
|
|
|
|
read = try ss.stream.read(dest[0..4]);
|
|
|
|
assert(read == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "PeekStream" {
|
2018-08-20 21:46:42 -07:00
|
|
|
const bytes = []const u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
|
2018-07-29 11:52:10 -07:00
|
|
|
var ss = io.SliceInStream.init(bytes);
|
|
|
|
var ps = io.PeekStream(2, io.SliceInStream.Error).init(&ss.stream);
|
2018-07-23 23:24:53 -07:00
|
|
|
|
|
|
|
var dest: [4]u8 = undefined;
|
|
|
|
|
|
|
|
ps.putBackByte(9);
|
|
|
|
ps.putBackByte(10);
|
|
|
|
|
|
|
|
var read = try ps.stream.read(dest[0..4]);
|
|
|
|
assert(read == 4);
|
|
|
|
assert(dest[0] == 10);
|
|
|
|
assert(dest[1] == 9);
|
|
|
|
assert(mem.eql(u8, dest[2..4], bytes[0..2]));
|
|
|
|
|
|
|
|
read = try ps.stream.read(dest[0..4]);
|
|
|
|
assert(read == 4);
|
|
|
|
assert(mem.eql(u8, dest[0..4], bytes[2..6]));
|
|
|
|
|
|
|
|
read = try ps.stream.read(dest[0..4]);
|
|
|
|
assert(read == 2);
|
|
|
|
assert(mem.eql(u8, dest[0..2], bytes[6..8]));
|
|
|
|
|
|
|
|
ps.putBackByte(11);
|
|
|
|
ps.putBackByte(12);
|
|
|
|
|
|
|
|
read = try ps.stream.read(dest[0..4]);
|
|
|
|
assert(read == 2);
|
|
|
|
assert(dest[0] == 12);
|
|
|
|
assert(dest[1] == 11);
|
|
|
|
}
|
2018-07-29 11:52:10 -07:00
|
|
|
|
|
|
|
test "SliceOutStream" {
|
|
|
|
var buffer: [10]u8 = undefined;
|
|
|
|
var ss = io.SliceOutStream.init(buffer[0..]);
|
|
|
|
|
|
|
|
try ss.stream.write("Hello");
|
|
|
|
assert(mem.eql(u8, ss.getWritten(), "Hello"));
|
|
|
|
|
|
|
|
try ss.stream.write("world");
|
|
|
|
assert(mem.eql(u8, ss.getWritten(), "Helloworld"));
|
|
|
|
|
|
|
|
assertError(ss.stream.write("!"), error.OutOfSpace);
|
|
|
|
assert(mem.eql(u8, ss.getWritten(), "Helloworld"));
|
|
|
|
|
|
|
|
ss.reset();
|
|
|
|
assert(ss.getWritten().len == 0);
|
|
|
|
|
|
|
|
assertError(ss.stream.write("Hello world!"), error.OutOfSpace);
|
|
|
|
assert(mem.eql(u8, ss.getWritten(), "Hello worl"));
|
|
|
|
}
|