Line data Source code
1 : // Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/prinbee
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 : #pragma once
19 :
20 : // snapcatch2
21 : //
22 : #include <catch2/snapcatch2.hpp>
23 :
24 :
25 : // prinbee
26 : //
27 : #include <prinbee/bigint/uint512.h>
28 :
29 :
30 : // snaplogger
31 : //
32 : #include <snaplogger/snapcatch2.hpp>
33 :
34 :
35 : // C++
36 : //
37 : #include <string>
38 : #include <cstring>
39 : #include <cstdlib>
40 : #include <iostream>
41 :
42 :
43 :
44 : namespace SNAP_CATCH2_NAMESPACE
45 : {
46 :
47 :
48 :
49 : std::string setup_context(std::string const & path, std::vector<std::string> const & xmls);
50 :
51 :
52 :
53 : // TODO: replace with the snapcatch2 versions
54 :
55 : inline char32_t rand_char(bool full_range = false)
56 : {
57 : // -1 so we can avoid '\0' which in most cases is not useful
58 : //
59 : char32_t const max((full_range ? 0x0110000 : 0x0010000) - (0xE000 - 0xD800) - 1);
60 :
61 : char32_t const wc(((rand() << 16) ^ rand()) % max + 1);
62 :
63 : // skip the surrogates for the larger characters
64 : //
65 : return wc >= 0xD800 ? wc + (0xE000 - 0xD800) : wc;
66 : }
67 :
68 :
69 854 : inline std::string rand_string(int len = rand() % 200 + 10)
70 : {
71 854 : std::string result;
72 20001939 : for(int i(0); i < len; ++i)
73 : {
74 20001085 : result += rand() % 26 + 'a';
75 : }
76 854 : return result;
77 0 : }
78 :
79 :
80 1039 : inline std::uint32_t rand32()
81 : {
82 1039 : return rand() ^ (rand() << 16);
83 : }
84 :
85 :
86 35678 : inline std::uint64_t rand64()
87 : {
88 35678 : std::uint64_t const a(rand());
89 35678 : std::uint64_t const b(rand());
90 35678 : std::uint64_t const c(rand());
91 35678 : std::uint64_t const d(rand());
92 35678 : return a ^ (b << 16) ^ (c << 32) ^ (d << 48);
93 : }
94 :
95 :
96 : #pragma GCC diagnostic push
97 : #pragma GCC diagnostic ignored "-Wpedantic"
98 497 : inline unsigned __int128 rand128()
99 : {
100 497 : unsigned __int128 const a(rand());
101 497 : unsigned __int128 const b(rand());
102 497 : unsigned __int128 const c(rand());
103 497 : unsigned __int128 const d(rand());
104 497 : unsigned __int128 const e(rand());
105 497 : unsigned __int128 const f(rand());
106 497 : unsigned __int128 const g(rand());
107 497 : unsigned __int128 const h(rand());
108 497 : return (a << 0) ^ (b << 16) ^ (c << 32) ^ (d << 48)
109 497 : ^ (e << 64) ^ (f << 80) ^ (g << 96) ^ (h << 112);
110 : }
111 : #pragma GCC diagnostic pop
112 :
113 :
114 2100 : inline void rand512(prinbee::uint512_t & a)
115 : {
116 18900 : for(std::size_t i(0); i < 8; ++i)
117 : {
118 16800 : SNAP_CATCH2_NAMESPACE::random(a.f_value[i]);
119 : }
120 2100 : }
121 :
122 :
123 600 : inline void rand512(prinbee::int512_t & a)
124 : {
125 4800 : for(std::size_t i(0); i < 7; ++i)
126 : {
127 4200 : SNAP_CATCH2_NAMESPACE::random(a.f_value[i]);
128 : }
129 600 : SNAP_CATCH2_NAMESPACE::random(a.f_high_value);
130 600 : }
131 :
132 :
133 :
134 : } // namespace SNAP_CATCH2_NAMESPACE
135 : // vim: ts=4 sw=4 et
|