• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Convert test and usage example
2 // Copyright (c) 2009-2016 Vladimir Batov.
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
5 
6 #include "./test.hpp"
7 
8 #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
main(int,char const * [])9 int main(int, char const* []) { return 0; }
10 #else
11 
12 #include <boost/convert.hpp>
13 #include <boost/convert/stream.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <boost/array.hpp>
16 #include <vector>
17 
18 using std::string;
19 using std::wstring;
20 
21 static
22 void
test_user_type()23 test_user_type()
24 {
25     boost::cnv::cstream cnv; // stringstream-based char converter
26 
27     direction const            up_dir1 = direction::up;
28     direction const            dn_dir1 = direction::dn;
29 //  string const           up_dir0_str = boost::convert<string, direction>(direction::up, cnv).value();
30 //  string const           dn_dir0_str = boost::convert<string, direction>(direction::dn, cnv).value();
31     string const           up_dir1_str = boost::convert<string>(up_dir1, cnv).value();
32     string const           dn_dir1_str = boost::convert<string>(dn_dir1, cnv).value();
33     direction const            up_dir2 = boost::convert<direction>(up_dir1_str, cnv).value();
34     direction const            dn_dir2 = boost::convert<direction>(dn_dir1_str, cnv).value();
35     direction const            up_dir3 = boost::convert<direction>(up_dir1_str, cnv).value();
36     direction const            dn_dir3 = boost::convert<direction>(dn_dir1_str, cnv).value();
37     direction const            dn_dir4 = boost::convert<direction>("junk", cnv).value_or(direction::dn);
38     boost::optional<direction> up_dir4 = boost::convert<direction>("junk", cnv);
39 
40 //  BOOST_TEST(up_dir0_str == "up");
41 //  BOOST_TEST(dn_dir0_str == "dn");
42     BOOST_TEST(up_dir1_str == "up");
43     BOOST_TEST(dn_dir1_str == "dn");
44     BOOST_TEST(up_dir2 == up_dir1);
45     BOOST_TEST(dn_dir2 == dn_dir1);
46     BOOST_TEST(up_dir3 == direction::up);
47     BOOST_TEST(dn_dir3 == direction::dn);
48     BOOST_TEST(dn_dir4 == direction::dn);
49     BOOST_TEST(!up_dir4); // Failed conversion
50 }
51 
52 static
53 void
test_algorithms()54 test_algorithms()
55 {
56 //[algorithm_example6
57     boost::array<change, 3>             chgs1 = {{ change::no, change::up, change::dn }};
58     boost::array<change::value_type, 3> chgs2 = {{ change::no, change::up, change::dn }};
59     std::vector<std::string>            strs1;
60     std::vector<std::string>            strs2;
61     std::vector<std::string>            strs3;
62     boost::cnv::cstream                   cnv;
63 
64     std::transform(chgs1.begin(), chgs1.end(), std::back_inserter(strs1),
65         boost::cnv::apply<string>(boost::cref(cnv))); // Deduced TypeIn is 'change'
66 
67     std::transform(chgs2.begin(), chgs2.end(), std::back_inserter(strs2),
68         boost::cnv::apply<string>(boost::cref(cnv))); // Deduced TypeIn is 'change::value_type'
69 
70     BOOST_TEST(strs1.size() == 3);
71     BOOST_TEST(strs1[0] == "no");
72     BOOST_TEST(strs1[1] == "up");
73     BOOST_TEST(strs1[2] == "dn");
74 
75     BOOST_TEST(strs2.size() == 3);
76     BOOST_TEST(strs2[0] == "0");
77     BOOST_TEST(strs2[1] == "1");
78     BOOST_TEST(strs2[2] == "2");
79 //]
80 //[algorithm_example7
81 
82     std::transform(chgs2.begin(), chgs2.end(), std::back_inserter(strs3),
83         boost::cnv::apply<string, change>(boost::cref(cnv)));
84 
85     BOOST_TEST(strs3.size() == 3);
86     BOOST_TEST(strs3[0] == "no");
87     BOOST_TEST(strs3[1] == "up");
88     BOOST_TEST(strs3[2] == "dn");
89 //]
90 }
91 
92 int
main(int,char const * [])93 main(int, char const* [])
94 {
95     test_user_type();
96     test_algorithms();
97 
98     return boost::report_errors();
99 }
100 
101 #endif
102