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.
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 <boost/convert.hpp>
17 #include <boost/convert/spirit.hpp>
18 #include <boost/convert/strtol.hpp>
19 #include <boost/convert/lexical_cast.hpp>
20 #include "./prepare.hpp"
22 //#define main() old_str_to_int_test_spirit()
23 //#include <libs/spirit/optimization/qi/int_parser.cpp>
24 #include <libs/spirit/workbench/measure.hpp>
28 #include <boost/spirit/include/qi.hpp>
34 struct base
: test::base
36 base() : strings_(local::get_strs()) {}
38 // Test strings are created as part of the object, i.e. on the stack to make sure
39 // they are easily accessed.
40 local::strings strings_
;
43 struct raw_lxcast_str_to_int_test
: local::base
47 for (size_t i
= 0; i
< strings_
.size(); ++i
)
48 this->val
+= boost::lexical_cast
<int>(strings_
[i
].c_str());
51 struct cnv_lxcast_str_to_int_test
: local::base
55 for (size_t i
= 0; i
< strings_
.size(); ++i
)
56 this->val
+= boost::convert
<int>(strings_
[i
].c_str(), cnv
).value();
58 boost::cnv::lexical_cast cnv
;
60 struct raw_spirit_str_to_int_test
: local::base
62 static int parse(char const* str
)
64 char const* beg
= str
;
65 char const* end
= beg
+ strlen(str
);
68 if (boost::spirit::qi::parse(beg
, end
, boost::spirit::qi::int_
, n
))
72 return (BOOST_ASSERT(0), 0);
76 for (size_t i
= 0; i
< strings_
.size(); ++i
)
77 this->val
+= parse(strings_
[i
].c_str());
80 struct cnv_spirit_str_to_int_test
: local::base
84 for (size_t i
= 0; i
< strings_
.size(); ++i
)
85 this->val
+= boost::convert
<int>(strings_
[i
].c_str(), cnv
).value();
87 boost::cnv::spirit cnv
;
92 main(int, char const* [])
94 // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
95 // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
96 // See these mentioned files for the copyright notice.
98 BOOST_SPIRIT_TEST_BENCHMARK(
99 10000000, // This is the maximum repetitions to execute
100 (raw_lxcast_str_to_int_test
)
101 (cnv_lxcast_str_to_int_test
)
102 (raw_spirit_str_to_int_test
)
103 (cnv_spirit_str_to_int_test
)
106 // This is ultimately responsible for preventing all the test code
107 // from being optimized away. Change this to return 0 and you
108 // unplug the whole test's life support system.
109 return test::live_code
!= 0;