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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include "catch2/catch.hpp"
#include <fastsignals/function.h>
#include <array>

using namespace fastsignals;

namespace
{
int Abs(int x)

{
	return x >= 0 ? x : -x;
}

int Sum(int a, int b)

{
	return a + b;
}

void InplaceAbs(int& x)

{
	x = Abs(x);
}

std::string GetStringHello()

{
	return "hello";
}

class AbsFunctor
{
public:
	int operator()(int x) const

	{
		return Abs(x);
	}
};

class SumFunctor
{
public:
	int operator()(int a, int b) const

	{
		return Sum(a, b);
	}
};

class InplaceAbsFunctor
{
public:
	void operator()(int& x) /* non-const */

	{
		if (m_calledOnce)
		{
			abort();
		}
		m_calledOnce = true;
		InplaceAbs(x);
	}

private:
	bool m_calledOnce = false;
};

class GetStringFunctor
{
public:
	explicit GetStringFunctor(const std::string& value)

		: m_value(value)

	{
	}

	std::string operator()() /* non-const */

	{
		if (m_calledOnce)
		{
			abort();
		}
		m_calledOnce = true;
		return m_value;
	}

private:
	bool m_calledOnce = false;
	std::string m_value;
};
} // namespace

TEST_CASE("Can use free function with 1 argument", "[function]")
{
	function<int(int)> fn = Abs;
	REQUIRE(fn(10) == 10);
	REQUIRE(fn(-10) == 10);
	REQUIRE(fn(0) == 0);
}

TEST_CASE("Can use free function with 2 arguments", "[function]")
{
	function<int(int, int)> fn = Sum;
	REQUIRE(fn(10, 5) == 15);
	REQUIRE(fn(-10, 0) == -10);
}

TEST_CASE("Can use free function without arguments", "[function]")
{
	function<std::string()> fn = GetStringHello;
	REQUIRE(fn() == "hello");
}

TEST_CASE("Can use free function without return value", "[function]")
{
	function<void(int&)> fn = InplaceAbs;
	int a = -10;
	fn(a);
	REQUIRE(a == 10);
}

TEST_CASE("Can use lambda with 1 argument", "[function]")
{
	function<int(int)> fn = [](int value) {
		return Abs(value);
	};
	REQUIRE(fn(10) == 10);
	REQUIRE(fn(-10) == 10);
	REQUIRE(fn(0) == 0);
}

TEST_CASE("Can use lambda with 2 arguments", "[function]")
{
	function<int(int, int)> fn = [](auto&& a, auto&& b) {
		return Sum(a, b);
	};
	REQUIRE(fn(10, 5) == 15);
	REQUIRE(fn(-10, 0) == -10);
}

TEST_CASE("Can use lambda without arguments", "[function]")
{
	function<std::string()> fn = [] {
		return GetStringHello();
	};
	REQUIRE(fn() == "hello");
}

TEST_CASE("Can use lambda without return value", "[function]")
{
	bool calledOnce = false;
	function<void(int&)> fn = [calledOnce](auto& value) mutable {
		if (calledOnce)
		{
			abort();
		}
		calledOnce = true;
		InplaceAbs(value);
	};
	int a = -10;
	fn(a);
	REQUIRE(a == 10);
}

TEST_CASE("Can use functor with 1 argument", "[function]")
{
	function<int(int)> fn = AbsFunctor();
	REQUIRE(fn(10) == 10);
	REQUIRE(fn(-10) == 10);
	REQUIRE(fn(0) == 0);
}

TEST_CASE("Can use functor with 2 arguments", "[function]")
{
	function<int(int, int)> fn = SumFunctor();
	REQUIRE(fn(10, 5) == 15);
	REQUIRE(fn(-10, 0) == -10);
}

TEST_CASE("Can use functor without arguments", "[function]")
{
	function<std::string()> fn = GetStringFunctor("hello");
	REQUIRE(fn() == "hello");
}

TEST_CASE("Can use functor without return value", "[function]")
{
	function<void(int&)> fn = InplaceAbsFunctor();
	int a = -10;
	fn(a);
	REQUIRE(a == 10);
}

TEST_CASE("Can construct function with cons std::function<>&", "[function]")
{
	using BoolCallback = std::function<void(bool succeed)>;
	bool value = false;
	const BoolCallback& cb = [&value](bool succeed) {
		value = succeed;
	};

	function<void(bool)> fn = cb;
	fn(true);
	REQUIRE(value == true);
	fn(false);
	REQUIRE(value == false);
	fn(true);
	REQUIRE(value == true);
}

TEST_CASE("Can copy function", "[function]")
{
	unsigned calledCount = 0;
	bool value = false;
	function<void(bool)> callback = [&](bool gotValue) {
		++calledCount;
		value = gotValue;
	};
	auto callback2 = callback;
	REQUIRE(calledCount == 0);
	CHECK(!value);
	callback(true);
	REQUIRE(calledCount == 1);
	CHECK(value);
	callback2(false);
	REQUIRE(calledCount == 2);
	CHECK(!value);
}

TEST_CASE("Can move function", "[function]")
{
	bool called = false;
	function<void()> callback = [&] {
		called = true;
	};
	auto callback2(std::move(callback));
	REQUIRE_THROWS(callback());
	REQUIRE(!called);
	callback2();
	REQUIRE(called);
}

TEST_CASE("Works when copying self", "[function]")
{
	bool called = false;
	function<void()> callback = [&] {
		called = true;
	};
	callback = callback;
	callback();
	REQUIRE(called);
}

TEST_CASE("Can release packed function", "[function]")
{
	function<int()> iota = [v = 0]() mutable {
		return v++;
	};
	REQUIRE(iota() == 0);

	auto packedFn = std::move(iota).release();
	REQUIRE_THROWS_AS(iota(), std::bad_function_call);

	auto&& proxy = packedFn.get<int()>();
	REQUIRE(proxy() == 1);
	REQUIRE(proxy() == 2);
}

TEST_CASE("Function copy has its own packed function", "[function]")
{
	function<int()> iota = [v = 0]() mutable {
		return v++;
	};

	REQUIRE(iota() == 0);

	auto iotaCopy(iota);

	REQUIRE(iota() == 1);
	REQUIRE(iota() == 2);

	REQUIRE(iotaCopy() == 1);
	REQUIRE(iotaCopy() == 2);
}

TEST_CASE("can work with callables that have vtable", "[function]")
{
	class Base
	{
	};

	class Interface : public Base
	{
	public:
		virtual ~Interface() = default;
		virtual void operator()() const = 0;
	};
	class Class : public Interface
	{
	public:
		Class(bool* destructorCalled)
			: m_destructorCalled(destructorCalled)
		{
		}

		~Class()
		{
			*m_destructorCalled = true;
		}

		void operator()() const override

		{
		}

		bool* m_destructorCalled = nullptr;
	};
	bool destructorCalled = false;
	{
		function<void()> f = Class(&destructorCalled);
		f();
		auto packed = f.release();
		destructorCalled = false;
	}
	CHECK(destructorCalled);
}

TEST_CASE("can work with callables with virtual inheritance", "[function]")
{
	struct A
	{
		void operator()() const

		{
			m_called = true;
		}

		~A()
		{
			*m_destructorCalled = true;
		}

		mutable bool m_called = false;
		bool* m_destructorCalled = nullptr;
	};
	struct B : public virtual A
	{
	};
	struct C : public virtual A
	{
	};
	struct D : virtual public B
		, virtual public C
	{
		D(bool* destructorCalled)
		{
			m_destructorCalled = destructorCalled;
		}

		using A::operator();
	};
	bool destructorCalled = false;
	{
		function<void()> f = D(&destructorCalled);
		f();
		auto packed = f.release();
		destructorCalled = false;
	}
	CHECK(destructorCalled);
}

TEST_CASE("uses copy constructor if callable's move constructor throws", "[function]")
{
	struct Callable
	{
		Callable() = default;
		Callable(Callable&&)
		{
			throw std::runtime_error("throw");
		}
		Callable(const Callable& other) = default;
		void operator()() const

		{
		}
	};
	Callable c;
	function<void()> f(c);
	auto f2 = std::move(f);
	f2();
	CHECK_THROWS(f());
}

TEST_CASE("uses move constructor if it is noexcept", "[function]")
{
	struct Callable
	{
		Callable() = default;
		Callable(Callable&& other) noexcept = default;
		Callable(const Callable&)
		{
			throw std::runtime_error("throw");
		}
		void operator()() const

		{
		}
	};
	Callable c;
	function<void()> f(std::move(c));
	auto f2 = std::move(f);
	f2();
	CHECK_THROWS(f());
}

TEST_CASE("can copy and move empty function", "[function]")
{
	function<void()> f;
	auto f2 = f;
	auto f3 = std::move(f);
}

TEST_CASE("properly copies callable on assignment", "[function]")
{
	struct Callable
	{
		Callable(int& aliveCounter)
			: m_aliveCounter(&aliveCounter)
		{
			++*m_aliveCounter;
		}
		Callable(const Callable& other)
			: m_aliveCounter(other.m_aliveCounter)
		{
			if (m_aliveCounter)
			{
				++*m_aliveCounter;
			}
		}
		Callable(Callable&& other) noexcept
			: m_aliveCounter(other.m_aliveCounter)
		{
			other.m_aliveCounter = nullptr;
		}
		~Callable()
		{
			if (m_aliveCounter)
			{
				--*m_aliveCounter;
			}
		}
		void operator()() const

		{
		}

		int* m_aliveCounter = nullptr;
	};
	int aliveCounter1 = 0;
	int aliveCounter2 = 0;
	function<void()> f = Callable(aliveCounter1);
	function<void()> f2 = Callable(aliveCounter2);
	CHECK(aliveCounter1 == 1);
	CHECK(aliveCounter2 == 1);
	f = f2;
	CHECK(aliveCounter1 == 0);
	CHECK(aliveCounter2 == 2);
	f = function<void()>();
	f2 = function<void()>();
	CHECK(aliveCounter1 == 0);
	CHECK(aliveCounter2 == 0);
}

TEST_CASE("copy assignment operator provides strong exception safety", "[function]")
{
	struct State
	{
		int callCount = 0;
		bool throwOnCopy = false;
	};
	struct Callable
	{
		Callable(State& state)
			: state(&state)
		{
		}
		void operator()()

		{
			++state->callCount;
		}
		Callable(const Callable& other)
			: state(other.state)
		{
			if (state->throwOnCopy)
			{
				throw std::runtime_error("throw on request");
			}
		}
		State* state = nullptr;
	};
	static_assert(!detail::can_use_inplace_buffer<Callable>);

	State srcState;
	State dstState;

	function<void()> srcFn(Callable{ srcState });
	function<void()> dstFn(Callable{ dstState });

	srcFn();
	dstFn();

	REQUIRE(srcState.callCount == 1);
	REQUIRE(dstState.callCount == 1);

	srcState.throwOnCopy = true;

	REQUIRE_THROWS_AS(dstFn = srcFn, std::runtime_error);

	// srcFn and dstFn must not be emptied even if assignment throws
	REQUIRE_NOTHROW(srcFn());
	REQUIRE_NOTHROW(dstFn());

	// srcFn and dstFn must keep their state
	REQUIRE(srcState.callCount == 2);
	REQUIRE(dstState.callCount == 2);

	// The next copy will succeed
	srcState.throwOnCopy = false;
	REQUIRE_NOTHROW(dstFn = srcFn);

	// Both functions are usable
	REQUIRE_NOTHROW(srcFn());
	REQUIRE_NOTHROW(dstFn());

	// After assignment, dstFn and srcFn refer the same state - srcState
	REQUIRE(srcState.callCount == 4);
	REQUIRE(dstState.callCount == 2);
}

TEST_CASE("assignment of variously allocated functions", "[function]")
{
	int heapCalls = 0;
	auto onHeap = [&heapCalls, largeVar = std::array<std::string, 1000>()]() mutable {
		std::fill(largeVar.begin(), largeVar.end(), "large string to be allocated on heap instead of stack");
		++heapCalls;
	};
	int stackCalls = 0;
	auto onStack = [&stackCalls] {
		++stackCalls;
	};

	static_assert(detail::can_use_inplace_buffer<detail::function_proxy_impl<decltype(onStack), void>>);
	static_assert(!detail::can_use_inplace_buffer<detail::function_proxy_impl<decltype(onHeap), void>>);

	using Fn = function<void()>;
	{
		Fn heap(onHeap);
		Fn stack(onStack);
		heap = stack;
		heap();
		REQUIRE(stackCalls == 1);
		REQUIRE(heapCalls == 0);
	}
	{
		Fn heap(onHeap);
		Fn stack(onStack);
		stack = heap;
		stack();
		REQUIRE(stackCalls == 1);
		REQUIRE(heapCalls == 1);
	}
	{
		Fn heap(onHeap);
		Fn heap1(onHeap);
		heap = heap1;
		heap();
		REQUIRE(stackCalls == 1);
		REQUIRE(heapCalls == 2);
	}
	{
		Fn stack(onStack);
		Fn stack1(onStack);
		stack = stack1;
		stack();
		REQUIRE(stackCalls == 2);
		REQUIRE(heapCalls == 2);
	}
	{
		Fn heap(onHeap);
		Fn empty;
		heap = empty;
		REQUIRE_THROWS(heap());
		REQUIRE(stackCalls == 2);
		REQUIRE(heapCalls == 2);
	}
	{
		Fn stack(onStack);
		Fn empty;
		stack = empty;
		REQUIRE_THROWS(stack());
		REQUIRE(stackCalls == 2);
		REQUIRE(heapCalls == 2);
	}
	{
		Fn empty;
		Fn heap(onHeap);
		empty = heap;
		empty();
		REQUIRE(stackCalls == 2);
		REQUIRE(heapCalls == 3);
	}
	{
		Fn empty;
		Fn stack(onStack);
		empty = stack;
		empty();
		REQUIRE(stackCalls == 3);
		REQUIRE(heapCalls == 3);
	}
	{
		Fn empty;
		Fn empty1;
		empty = empty1;
		REQUIRE_THROWS(empty());
		REQUIRE(stackCalls == 3);
		REQUIRE(heapCalls == 3);
	}
}