1bb611c8fSApple OSS Distributions //
2bb611c8fSApple OSS Distributions // Tests for
3bb611c8fSApple OSS Distributions //  explicit safe_allocation(size_t n, allocate_memory_t);
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 <limits>
11bb611c8fSApple OSS Distributions 
12bb611c8fSApple OSS Distributions struct T {
13bb611c8fSApple OSS Distributions 	int i;
14bb611c8fSApple OSS Distributions };
15bb611c8fSApple OSS Distributions 
16bb611c8fSApple OSS Distributions struct TrackInit {
17bb611c8fSApple OSS Distributions 	bool initialized;
TrackInitTrackInit18bb611c8fSApple OSS Distributions 	TrackInit() : initialized(true)
19bb611c8fSApple OSS Distributions 	{
20bb611c8fSApple OSS Distributions 	}
21bb611c8fSApple OSS Distributions };
22bb611c8fSApple OSS Distributions 
23bb611c8fSApple OSS Distributions template <typename T>
24bb611c8fSApple OSS Distributions static void
tests()25bb611c8fSApple OSS Distributions tests()
26bb611c8fSApple OSS Distributions {
27bb611c8fSApple OSS Distributions 	{
28bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
29bb611c8fSApple OSS Distributions 		{
30bb611c8fSApple OSS Distributions 			tracked_safe_allocation<T> array(10, libkern::allocate_memory);
31bb611c8fSApple OSS Distributions 			CHECK(tracking_allocator::allocated_size == 10 * sizeof(T));
32bb611c8fSApple OSS Distributions 			CHECK(array.data() != nullptr);
33bb611c8fSApple OSS Distributions 			CHECK(array.size() == 10);
34bb611c8fSApple OSS Distributions 			CHECK(array.begin() == array.data());
35bb611c8fSApple OSS Distributions 			CHECK(array.end() == array.data() + 10);
36bb611c8fSApple OSS Distributions 		}
37bb611c8fSApple OSS Distributions 		CHECK(tracking_allocator::deallocated_size == 10 * sizeof(T));
38bb611c8fSApple OSS Distributions 	}
39bb611c8fSApple OSS Distributions 
40bb611c8fSApple OSS Distributions 	// Check with a huge number of elements that will overflow size_t
41bb611c8fSApple OSS Distributions 	{
42bb611c8fSApple OSS Distributions 		std::size_t max_n = std::numeric_limits<std::size_t>::max() / sizeof(T);
43bb611c8fSApple OSS Distributions 		tracking_allocator::reset();
44bb611c8fSApple OSS Distributions 
45bb611c8fSApple OSS Distributions 		{
46bb611c8fSApple OSS Distributions 			tracked_safe_allocation<T> array(max_n + 1, libkern::allocate_memory);
47bb611c8fSApple OSS Distributions 			CHECK(array.data() == nullptr);
48bb611c8fSApple OSS Distributions 			CHECK(array.size() == 0);
49bb611c8fSApple OSS Distributions 			CHECK(array.begin() == array.end());
50bb611c8fSApple OSS Distributions 			CHECK(!tracking_allocator::did_allocate);
51bb611c8fSApple OSS Distributions 		}
52bb611c8fSApple OSS Distributions 		CHECK(!tracking_allocator::did_deallocate);
53bb611c8fSApple OSS Distributions 	}
54bb611c8fSApple OSS Distributions }
55bb611c8fSApple OSS Distributions 
56*8d741a5dSApple OSS Distributions T_DECL(ctor_allocate, "safe_allocation.ctor.allocate", T_META_TAG_VM_PREFERRED) {
57bb611c8fSApple OSS Distributions 	tests<T>();
58bb611c8fSApple OSS Distributions 	tests<T const>();
59bb611c8fSApple OSS Distributions 
60bb611c8fSApple OSS Distributions 	// Make sure we value-initialize elements
61bb611c8fSApple OSS Distributions 	{
62bb611c8fSApple OSS Distributions 		tracked_safe_allocation<TrackInit> array(10, libkern::allocate_memory);
63bb611c8fSApple OSS Distributions 		for (int i = 0; i != 10; ++i) {
64bb611c8fSApple OSS Distributions 			CHECK(array[i].initialized == true);
65bb611c8fSApple OSS Distributions 		}
66bb611c8fSApple OSS Distributions 	}
67bb611c8fSApple OSS Distributions }
68