Blame view

3rdparty/boost_1_81_0/libs/convert/test/prepare.hpp 1.66 KB
977ed18d   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  // Boost.Convert test and usage example
  // Copyright (c) 2009-2020 Vladimir Batov.
  // Use, modification and distribution are subject to the Boost Software License,
  // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  
  #ifndef BOOST_CONVERT_TEST_PREPARE_HPP
  #define BOOST_CONVERT_TEST_PREPARE_HPP
  
  #include <array>
  #include <ctime>
  #include <cstdlib>
  
  // boostinspect:nounnamed
  namespace { namespace local
  {
      // C1. 18 = 9 positive + 9 negative numbers with the number of digits from 1 to 9.
      //     Even though INT_MAX(32) = 2147483647, i.e. 10 digits (not to mention long int)
      //     we only test up to 9 digits as Spirit does not handle more than 9.
  
      using strings = std::array<my_string, 18>; //C1
  
      // Generate a random number string with N digits
      std::string
      gen_int(int digits, bool negative)
      {
          std::string result;
  
          if (negative) // Prepend a '-'
              result += '-';
  
          result += '1' + (std::rand() % 9); // The first digit cannot be '0'
  
          for (int i = 1; i < digits; ++i) // Generate the remaining digits
              result += '0' + (std::rand()%10);
  
          return result;
      }
  
      local::strings const&
      get_strs()
      {
          static auto  strings = local::strings();
          static bool   filled = false;
          static bool negative = true;
  
          if (!filled)
          {
              // Seed the random generator
              std::srand(std::time(0));
  
              for (size_t k = 0; k < strings.size(); ++k)
                  strings[k] = local::gen_int(k/2 + 1, negative = !negative).c_str();
  
              filled = true;
          }
          return strings;
      }
  }}
  
  #endif // BOOST_CONVERT_TEST_PREPARE_HPP