2018-03-04 21:57:02 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const os = std.os;
|
2018-04-03 21:08:10 -07:00
|
|
|
const assert = std.debug.assert;
|
2018-03-04 21:57:02 -08:00
|
|
|
const io = std.io;
|
|
|
|
|
|
|
|
const a = std.debug.global_allocator;
|
|
|
|
|
2018-04-03 21:08:10 -07:00
|
|
|
const builtin = @import("builtin");
|
2018-04-13 23:12:19 -07:00
|
|
|
const AtomicRmwOp = builtin.AtomicRmwOp;
|
|
|
|
const AtomicOrder = builtin.AtomicOrder;
|
2018-04-03 21:08:10 -07:00
|
|
|
|
2018-03-04 21:57:02 -08:00
|
|
|
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| {
|
2018-04-03 21:08:10 -07:00
|
|
|
@panic("expected error");
|
2018-03-29 01:23:44 -07:00
|
|
|
} else |err| {
|
2018-04-03 21:08:10 -07:00
|
|
|
assert(err == error.PathNotFound);
|
2018-03-29 01:23:44 -07:00
|
|
|
}
|
2018-03-04 21:57:02 -08:00
|
|
|
}
|
2018-04-13 02:27:09 -07:00
|
|
|
|
|
|
|
test "access file" {
|
|
|
|
try os.makePath(a, "os_test_tmp");
|
2018-07-22 21:35:53 -07:00
|
|
|
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", "");
|
2018-07-22 21:35:53 -07:00
|
|
|
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");
|
|
|
|
}
|
2018-04-13 23:12:19 -07:00
|
|
|
|
2018-08-06 13:48:49 -07:00
|
|
|
fn testThreadIdFn(thread_id: *os.Thread.Id) void {
|
|
|
|
thread_id.* = os.Thread.getCurrentId();
|
2018-08-02 14:44:20 -07:00
|
|
|
}
|
|
|
|
|
2018-08-06 13:48:49 -07:00
|
|
|
test "std.os.Thread.getCurrentId" {
|
|
|
|
var thread_current_id: os.Thread.Id = undefined;
|
|
|
|
const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
|
2018-08-06 14:30:55 -07:00
|
|
|
const thread_id = thread.handle();
|
2018-08-02 14:44:20 -07:00
|
|
|
thread.wait();
|
2018-08-06 14:25:24 -07:00
|
|
|
switch (builtin.os) {
|
|
|
|
builtin.Os.windows => assert(os.Thread.getCurrentId() != thread_current_id),
|
|
|
|
else => {
|
|
|
|
assert(thread_current_id == thread_id);
|
|
|
|
},
|
|
|
|
}
|
2018-08-02 14:44:20 -07:00
|
|
|
}
|
|
|
|
|
2018-04-13 23:12:19 -07:00
|
|
|
test "spawn threads" {
|
|
|
|
var shared_ctx: i32 = 1;
|
|
|
|
|
2018-04-28 23:40:22 -07: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-13 23:12:19 -07:00
|
|
|
|
|
|
|
thread1.wait();
|
|
|
|
thread2.wait();
|
|
|
|
thread3.wait();
|
|
|
|
thread4.wait();
|
|
|
|
|
|
|
|
assert(shared_ctx == 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start1(ctx: void) u8 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn start2(ctx: *i32) u8 {
|
2018-04-13 23:12:19 -07:00
|
|
|
_ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-06 22:23:18 -07:00
|
|
|
|
|
|
|
test "cpu count" {
|
|
|
|
const cpu_count = try std.os.cpuCount(a);
|
|
|
|
assert(cpu_count >= 1);
|
|
|
|
}
|