GPUdb C++ API  Version 6.2.0.3
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 // Create an inline std::stringstream stream object and return the
15 // std::string from it.
16 // E.g. std::string msg( KINETICA_STREAM_TO_STRING( "Hello " << user_name << ", how are you?" ) );
17 // Note: Extra cast needed for gcc 4.8.7 (at least).
18 #define KINETICA_STREAM_TO_STRING(...) ( static_cast<const std::ostringstream&> (std::ostringstream() << __VA_ARGS__).str() )
19 
20 // Create an inline std::stringstream stream object and return the
21 // char array from it.
22 // E.g. std::string msg( KINETICA_STREAM_TO_STRING( "Hello " << user_name << ", how are you?" ) );
23 #define KINETICA_STREAM_TO_CSTRING(...) ( KINETICA_STREAM_TO_STRING(__VA_ARGS__).c_str() )
24 
25 // Log a informational message
26 #define LOG_KINETICA_INFO(logger, logEvent) do { LOG4CPLUS_INFO(logger, logEvent); } while(0)
27 
28 // Log a debug message
29 #define LOG_KINETICA_DEBUG(logger, logEvent) do { LOG4CPLUS_DEBUG(logger, logEvent); } while(0)
30 
31 // Log a warning
32 #define LOG_KINETICA_WARN(logger, logEvent) do { LOG4CPLUS_WARN(logger, logEvent); } while(0)
33 
34 // Log an error
35 #define LOG_KINETICA_ERROR(logger, logEvent) do { LOG4CPLUS_ERROR(logger, logEvent); } while(0)
36 
37 
38 
39 
40 // =================================================================
41 // Data Generator Utility Functions
42 
43 #define NORMALIZER 100
44 #define USE_NULL_VALUE(null_percentage) \
45  ((std::rand() % NORMALIZER) < null_percentage)
46 
47 #define PERFORM_ACTION( percentage ) \
48  ((std::rand() % NORMALIZER) < percentage)
49 
50 #define GENERATE_INT8() \
51  ( (std::rand() % 256) - 128)
52 
53 #define GENERATE_INT16() \
54  ( (std::rand() % 65536) - 32768)
55 
56 #define GENERATE_FLOAT() \
57  ( ((float)std::rand()) / std::rand() * std::pow(-1, (std::rand() % 2)) )
58 
59 
60 #define GENERATE_DOUBLE() \
61  ( ((double)std::rand()) / std::rand() * std::pow(-1, (std::rand() % 2)) )
62 
63 #define GENERATE_TIMESTAMP() \
64  ( std::rand() - std::rand() ) // Range is actually [-30610239758979, 29379542399999]
65 
66 
67 // For generating strings and charNs
68 static const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
69 static const int alphabet_size = 62; // [0-9] ten, [A-Z] 26, [a-z] 26
70 
71 /*
72  * Generate a random string made of the characters in [A-Za-z0-9], up to
73  * 256 characters in length.
74  */
75 std::string generate_random_string();
76 
77 
78 /*
79  * Generate a random string made of the characters in [A-Za-z0-9], up to
80  * N characters in length.
81  */
82 std::string generate_charN( size_t N );
83 
84 
86 #define GENERATE_IPV4() \
87  ( std::to_string(std::rand() % 256) + "." \
88  + std::to_string(std::rand() % 256) + "." \
89  + std::to_string(std::rand() % 256) + "." \
90  + std::to_string(std::rand() % 256) )
91 
92 
93 /*
94  * Generate a random date within the year range [1000, 2900]
95  */
96 std::string generate_date();
97 
98 
99 
100 /*
101  * Generate a random datetime with optional milliseconds
102  */
103 std::string generate_datetime( int chance_percentage_has_time, int chance_percentage_has_ms );
104 
105 
106 /*
107  * Generate a random time with optional milliseconds
108  */
109 std::string generate_time( int chance_percentage_has_ms );
110 
111 
112 
113 /*
114  * Generate a random decimal value with optional fractions and signs.
115  */
116 std::string generate_decimal( int frac_only_percentage, int has_frac_percentage, int negative_percentage, int positive_percentage );
117 
118 
119 // ============================================================================
120 
123 void create_table( const gpudb::GPUdb& db, const std::string table_name,
124  const gpudb::Type& type);
125 
128 void create_table( const gpudb::GPUdb& db, const std::string table_name,
129  const std::vector<gpudb::Type::Column>& columns);
130 
131 
133 void clear_table( const gpudb::GPUdb& db, const std::string table_name );
134 
135 // ====================================================================
136 
137 
138 #endif // __TEST_UTILS_H__
static const char alphanum[]
Definition: test_utils.h:68
std::string generate_date()
static const int alphabet_size
Definition: test_utils.h:69
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)