zig/std/os/test.zig

66 lines
1.7 KiB
Zig
Raw Normal View History

const std = @import("../index.zig");
const os = std.os;
const assert = std.debug.assert;
const io = std.io;
const a = std.debug.global_allocator;
const builtin = @import("builtin");
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
test "makePath, put some files in it, deleteTree" {
try os.makePath(a, "os_test_tmp/b/c");
try io.writeFile(a, "os_test_tmp/b/c/file.txt", "nonsense");
try io.writeFile(a, "os_test_tmp/b/file2.txt", "blah");
try os.deleteTree(a, "os_test_tmp");
2018-03-29 01:23:44 -07:00
if (os.Dir.open(a, "os_test_tmp")) |dir| {
@panic("expected error");
2018-03-29 01:23:44 -07:00
} else |err| {
assert(err == error.PathNotFound);
2018-03-29 01:23:44 -07:00
}
}
2018-04-13 02:27:09 -07:00
test "access file" {
try os.makePath(a, "os_test_tmp");
if (os.File.access(a, "os_test_tmp/file.txt")) |ok| {
@panic("expected error");
2018-04-13 02:27:09 -07:00
} else |err| {
assert(err == error.NotFound);
}
try io.writeFile(a, "os_test_tmp/file.txt", "");
try os.File.access(a, "os_test_tmp/file.txt");
2018-04-13 02:27:09 -07:00
try os.deleteTree(a, "os_test_tmp");
}
test "spawn threads" {
var shared_ctx: i32 = 1;
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);
thread1.wait();
thread2.wait();
thread3.wait();
thread4.wait();
assert(shared_ctx == 4);
}
fn start1(ctx: void) u8 {
return 0;
}
fn start2(ctx: *i32) u8 {
_ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
return 0;
}
test "cpu count" {
const cpu_count = try std.os.cpuCount(a);
assert(cpu_count >= 1);
}