-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtesting_utilities.h
More file actions
185 lines (152 loc) · 5.61 KB
/
testing_utilities.h
File metadata and controls
185 lines (152 loc) · 5.61 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef DFTRACER_UTILS_TESTS_TESTING_UTILITIES_H
#define DFTRACER_UTILS_TESTS_TESTING_UTILITIES_H
#ifdef __cplusplus
#include <dftracer/utils/core/common/filesystem.h>
#include <atomic>
#include <chrono>
#include <functional>
#include <string>
#include <thread>
#include <vector>
extern "C" {
#endif
// C API for testing utilities
typedef struct test_environment* test_environment_handle_t;
/**
* Create a test environment with default number of lines (100)
*/
test_environment_handle_t test_environment_create(void);
/**
* Create a test environment with specified number of lines
*/
test_environment_handle_t test_environment_create_with_lines(size_t lines);
/**
* Destroy a test environment and clean up resources
*/
void test_environment_destroy(test_environment_handle_t env);
/**
* Check if test environment is valid
*/
int test_environment_is_valid(test_environment_handle_t env);
/**
* Get the test directory path
* Returns a pointer to internal string - do not free
*/
const char* test_environment_get_dir(test_environment_handle_t env);
/**
* Archive formats supported by the test environment
*/
typedef enum { TEST_FORMAT_GZIP = 0, TEST_FORMAT_TAR_GZIP = 1 } test_format_t;
/**
* Create a test gzip file and return the path
* Returns allocated string - caller must free
*/
char* test_environment_create_test_gzip_file(test_environment_handle_t env);
/**
* Create a test file with specified format and return the path
* Returns allocated string - caller must free
*/
char* test_environment_create_test_file_with_format(
test_environment_handle_t env, test_format_t format);
/**
* Create a tar.gz archive with multiple files
* Returns allocated string - caller must free
*/
char* test_environment_create_test_tar_gzip_file(test_environment_handle_t env);
/**
* Create a tar archive and then compress it to gzip
* Returns 1 on success, 0 on failure
*/
int create_tar_archive_and_compress(const char** file_paths, size_t num_files,
const char* output_file);
/**
* Extract a specific file from tar.gz archive
* Returns 1 on success, 0 on failure
*/
int extract_file_from_tar_gz(const char* tar_gz_path, const char* file_path,
const char* output_path);
/**
* Get list of files in tar archive
* Returns allocated array of strings - caller must free each string and the
* array
*/
char** get_tar_file_list(const char* tar_path, size_t* num_files);
/**
* Free file list returned by get_tar_file_list
*/
void free_tar_file_list(char** file_list, size_t num_files);
/**
* Get the `.dftindex` path for a given gzip file
* Returns allocated string - caller must free
*/
char* test_environment_get_index_path(test_environment_handle_t env,
const char* gz_file);
/**
* Compress a file to gzip format
* Returns 1 on success, 0 on failure
*/
int compress_file_to_gzip_c(const char* input_file, const char* output_file);
size_t mb_to_b(double mb);
/**
* Generate a unique path for temporary tests.
* Returns allocated string - caller must free
*/
char* test_make_unique_test_path(const char* name);
#ifdef __cplusplus
}
namespace dft_utils_test {
enum class Format { GZIP = 0, TAR_GZIP = 1 };
inline fs::path make_unique_test_path(const std::string& name) {
static std::atomic<unsigned long long> counter{0};
const auto now = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
const auto tid = std::hash<std::thread::id>{}(std::this_thread::get_id());
const auto unique_id =
std::to_string(now) + "_" + std::to_string(tid) + "_" +
std::to_string(counter.fetch_add(1, std::memory_order_relaxed));
return fs::temp_directory_path() / (name + "_" + unique_id);
}
struct TarFileInfo {
std::string filename;
std::string content;
std::size_t num_lines;
};
bool compress_file_to_gzip(const std::string& input_file,
const std::string& output_file);
bool create_tar_archive(const std::vector<TarFileInfo>& files,
const std::string& output_path);
bool create_tar_gz_archive(const std::vector<TarFileInfo>& files,
const std::string& output_path);
std::vector<std::string> list_tar_contents(const std::string& tar_path);
bool extract_from_tar_gz(const std::string& tar_gz_path,
const std::string& file_path,
const std::string& output_path);
class TestEnvironment {
public:
TestEnvironment() : TestEnvironment(100, Format::GZIP) {}
TestEnvironment(std::size_t lines) : TestEnvironment(lines, Format::GZIP) {}
TestEnvironment(std::size_t lines, Format format);
TestEnvironment(const TestEnvironment&) = delete;
TestEnvironment& operator=(const TestEnvironment&) = delete;
~TestEnvironment();
const std::string& get_dir() const;
bool is_valid() const;
std::string create_test_file(); // Format-aware file creation
std::string create_test_gzip_file();
std::string create_test_tar_gzip_file();
std::string get_index_path(const std::string& gz_file);
Format get_format() const { return format_; }
// DFTracer-specific test file creation
std::string create_dft_test_file(int num_events = 100);
std::string create_dft_test_gzip_file(int num_events = 100);
private:
std::size_t num_lines;
std::string test_dir;
Format format_;
std::string create_test_gzip_file_impl();
std::string create_test_tar_gzip_file_impl();
};
} // namespace dft_utils_test
#endif
#endif // DFTRACER_UTILS_TESTS_TESTING_UTILITIES_H