]>
git.saurik.com Git - apple/xnu.git/blob - tests/safe_allocation_src/operator.subscript.cpp
3 // T& operator[](std::ptrdiff_t n);
4 // T const& operator[](std::ptrdiff_t n) const;
7 #include <libkern/c++/safe_allocation.h>
8 #include <darwintest.h>
9 #include "test_utils.h"
17 template <typename RawT
, typename QualT
>
21 // Test the non-const version
23 RawT
* memory
= reinterpret_cast<RawT
*>(malloc_allocator::allocate(10 * sizeof(RawT
)));
24 for (RawT
* ptr
= memory
; ptr
!= memory
+ 10; ++ptr
) {
25 *ptr
= RawT
{ptr
- memory
}; // number from 0 to 9
28 test_safe_allocation
<QualT
> array(memory
, 10, libkern::adopt_memory
);
29 for (std::ptrdiff_t n
= 0; n
!= 10; ++n
) {
30 QualT
& element
= array
[n
];
31 CHECK(&element
== memory
+ n
);
35 // Test the const version
37 RawT
* memory
= reinterpret_cast<RawT
*>(malloc_allocator::allocate(10 * sizeof(RawT
)));
38 for (RawT
* ptr
= memory
; ptr
!= memory
+ 10; ++ptr
) {
39 *ptr
= RawT
{ptr
- memory
}; // number from 0 to 9
42 test_safe_allocation
<QualT
> const array(memory
, 10, libkern::adopt_memory
);
43 for (std::ptrdiff_t n
= 0; n
!= 10; ++n
) {
44 QualT
const& element
= array
[n
];
45 CHECK(&element
== memory
+ n
);
49 // Test with OOB offsets (should trap)
51 using Alloc
= libkern::safe_allocation
<RawT
, malloc_allocator
, tracking_trapping_policy
>;
52 RawT
* memory
= reinterpret_cast<RawT
*>(malloc_allocator::allocate(10 * sizeof(RawT
)));
53 Alloc
const array(memory
, 10, libkern::adopt_memory
);
57 tracking_trapping_policy::reset();
59 CHECK(tracking_trapping_policy::did_trap
);
62 tracking_trapping_policy::reset();
64 CHECK(tracking_trapping_policy::did_trap
);
69 tracking_trapping_policy::reset();
71 CHECK(tracking_trapping_policy::did_trap
);
74 tracking_trapping_policy::reset();
76 CHECK(tracking_trapping_policy::did_trap
);
81 T_DECL(operator_subscript
, "safe_allocation.operator.subscript") {