]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/unordered/test/helpers/generators.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / unordered / test / helpers / generators.hpp
1
2 // Copyright 2005-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 // This uses std::rand to generate random values for tests.
7 // Which is not good as different platforms will be running different tests.
8 // It would be much better to use Boost.Random, but it doesn't
9 // support all the compilers that I want to test on.
10
11 #if !defined(BOOST_UNORDERED_TEST_HELPERS_GENERATORS_HEADER)
12 #define BOOST_UNORDERED_TEST_HELPERS_GENERATORS_HEADER
13
14 #include "./fwd.hpp"
15 #include <boost/type_traits/add_const.hpp>
16 #include <cstdlib>
17 #include <stdexcept>
18 #include <string>
19 #include <utility>
20
21 namespace test {
22 struct seed_t
23 {
24 seed_t(unsigned int x)
25 {
26 using namespace std;
27 srand(x);
28 }
29 };
30
31 std::size_t random_value(std::size_t max)
32 {
33 using namespace std;
34 return static_cast<std::size_t>(rand()) % max;
35 }
36
37 inline int generate(int const*, random_generator g)
38 {
39 using namespace std;
40 int value = rand();
41 if (g == limited_range) {
42 value = value % 100;
43 }
44 return value;
45 }
46
47 inline char generate(char const*, random_generator)
48 {
49 using namespace std;
50 return static_cast<char>((rand() >> 1) % (128 - 32) + 32);
51 }
52
53 inline signed char generate(signed char const*, random_generator)
54 {
55 using namespace std;
56 return static_cast<signed char>(rand());
57 }
58
59 inline std::string generate(std::string const*, random_generator g)
60 {
61 using namespace std;
62
63 char* char_ptr = 0;
64
65 std::string result;
66
67 if (g == limited_range) {
68 std::size_t length = test::random_value(2) + 2;
69
70 char const* strings[] = {"'vZh(3~ms", "%m", "_Y%U", "N'Y", "4,J_J"};
71 for (std::size_t i = 0; i < length; ++i) {
72 result += strings[random_value(sizeof(strings) / sizeof(strings[0]))];
73 }
74 } else {
75 std::size_t length = test::random_value(10) + 1;
76 for (std::size_t i = 0; i < length; ++i) {
77 result += generate(char_ptr, g);
78 }
79 }
80
81 return result;
82 }
83
84 float generate(float const*, random_generator g)
85 {
86 using namespace std;
87 int x = 0;
88 int value = generate(&x, g);
89 return (float)value / (float)RAND_MAX;
90 }
91 }
92
93 #endif