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