2017-11-10 11:02:45 -08:00
|
|
|
const std = @import("index.zig");
|
|
|
|
const io = std.io;
|
|
|
|
const allocator = std.debug.global_allocator;
|
|
|
|
const Rand = std.rand.Rand;
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
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" {
|
|
|
|
var data: [1024]u8 = undefined;
|
|
|
|
var rng = Rand.init(1234);
|
|
|
|
rng.fillBytes(data[0..]);
|
|
|
|
const tmp_file_name = "temp_test_file.txt";
|
|
|
|
{
|
2018-01-08 21:07:01 -08:00
|
|
|
var file = try io.File.openWrite(tmp_file_name, allocator);
|
2017-11-10 11:02:45 -08:00
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
var file_out_stream = io.FileOutStream.init(&file);
|
|
|
|
var buf_stream = io.BufferedOutStream.init(&file_out_stream.stream);
|
|
|
|
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-01-08 21:07:01 -08:00
|
|
|
var file = try io.File.openRead(tmp_file_name, allocator);
|
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);
|
|
|
|
var buf_stream = io.BufferedInStream.init(&file_in_stream.stream);
|
|
|
|
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"));
|
|
|
|
assert(mem.eql(u8, contents["begin".len..contents.len - "end".len], data));
|
|
|
|
assert(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
|
|
|
|
}
|
2018-01-08 21:07:01 -08:00
|
|
|
try os.deleteFile(allocator, tmp_file_name);
|
2017-11-10 11:02:45 -08:00
|
|
|
}
|