-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data_generator.cpp
72 lines (60 loc) · 1.4 KB
/
test_data_generator.cpp
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
#include "test_data_generator.h"
#include <random>
#include "file_writer.h"
namespace fs
{
test_data_generator::test_data_generator() :
symbols_(get_symbols()),
gen_((std::random_device())()),
sym_distr_(0, symbols_.size() - 1),
size_distr_(10, 100)
{
}
test_data_generator::~test_data_generator()
{
}
void test_data_generator::generate_file(const std::string& filename, std::uint64_t size)
{
// Result file
file_writer out(filename);
while (out.written() < size)
{
auto sz = size_distr_(gen_);
if (out.written() + sz > size)
sz = static_cast<size_t>(size - out.written());
auto line = generate_string(sz);
out.write(line);
}
}
std::vector<std::string> test_data_generator::generate_strings(size_t num_lines)
{
std::vector<std::string> r;
for (size_t i = 0; i < num_lines; ++i)
{
const auto sz = size_distr_(gen_);
r.push_back(generate_string(sz));
}
return r;
}
std::string test_data_generator::generate_string(size_t size)
{
assert(size);
std::string line(size, 0);
for (size_t i = 0; i < size - 1; ++i)
line[i] = symbols_[sym_distr_(gen_)];
line.back() = '\n';
return line;
}
std::string test_data_generator::get_symbols()
{
// Allowed symbols
std::string symbols;
for (char ch = 'a'; ch <= 'z'; ++ch)
symbols.push_back(ch);
for (char ch = 'A'; ch <= 'Z'; ++ch)
symbols.push_back(ch);
for (char ch = '0'; ch <= '9'; ++ch)
symbols.push_back(ch);
return symbols;
}
}