1 // 2 // Tests for 3 // T& operator[](ptrdiff_t n); 4 // T const& operator[](ptrdiff_t n) const; 5 // 6 7 #include <libkern/c++/bounded_array.h> 8 #include "test_policy.h" 9 #include <darwintest.h> 10 #include <type_traits> 11 12 struct T { int i; }; 13 inline bool 14 operator==(T const& a, T const& b) 15 { 16 return a.i == b.i; 17 } 18 19 template <typename T> 20 static void 21 tests() 22 { 23 { 24 test_bounded_array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}}; 25 T& a0 = array[0]; 26 CHECK(&a0 == array.data()); 27 CHECK(a0 == T{0}); 28 T& a1 = array[1]; 29 CHECK(a1 == T{1}); 30 T& a2 = array[2]; 31 CHECK(a2 == T{2}); 32 T& a3 = array[3]; 33 CHECK(a3 == T{3}); 34 T& a4 = array[4]; 35 CHECK(a4 == T{4}); 36 } 37 38 { 39 test_bounded_array<T, 5> const array = {T{0}, T{1}, T{2}, T{3}, T{4}}; 40 T const& a0 = array[0]; 41 CHECK(&a0 == array.data()); 42 CHECK(a0 == T{0}); 43 T const& a1 = array[1]; 44 CHECK(a1 == T{1}); 45 T const& a2 = array[2]; 46 CHECK(a2 == T{2}); 47 T const& a3 = array[3]; 48 CHECK(a3 == T{3}); 49 T const& a4 = array[4]; 50 CHECK(a4 == T{4}); 51 } 52 } 53 54 T_DECL(operator_subscript, "bounded_array.operator.subscript") { 55 tests<T>(); 56 } 57