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