Skip to content

Commit

Permalink
[view] Add tests for modifying the elements of a view
Browse files Browse the repository at this point in the history
  • Loading branch information
ldionne committed Oct 1, 2016
1 parent c0544cd commit 8318ab3
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/boost/hana/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,14 @@ BOOST_HANA_NAMESPACE_BEGIN

// single_view
template <typename T, typename N>
static constexpr decltype(auto) apply(detail::single_view_t<T> view, N const&) {
static constexpr T& apply(detail::single_view_t<T>& view, N const&) {
static_assert(N::value == 0,
"trying to fetch an out-of-bounds element in a hana::single_view");
return view.value_;
}

template <typename T, typename N>
static constexpr T const& apply(detail::single_view_t<T> const& view, N const&) {
static_assert(N::value == 0,
"trying to fetch an out-of-bounds element in a hana::single_view");
return view.value_;
Expand Down
44 changes: 44 additions & 0 deletions test/view/flattened/modifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/view.hpp>

#include <support/seq.hpp>
namespace hana = boost::hana;


int main() {
auto container = ::seq;

auto storage = container(container(0, 1, 2),
container(3, 4),
container(),
container(5));
auto view = hana::detail::flattened(storage);
hana::at_c<0>(view) = 90;
hana::at_c<1>(view) = 91;
hana::at_c<2>(view) = 92;
hana::at_c<3>(view) = 93;
hana::at_c<4>(view) = 94;
hana::at_c<5>(view) = 95;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<3>(view) == 93);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<4>(view) == 94);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<5>(view) == 95);

auto& storage1 = hana::at_c<0>(storage);
auto& storage2 = hana::at_c<1>(storage);
auto& storage4 = hana::at_c<3>(storage);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage1) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage1) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage1) == 92);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage2) == 93);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage2) == 94);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage4) == 95);
}
29 changes: 29 additions & 0 deletions test/view/identity/modifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/view.hpp>

#include <support/seq.hpp>
namespace hana = boost::hana;


int main() {
auto container = ::seq;

auto storage = container(0, 1, 2);
auto view = hana::detail::identity_view(storage);
hana::at_c<0>(view) = 90;
hana::at_c<1>(view) = 91;
hana::at_c<2>(view) = 92;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 92);
}
36 changes: 36 additions & 0 deletions test/view/joined/modifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/view.hpp>

#include <support/seq.hpp>
namespace hana = boost::hana;


int main() {
auto container = ::seq;

auto storage1 = container(0, 1, 2);
auto storage2 = container(3, 4);
auto view = hana::detail::joined(storage1, storage2);
hana::at_c<0>(view) = 90;
hana::at_c<1>(view) = 91;
hana::at_c<2>(view) = 92;
hana::at_c<3>(view) = 93;
hana::at_c<4>(view) = 94;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<3>(view) == 93);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<4>(view) == 94);

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage1) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage1) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage1) == 92);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage2) == 93);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage2) == 94);
}
16 changes: 16 additions & 0 deletions test/view/single/modifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/view.hpp>
namespace hana = boost::hana;


int main() {
auto view = hana::detail::single_view(3);
hana::at_c<0>(view) = 93;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 93);
}
62 changes: 62 additions & 0 deletions test/view/sliced/modifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/view.hpp>

#include <support/seq.hpp>

#include <cstddef>
namespace hana = boost::hana;


int main() {
auto container = ::seq;

{
auto storage = container(0, 1, 2);
auto view = hana::detail::sliced(storage, hana::range_c<std::size_t, 0, 3>);
hana::at_c<0>(view) = 90;
hana::at_c<1>(view) = 91;
hana::at_c<2>(view) = 92;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(view) == 92);

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 92);
}

{
auto storage = container(0, 1, 2);
auto view = hana::detail::sliced(storage, hana::range_c<std::size_t, 0, 2>);
hana::at_c<0>(view) = 90;
hana::at_c<1>(view) = 91;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 91);

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 90);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 02);
}

{
auto storage = container(7, 1, 2);
auto view = hana::detail::sliced(storage, hana::range_c<std::size_t, 1, 3>);
hana::at_c<0>(view) = 91;
hana::at_c<1>(view) = 92;

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(view) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(view) == 92);

BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(storage) == 07);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(storage) == 91);
BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(storage) == 92);
}
}

0 comments on commit 8318ab3

Please sign in to comment.