• 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 #ifndef BOOST_CONVERT_TEST_PREPARE_HPP
7 #define BOOST_CONVERT_TEST_PREPARE_HPP
8 
9 #include <boost/array.hpp>
10 #include <ctime>
11 #include <cstdlib>
12 
13 // boostinspect:nounnamed
14 namespace { namespace local
15 {
16     // C1. 18 = 9 positive + 9 negative numbers with the number of digits from 1 to 9.
17     //     Even though INT_MAX(32) = 2147483647, i.e. 10 digits (not to mention long int)
18     //     we only test up to 9 digits as Spirit does not handle more than 9.
19 
20     typedef boost::array<my_string, 18> strings; //C1
21     ///////////////////////////////////////////////////////////////////////////
22     // Generate a random number string with N digits
23     std::string
gen_int(int digits,bool negative)24     gen_int(int digits, bool negative)
25     {
26         std::string result;
27 
28         if (negative)                       // Prepend a '-'
29             result += '-';
30 
31         result += '1' + (std::rand()%9);         // The first digit cannot be '0'
32 
33         for (int i = 1; i < digits; ++i)    // Generate the remaining digits
34             result += '0' + (std::rand()%10);
35         return result;
36     }
37 
38     local::strings const&
get_strs()39     get_strs()
40     {
41         static local::strings strings;
42         static bool            filled;
43         static bool          negative = true;
44 
45         if (!filled)
46         {
47             // Seed the random generator
48             std::srand(std::time(0));
49 
50             for (size_t k = 0; k < strings.size(); ++k)
51                 strings[k] = local::gen_int(k/2 + 1, negative = !negative).c_str();
52 
53             filled = true;
54         }
55         return strings;
56     }
57 }}
58 
59 #endif // BOOST_CONVERT_TEST_PREPARE_HPP
60