src/base/coroutine.test.cc
/**
 * This test is using a bi-channel coroutine (accepting and
 * yielding values) for testing a cooperative task.
 * The caller and the coroutine have a string each; they are
 * composing a new string by merging the strings together one
 * character per time.
 * The result string is hence passed back and forth between the
 * coroutine and the caller.
 */
This test is using a bi-channel coroutine (accepting and
yielding values) for testing a cooperative task.
The caller and the coroutine have a string each; they are
composing a new string by merging the strings together one
character per time.
The result string is hence passed back and forth between the
coroutine and the caller.
This test is using a bi - channel coroutine ( accepting and yielding values ) for testing a cooperative task . The caller and the coroutine have a string each ; they are composing a new string by merging the strings together one character per time . The result string is hence passed back and forth between the coroutine and the caller .
TEST(Coroutine, Cooperative)
{
    const std::string caller_str("HloWrd");
    const std::string coro_str("el ol!");
    const std::string expected("Hello World!");
    auto cooperative_task =
    [&coro_str] (Coroutine<std::string, std::string>::CallerType& yield)
    {
        for (auto& appended_c : coro_str) {
            auto old_str = yield.get();
            yield(old_str + appended_c);
        }
    };
    Coroutine<std::string, std::string> coro(cooperative_task);
    std::string result;
    for (auto& c : caller_str) {
        ASSERT_TRUE(coro);
        result += c;
        result = coro(result).get();
    }
    ASSERT_EQ(result, expected);
}
TEST ( Coroutine , Cooperative ) { const std :: string caller_str ( " " ) ; const std :: string coro_str ( " " ) ; const std :: string expected ( " " ) ; auto cooperative_task = [ & coro_str ] ( Coroutine < std :: string , std :: string > :: CallerType & yield ) { for ( auto & appended_c : coro_str ) { auto old_str = yield . get ( ) ; yield ( old_str + appended_c ) ; } } ; Coroutine < std :: string , std :: string > coro ( cooperative_task ) ; std :: string result ; for ( auto & c : caller_str ) { ASSERT_TRUE ( coro ) ; result += c ; result = coro ( result ) . get ( ) ; } ASSERT_EQ ( result , expected ) ; }
This test is using a bi-channel coroutine (accepting and
 yielding values) for testing a cooperative task.
This test is using a bi - channel coroutine ( accepting and yielding values ) for testing a cooperative task .