1 // Boost.Convert test and usage example
2 // Copyright (c) 2009-2020 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.
6 // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
7 // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
8 // See these mentioned files for the copyright notice.
12 #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
13 int main(int, char const* []) { return 0; }
16 #include "./prepare.hpp"
17 #include <boost/convert.hpp>
18 #include <boost/convert/spirit.hpp>
19 #include <boost/convert/strtol.hpp>
20 #include <boost/convert/lexical_cast.hpp>
22 #include <libs/spirit/workbench/measure.hpp>
23 #include <boost/spirit/include/qi.hpp>
30 struct base
: test::base
32 base() : strings_(local::get_strs()) {}
34 // Test strings are created as part of the object, i.e. on the stack to make sure
35 // they are easily accessed.
36 local::strings strings_
;
39 struct raw_lxcast_str_to_int_test
: local::base
43 for (size_t i
= 0; i
< strings_
.size(); ++i
)
44 this->val
+= boost::lexical_cast
<int>(strings_
[i
].c_str());
47 struct cnv_lxcast_str_to_int_test
: local::base
51 for (size_t i
= 0; i
< strings_
.size(); ++i
)
52 this->val
+= boost::convert
<int>(strings_
[i
].c_str(), cnv
).value();
54 boost::cnv::lexical_cast cnv
;
56 struct raw_spirit_str_to_int_test
: local::base
58 static int parse(char const* str
)
60 char const* beg
= str
;
61 char const* end
= beg
+ strlen(str
);
64 if (boost::spirit::qi::parse(beg
, end
, boost::spirit::qi::int_
, n
))
68 return (BOOST_ASSERT(0), 0);
72 for (size_t i
= 0; i
< strings_
.size(); ++i
)
73 this->val
+= parse(strings_
[i
].c_str());
76 struct cnv_spirit_str_to_int_test
: local::base
80 for (size_t i
= 0; i
< strings_
.size(); ++i
)
81 this->val
+= boost::convert
<int>(strings_
[i
].c_str(), cnv
).value();
83 boost::cnv::spirit cnv
;
88 main(int, char const* [])
90 // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
91 // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
92 // See these mentioned files for the copyright notice.
94 BOOST_SPIRIT_TEST_BENCHMARK(
95 10000000, // This is the maximum repetitions to execute
96 (raw_lxcast_str_to_int_test
)
97 (cnv_lxcast_str_to_int_test
)
98 (raw_spirit_str_to_int_test
)
99 (cnv_spirit_str_to_int_test
)
102 // This is ultimately responsible for preventing all the test code
103 // from being optimized away. Change this to return 0 and you
104 // unplug the whole test's life support system.
105 return test::live_code
!= 0;