Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / spirit / optimization / qi / int_parser.cpp
1 /*=============================================================================
2     Copyright (c) 2001-2010 Joel de Guzman
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 "../measure.hpp"
8 #include <string>
9 #include <vector>
10 #include <cstdlib>
11 #include <boost/spirit/include/qi.hpp>
12
13 namespace
14 {
15     ///////////////////////////////////////////////////////////////////////////
16     // Generate a random number string with N digits
17     std::string
18     gen_int(int digits)
19     {
20         std::string result;
21         if (rand()%2)                       // Prepend a '-'
22             result += '-';
23         result += '1' + (rand()%9);         // The first digit cannot be '0'
24         
25         for (int i = 1; i < digits; ++i)    // Generate the remaining digits
26             result += '0' + (rand()%10);
27         return result;
28     }
29     
30     std::string numbers[9];
31     char const* first[9];
32     char const* last[9];
33
34     ///////////////////////////////////////////////////////////////////////////
35     struct atoi_test : test::base
36     {
37         void benchmark()
38         {
39             for (int i = 0; i < 9; ++i) 
40                 this->val += atoi(first[i]);
41         }
42     };
43     
44     ///////////////////////////////////////////////////////////////////////////
45     struct strtol_test : test::base
46     {        
47         void benchmark()
48         {
49             for (int i = 0; i < 9; ++i) 
50                 this->val += strtol(first[i], const_cast<char**>(&last[i]), 10);
51         }
52     };
53     
54     ///////////////////////////////////////////////////////////////////////////
55     struct spirit_int_test : test::base
56     {
57         static int parse(char const* first, char const* last)
58         {
59             int n;
60             namespace qi = boost::spirit::qi;
61             using qi::int_;
62             qi::parse(first, last, int_, n);
63             return n;
64         }
65
66         void benchmark()
67         {           
68             for (int i = 0; i < 9; ++i) 
69                 this->val += parse(first[i], last[i]);
70         }
71     };
72 }
73
74 int main()
75 {
76     // Seed the random generator
77     srand(time(0));
78     
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)
84     {
85         numbers[i] = gen_int(i+1);
86         first[i] = numbers[i].c_str();
87         last[i] = first[i];
88         while (*last[i])
89             last[i]++;
90         std::cout << i+1 << " digit number:" << numbers[i] << std::endl;
91     }
92     std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
93
94     BOOST_SPIRIT_TEST_BENCHMARK(
95         10000000,     // This is the maximum repetitions to execute
96         (atoi_test)
97         (strtol_test)
98         (spirit_int_test)
99     )
100     
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;
105 }
106