1*bb611c8fSApple OSS Distributions // 2*bb611c8fSApple OSS Distributions // Tests for 3*bb611c8fSApple OSS Distributions // explicit bounded_array_ref(bounded_ptr<T, TrappingPolicy> data, size_t n); 4*bb611c8fSApple OSS Distributions // 5*bb611c8fSApple OSS Distributions 6*bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_array_ref.h> 7*bb611c8fSApple OSS Distributions #include "test_policy.h" 8*bb611c8fSApple OSS Distributions #include <cstddef> 9*bb611c8fSApple OSS Distributions #include <darwintest.h> 10*bb611c8fSApple OSS Distributions #include <darwintest_utils.h> 11*bb611c8fSApple OSS Distributions 12*bb611c8fSApple OSS Distributions struct T { int i; }; 13*bb611c8fSApple OSS Distributions inline bool 14*bb611c8fSApple OSS Distributions operator==(T const& a, T const& b) 15*bb611c8fSApple OSS Distributions { 16*bb611c8fSApple OSS Distributions return a.i == b.i; 17*bb611c8fSApple OSS Distributions }; 18*bb611c8fSApple OSS Distributions 19*bb611c8fSApple OSS Distributions template <typename T> 20*bb611c8fSApple OSS Distributions static void 21*bb611c8fSApple OSS Distributions tests() 22*bb611c8fSApple OSS Distributions { 23*bb611c8fSApple OSS Distributions T array[5] = {T{0}, T{1}, T{2}, T{3}, T{4}}; 24*bb611c8fSApple OSS Distributions T* const begin = &array[0]; 25*bb611c8fSApple OSS Distributions T* const end = &array[5]; 26*bb611c8fSApple OSS Distributions 27*bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last> 28*bb611c8fSApple OSS Distributions // ^ ^ 29*bb611c8fSApple OSS Distributions // | | 30*bb611c8fSApple OSS Distributions // begin,ptr end 31*bb611c8fSApple OSS Distributions // 32*bb611c8fSApple OSS Distributions // ^------------------- view -----------------------^ 33*bb611c8fSApple OSS Distributions { 34*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr(&array[0], begin, end); 35*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 5); 36*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[0]); 37*bb611c8fSApple OSS Distributions CHECK(view.size() == 5); 38*bb611c8fSApple OSS Distributions CHECK(view[0] == T{0}); 39*bb611c8fSApple OSS Distributions CHECK(view[1] == T{1}); 40*bb611c8fSApple OSS Distributions CHECK(view[2] == T{2}); 41*bb611c8fSApple OSS Distributions CHECK(view[3] == T{3}); 42*bb611c8fSApple OSS Distributions CHECK(view[4] == T{4}); 43*bb611c8fSApple OSS Distributions } 44*bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last> 45*bb611c8fSApple OSS Distributions // ^ ^ 46*bb611c8fSApple OSS Distributions // | | 47*bb611c8fSApple OSS Distributions // begin,ptr end 48*bb611c8fSApple OSS Distributions // 49*bb611c8fSApple OSS Distributions // ^----- view -----^ 50*bb611c8fSApple OSS Distributions { 51*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr(&array[0], begin, end); 52*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 3); 53*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[0]); 54*bb611c8fSApple OSS Distributions CHECK(view.size() == 3); 55*bb611c8fSApple OSS Distributions CHECK(view[0] == T{0}); 56*bb611c8fSApple OSS Distributions CHECK(view[1] == T{1}); 57*bb611c8fSApple OSS Distributions CHECK(view[2] == T{2}); 58*bb611c8fSApple OSS Distributions } 59*bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last> 60*bb611c8fSApple OSS Distributions // ^ ^ ^ 61*bb611c8fSApple OSS Distributions // | | | 62*bb611c8fSApple OSS Distributions // begin ptr end 63*bb611c8fSApple OSS Distributions // 64*bb611c8fSApple OSS Distributions // ^------- view --------^ 65*bb611c8fSApple OSS Distributions { 66*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr(&array[3], begin, end); 67*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 2); 68*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[3]); 69*bb611c8fSApple OSS Distributions CHECK(view.size() == 2); 70*bb611c8fSApple OSS Distributions CHECK(view[0] == T{3}); 71*bb611c8fSApple OSS Distributions CHECK(view[1] == T{4}); 72*bb611c8fSApple OSS Distributions } 73*bb611c8fSApple OSS Distributions // Check with a valid `bounded_ptr` and a size of 0. 74*bb611c8fSApple OSS Distributions { 75*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr(&array[0], begin, end); 76*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 0); 77*bb611c8fSApple OSS Distributions CHECK(view.size() == 0); 78*bb611c8fSApple OSS Distributions } 79*bb611c8fSApple OSS Distributions // Check with a null `bounded_ptr` and a size of 0. 80*bb611c8fSApple OSS Distributions { 81*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr = nullptr; 82*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 0); 83*bb611c8fSApple OSS Distributions CHECK(view.size() == 0); 84*bb611c8fSApple OSS Distributions } 85*bb611c8fSApple OSS Distributions // Check with a non-null but invalid `bounded_ptr` and a size of 0. 86*bb611c8fSApple OSS Distributions { 87*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr(end, begin, end); 88*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 0); 89*bb611c8fSApple OSS Distributions CHECK(view.size() == 0); 90*bb611c8fSApple OSS Distributions } 91*bb611c8fSApple OSS Distributions // Make sure there's no ambiguity between constructors. 92*bb611c8fSApple OSS Distributions { 93*bb611c8fSApple OSS Distributions test_bounded_ptr<T> ptr(begin, begin, end); 94*bb611c8fSApple OSS Distributions std::ptrdiff_t size = sizeof(array) / sizeof(*array); 95*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, size); 96*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[0]); 97*bb611c8fSApple OSS Distributions CHECK(view.size() == 5); 98*bb611c8fSApple OSS Distributions } 99*bb611c8fSApple OSS Distributions } 100*bb611c8fSApple OSS Distributions 101*bb611c8fSApple OSS Distributions T_DECL(ctor_bounded_ptr, "bounded_array_ref.ctor.bounded_ptr") { 102*bb611c8fSApple OSS Distributions tests<T>(); 103*bb611c8fSApple OSS Distributions tests<T const>(); 104*bb611c8fSApple OSS Distributions } 105