prepare.hpp 1.8 KB

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