1/*
2Copyright 2019-2023 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7*/
8#include <boost/config.hpp>
9#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE)
10#include <boost/core/span.hpp>
11#include <boost/core/lightweight_test_trait.hpp>
12
13template<class T>
14struct range {
15 T* data() {
16 return 0;
17 }
18
19 const T* data() const {
20 return 0;
21 }
22
23 std::size_t size() const {
24 return 0;
25 }
26};
27
28struct base { };
29
30struct derived
31 : base { };
32
33void test_default()
34{
35 BOOST_TEST_TRAIT_TRUE((std::is_default_constructible<
36 boost::span<int> >));
37 BOOST_TEST_TRAIT_TRUE((std::is_default_constructible<
38 boost::span<int, 0> >));
39 BOOST_TEST_TRAIT_FALSE((std::is_default_constructible<
40 boost::span<int, 2> >));
41}
42
43void test_data_size()
44{
45 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
46 int*, std::size_t>));
47 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
48 int*, std::size_t>));
49 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
50 int, std::size_t>));
51 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
52 const int*, std::size_t>));
53 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
54 derived*, std::size_t>));
55}
56
57void test_first_last()
58{
59 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
60 int*, int*>));
61 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
62 int*, const int*>));
63 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
64 int*, int*>));
65 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
66 int, int*>));
67 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
68 const int*, int*>));
69 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
70 derived*, derived*>));
71}
72
73void test_array()
74{
75 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
76 int(&)[4]>));
77 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int, 4>,
78 int(&)[4]>));
79 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 2>,
80 int(&)[4]>));
81 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
82 int(&)[2]>));
83}
84
85void test_std_array()
86{
87 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
88 std::array<int, 4>&>));
89 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
90 std::array<int, 4>&>));
91 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int, 4>,
92 std::array<int, 4>&>));
93 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int,
94 4>, std::array<int, 4>&>));
95 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
96 std::array<const int, 4>&>));
97 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
98 std::array<derived, 4>&>));
99 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 2>,
100 std::array<int, 4>&>));
101 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
102 std::array<const int, 4>&>));
103 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base, 4>,
104 std::array<derived, 4>&>));
105}
106
107void test_const_std_array()
108{
109 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
110 const std::array<int, 4> >));
111 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int,
112 4>, const std::array<int, 4> >));
113 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
114 const std::array<int, 4> >));
115 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<const
116 base>, const std::array<derived, 4> >));
117 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<const int,
118 2>, const std::array<int, 4> >));
119 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
120 const std::array<int, 4> >));
121 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<const
122 base, 4>, const std::array<derived, 4> >));
123}
124
125void test_range()
126{
127 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
128 range<int>&>));
129 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
130 range<int>&>));
131 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
132 range<int> >));
133 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
134 int*>));
135 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
136 range<int> >));
137 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
138 const range<int>&>));
139 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
140 range<derived>&>));
141}
142
143void test_initializer_list()
144{
145 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
146 std::initializer_list<int> >));
147 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
148 std::initializer_list<int> >));
149}
150
151void test_span()
152{
153 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int>,
154 boost::span<int> >));
155 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int>,
156 boost::span<int, 4> >));
157 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<int, 4>,
158 boost::span<int> >));
159 BOOST_TEST_TRAIT_TRUE((std::is_constructible<boost::span<const int,
160 4>, boost::span<int, 4> >));
161 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int>,
162 boost::span<const int> >));
163 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base>,
164 boost::span<derived> >));
165 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 2>,
166 boost::span<int, 4> >));
167 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<int, 4>,
168 boost::span<const int, 4> >));
169 BOOST_TEST_TRAIT_FALSE((std::is_constructible<boost::span<base, 4>,
170 boost::span<derived, 4> >));
171}
172
173void test_copy()
174{
175 BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible<
176 boost::span<int> >));
177 BOOST_TEST_TRAIT_TRUE((std::is_copy_constructible<
178 boost::span<int, 4> >));
179}
180
181void test_assign()
182{
183 BOOST_TEST_TRAIT_TRUE((std::is_copy_assignable<
184 boost::span<int> >));
185 BOOST_TEST_TRAIT_TRUE((std::is_copy_assignable<
186 boost::span<int, 4> >));
187}
188
189int main()
190{
191 test_default();
192 test_data_size();
193 test_first_last();
194 test_array();
195 test_std_array();
196 test_const_std_array();
197 test_range();
198 test_initializer_list();
199 test_span();
200 test_copy();
201 test_assign();
202 return boost::report_errors();
203}
204#else
205int main()
206{
207 return 0;
208}
209#endif
210

source code of boost/libs/core/test/span_constructible_test.cpp