summaryrefslogtreecommitdiff
path: root/source/tests/async_function.test.c
blob: 35ce0ce636bedd934615110fc4155d23c0f8eef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "../kit/async_function.h"

#define KIT_TEST_FILE async_function
#include "../kit_test/test.h"

AF_STATE(int, test_foo, );
static AF_DECL(test_foo);

CORO_IMPL(test_foo) {
  AF_RETURN(42);
}
CORO_END

AF_STATE(int, test_bar, );
static AF_DECL(test_bar);

CORO_IMPL(test_bar) {
  AF_YIELD_VOID;
  AF_RETURN(42);
}
CORO_END

STATIC_CORO(int, test_gen, int i; int min; int max;) {
  for (self->i = self->min; self->i < self->max; self->i++)
    AF_YIELD(self->i);
  AF_RETURN(self->max);
}
CORO_END

STATIC_CORO_VOID(test_task, ) {
  AF_YIELD_VOID;
  AF_YIELD_VOID;
  AF_RETURN_VOID;
}
CORO_END

STATIC_CORO_VOID(test_nest_task, AF_TYPE(test_task) promise;) {
  AF_INIT(self->promise, test_task, );
  AF_AWAIT(self->promise);
  AF_AWAIT(self->promise);
  AF_AWAIT(self->promise);
}
CORO_END

STATIC_CORO(int, test_nest_generator, AF_TYPE(test_gen) promise;) {
  AF_INIT(self->promise, test_gen, .min = 1, .max = 3);
  AF_YIELD_AWAIT(self->promise);
}
CORO_END

TEST("coroutine create") {
  AF_CREATE(promise, test_foo, );
  REQUIRE(!AF_FINISHED(promise));
}

TEST("coroutine init") {
  AF_TYPE(test_foo) promise;
  AF_INIT(promise, test_foo, );
  REQUIRE(!AF_FINISHED(promise));
}

TEST("coroutine init with value") {
  AF_TYPE(test_foo) promise;
  AF_INIT(promise, test_foo, .return_value = 42);
  REQUIRE(promise.return_value == 42);
  REQUIRE(!AF_FINISHED(promise));
}

TEST("coroutine create with value") {
  AF_CREATE(promise, test_foo, .return_value = -1);
  REQUIRE(promise.return_value == -1);
  REQUIRE(!AF_FINISHED(promise));
}

TEST("coroutine execute and return") {
  AF_CREATE(promise, test_foo, );
  REQUIRE(AF_NEXT(promise) == 42);
  REQUIRE(AF_FINISHED(promise));
}

TEST("coroutine execute two steps") {
  AF_CREATE(promise, test_bar, .return_value = 0);
  AF_EXECUTE(promise);
  REQUIRE(promise.return_value == 0);
  AF_EXECUTE(promise);
  REQUIRE(promise.return_value == 42);
}

TEST("coroutine generator") {
  int i;
  AF_CREATE(promise, test_gen, .min = 10, .max = 15);
  for (i = 0; i <= 5; i++) REQUIRE(AF_NEXT(promise) == 10 + i);
}

TEST("coroutine status finished") {
  AF_CREATE(promise, test_bar, );
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(AF_FINISHED(promise));
}

TEST("coroutine task") {
  AF_CREATE(promise, test_task, );
  AF_EXECUTE(promise);
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(AF_FINISHED(promise));
}

TEST("coroutine nested task") {
  AF_CREATE(promise, test_nest_task, );
  AF_EXECUTE(promise);
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(AF_FINISHED(promise));
}

TEST("coroutine nested generator") {
  AF_CREATE(promise, test_nest_generator, );
  REQUIRE(AF_NEXT(promise) == 1);
  REQUIRE(AF_NEXT(promise) == 2);
  REQUIRE(AF_NEXT(promise) == 3);
  REQUIRE(!AF_FINISHED(promise));
  AF_EXECUTE(promise);
  REQUIRE(AF_FINISHED(promise));
}