-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.cpp
87 lines (75 loc) · 1.66 KB
/
tests.cpp
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
// Copyright 2013 A. Douglas Gale
// Permission is granted to use the fastcoroutine implementation
// for any use (including commercial use), provided this copyright
// notice is present in your product's source code.
// tests.cpp
#include "fastcoroutine.h"
#include <iostream>
using namespace FastCoroutine;
void EmptyCoroutine(YieldBuffer<int> &)
{
}
void TypicalCoroutine(YieldBuffer<int> &out)
{
int i = 1;
do
{
i <<= 1;
out.YieldReturn(i);
}
while (i < (1<<20));
}
void ThrowCatchCoroutine(YieldBuffer<int> &)
{
try
{
throw std::logic_error("Expected exception for testing");
}
catch (const std::logic_error &e)
{
std::cout << "Exception caught" << std::endl;
}
}
void YieldFloatsCoroutine(YieldBuffer<float> &out)
{
out.YieldReturn(1.0f);
out.YieldReturn(2.0f);
}
void NestedCoroutine(YieldBuffer<int> &out)
{
for (Enumerator<float> enumerator(YieldFloatsCoroutine); enumerator.Next(); )
{
out.YieldReturn((int)enumerator.GetYield());
}
}
template<typename Y>
void TestRunToCompletion(void (*coroutine)(YieldBuffer<Y>&))
{
for (Enumerator<Y> enumerator(coroutine); enumerator.Next(); )
{
std::cout << enumerator.GetYield() << std::endl;
}
}
template<typename Y>
void TestAbandon(void (*coroutine)(YieldBuffer<Y>&))
{
for (Enumerator<Y> enumerator(coroutine); enumerator.Next(); )
{
std::cout << enumerator.GetYield() << std::endl;
break;
}
}
template<typename Y>
void Test(void (*coroutine)(YieldBuffer<Y>&))
{
TestRunToCompletion(coroutine);
TestAbandon(coroutine);
}
int main()
{
Test(EmptyCoroutine);
Test(TypicalCoroutine);
Test(YieldFloatsCoroutine);
Test(NestedCoroutine);
return 0;
}