1 /*=============================================================================
2 Copyright (c) 2001-2010 Joel de Guzman
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 "../measure.hpp"
11 #include <boost/spirit/include/qi.hpp>
15 ///////////////////////////////////////////////////////////////////////////
16 // Generate a random number string with N digits
21 if (rand()%2) // Prepend a '-'
23 result += '1' + (rand()%9); // The first digit cannot be '0'
25 for (int i = 1; i < digits; ++i) // Generate the remaining digits
26 result += '0' + (rand()%10);
30 std::string numbers[9];
34 ///////////////////////////////////////////////////////////////////////////
35 struct atoi_test : test::base
39 for (int i = 0; i < 9; ++i)
40 this->val += atoi(first[i]);
44 ///////////////////////////////////////////////////////////////////////////
45 struct strtol_test : test::base
49 for (int i = 0; i < 9; ++i)
50 this->val += strtol(first[i], const_cast<char**>(&last[i]), 10);
54 ///////////////////////////////////////////////////////////////////////////
55 struct spirit_int_test : test::base
57 static int parse(char const* first, char const* last)
60 namespace qi = boost::spirit::qi;
62 qi::parse(first, last, int_, n);
68 for (int i = 0; i < 9; ++i)
69 this->val += parse(first[i], last[i]);
76 // Seed the random generator
79 // Generate random integers with 1 .. 9 digits
80 // We test only 9 digits to avoid overflow
81 std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
82 std::cout << "Numbers to test:" << std::endl;
83 for (int i = 0; i < 9; ++i)
85 numbers[i] = gen_int(i+1);
86 first[i] = numbers[i].c_str();
90 std::cout << i+1 << " digit number:" << numbers[i] << std::endl;
92 std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
94 BOOST_SPIRIT_TEST_BENCHMARK(
95 10000000, // This is the maximum repetitions to execute
101 // This is ultimately responsible for preventing all the test code
102 // from being optimized away. Change this to return 0 and you
103 // unplug the whole test's life support system.
104 return test::live_code != 0;