GPUdb C++ API  Version 7.0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test_utils.h
Go to the documentation of this file.
1 #ifndef __TEST_UTILS_H__
2 #define __TEST_UTILS_H__
3 
4 #include "gpudb/GPUdb.hpp"
5 
6 #include <string>
7 
8 #include <log4cplus/logger.h>
9 #include <log4cplus/loggingmacros.h>
10 
11 // =================================================================
12 // Logging Helpers
13 
14 // Global logger (can't define it here; it's defined in the .cpp file)
15 extern log4cplus::Logger logger;
16 
17 
18 // Create an inline std::stringstream stream object and return the
19 // std::string from it.
20 // E.g. std::string msg( KINETICA_STREAM_TO_STRING( "Hello " << user_name << ", how are you?" ) );
21 // Note: Extra cast needed for gcc 4.8.7 (at least).
22 #define KINETICA_STREAM_TO_STRING(...) ( static_cast<const std::ostringstream&> (std::ostringstream() << __VA_ARGS__).str() )
23 
24 // Create an inline std::stringstream stream object and return the
25 // char array from it.
26 // E.g. std::string msg( KINETICA_STREAM_TO_STRING( "Hello " << user_name << ", how are you?" ) );
27 #define KINETICA_STREAM_TO_CSTRING(...) ( KINETICA_STREAM_TO_STRING(__VA_ARGS__).c_str() )
28 
29 // Log a informational message
30 #define LOG_KINETICA_INFO(logger, logEvent) do { LOG4CPLUS_INFO(logger, logEvent); } while(0)
31 
32 // Log a debug message
33 #define LOG_KINETICA_DEBUG(logger, logEvent) do { LOG4CPLUS_DEBUG(logger, logEvent); } while(0)
34 
35 // Log a warning
36 #define LOG_KINETICA_WARN(logger, logEvent) do { LOG4CPLUS_WARN(logger, logEvent); } while(0)
37 
38 // Log an error
39 #define LOG_KINETICA_ERROR(logger, logEvent) do { LOG4CPLUS_ERROR(logger, logEvent); } while(0)
40 
41 
42 
43 
44 // =================================================================
45 // Data Generator Utility Functions
46 
47 #define NORMALIZER 100
48 #define USE_NULL_VALUE(null_percentage) \
49  ((std::rand() % NORMALIZER) < null_percentage)
50 
51 #define PERFORM_ACTION( percentage ) \
52  ((std::rand() % NORMALIZER) < percentage)
53 
54 #define GENERATE_INT8() \
55  ( (std::rand() % 256) - 128)
56 
57 #define GENERATE_INT16() \
58  ( (std::rand() % 65536) - 32768)
59 
60 #define GENERATE_FLOAT() \
61  ( ((float)std::rand()) / std::rand() * std::pow(-1, (std::rand() % 2)) )
62 
63 #define GENERATE_DOUBLE() \
64  ( ((double)std::rand()) / std::rand() * std::pow(-1, (std::rand() % 2)) )
65 
66 #define GENERATE_UNSIGNED_INT16() \
67  ( std::rand() % 65536 )
68 
69 // This macro generates mostly very large numbers
70 #define GENERATE_UNSIGNED_LONG_LONG() \
71  ( ( (unsigned long long)GENERATE_UNSIGNED_INT16() << 48 ) | \
72  ( (unsigned long long)GENERATE_UNSIGNED_INT16() << 32 ) | \
73  ( (unsigned long long)GENERATE_UNSIGNED_INT16() << 16 ) | \
74  ( (unsigned long long)GENERATE_UNSIGNED_INT16() ) )
75 
76 // This macro generates very large values, no small values, but can achieve
77 // the actual max of ull. For more varied values--to have smaller numbers--
78 // try generate_unsigned_long_long_str() (but look at its caveat).
79 #define GENERATE_UNSIGNED_LONG_LONG_STRING() \
80  ( std::to_string( GENERATE_UNSIGNED_LONG_LONG() ) )
81 
82 
83 #define GENERATE_TIMESTAMP() \
84  ( std::rand() - std::rand() ) // Range is actually [-30610239758979, 29379542399999]
85 
86 
87 // For generating strings and charNs
88 static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
89 static const char numeric[] = "0123456789";
90 static const int alphabet_size = 62; // [0-9] ten, [A-Z] 26, [a-z] 26
91 
92 /*
93  * Generate a random string made of the characters in [A-Za-z0-9], up to
94  * 256 characters in length.
95  */
96 std::string generate_random_string();
97 
98 
99 /*
100  * Generate a random string made of the characters in [A-Za-z0-9], up to
101  * 256 characters in length. The prefix is prepended to the randomly generated
102  * string.
103  */
104 std::string generate_random_string( const std::string& prefix );
105 
106 
107 /*
108  * Generate a random string made of the characters in [A-Za-z0-9], up to
109  * N characters in length.
110  */
111 std::string generate_charN( size_t N );
112 
113 
114 /*
115  * Generate a random string containing an unsigned long long (small or large).
116  * The maximum value reached here is 9,999,999,999,999,999,999, which is
117  * significantly less than the actual maximum value of 8,446,744,073,709,551,615.
118  * However, we can get small values, too, unlike the macro
119  * GENERATE_UNSIGNED_LONG_LONG_STRING() (which gets mostly very large values
120  * upto, and including, the unsigned long long maximum).
121  */
122 std::string generate_unsigned_long_long_str();
123 
124 
125 /*
126  * Generate a random string containing an unsigned long long (small or large).
127  * The maximum value reached here is 9,999,999,999,999,999,999, which is
128  * significantly less than the actual maximum value of 8,446,744,073,709,551,615.
129  * However, we can get small values, too, unlike the macro
130  * GENERATE_UNSIGNED_LONG_LONG_STRING() (which gets mostly very large values
131  * upto, and including, the unsigned long long maximum).
132  *
133  * The generated value is guaranteed to not have any leading zero.
134  */
136 
137 
139 #define GENERATE_IPV4() \
140  ( std::to_string(std::rand() % 256) + "." \
141  + std::to_string(std::rand() % 256) + "." \
142  + std::to_string(std::rand() % 256) + "." \
143  + std::to_string(std::rand() % 256) )
144 
145 
146 /*
147  * Generate a random date within the year range [1000, 2900]
148  */
149 std::string generate_date();
150 
151 
152 
153 /*
154  * Generate a random datetime with optional milliseconds
155  */
156 std::string generate_datetime( int chance_percentage_has_time, int chance_percentage_has_ms );
157 
158 
159 /*
160  * Generate a random time with optional milliseconds
161  */
162 std::string generate_time( int chance_percentage_has_ms );
163 
164 
165 
166 /*
167  * Generate a random decimal value with optional fractions and signs.
168  */
169 std::string generate_decimal( int frac_only_percentage, int has_frac_percentage, int negative_percentage, int positive_percentage );
170 
171 
172 // ============================================================================
173 
175 std::string generate_table_name( const std::string& prefix );
176 
179 void create_table( const gpudb::GPUdb& db, const std::string table_name,
180  const gpudb::Type& type);
181 
184 void create_table( const gpudb::GPUdb& db, const std::string table_name,
185  const std::vector<gpudb::Type::Column>& columns);
186 
188 void clear_table( const gpudb::GPUdb& db, const std::string table_name );
189 
191 size_t get_table_size( const gpudb::GPUdb& db, const std::string table_name );
192 
193 // ====================================================================
194 
195 
196 #endif // __TEST_UTILS_H__
std::string generate_unsigned_long_long_str()
static const char numeric[]
Definition: test_utils.h:89
static const char alphanum[]
Definition: test_utils.h:88
std::string generate_unsigned_long_long_str_no_leading_zero()
std::string generate_date()
size_t get_table_size(const gpudb::GPUdb &db, const std::string table_name)
Get a given table&#39;s size.
std::string generate_table_name(const std::string &prefix)
Generate a table name with the given prefix.
log4cplus::Logger logger
static const int alphabet_size
Definition: test_utils.h:90
std::string generate_datetime(int chance_percentage_has_time, int chance_percentage_has_ms)
std::string generate_time(int chance_percentage_has_ms)
void clear_table(const gpudb::GPUdb &db, const std::string table_name)
Clear a given table.
void create_table(const gpudb::GPUdb &db, const std::string table_name, const gpudb::Type &type)
Create a table given a DB hancle, table name, and a type.
std::string generate_random_string()
std::string generate_decimal(int frac_only_percentage, int has_frac_percentage, int negative_percentage, int positive_percentage)
std::string generate_charN(size_t N)