summaryrefslogtreecommitdiff
path: root/test/tasks_test.cc
blob: c6909c09e4745e875346b3660356cfda90c528eb (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
#include <uunit.h>

#include <ctor.h>
#include <tasks.h>

namespace
{
ctor::build_configurations ctorTestConfigs1(const ctor::settings&)
{
	return
	{
		{
			.target = "target1",
			.sources = {"foo.cc", "bar.c"},
		},
		{
			.target = "target2",
		},
	};
}

ctor::build_configurations ctorTestConfigs2(const ctor::settings&)
{
	return
	{
		{
			.target = "target3",
		},
		{
			.target = "target4",
		},
	};
}
}

REG(ctorTestConfigs1);
REG(ctorTestConfigs2);

std::size_t count(const std::vector<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;
		ctor::settings settings{};
		const auto& targets = getTargets(settings);
		assert_equal(4u, targets.size(), __FILE__, __LINE__);

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

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

	void getTasks_test()
	{
		using namespace std::string_literals;
		ctor::settings settings{ .builddir = "foo" };
		{
			auto tasks = getTasks(settings);
			assert_equal(6u, tasks.size(), __FILE__, __LINE__);
			// Note: count() is used here because the order doesn't matter
			assert_equal(1u, count(tasks, "target1"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "target2"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "target3"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "target4"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "test/target1-foo_cc.o"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "test/target1-bar_c.o"s), __FILE__, __LINE__);
		}
		{
			auto tasks = getTasks(settings, {"target1", "target3"});
			assert_equal(4u, tasks.size(), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "target1"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "target3"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "test/target1-foo_cc.o"s), __FILE__, __LINE__);
			assert_equal(1u, count(tasks, "test/target1-bar_c.o"s), __FILE__, __LINE__);
		}
		{
			auto tasks = getTasks(settings, {"no-such-target"});
			assert_equal(0u, tasks.size(), __FILE__, __LINE__);
		}
	}

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

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

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

			assert_equal(nullptr, getNextTask(allTasks, dirtyTasks), __FILE__, __LINE__);
		}

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

			std::vector<std::shared_ptr<Task>> allTasks;
			allTasks.push_back(task1);

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

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

			assert_equal(nullptr, getNextTask(allTasks, dirtyTasks), __FILE__, __LINE__);
		}

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

			std::vector<std::shared_ptr<Task>> allTasks;
			allTasks.push_back(task1);

			std::vector<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.push_back(task1);

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

			assert_equal(task1, getNextTask(allTasks, dirtyTasks), __FILE__, __LINE__);
			assert_equal(0u, dirtyTasks.size(), __FILE__, __LINE__);
		}

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

			std::vector<std::shared_ptr<Task>> allTasks;
			allTasks.push_back(task1);
			allTasks.push_back(task2);

			std::vector<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.push_back(task2);

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

			assert_equal(task2, getNextTask(allTasks, dirtyTasks), __FILE__, __LINE__);
			assert_equal(0u, dirtyTasks.size(), __FILE__, __LINE__);
		}

		{ // 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::vector<std::shared_ptr<Task>> allTasks;
			allTasks.push_back(task1);
			allTasks.push_back(task2);

			std::vector<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.push_back(task2);

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

			assert_equal(task2, getNextTask(allTasks, dirtyTasks), __FILE__, __LINE__);
			assert_equal(0u, dirtyTasks.size(), __FILE__, __LINE__);
		}

		{ // 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::vector<std::shared_ptr<Task>> allTasks;
			allTasks.push_back(task2);
			allTasks.push_back(task1);

			std::vector<std::shared_ptr<Task>> dirtyTasks;
			dirtyTasks.push_back(task2);
			dirtyTasks.push_back(task1);

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

			assert_equal(task1, getNextTask(allTasks, dirtyTasks), __FILE__, __LINE__);
			assert_equal(1u, dirtyTasks.size(), __FILE__, __LINE__);
		}

	}
};

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