]> git.saurik.com Git - apple/xnu.git/blob - tests/bounded_array_src/begin_end.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / tests / bounded_array_src / begin_end.cpp
1 //
2 // Tests for
3 // iterator begin();
4 // const_iterator begin() const;
5 //
6 // iterator end();
7 // const_iterator end() const;
8 //
9
10 #include <libkern/c++/bounded_array.h>
11 #include "test_policy.h"
12 #include <darwintest.h>
13 #include <type_traits>
14
15 struct T { int i; };
16
17 template <typename T>
18 static void
19 tests()
20 {
21 // Check begin()/end() for a non-empty array
22 {
23 test_bounded_array<T, 5> array = {T{0}, T{1}, T{2}, T{3}, T{4}};
24 test_bounded_ptr<T> begin = array.begin();
25 test_bounded_ptr<T> end = array.end();
26 CHECK(begin.discard_bounds() == array.data());
27 CHECK(end.unsafe_discard_bounds() == array.data() + 5);
28 }
29 {
30 test_bounded_array<T, 5> const array = {T{0}, T{1}, T{2}, T{3}, T{4}};
31 test_bounded_ptr<T const> begin = array.begin();
32 test_bounded_ptr<T const> end = array.end();
33 CHECK(begin.discard_bounds() == array.data());
34 CHECK(end.unsafe_discard_bounds() == array.data() + 5);
35 }
36
37 // Check begin()/end() for an empty array
38 {
39 test_bounded_array<T, 0> array = {};
40 test_bounded_ptr<T> begin = array.begin();
41 test_bounded_ptr<T> end = array.end();
42 CHECK(begin.unsafe_discard_bounds() == array.data());
43 CHECK(end.unsafe_discard_bounds() == array.data());
44 }
45 {
46 test_bounded_array<T, 0> const array = {};
47 test_bounded_ptr<T const> begin = array.begin();
48 test_bounded_ptr<T const> end = array.end();
49 CHECK(begin.unsafe_discard_bounds() == array.data());
50 CHECK(end.unsafe_discard_bounds() == array.data());
51 }
52
53 // Check associated types
54 {
55 using A = test_bounded_array<T, 10>;
56 static_assert(std::is_same_v<typename A::iterator, test_bounded_ptr<T> >);
57 static_assert(std::is_same_v<typename A::const_iterator, test_bounded_ptr<T const> >);
58 }
59 }
60
61 T_DECL(begin_end, "bounded_array.begin_end") {
62 tests<T>();
63 }