2018-03-05 05:57:02 +00:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const os = std.os;
|
2018-04-04 04:08:10 +00:00
|
|
|
const assert = std.debug.assert;
|
2018-03-05 05:57:02 +00:00
|
|
|
const io = std.io;
|
2018-10-01 17:43:25 +00:00
|
|
|
const mem = std.mem;
|
2018-03-05 05:57:02 +00:00
|
|
|
|
|
|
|
const a = std.debug.global_allocator;
|
|
|
|
|
2018-04-04 04:08:10 +00:00
|
|
|
const builtin = @import("builtin");
|
2018-04-14 06:12:19 +00:00
|
|
|
const AtomicRmwOp = builtin.AtomicRmwOp;
|
|
|
|
const AtomicOrder = builtin.AtomicOrder;
|
2018-04-04 04:08:10 +00:00
|
|
|
|
2018-03-05 05:57:02 +00:00
|
|
|
test "makePath, put some files in it, deleteTree" {
|
2018-08-21 20:07:28 +00:00
|
|
|
try os.makePath(a, "os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c");
|
|
|
|
try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c" ++ os.path.sep_str ++ "file.txt", "nonsense");
|
|
|
|
try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "file2.txt", "blah");
|
2018-03-05 05:57:02 +00:00
|
|
|
try os.deleteTree(a, "os_test_tmp");
|
2018-03-29 08:23:44 +00:00
|
|
|
if (os.Dir.open(a, "os_test_tmp")) |dir| {
|
2018-04-04 04:08:10 +00:00
|
|
|
@panic("expected error");
|
2018-03-29 08:23:44 +00:00
|
|
|
} else |err| {
|
2018-08-22 00:28:37 +00:00
|
|
|
assert(err == error.FileNotFound);
|
2018-03-29 08:23:44 +00:00
|
|
|
}
|
2018-03-05 05:57:02 +00:00
|
|
|
}
|
2018-04-13 09:27:09 +00:00
|
|
|
|
|
|
|
test "access file" {
|
|
|
|
try os.makePath(a, "os_test_tmp");
|
2018-08-21 20:07:28 +00:00
|
|
|
if (os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| {
|
2018-07-23 04:35:53 +00:00
|
|
|
@panic("expected error");
|
2018-04-13 09:27:09 +00:00
|
|
|
} else |err| {
|
2018-08-21 20:07:28 +00:00
|
|
|
assert(err == error.FileNotFound);
|
2018-04-13 09:27:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 20:07:28 +00:00
|
|
|
try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", "");
|
|
|
|
try os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt");
|
2018-04-13 09:27:09 +00:00
|
|
|
try os.deleteTree(a, "os_test_tmp");
|
|
|
|
}
|
2018-04-14 06:12:19 +00:00
|
|
|
|
2018-08-06 20:48:49 +00:00
|
|
|
fn testThreadIdFn(thread_id: *os.Thread.Id) void {
|
|
|
|
thread_id.* = os.Thread.getCurrentId();
|
2018-08-02 21:44:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 20:48:49 +00:00
|
|
|
test "std.os.Thread.getCurrentId" {
|
2019-02-01 22:49:29 +00:00
|
|
|
if (builtin.single_threaded) return error.SkipZigTest;
|
|
|
|
|
2018-08-06 20:48:49 +00:00
|
|
|
var thread_current_id: os.Thread.Id = undefined;
|
|
|
|
const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
|
2018-08-06 21:30:55 +00:00
|
|
|
const thread_id = thread.handle();
|
2018-08-02 21:44:20 +00:00
|
|
|
thread.wait();
|
2018-08-06 21:25:24 +00:00
|
|
|
switch (builtin.os) {
|
|
|
|
builtin.Os.windows => assert(os.Thread.getCurrentId() != thread_current_id),
|
|
|
|
else => {
|
|
|
|
assert(thread_current_id == thread_id);
|
|
|
|
},
|
|
|
|
}
|
2018-08-02 21:44:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 06:12:19 +00:00
|
|
|
test "spawn threads" {
|
2019-02-01 22:49:29 +00:00
|
|
|
if (builtin.single_threaded) return error.SkipZigTest;
|
|
|
|
|
2018-04-14 06:12:19 +00:00
|
|
|
var shared_ctx: i32 = 1;
|
|
|
|
|
2018-04-29 06:40:22 +00:00
|
|
|
const thread1 = try std.os.spawnThread({}, start1);
|
|
|
|
const thread2 = try std.os.spawnThread(&shared_ctx, start2);
|
|
|
|
const thread3 = try std.os.spawnThread(&shared_ctx, start2);
|
|
|
|
const thread4 = try std.os.spawnThread(&shared_ctx, start2);
|
2018-04-14 06:12:19 +00:00
|
|
|
|
|
|
|
thread1.wait();
|
|
|
|
thread2.wait();
|
|
|
|
thread3.wait();
|
|
|
|
thread4.wait();
|
|
|
|
|
|
|
|
assert(shared_ctx == 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start1(ctx: void) u8 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:56:59 +00:00
|
|
|
fn start2(ctx: *i32) u8 {
|
2018-04-14 06:12:19 +00:00
|
|
|
_ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-07 05:23:18 +00:00
|
|
|
|
|
|
|
test "cpu count" {
|
|
|
|
const cpu_count = try std.os.cpuCount(a);
|
|
|
|
assert(cpu_count >= 1);
|
|
|
|
}
|
2018-10-01 17:43:25 +00:00
|
|
|
|
|
|
|
test "AtomicFile" {
|
|
|
|
var buffer: [1024]u8 = undefined;
|
|
|
|
const allocator = &std.heap.FixedBufferAllocator.init(buffer[0..]).allocator;
|
|
|
|
const test_out_file = "tmp_atomic_file_test_dest.txt";
|
|
|
|
const test_content =
|
|
|
|
\\ hello!
|
|
|
|
\\ this is a test file
|
|
|
|
;
|
|
|
|
{
|
|
|
|
var af = try os.AtomicFile.init(test_out_file, os.File.default_mode);
|
|
|
|
defer af.deinit();
|
|
|
|
try af.file.write(test_content);
|
|
|
|
try af.finish();
|
|
|
|
}
|
|
|
|
const content = try io.readFileAlloc(allocator, test_out_file);
|
|
|
|
assert(mem.eql(u8, content, test_content));
|
|
|
|
|
|
|
|
try os.deleteFile(test_out_file);
|
|
|
|
}
|