1*bb611c8fSApple OSS Distributions // 2*bb611c8fSApple OSS Distributions // Tests for 3*bb611c8fSApple OSS Distributions // explicit bounded_array_ref(T* 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 25*bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last> 26*bb611c8fSApple OSS Distributions // ^ 27*bb611c8fSApple OSS Distributions // | 28*bb611c8fSApple OSS Distributions // ptr 29*bb611c8fSApple OSS Distributions // 30*bb611c8fSApple OSS Distributions // ^------------------- view -----------------------^ 31*bb611c8fSApple OSS Distributions { 32*bb611c8fSApple OSS Distributions T* ptr = &array[0]; 33*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 5); 34*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[0]); 35*bb611c8fSApple OSS Distributions CHECK(view.size() == 5); 36*bb611c8fSApple OSS Distributions CHECK(view[0] == T{0}); 37*bb611c8fSApple OSS Distributions CHECK(view[1] == T{1}); 38*bb611c8fSApple OSS Distributions CHECK(view[2] == T{2}); 39*bb611c8fSApple OSS Distributions CHECK(view[3] == T{3}); 40*bb611c8fSApple OSS Distributions CHECK(view[4] == T{4}); 41*bb611c8fSApple OSS Distributions } 42*bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last> 43*bb611c8fSApple OSS Distributions // ^ 44*bb611c8fSApple OSS Distributions // | 45*bb611c8fSApple OSS Distributions // ptr 46*bb611c8fSApple OSS Distributions // 47*bb611c8fSApple OSS Distributions // ^----- view -----^ 48*bb611c8fSApple OSS Distributions { 49*bb611c8fSApple OSS Distributions T* ptr = &array[0]; 50*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 3); 51*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[0]); 52*bb611c8fSApple OSS Distributions CHECK(view.size() == 3); 53*bb611c8fSApple OSS Distributions CHECK(view[0] == T{0}); 54*bb611c8fSApple OSS Distributions CHECK(view[1] == T{1}); 55*bb611c8fSApple OSS Distributions CHECK(view[2] == T{2}); 56*bb611c8fSApple OSS Distributions } 57*bb611c8fSApple OSS Distributions // T{0} T{1} T{2} T{3} T{4} <one-past-last> 58*bb611c8fSApple OSS Distributions // ^ 59*bb611c8fSApple OSS Distributions // | 60*bb611c8fSApple OSS Distributions // ptr 61*bb611c8fSApple OSS Distributions // 62*bb611c8fSApple OSS Distributions // ^------- view --------^ 63*bb611c8fSApple OSS Distributions { 64*bb611c8fSApple OSS Distributions T* ptr = &array[3]; 65*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, 2); 66*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[3]); 67*bb611c8fSApple OSS Distributions CHECK(view.size() == 2); 68*bb611c8fSApple OSS Distributions CHECK(view[0] == T{3}); 69*bb611c8fSApple OSS Distributions CHECK(view[1] == T{4}); 70*bb611c8fSApple OSS Distributions } 71*bb611c8fSApple OSS Distributions // Check with a valid pointer and a size of 0. 72*bb611c8fSApple OSS Distributions { 73*bb611c8fSApple OSS Distributions T* ptr = &array[0]; 74*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, static_cast<std::size_t>(0)); 75*bb611c8fSApple OSS Distributions CHECK(view.size() == 0); 76*bb611c8fSApple OSS Distributions } 77*bb611c8fSApple OSS Distributions // Check with a null pointer and a size of 0. 78*bb611c8fSApple OSS Distributions { 79*bb611c8fSApple OSS Distributions T* ptr = nullptr; 80*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, static_cast<std::size_t>(0)); 81*bb611c8fSApple OSS Distributions CHECK(view.size() == 0); 82*bb611c8fSApple OSS Distributions } 83*bb611c8fSApple OSS Distributions // Check with a non-null but invalid pointer and a size of 0. 84*bb611c8fSApple OSS Distributions { 85*bb611c8fSApple OSS Distributions T* ptr = &array[5]; 86*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, static_cast<std::size_t>(0)); 87*bb611c8fSApple OSS Distributions CHECK(view.size() == 0); 88*bb611c8fSApple OSS Distributions } 89*bb611c8fSApple OSS Distributions // Make sure there's no ambiguity between constructors. 90*bb611c8fSApple OSS Distributions { 91*bb611c8fSApple OSS Distributions T* ptr = &array[0]; 92*bb611c8fSApple OSS Distributions std::ptrdiff_t size = 5; 93*bb611c8fSApple OSS Distributions test_bounded_array_ref<T> view(ptr, size); 94*bb611c8fSApple OSS Distributions CHECK(view.data() == &array[0]); 95*bb611c8fSApple OSS Distributions CHECK(view.size() == 5); 96*bb611c8fSApple OSS Distributions } 97*bb611c8fSApple OSS Distributions 98*bb611c8fSApple OSS Distributions // Make sure we can create nested bounded_array_refs 99*bb611c8fSApple OSS Distributions { 100*bb611c8fSApple OSS Distributions int array1[] = {1, 2, 3, 4, 5}; 101*bb611c8fSApple OSS Distributions int array2[] = {6, 7, 8}; 102*bb611c8fSApple OSS Distributions int array3[] = {9, 10, 11, 12, 13, 14}; 103*bb611c8fSApple OSS Distributions test_bounded_array_ref<int> views[] = { 104*bb611c8fSApple OSS Distributions test_bounded_array_ref<int>(array1, 5), 105*bb611c8fSApple OSS Distributions test_bounded_array_ref<int>(array2, 3), 106*bb611c8fSApple OSS Distributions test_bounded_array_ref<int>(array3, 6) 107*bb611c8fSApple OSS Distributions }; 108*bb611c8fSApple OSS Distributions 109*bb611c8fSApple OSS Distributions test_bounded_array_ref<test_bounded_array_ref<int> > two_dim(views, 3); 110*bb611c8fSApple OSS Distributions CHECK(two_dim.size() == 3); 111*bb611c8fSApple OSS Distributions CHECK(two_dim.data() == &views[0]); 112*bb611c8fSApple OSS Distributions CHECK(&two_dim[0] == &views[0]); 113*bb611c8fSApple OSS Distributions CHECK(&two_dim[1] == &views[1]); 114*bb611c8fSApple OSS Distributions CHECK(&two_dim[2] == &views[2]); 115*bb611c8fSApple OSS Distributions } 116*bb611c8fSApple OSS Distributions } 117*bb611c8fSApple OSS Distributions 118*bb611c8fSApple OSS Distributions T_DECL(ctor_raw_ptr, "bounded_array_ref.ctor.raw_ptr") { 119*bb611c8fSApple OSS Distributions tests<T>(); 120*bb611c8fSApple OSS Distributions tests<T const>(); 121*bb611c8fSApple OSS Distributions } 122