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