File: test_Future.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (136 lines) | stat: -rw-r--r-- 3,708 bytes parent folder | download
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
133
134
135
136
/* Copyright (C) 2021 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/Future.h"

#include <functional>
#include <type_traits>

class TestFuture : public CxxTest::TestSuite
{
public:
	void test_future_basic()
	{
		int counter = 0;
		{
			Future<void> noret;
			std::function<void()> task = noret.Wrap([&counter]() mutable { counter++; });
			task();
			TS_ASSERT_EQUALS(counter, 1);
		}

		{
			Future<void> noret;
			{
				std::function<void()> task = noret.Wrap([&counter]() mutable { counter++; });
				// Auto-cancels the task.
			}
		}
		TS_ASSERT_EQUALS(counter, 1);
	}

	void test_future_return()
	{
		{
			Future<int> future;
			std::function<void()> task = future.Wrap([]() { return 1; });
			task();
			TS_ASSERT_EQUALS(future.Get(), 1);
		}

		// Convertible type.
		{
			Future<int> future;
			std::function<void()> task = future.Wrap([]() -> u8 { return 1; });
			task();
			TS_ASSERT_EQUALS(future.Get(), 1);
		}

		static int destroyed = 0;
		// No trivial constructor or destructor. Also not copiable.
		struct NonDef
		{
			NonDef() = delete;
			NonDef(int input) : value(input) {};
			NonDef(const NonDef&) = delete;
			NonDef(NonDef&& o)
			{
				value = o.value;
				o.value = 0;
			}
			~NonDef() { if (value != 0) destroyed++; }
			int value = 0;
		};
		TS_ASSERT_EQUALS(destroyed, 0);
		{
			Future<NonDef> future;
			std::function<void()> task = future.Wrap([]() { return 1; });
			task();
			TS_ASSERT_EQUALS(future.Get().value, 1);
		}
		TS_ASSERT_EQUALS(destroyed, 1);
		{
			Future<NonDef> future;
			std::function<void()> task = future.Wrap([]() { return 1; });
		}
		TS_ASSERT_EQUALS(destroyed, 1);
		/**
		 * TODO: find a way to test this
		{
			Future<NonDef> future;
			std::function<void()> task = future.Wrap([]() { return 1; });
			future.Cancel();
			future.Wait();
			TS_ASSERT_THROWS(future.Get(), const Future<NonDef>::BadFutureAccess&);
		}
		 */
		TS_ASSERT_EQUALS(destroyed, 1);
	}

	void test_future_moving()
	{
		Future<int> future;
		std::function<int()> function;

		// Set things up so all temporaries passed into the futures will be reset to obviously invalid memory.
		std::aligned_storage_t<sizeof(Future<int>), alignof(Future<int>)> futureStorage;
		std::aligned_storage_t<sizeof(std::function<int()>), alignof(std::function<int()>)> functionStorage;
		Future<int>* f = &future; // CppCheck doesn't read placement new correctly, do this to silence errors.
		std::function<int()>* c = &function;
		f = new (&futureStorage) Future<int>{};
		c = new (&functionStorage) std::function<int()>{};

		*c = []() { return 7; };
		std::function<void()> task = f->Wrap(std::move(*c));

		future = std::move(*f);
		function = std::move(*c);

		// Destroy and clear the memory
		f->~Future();
		c->~function();
		memset(&futureStorage, 0xFF, sizeof(decltype(futureStorage)));
		memset(&functionStorage, 0xFF, sizeof(decltype(functionStorage)));

		// Let's move the packaged task while at it.
		std::function<void()> task2 = std::move(task);
		task2();
		TS_ASSERT_EQUALS(future.Get(), 7);
	}
};