zig/test/cases/coroutines.zig

44 lines
783 B
Zig
Raw Normal View History

const std = @import("std");
const assert = std.debug.assert;
var x: i32 = 1;
test "create a coroutine and cancel it" {
const p = try (async(std.debug.global_allocator) simpleAsyncFn());
cancel p;
assert(x == 2);
}
async fn simpleAsyncFn() void {
x += 1;
suspend;
x += 1;
}
2018-02-28 19:18:48 -08:00
test "coroutine suspend, resume, cancel" {
seq('a');
const p = (async(std.debug.global_allocator) testAsyncSeq()) catch unreachable;
seq('c');
resume p;
seq('f');
cancel p;
seq('g');
assert(std.mem.eql(u8, points, "abcdefg"));
}
async fn testAsyncSeq() void {
defer seq('e');
seq('b');
suspend;
seq('d');
}
var points = []u8{0} ** "abcdefg".len;
var index: usize = 0;
fn seq(c: u8) void {
points[index] = c;
index += 1;
}