Skip to content

Commit 8318ab3

Browse files
committed
[view] Add tests for modifying the elements of a view
1 parent c0544cd commit 8318ab3

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed

Diff for: include/boost/hana/view.hpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,14 @@ BOOST_HANA_NAMESPACE_BEGIN
349349

350350
// single_view
351351
template <typename T, typename N>
352-
static constexpr decltype(auto) apply(detail::single_view_t<T> view, N const&) {
352+
static constexpr T& apply(detail::single_view_t<T>& view, N const&) {
353+
static_assert(N::value == 0,
354+
"trying to fetch an out-of-bounds element in a hana::single_view");
355+
return view.value_;
356+
}
357+
358+
template <typename T, typename N>
359+
static constexpr T const& apply(detail::single_view_t<T> const& view, N const&) {
353360
static_assert(N::value == 0,
354361
"trying to fetch an out-of-bounds element in a hana::single_view");
355362
return view.value_;

Diff for: test/view/flattened/modifications.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Louis Dionne 2013-2016
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include <boost/hana/assert.hpp>
6+
#include <boost/hana/at.hpp>
7+
#include <boost/hana/view.hpp>
8+
9+
#include <support/seq.hpp>
10+
namespace hana = boost::hana;
11+
12+
13+
int main() {
14+
auto container = ::seq;
15+
16+
auto storage = container(container(0, 1, 2),
17+
container(3, 4),
18+
container(),
19+
container(5));
20+
auto view = hana::detail::flattened(storage);
21+
hana::at_c<0>(view) = 90;
22+
hana::at_c<1>(view) = 91;
23+
hana::at_c<2>(view) = 92;
24+
hana::at_c<3>(view) = 93;
25+
hana::at_c<4>(view) = 94;
26+
hana::at_c<5>(view) = 95;
27+
28+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
29+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
30+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);
31+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<3>(view) == 93);
32+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<4>(view) == 94);
33+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<5>(view) == 95);
34+
35+
auto& storage1 = hana::at_c<0>(storage);
36+
auto& storage2 = hana::at_c<1>(storage);
37+
auto& storage4 = hana::at_c<3>(storage);
38+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage1) == 90);
39+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage1) == 91);
40+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage1) == 92);
41+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage2) == 93);
42+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage2) == 94);
43+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage4) == 95);
44+
}

Diff for: test/view/identity/modifications.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Louis Dionne 2013-2016
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include <boost/hana/assert.hpp>
6+
#include <boost/hana/at.hpp>
7+
#include <boost/hana/view.hpp>
8+
9+
#include <support/seq.hpp>
10+
namespace hana = boost::hana;
11+
12+
13+
int main() {
14+
auto container = ::seq;
15+
16+
auto storage = container(0, 1, 2);
17+
auto view = hana::detail::identity_view(storage);
18+
hana::at_c<0>(view) = 90;
19+
hana::at_c<1>(view) = 91;
20+
hana::at_c<2>(view) = 92;
21+
22+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
23+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
24+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);
25+
26+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 90);
27+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
28+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 92);
29+
}

Diff for: test/view/joined/modifications.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Louis Dionne 2013-2016
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include <boost/hana/assert.hpp>
6+
#include <boost/hana/at.hpp>
7+
#include <boost/hana/view.hpp>
8+
9+
#include <support/seq.hpp>
10+
namespace hana = boost::hana;
11+
12+
13+
int main() {
14+
auto container = ::seq;
15+
16+
auto storage1 = container(0, 1, 2);
17+
auto storage2 = container(3, 4);
18+
auto view = hana::detail::joined(storage1, storage2);
19+
hana::at_c<0>(view) = 90;
20+
hana::at_c<1>(view) = 91;
21+
hana::at_c<2>(view) = 92;
22+
hana::at_c<3>(view) = 93;
23+
hana::at_c<4>(view) = 94;
24+
25+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
26+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
27+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);
28+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<3>(view) == 93);
29+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<4>(view) == 94);
30+
31+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage1) == 90);
32+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage1) == 91);
33+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage1) == 92);
34+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage2) == 93);
35+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage2) == 94);
36+
}

Diff for: test/view/single/modifications.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright Louis Dionne 2013-2016
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include <boost/hana/assert.hpp>
6+
#include <boost/hana/at.hpp>
7+
#include <boost/hana/view.hpp>
8+
namespace hana = boost::hana;
9+
10+
11+
int main() {
12+
auto view = hana::detail::single_view(3);
13+
hana::at_c<0>(view) = 93;
14+
15+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 93);
16+
}

Diff for: test/view/sliced/modifications.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright Louis Dionne 2013-2016
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
4+
5+
#include <boost/hana/assert.hpp>
6+
#include <boost/hana/at.hpp>
7+
#include <boost/hana/range.hpp>
8+
#include <boost/hana/view.hpp>
9+
10+
#include <support/seq.hpp>
11+
12+
#include <cstddef>
13+
namespace hana = boost::hana;
14+
15+
16+
int main() {
17+
auto container = ::seq;
18+
19+
{
20+
auto storage = container(0, 1, 2);
21+
auto view = hana::detail::sliced(storage, hana::range_c<std::size_t, 0, 3>);
22+
hana::at_c<0>(view) = 90;
23+
hana::at_c<1>(view) = 91;
24+
hana::at_c<2>(view) = 92;
25+
26+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
27+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
28+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);
29+
30+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 90);
31+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
32+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 92);
33+
}
34+
35+
{
36+
auto storage = container(0, 1, 2);
37+
auto view = hana::detail::sliced(storage, hana::range_c<std::size_t, 0, 2>);
38+
hana::at_c<0>(view) = 90;
39+
hana::at_c<1>(view) = 91;
40+
41+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
42+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
43+
44+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 90);
45+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
46+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 02);
47+
}
48+
49+
{
50+
auto storage = container(7, 1, 2);
51+
auto view = hana::detail::sliced(storage, hana::range_c<std::size_t, 1, 3>);
52+
hana::at_c<0>(view) = 91;
53+
hana::at_c<1>(view) = 92;
54+
55+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 91);
56+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 92);
57+
58+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 07);
59+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
60+
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 92);
61+
}
62+
}

0 commit comments

Comments
 (0)