mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 13:23:56 -07:00
Some tasks are invoked multiple times. Normally those tasks are broken up inside a loop and had to be continued there. With coroutines that is easier as it's possible to suspend inside a loop. Coroutines which are lambdas should not capture anythig as the lifetime of the captured values might end before the coroutine completes. For that purpose `std::bind_front` is used.
104 lines
2.3 KiB
C++
104 lines
2.3 KiB
C++
/* Copyright (C) 2025 Wildfire Games.
|
|
* This file is part of 0 A.D.
|
|
*
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "lib/self_test.h"
|
|
|
|
#include "ps/Loader.h"
|
|
|
|
class TestLoader : public CxxTest::TestSuite
|
|
{
|
|
public:
|
|
void test_PausedYield()
|
|
{
|
|
int position{0};
|
|
PS::Loader::Task task{[](int& p) -> PS::Loader::Task
|
|
{
|
|
p = 1;
|
|
co_yield 50;
|
|
p = 2;
|
|
co_return 0;
|
|
}(position)};
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 0);
|
|
TS_ASSERT(!task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 0);
|
|
|
|
task.Step(-1);
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 50);
|
|
TS_ASSERT(!task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 1);
|
|
|
|
task.Step(-1);
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 50);
|
|
TS_ASSERT(task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 2);
|
|
}
|
|
|
|
void test_UnpausedYield()
|
|
{
|
|
int position{0};
|
|
PS::Loader::Task task{[](int& p) -> PS::Loader::Task
|
|
{
|
|
p = 1;
|
|
co_yield 50;
|
|
p = 2;
|
|
co_return 0;
|
|
}(position)};
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 0);
|
|
TS_ASSERT(!task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 0);
|
|
|
|
task.Step(10);
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 50);
|
|
TS_ASSERT(task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 2);
|
|
}
|
|
|
|
void test_ForceAwait()
|
|
{
|
|
int position{0};
|
|
PS::Loader::Task task{[](int& p) -> PS::Loader::Task
|
|
{
|
|
p = 1;
|
|
co_yield 50;
|
|
p = 2;
|
|
co_await std::suspend_always{};
|
|
p = 3;
|
|
co_return 0;
|
|
}(position)};
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 0);
|
|
TS_ASSERT(!task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 0);
|
|
|
|
task.Step(10);
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 50);
|
|
TS_ASSERT(!task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 2);
|
|
|
|
task.Step(-1);
|
|
|
|
TS_ASSERT_EQUALS(task.GetProgress(), 50);
|
|
TS_ASSERT(task.IsDone());
|
|
TS_ASSERT_EQUALS(position, 3);
|
|
}
|
|
};
|