• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Louis Dionne 2013-2017
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_key.hpp>
7 #include <boost/hana/map.hpp>
8 #include <boost/hana/pair.hpp>
9 
10 #include <laws/base.hpp>
11 
12 #include <string>
13 #include <utility>
14 namespace hana = boost::hana;
15 namespace test = hana::test;
16 
17 
18 struct MoveOnly {
19     int data_;
20     MoveOnly(MoveOnly const&) = delete;
21     MoveOnly& operator=(MoveOnly const&) = delete;
MoveOnlyMoveOnly22     MoveOnly(int data = 1) : data_(data) { }
MoveOnlyMoveOnly23     MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; }
24 
operator =MoveOnly25     MoveOnly& operator=(MoveOnly&& x)
26     { data_ = x.data_; x.data_ = 0; return *this; }
27 
getMoveOnly28     int get() const {return data_;}
operator ==MoveOnly29     bool operator==(const MoveOnly& x) const { return data_ == x.data_; }
operator <MoveOnly30     bool operator< (const MoveOnly& x) const { return data_ <  x.data_; }
31 };
32 
main()33 int main() {
34     {
35         using Map = hana::map<>;
36         Map map0;
37         Map map;
38         map0 = std::move(map);
39     }
40     {
41         using Map = hana::map<hana::pair<test::ct_eq<0>, MoveOnly>>;
42         Map map0 = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{999}));
43         Map map  = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{4}));
44         map0 = std::move(map);
45         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<0>{}) == MoveOnly{4});
46     }
47     {
48         using Map = hana::map<hana::pair<test::ct_eq<0>, MoveOnly>,
49                               hana::pair<test::ct_eq<1>, MoveOnly>>;
50         Map map0 = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{999}),
51                                   hana::make_pair(test::ct_eq<1>{}, MoveOnly{888}));
52         Map map  = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{4}),
53                                   hana::make_pair(test::ct_eq<1>{}, MoveOnly{5}));
54         map0 = std::move(map);
55         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<0>{}) == MoveOnly{4});
56         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<1>{}) == MoveOnly{5});
57     }
58     {
59         using Map = hana::map<hana::pair<test::ct_eq<0>, MoveOnly>,
60                               hana::pair<test::ct_eq<1>, MoveOnly>,
61                               hana::pair<test::ct_eq<2>, MoveOnly>>;
62         Map map0 = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{999}),
63                                   hana::make_pair(test::ct_eq<1>{}, MoveOnly{888}),
64                                   hana::make_pair(test::ct_eq<2>{}, MoveOnly{777}));
65         Map map  = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{4}),
66                                   hana::make_pair(test::ct_eq<1>{}, MoveOnly{5}),
67                                   hana::make_pair(test::ct_eq<2>{}, MoveOnly{6}));
68         map0 = std::move(map);
69         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<0>{}) == MoveOnly{4});
70         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<1>{}) == MoveOnly{5});
71         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<2>{}) == MoveOnly{6});
72     }
73     {
74         using Map = hana::map<hana::pair<test::ct_eq<0>, MoveOnly>,
75                               hana::pair<test::ct_eq<1>, MoveOnly>,
76                               hana::pair<test::ct_eq<2>, MoveOnly>,
77                               hana::pair<test::ct_eq<3>, std::string>>;
78         Map map0 = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{999}),
79                                   hana::make_pair(test::ct_eq<1>{}, MoveOnly{888}),
80                                   hana::make_pair(test::ct_eq<2>{}, MoveOnly{777}),
81                                   hana::make_pair(test::ct_eq<3>{}, std::string{"zzzzz"}));
82         Map map  = hana::make_map(hana::make_pair(test::ct_eq<0>{}, MoveOnly{4}),
83                                   hana::make_pair(test::ct_eq<1>{}, MoveOnly{5}),
84                                   hana::make_pair(test::ct_eq<2>{}, MoveOnly{6}),
85                                   hana::make_pair(test::ct_eq<3>{}, std::string{"abc"}));
86         map0 = std::move(map);
87         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<0>{}) == MoveOnly{4});
88         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<1>{}) == MoveOnly{5});
89         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<2>{}) == MoveOnly{6});
90         BOOST_HANA_RUNTIME_CHECK(hana::at_key(map0, test::ct_eq<3>{}) == std::string{"abc"});
91     }
92 }
93