summaryrefslogtreecommitdiff
path: root/test/pointerlist_test.cc
blob: 427447356b313bf697f03724804c250f9a48e100 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include <uunit.h>

#include <set>
#include <algorithm>

#include "pointerlist.h"

class PointerListTest
	: public uUnit
{
public:
	PointerListTest()
	{
		uTEST(PointerListTest::test_zom_pointerlist_push);
		uTEST(PointerListTest::test_zom_pointerlist_from_args);
		uTEST(PointerListTest::test_zom_envmap_insert);
		uTEST(PointerListTest::test_zom_envmap_from_env);
		uTEST(PointerListTest::test_exceptional_env);
	}

	void test_zom_pointerlist_push()
	{
		using namespace std::string_literals;

		{ // Zero
			PointerList args;
			uASSERT_EQUAL(0u, args.size());
			auto [argc, argv] = args.get();
			uASSERT_EQUAL(0, argc);
			uASSERT(nullptr != argv);
			uASSERT_EQUAL(nullptr, argv[0]);
		}

		{ // One
			PointerList args;
			args.push_back("hello");
			uASSERT_EQUAL(1u, args.size());
			auto [argc, argv] = args.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello"s, std::string(argv[0]));
		}

		{ // Many
			PointerList args;
			args.push_back("hello");
			args.push_back("dear");
			args.push_back("world");
			uASSERT_EQUAL(3u, args.size());
			auto [argc, argv] = args.get();
			uASSERT_EQUAL(3, argc);
			uASSERT_EQUAL("hello"s, std::string(argv[0]));
			uASSERT_EQUAL("dear"s, std::string(argv[1]));
			uASSERT_EQUAL("world"s, std::string(argv[2]));
		}
	}

	void test_zom_pointerlist_from_args()
	{
		using namespace std::string_literals;

		{ // Zero
			PointerList args(0, nullptr);
			uASSERT_EQUAL(0u, args.size());
			auto [argc, argv] = args.get();
			uASSERT_EQUAL(0, argc);
			uASSERT(nullptr != argv);
			uASSERT_EQUAL(nullptr, argv[0]);
		}

		{ // One
			int _argc{1};
			const char* _argv[] = { "hello" };
			PointerList args(_argc, _argv);
			uASSERT_EQUAL(1u, args.size());
			auto [argc, argv] = args.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello"s, std::string(argv[0]));
		}

		{ // Many
			int _argc{3};
			const char* _argv[] = { "hello", "dear", "world" };
			PointerList args(_argc, _argv);
			uASSERT_EQUAL(3u, args.size());
			auto [argc, argv] = args.get();
			uASSERT_EQUAL(3, argc);
			// order must be preserved
			uASSERT_EQUAL("hello"s, std::string(argv[0]));
			uASSERT_EQUAL("dear"s, std::string(argv[1]));
			uASSERT_EQUAL("world"s, std::string(argv[2]));
		}
	}

	void test_zom_envmap_insert()
	{
		using namespace std::string_literals;

		{ // Zero
			EnvMap env;
			uASSERT_EQUAL(0u, env.size());

			auto [argc, argv] = env.get();
			uASSERT_EQUAL(0, argc);
			uASSERT(nullptr != argv);
			uASSERT_EQUAL(nullptr, argv[0]);

			auto str = env.stringify();
			uASSERT_EQUAL(1u, str.size());
			uASSERT_EQUAL('\0', str[0]);
		}

		{ // One (key only)
			EnvMap env;
			env.insert("hello");
			uASSERT_EQUAL(1u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello="s, std::string(argv[0]));
			uASSERT_EQUAL(""s, env["hello"]);
		}
		{ // One (with value)
			EnvMap env;
			env.insert("hello=world");
			uASSERT_EQUAL(1u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello=world"s, std::string(argv[0]));
			uASSERT_EQUAL("world"s, env["hello"]);

			uASSERT_EQUAL("hello=world\0\0"s, env.stringify());
		}

		{ // Overwrite one
			EnvMap env;
			env.insert("hello=world");
			uASSERT_EQUAL(1u, env.size());
			uASSERT_EQUAL("world"s, env["hello"s]);

			env.insert("hello=foo");
			uASSERT_EQUAL(1u, env.size());
			uASSERT_EQUAL("foo"s, env["hello"s]);
		}

		{ // Many
			EnvMap env;
			env.insert("hello=world");
			env.insert("world=leader");
			env.insert("dear=boar");
			uASSERT_EQUAL(3u, env.size());

			uASSERT_EQUAL("boar"s, env["dear"s]);
			uASSERT_EQUAL("world"s, env["hello"s]);
			uASSERT_EQUAL("leader"s, env["world"s]);

			auto [argc, argv] = env.get();
			uASSERT_EQUAL(3, argc);
			// store and sort to verify unordered
			std::vector<std::string> vals{argv[0], argv[1], argv[2]};
			std::ranges::sort(vals);
			uASSERT_EQUAL("dear=boar"s, vals[0]);
			uASSERT_EQUAL("hello=world"s, vals[1]);
			uASSERT_EQUAL("world=leader"s, vals[2]);

			// test all combinations since ordering is not a requirement
			// exactly one of the must be true (boolean XOR)
			auto str = env.stringify();
			uASSERT(((((("dear=boar\0hello=world\0world=leader\0\0"s == str) !=
			            ("dear=boar\0world=leader\0hello=world\0\0"s == str)) !=
			           ("hello=world\0dear=boar\0world=leader\0\0"s == str)) !=
			          ("hello=world\0world=leader\0dear=boar\0\0"s == str)) !=
			         ("world=leader\0dear=boar\0hello=world\0\0"s == str)) !=
			        ("world=leader\0hello=world\0dear=boar\0\0"s == str));
		}
	}

	void test_zom_envmap_from_env()
	{
		using namespace std::string_literals;

		{ // Zero
			const char* penv = nullptr;
			EnvMap env(penv);
			uASSERT_EQUAL(0u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(0, argc);
			uASSERT(nullptr != argv);
			uASSERT_EQUAL(nullptr, argv[0]);
		}

		{ // Zero
			const char** ppenv = nullptr;
			EnvMap env(ppenv);
			uASSERT_EQUAL(0u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(0, argc);
			uASSERT(nullptr != argv);
			uASSERT_EQUAL(nullptr, argv[0]);
		}

		{ // One (key only)
			const char* ptr = "hello\0\0";
			EnvMap env(ptr);
			uASSERT_EQUAL(1u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello="s, std::string(argv[0]));
			uASSERT_EQUAL(""s, env["hello"]);
		}

		{ // One (key only)
			const char* ptr[] = {"hello", nullptr};
			EnvMap env(ptr);
			uASSERT_EQUAL(1u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello="s, std::string(argv[0]));
			uASSERT_EQUAL(""s, env["hello"]);
		}

		{ // One (with value)
			const char* ptr = "hello=world\0\0";
			EnvMap env(ptr);
			uASSERT_EQUAL(1u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello=world"s, std::string(argv[0]));
			uASSERT_EQUAL("world"s, env["hello"]);

			uASSERT_EQUAL("hello=world\0\0"s, env.stringify());
		}

		{ // One (with value)
			const char* ptr[] = {"hello=world\0", nullptr};
			EnvMap env(ptr);
			uASSERT_EQUAL(1u, env.size());
			auto [argc, argv] = env.get();
			uASSERT_EQUAL(1, argc);
			uASSERT_EQUAL("hello=world"s, std::string(argv[0]));
			uASSERT_EQUAL("world"s, env["hello"]);

			uASSERT_EQUAL("hello=world\0\0"s, env.stringify());
		}

		{ // Many
			const char* ptr = "hello=world\0world=leader\0dear=boar\0\0";
			EnvMap env(ptr);
			uASSERT_EQUAL(3u, env.size());

			uASSERT_EQUAL("boar"s, env["dear"s]);
			uASSERT_EQUAL("world"s, env["hello"s]);
			uASSERT_EQUAL("leader"s, env["world"s]);

			auto [argc, argv] = env.get();
			uASSERT_EQUAL(3, argc);
			// store and sort to verify unordered
			std::vector<std::string> vals{argv[0], argv[1], argv[2]};
			std::ranges::sort(vals);
			uASSERT_EQUAL("dear=boar"s, vals[0]);
			uASSERT_EQUAL("hello=world"s, vals[1]);
			uASSERT_EQUAL("world=leader"s, vals[2]);

			// test all combinations since ordering is not a requirement
			// exactly one of the must be true (boolean XOR)
			auto str = env.stringify();
			uASSERT(((((("dear=boar\0hello=world\0world=leader\0\0"s == str) !=
			            ("dear=boar\0world=leader\0hello=world\0\0"s == str)) !=
			           ("hello=world\0dear=boar\0world=leader\0\0"s == str)) !=
			          ("hello=world\0world=leader\0dear=boar\0\0"s == str)) !=
			         ("world=leader\0dear=boar\0hello=world\0\0"s == str)) !=
			        ("world=leader\0hello=world\0dear=boar\0\0"s == str));
		}

		{ // Many
			const char* ptr[] =
				{"hello=world\0", "world=leader\0", "dear=boar\0", nullptr};
			EnvMap env(ptr);
			uASSERT_EQUAL(3u, env.size());

			uASSERT_EQUAL("boar"s, env["dear"s]);
			uASSERT_EQUAL("world"s, env["hello"s]);
			uASSERT_EQUAL("leader"s, env["world"s]);

			auto [argc, argv] = env.get();
			uASSERT_EQUAL(3, argc);
			// store and sort to verify unordered
			std::vector<std::string> vals{argv[0], argv[1], argv[2]};
			std::ranges::sort(vals);
			uASSERT_EQUAL("dear=boar"s, vals[0]);
			uASSERT_EQUAL("hello=world"s, vals[1]);
			uASSERT_EQUAL("world=leader"s, vals[2]);

			// test all combinations since ordering is not a requirement
			// exactly one of the must be true (boolean XOR)
			auto str = env.stringify();
			uASSERT(((((("dear=boar\0hello=world\0world=leader\0\0"s == str) !=
			            ("dear=boar\0world=leader\0hello=world\0\0"s == str)) !=
			           ("hello=world\0dear=boar\0world=leader\0\0"s == str)) !=
			          ("hello=world\0world=leader\0dear=boar\0\0"s == str)) !=
			         ("world=leader\0dear=boar\0hello=world\0\0"s == str)) !=
			        ("world=leader\0hello=world\0dear=boar\0\0"s == str));
		}
	}

	void test_exceptional_env()
	{
		using namespace std::string_literals;

		{ // Zero
			EnvMap env;
			uASSERT_EQUAL(0u, env.size());
			uASSERT_EQUAL(""s, env["foo"]); // lookup of non-existing key
		}
	}
};

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