1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //      size_t size() const;
4bb611c8fSApple OSS Distributions //
5bb611c8fSApple OSS Distributions 
6bb611c8fSApple OSS Distributions #include <libkern/c++/safe_allocation.h>
7bb611c8fSApple OSS Distributions #include <darwintest.h>
8bb611c8fSApple OSS Distributions #include "test_utils.h"
9bb611c8fSApple OSS Distributions #include <cstddef>
10bb611c8fSApple OSS Distributions #include <type_traits>
11bb611c8fSApple OSS Distributions #include <utility>
12bb611c8fSApple OSS Distributions 
13bb611c8fSApple OSS Distributions struct T {
14bb611c8fSApple OSS Distributions 	int i;
15bb611c8fSApple OSS Distributions };
16bb611c8fSApple OSS Distributions 
17bb611c8fSApple OSS Distributions template <typename T>
18bb611c8fSApple OSS Distributions static void
tests()19bb611c8fSApple OSS Distributions tests()
20bb611c8fSApple OSS Distributions {
21bb611c8fSApple OSS Distributions 	{
22bb611c8fSApple OSS Distributions 		test_safe_allocation<T> const array(10, libkern::allocate_memory);
23bb611c8fSApple OSS Distributions 		CHECK(array.size() == 10);
24bb611c8fSApple OSS Distributions 	}
25bb611c8fSApple OSS Distributions 	{
26bb611c8fSApple OSS Distributions 		T* memory = reinterpret_cast<T*>(malloc_allocator::allocate(10 * sizeof(T)));
27bb611c8fSApple OSS Distributions 		test_safe_allocation<T> const array(memory, 10, libkern::adopt_memory);
28bb611c8fSApple OSS Distributions 		CHECK(array.size() == 10);
29bb611c8fSApple OSS Distributions 	}
30bb611c8fSApple OSS Distributions 	{
31bb611c8fSApple OSS Distributions 		test_safe_allocation<T> const array(nullptr, 0, libkern::adopt_memory);
32bb611c8fSApple OSS Distributions 		CHECK(array.size() == 0);
33bb611c8fSApple OSS Distributions 	}
34bb611c8fSApple OSS Distributions 	{
35bb611c8fSApple OSS Distributions 		test_safe_allocation<T> const array;
36bb611c8fSApple OSS Distributions 		CHECK(array.size() == 0);
37bb611c8fSApple OSS Distributions 	}
38bb611c8fSApple OSS Distributions 
39bb611c8fSApple OSS Distributions 	{
40bb611c8fSApple OSS Distributions 		using Size = decltype(std::declval<test_safe_allocation<T> const&>().size());
41bb611c8fSApple OSS Distributions 		static_assert(std::is_same_v<Size, std::size_t>);
42bb611c8fSApple OSS Distributions 	}
43bb611c8fSApple OSS Distributions }
44bb611c8fSApple OSS Distributions 
45*8d741a5dSApple OSS Distributions T_DECL(size, "safe_allocation.size", T_META_TAG_VM_PREFERRED) {
46bb611c8fSApple OSS Distributions 	tests<T>();
47bb611c8fSApple OSS Distributions 	tests<T const>();
48bb611c8fSApple OSS Distributions }
49