1 // Copyright (c) 2016 Agustin Berge
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <hpx/config.hpp>
7 #include <hpx/util/range.hpp>
8 #include <hpx/util/lightweight_test.hpp>
9
10 #include <vector>
11
12 ///////////////////////////////////////////////////////////////////////////////
array_range()13 void array_range()
14 {
15 int r[3] = { 0, 1, 2 };
16 HPX_TEST_EQ(hpx::util::begin(r), &r[0]);
17 HPX_TEST_EQ(hpx::util::end(r), &r[3]);
18
19 int const cr[3] = { 0, 1, 2 };
20 HPX_TEST_EQ(hpx::util::begin(cr), &cr[0]);
21 HPX_TEST_EQ(hpx::util::end(cr), &cr[3]);
22 HPX_TEST_EQ(hpx::util::size(cr), 3u);
23 HPX_TEST_EQ(hpx::util::empty(cr), false);
24 }
25
26 ///////////////////////////////////////////////////////////////////////////////
27 struct member
28 {
29 int x;
30
beginmember31 int* begin()
32 {
33 return &x;
34 }
35
beginmember36 int const* begin() const
37 {
38 return &x;
39 }
40
endmember41 int* end()
42 {
43 return &x + 1;
44 }
45
endmember46 int const* end() const
47 {
48 return &x + 1;
49 }
50 };
51
member_range()52 void member_range()
53 {
54 member r = member();
55 HPX_TEST_EQ(hpx::util::begin(r), &r.x);
56 HPX_TEST_EQ(hpx::util::end(r), &r.x + 1);
57
58 member const cr = member();
59 HPX_TEST_EQ(hpx::util::begin(cr), &cr.x);
60 HPX_TEST_EQ(hpx::util::end(cr), &cr.x + 1);
61 HPX_TEST_EQ(hpx::util::size(cr), 1u);
62 HPX_TEST_EQ(hpx::util::empty(cr), false);
63 }
64
65 ///////////////////////////////////////////////////////////////////////////////
66 namespace adl
67 {
68 struct free
69 {
70 int x;
71 };
72
begin(free & r)73 int* begin(free& r)
74 {
75 return &r.x;
76 }
77
begin(free const & r)78 int const* begin(free const& r)
79 {
80 return &r.x;
81 }
82
end(free & r)83 int* end(free& r)
84 {
85 return &r.x + 1;
86 }
87
end(free const & r)88 int const* end(free const& r)
89 {
90 return &r.x + 1;
91 }
92 }
93
adl_range()94 void adl_range()
95 {
96 adl::free r = adl::free();
97 HPX_TEST_EQ(hpx::util::begin(r), &r.x);
98 HPX_TEST_EQ(hpx::util::end(r), &r.x + 1);
99
100 adl::free const cr = adl::free();
101 HPX_TEST_EQ(hpx::util::begin(cr), &cr.x);
102 HPX_TEST_EQ(hpx::util::end(cr), &cr.x + 1);
103 HPX_TEST_EQ(hpx::util::size(cr), 1u);
104 HPX_TEST_EQ(hpx::util::empty(cr), false);
105 }
106
107 ///////////////////////////////////////////////////////////////////////////////
vector_range()108 void vector_range()
109 {
110 std::vector<int> r(3);
111 HPX_TEST(hpx::util::begin(r) == r.begin());
112 HPX_TEST(hpx::util::end(r) == r.end());
113
114 std::vector<int> cr(3);
115 HPX_TEST(hpx::util::begin(cr) == cr.begin());
116 HPX_TEST(hpx::util::end(cr) == cr.end());
117 HPX_TEST_EQ(hpx::util::size(cr), 3u);
118 HPX_TEST_EQ(hpx::util::empty(cr), false);
119 }
120
121 ///////////////////////////////////////////////////////////////////////////////
main(int argc,char * argv[])122 int main(int argc, char* argv[])
123 {
124 {
125 array_range();
126 member_range();
127 adl_range();
128 vector_range();
129 }
130
131 return hpx::util::report_errors();
132 }
133