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