• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2009 Hartmut Kaiser
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 
9 #include <boost/fusion/include/vector.hpp>
10 #include <boost/fusion/include/struct.hpp>
11 #include <boost/fusion/include/equal_to.hpp>
12 #include <boost/fusion/include/io.hpp>
13 #include <boost/fusion/include/nview.hpp>
14 
15 #include <string>
16 #include <iostream>
17 
18 struct test {
19     int int_;
20     std::string string_;
21     double double_;
22 };
23 
24 BOOST_FUSION_ADAPT_STRUCT(
25     test,
26     (int, int_)
27     (std::string, string_)
28     (double, double_)
29 )
30 
31 namespace fusion = boost::fusion;
32 
33 template <typename Sequence>
check_size(Sequence const & s,int seqsize)34 bool check_size(Sequence const& s, int seqsize)
35 {
36     return fusion::size(s) == seqsize;
37 }
38 
39 template <typename Sequence, typename T>
check_deref_begin(Sequence const & s,T val)40 bool check_deref_begin(Sequence const& s, T val)
41 {
42     return fusion::deref(fusion::begin(s)) == val;
43 }
44 
45 template <typename Sequence, typename T>
check_deref_next(Sequence const & s,T val)46 bool check_deref_next(Sequence const& s, T val)
47 {
48     return fusion::deref(fusion::next(fusion::begin(s))) == val;
49 }
50 
51 template <int N, typename Sequence, typename T>
check_deref_advance(Sequence const & s,T val)52 bool check_deref_advance(Sequence const& s, T val)
53 {
54     return fusion::deref(fusion::advance_c<N>(fusion::begin(s))) == val;
55 }
56 
57 template <typename Sequence, typename T>
check_deref_prior(Sequence const & s,T val)58 bool check_deref_prior(Sequence const& s, T val)
59 {
60     return fusion::deref(fusion::prior(fusion::end(s))) == val;
61 }
62 
63 template <int N, typename Sequence, typename T>
check_at(Sequence const & s,T val)64 bool check_at(Sequence const& s, T val)
65 {
66     return fusion::at_c<N>(s) == val;
67 }
68 
69 template <typename Sequence>
check_distance(Sequence const & s,int val)70 bool check_distance(Sequence const& s, int val)
71 {
72     return fusion::distance(fusion::begin(s), fusion::end(s)) == val;
73 }
74 
main()75 int main()
76 {
77     test t;
78     t.int_ = 1;
79     t.string_ = "test";
80     t.double_ = 2.0;
81 
82     using fusion::as_nview;
83 
84     // check size()
85     {
86         BOOST_TEST(check_size(as_nview<0>(t), 1));
87         BOOST_TEST(check_size(as_nview<2, 1>(t), 2));
88         BOOST_TEST(check_size(as_nview<2, 1, 0>(t), 3));
89         BOOST_TEST(check_size(as_nview<2, 1, 0, 2, 0>(t), 5));
90     }
91 
92     // check deref/begin
93     {
94         BOOST_TEST(check_deref_begin(as_nview<0>(t), 1));
95         BOOST_TEST(check_deref_begin(as_nview<2, 1>(t), 2.0));
96         BOOST_TEST(check_deref_begin(as_nview<1, 2, 0>(t), "test"));
97         BOOST_TEST(check_deref_begin(as_nview<2, 1, 0, 2, 0>(t), 2.0));
98     }
99 
100     // check deref/next
101     {
102         BOOST_TEST(check_deref_next(as_nview<2, 1>(t), "test"));
103         BOOST_TEST(check_deref_next(as_nview<1, 2, 0>(t), 2.0));
104         BOOST_TEST(check_deref_next(as_nview<2, 0, 1, 2, 0>(t), 1));
105     }
106 
107     // check deref/advance
108     {
109         BOOST_TEST(check_deref_advance<0>(as_nview<2, 1>(t), 2.0));
110         BOOST_TEST(check_deref_advance<2>(as_nview<1, 2, 0>(t), 1));
111         BOOST_TEST(check_deref_advance<4>(as_nview<2, 0, 1, 2, 0>(t), 1));
112     }
113 
114     // check deref/prior
115     {
116         BOOST_TEST(check_deref_prior(as_nview<2, 1>(t), "test"));
117         BOOST_TEST(check_deref_prior(as_nview<1, 2, 0>(t), 1));
118         BOOST_TEST(check_deref_prior(as_nview<2, 0, 1, 2, 0>(t), 1));
119     }
120 
121     // check at
122     {
123         BOOST_TEST(check_at<0>(as_nview<0>(t), 1));
124         BOOST_TEST(check_at<1>(as_nview<2, 1>(t), "test"));
125         BOOST_TEST(check_at<2>(as_nview<1, 2, 0>(t), 1));
126         BOOST_TEST(check_at<4>(as_nview<2, 1, 0, 2, 0>(t), 1));
127     }
128 
129     // check distance
130     {
131         BOOST_TEST(check_distance(as_nview<0>(t), 1));
132         BOOST_TEST(check_distance(as_nview<2, 1>(t), 2));
133         BOOST_TEST(check_distance(as_nview<1, 2, 0>(t), 3));
134         BOOST_TEST(check_distance(as_nview<2, 1, 0, 2, 0>(t), 5));
135     }
136 
137     return boost::report_errors();
138 }
139 
140