<commit_before>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;
}

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;
}<commit_msg>add test for coroutine suspend with block<commit_after>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;
}

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;
}

test "coroutine suspend with block" {
    const p = (async(std.debug.global_allocator) testSuspendBlock()) catch unreachable;
    std.debug.assert(!result);
    resume a_promise;
    std.debug.assert(result);
    cancel p;
}

var a_promise: promise = undefined;
var result = false;

async fn testSuspendBlock() void {
    suspend |p| {
        a_promise = p;
    }
    result = true;
}
{'fraction_non_alphanumeric': 0.16232169208066896, 'fraction_numerical': 0.011805213969503197, 'mean_word_length': 3.043737574552684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5970746', 'n_tokens_mistral': 757, 'n_tokens_neox': 753, 'n_words': 204}