1*bb611c8fSApple OSS Distributions //
2*bb611c8fSApple OSS Distributions // Tests for
3*bb611c8fSApple OSS Distributions //      explicit operator bool() const;
4*bb611c8fSApple OSS Distributions //
5*bb611c8fSApple OSS Distributions 
6*bb611c8fSApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7*bb611c8fSApple OSS Distributions #include <darwintest.h>
8*bb611c8fSApple OSS Distributions #include "test_utils.h"
9*bb611c8fSApple OSS Distributions #include <type_traits>
10*bb611c8fSApple OSS Distributions 
11*bb611c8fSApple OSS Distributions struct T {
12*bb611c8fSApple OSS Distributions 	int i;
13*bb611c8fSApple OSS Distributions };
14*bb611c8fSApple OSS Distributions 
15*bb611c8fSApple OSS Distributions template <typename T>
16*bb611c8fSApple OSS Distributions static void
17*bb611c8fSApple OSS Distributions tests()
18*bb611c8fSApple OSS Distributions {
19*bb611c8fSApple OSS Distributions 	{
20*bb611c8fSApple OSS Distributions 		test_safe_allocation<T> const array(10, libkern::allocate_memory);
21*bb611c8fSApple OSS Distributions 		CHECK(static_cast<bool>(array));
22*bb611c8fSApple OSS Distributions 		if (array) {
23*bb611c8fSApple OSS Distributions 		} else {
24*bb611c8fSApple OSS Distributions 			CHECK(FALSE);
25*bb611c8fSApple OSS Distributions 		}
26*bb611c8fSApple OSS Distributions 	}
27*bb611c8fSApple OSS Distributions 	{
28*bb611c8fSApple OSS Distributions 		test_safe_allocation<T> const array = nullptr;
29*bb611c8fSApple OSS Distributions 		CHECK(!static_cast<bool>(array));
30*bb611c8fSApple OSS Distributions 		if (!array) {
31*bb611c8fSApple OSS Distributions 		} else {
32*bb611c8fSApple OSS Distributions 			CHECK(FALSE);
33*bb611c8fSApple OSS Distributions 		}
34*bb611c8fSApple OSS Distributions 	}
35*bb611c8fSApple OSS Distributions 
36*bb611c8fSApple OSS Distributions 	static_assert(!std::is_convertible_v<test_safe_allocation<T>, bool>);
37*bb611c8fSApple OSS Distributions }
38*bb611c8fSApple OSS Distributions 
39*bb611c8fSApple OSS Distributions T_DECL(operator_bool, "safe_allocation.operator.bool") {
40*bb611c8fSApple OSS Distributions 	tests<T>();
41*bb611c8fSApple OSS Distributions 	tests<T const>();
42*bb611c8fSApple OSS Distributions }
43