1*bb611c8fSApple OSS Distributions //
2*bb611c8fSApple OSS Distributions // Tests for
3*bb611c8fSApple OSS Distributions //  T& operator[](std::ptrdiff_t n);
4*bb611c8fSApple OSS Distributions //  T const& operator[](std::ptrdiff_t n) const;
5*bb611c8fSApple OSS Distributions //
6*bb611c8fSApple OSS Distributions 
7*bb611c8fSApple OSS Distributions #include <libkern/c++/safe_allocation.h>
8*bb611c8fSApple OSS Distributions #include <darwintest.h>
9*bb611c8fSApple OSS Distributions #include "test_utils.h"
10*bb611c8fSApple OSS Distributions #include <cstddef>
11*bb611c8fSApple OSS Distributions #include <limits>
12*bb611c8fSApple OSS Distributions 
13*bb611c8fSApple OSS Distributions struct T {
14*bb611c8fSApple OSS Distributions 	long i;
15*bb611c8fSApple OSS Distributions };
16*bb611c8fSApple OSS Distributions 
17*bb611c8fSApple OSS Distributions template <typename RawT, typename QualT>
18*bb611c8fSApple OSS Distributions static void
19*bb611c8fSApple OSS Distributions tests()
20*bb611c8fSApple OSS Distributions {
21*bb611c8fSApple OSS Distributions 	// Test the non-const version
22*bb611c8fSApple OSS Distributions 	{
23*bb611c8fSApple OSS Distributions 		RawT* memory = reinterpret_cast<RawT*>(malloc_allocator::allocate(10 * sizeof(RawT)));
24*bb611c8fSApple OSS Distributions 		for (RawT* ptr = memory; ptr != memory + 10; ++ptr) {
25*bb611c8fSApple OSS Distributions 			*ptr = RawT{ptr - memory}; // number from 0 to 9
26*bb611c8fSApple OSS Distributions 		}
27*bb611c8fSApple OSS Distributions 
28*bb611c8fSApple OSS Distributions 		test_safe_allocation<QualT> array(memory, 10, libkern::adopt_memory);
29*bb611c8fSApple OSS Distributions 		for (std::ptrdiff_t n = 0; n != 10; ++n) {
30*bb611c8fSApple OSS Distributions 			QualT& element = array[n];
31*bb611c8fSApple OSS Distributions 			CHECK(&element == memory + n);
32*bb611c8fSApple OSS Distributions 		}
33*bb611c8fSApple OSS Distributions 	}
34*bb611c8fSApple OSS Distributions 
35*bb611c8fSApple OSS Distributions 	// Test the const version
36*bb611c8fSApple OSS Distributions 	{
37*bb611c8fSApple OSS Distributions 		RawT* memory = reinterpret_cast<RawT*>(malloc_allocator::allocate(10 * sizeof(RawT)));
38*bb611c8fSApple OSS Distributions 		for (RawT* ptr = memory; ptr != memory + 10; ++ptr) {
39*bb611c8fSApple OSS Distributions 			*ptr = RawT{ptr - memory}; // number from 0 to 9
40*bb611c8fSApple OSS Distributions 		}
41*bb611c8fSApple OSS Distributions 
42*bb611c8fSApple OSS Distributions 		test_safe_allocation<QualT> const array(memory, 10, libkern::adopt_memory);
43*bb611c8fSApple OSS Distributions 		for (std::ptrdiff_t n = 0; n != 10; ++n) {
44*bb611c8fSApple OSS Distributions 			QualT const& element = array[n];
45*bb611c8fSApple OSS Distributions 			CHECK(&element == memory + n);
46*bb611c8fSApple OSS Distributions 		}
47*bb611c8fSApple OSS Distributions 	}
48*bb611c8fSApple OSS Distributions 
49*bb611c8fSApple OSS Distributions 	// Test with OOB offsets (should trap)
50*bb611c8fSApple OSS Distributions 	{
51*bb611c8fSApple OSS Distributions 		using Alloc = libkern::safe_allocation<RawT, malloc_allocator, tracking_trapping_policy>;
52*bb611c8fSApple OSS Distributions 		RawT* memory = reinterpret_cast<RawT*>(malloc_allocator::allocate(10 * sizeof(RawT)));
53*bb611c8fSApple OSS Distributions 		Alloc const array(memory, 10, libkern::adopt_memory);
54*bb611c8fSApple OSS Distributions 
55*bb611c8fSApple OSS Distributions 		// Negative offsets
56*bb611c8fSApple OSS Distributions 		{
57*bb611c8fSApple OSS Distributions 			tracking_trapping_policy::reset();
58*bb611c8fSApple OSS Distributions 			(void)array[-1];
59*bb611c8fSApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
60*bb611c8fSApple OSS Distributions 		}
61*bb611c8fSApple OSS Distributions 		{
62*bb611c8fSApple OSS Distributions 			tracking_trapping_policy::reset();
63*bb611c8fSApple OSS Distributions 			(void)array[-10];
64*bb611c8fSApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
65*bb611c8fSApple OSS Distributions 		}
66*bb611c8fSApple OSS Distributions 
67*bb611c8fSApple OSS Distributions 		// Too large offsets
68*bb611c8fSApple OSS Distributions 		{
69*bb611c8fSApple OSS Distributions 			tracking_trapping_policy::reset();
70*bb611c8fSApple OSS Distributions 			(void)array[10];
71*bb611c8fSApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
72*bb611c8fSApple OSS Distributions 		}
73*bb611c8fSApple OSS Distributions 		{
74*bb611c8fSApple OSS Distributions 			tracking_trapping_policy::reset();
75*bb611c8fSApple OSS Distributions 			(void)array[11];
76*bb611c8fSApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
77*bb611c8fSApple OSS Distributions 		}
78*bb611c8fSApple OSS Distributions 	}
79*bb611c8fSApple OSS Distributions }
80*bb611c8fSApple OSS Distributions 
81*bb611c8fSApple OSS Distributions T_DECL(operator_subscript, "safe_allocation.operator.subscript") {
82*bb611c8fSApple OSS Distributions 	tests<T, T>();
83*bb611c8fSApple OSS Distributions 	tests<T, T const>();
84*bb611c8fSApple OSS Distributions }
85