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