| ... | @@ -1,4 +1,8 @@ | ... | @@ -1,4 +1,8 @@ |
| | 1 | const builtin = @import("builtin"); |
| | 2 | const native_endian = builtin.cpu.arch.endian(); |
| | 3 | |
| 1 | const std = @import("std"); | 4 | const std = @import("std"); |
| | 5 | const Io = std.Io; |
| 2 | const DefaultPrng = std.Random.DefaultPrng; | 6 | const DefaultPrng = std.Random.DefaultPrng; |
| 3 | const expect = std.testing.expect; | 7 | const expect = std.testing.expect; |
| 4 | const expectEqual = std.testing.expectEqual; | 8 | const expectEqual = std.testing.expectEqual; |
| ... | @@ -6,7 +10,6 @@ const expectError = std.testing.expectError; | ... | @@ -6,7 +10,6 @@ const expectError = std.testing.expectError; |
| 6 | const mem = std.mem; | 10 | const mem = std.mem; |
| 7 | const fs = std.fs; | 11 | const fs = std.fs; |
| 8 | const File = std.fs.File; | 12 | const File = std.fs.File; |
| 9 | const native_endian = @import("builtin").target.cpu.arch.endian(); | | |
| 10 | | 13 | |
| 11 | const tmpDir = std.testing.tmpDir; | 14 | const tmpDir = std.testing.tmpDir; |
| 12 | | 15 | |
| ... | @@ -123,3 +126,46 @@ test "updateTimes" { | ... | @@ -123,3 +126,46 @@ test "updateTimes" { |
| 123 | try expect(stat_new.atime.nanoseconds < stat_old.atime.nanoseconds); | 126 | try expect(stat_new.atime.nanoseconds < stat_old.atime.nanoseconds); |
| 124 | try expect(stat_new.mtime.nanoseconds < stat_old.mtime.nanoseconds); | 127 | try expect(stat_new.mtime.nanoseconds < stat_old.mtime.nanoseconds); |
| 125 | } | 128 | } |
| | 129 | |
| | 130 | test "Group" { |
| | 131 | const io = std.testing.io; |
| | 132 | |
| | 133 | var group: Io.Group = .init; |
| | 134 | var results: [2]usize = undefined; |
| | 135 | |
| | 136 | group.async(io, count, .{ 1, 10, &results[0] }); |
| | 137 | group.async(io, count, .{ 20, 30, &results[1] }); |
| | 138 | |
| | 139 | group.wait(io); |
| | 140 | |
| | 141 | try std.testing.expectEqualSlices(usize, &.{ 45, 245 }, &results); |
| | 142 | } |
| | 143 | |
| | 144 | fn count(a: usize, b: usize, result: *usize) void { |
| | 145 | var sum: usize = 0; |
| | 146 | for (a..b) |i| { |
| | 147 | sum += i; |
| | 148 | } |
| | 149 | result.* = sum; |
| | 150 | } |
| | 151 | |
| | 152 | test "Group cancellation" { |
| | 153 | const io = std.testing.io; |
| | 154 | |
| | 155 | var group: Io.Group = .init; |
| | 156 | var results: [2]usize = undefined; |
| | 157 | |
| | 158 | group.async(io, sleep, .{ io, &results[0] }); |
| | 159 | group.async(io, sleep, .{ io, &results[1] }); |
| | 160 | |
| | 161 | group.cancel(io); |
| | 162 | |
| | 163 | try std.testing.expectEqualSlices(usize, &.{ 1, 1 }, &results); |
| | 164 | } |
| | 165 | |
| | 166 | fn sleep(io: Io, result: *usize) void { |
| | 167 | // TODO when cancellation race bug is fixed, make this timeout much longer so that |
| | 168 | // it causes the unit test to be failed if not cancelled. |
| | 169 | io.sleep(.fromMilliseconds(1), .awake) catch {}; |
| | 170 | result.* = 1; |
| | 171 | } |