summaryrefslogtreecommitdiff
path: root/test/tasks_test.cc
blob: 82296b7ac2c5ccf6dba61333f060b63bcd46c83a (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <uunit.h>

#include <libctor.h>
#include <tasks.h>
#include <settings.h>

namespace
{
BuildConfigurations ctorTestConfigs1()
{
	return
	{
		{
			.target = "target1",
			.sources = {"foo.cc", "bar.c"},
		},
		{
			.target = "target2",
		},
	};
}

BuildConfigurations ctorTestConfigs2()
{
	return
	{
		{
			.target = "target3",
		},
		{
			.target = "target4",
		},
	};
}
}

REG(ctorTestConfigs1);
REG(ctorTestConfigs2);

std::size_t count(const std::set<std::shared_ptr<Task>>& tasks,
                  const std::string& name)
{
	auto cnt{0u};
	for(const auto& task : tasks)
	{
		if(task->target() == name)
		{
			cnt++;
		}
	}
	return cnt;
}

class TestTask
	: public Task
{
public:
	TestTask(const std::string& name, bool dirty,
	         const std::vector<std::string>& deps = {})
		: Task({}, {}, {})
		, task_name(name)
		, task_dirty(dirty)
		, task_deps(deps)
	{
	}

	std::string name() const override { return task_name; }
	int clean() override { return 0; }
	std::vector<std::string> depends() const override { return task_deps; }
	std::string target() const override { return task_name; }
	std::filesystem::path targetFile() const override { return {}; }
	bool derived() const override { return false; }
	bool dirtyInner() override { return task_dirty; }

private:
	std::string task_name;
	bool task_dirty;
	std::vector<std::string> task_deps;
};

class TasksTest
	: public uUnit
{
public:
	TasksTest()
	{
		uTEST(TasksTest::getTargets_test);
		uTEST(TasksTest::getTasks_test);
		uTEST(TasksTest::getNextTask_test);
	}

	void getTargets_test()
	{
		using namespace std::string_literals;
		Settings settings{};
		const auto& targets = getTargets(settings);
		uASSERT_EQUAL(4u, targets.size());

		uASSERT_EQUAL("target1"s, targets[0].config.target);
		uASSERT_EQUAL("target2"s, targets[1].config.target);
		uASSERT_EQUAL("target3"s, targets[2].config.target);
		uASSERT_EQUAL("target4"s, targets[3].config.target);

		uASSERT_EQUAL("test"s, targets[0].path);
		uASSERT_EQUAL("test"s, targets[1].path);
		uASSERT_EQUAL("test"s, targets[2].path);
		uASSERT_EQUAL("test"s, targets[3].path);
	}

	void getTasks_test()
	{
		using namespace std::string_literals;
		Settings settings{ .builddir = "foo" };
		{
			auto tasks = getTasks(settings);
			uASSERT_EQUAL(6u, tasks.size());
			// Note: count() is used here because the order of
			// std::set<std::shared_ptr<T>> is not deterministic.
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target1"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target2"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target3"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target4"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target1-foo_cc.o"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target1-bar_c.o"s));
		}
		{
			auto tasks = getTasks(settings, {"target1", "target3"});
			uASSERT_EQUAL(4u, tasks.size());
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target1"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target3"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target1-foo_cc.o"s));
			uASSERT_EQUAL(1u, count(tasks, "foo/test/target1-bar_c.o"s));
		}
		{
			auto tasks = getTasks(settings, {"no-such-target"});
			uASSERT_EQUAL(0u, tasks.size());
		}
	}

	void getNextTask_test()
	{
		using namespace std::string_literals;
		Settings settings{};

		{ // Zero (Empty)
			std::set<std::shared_ptr<Task>> allTasks;
			std::set<std::shared_ptr<Task>> dirtyTasks;

			for(auto& task : dirtyTasks)
			{
				uASSERT_EQUAL(0, task->registerDepTasks(allTasks));
			}

			uASSERT_EQUAL(nullptr, getNextTask(allTasks, dirtyTasks));
		}

		{ // Zero (One task, no dirty)
			auto task1 = std::make_shared<TestTask>("task1", false);

			std::set<std::shared_ptr<Task>> allTasks;
			allTasks.insert(task1);

			std::set<std::shared_ptr<Task>> dirtyTasks;

			for(auto& task : dirtyTasks)
			{
				uASSERT_EQUAL(0, task->registerDepTasks(allTasks));
			}

			uASSERT_EQUAL(nullptr, getNextTask(allTasks, dirtyTasks));
		}

		{ // One (One task, one dirty)
			auto task1 = std::make_shared<TestTask>("task1", true);

			std::set<std::shared_ptr<Task>> allTasks;
			allTasks.insert(task1);

			std::set<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.insert(task1);

			for(auto& task : dirtyTasks)
			{
				uASSERT_EQUAL(0, task->registerDepTasks(allTasks));
			}

			uASSERT_EQUAL(task1, getNextTask(allTasks, dirtyTasks));
			uASSERT_EQUAL(0u, dirtyTasks.size());
		}

		{ // One (Two tasks, one dirty)
			auto task1 = std::make_shared<TestTask>("task1", false);
			auto task2 = std::make_shared<TestTask>("task2", true);

			std::set<std::shared_ptr<Task>> allTasks;
			allTasks.insert(task1);
			allTasks.insert(task2);

			std::set<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.insert(task2);

			for(auto& task : dirtyTasks)
			{
				uASSERT_EQUAL(0, task->registerDepTasks(allTasks));
			}

			uASSERT_EQUAL(task2, getNextTask(allTasks, dirtyTasks));
			uASSERT_EQUAL(0u, dirtyTasks.size());
		}

		{ // One (Two tasks, one dirty which depends on the other)
			auto task1 = std::make_shared<TestTask>("task1", false);

			std::vector<std::string> deps = {"task1"};
			auto task2 = std::make_shared<TestTask>("task2", true, deps);

			std::set<std::shared_ptr<Task>> allTasks;
			allTasks.insert(task1);
			allTasks.insert(task2);

			std::set<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.insert(task2);

			for(auto& task : dirtyTasks)
			{
				uASSERT_EQUAL(0, task->registerDepTasks(allTasks));
			}

			uASSERT_EQUAL(task2, getNextTask(allTasks, dirtyTasks));
			uASSERT_EQUAL(0u, dirtyTasks.size());
		}

		{ // One (Two tasks, Both dirty, one depends on the other)
			auto task1 = std::make_shared<TestTask>("task1", true);

			std::vector<std::string> deps = {"task1"};
			auto task2 = std::make_shared<TestTask>("task2", true, deps);

			std::set<std::shared_ptr<Task>> allTasks;
			allTasks.insert(task2);
			allTasks.insert(task1);

			std::set<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.insert(task2);
			dirtyTasks.insert(task1);

			for(auto& task : dirtyTasks)
			{
				uASSERT_EQUAL(0, task->registerDepTasks(allTasks));
			}

			uASSERT_EQUAL(task1, getNextTask(allTasks, dirtyTasks));
			uASSERT_EQUAL(1u, dirtyTasks.size());
		}

	}
};

// Registers the fixture into the 'registry'
static TasksTest test;