forked from solidoss/solidframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memory_file.cpp
103 lines (86 loc) · 2.57 KB
/
test_memory_file.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
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
#include "solid/system/debug.hpp"
#include "solid/system/common.hpp"
#include "solid/utility/memoryfile.hpp"
#include "solid/system/exception.hpp"
#include <cerrno>
#include <cstring>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
using namespace solid;
namespace{
std::string pattern;
size_t real_size(size_t _sz){
//offset + (align - (offset mod align)) mod align
return _sz + ((sizeof(uint64_t) - (_sz % sizeof(uint64_t))) % sizeof(uint64_t));
}
void init(string &_rostr, size_t _sz){
const size_t sz = real_size(_sz);
_rostr.resize(sz);
const size_t count = sz / sizeof(uint64_t);
uint64_t *pu = reinterpret_cast<uint64_t*>(const_cast<char*>(_rostr.data()));
const uint64_t *pup = reinterpret_cast<const uint64_t*>(pattern.data());
const size_t pattern_size = pattern.size() / sizeof(uint64_t);
for(uint64_t i = 0; i < count; ++i){
pu[i] = pup[(i) % pattern_size];//pattern[i % pattern.size()];
}
}
void test_read_write(size_t _sz, const std::vector<size_t> &_write_size_vec, const std::vector<size_t> &_read_size_vec){
MemoryFile mf;
std::string instr;
{
init(instr, _sz);
size_t idx = 0;
size_t off = 0;
do{
int to_write = static_cast<int>(_write_size_vec[idx % _write_size_vec.size()]);
if(to_write > static_cast<int>(instr.size() - off)){
to_write = static_cast<int>(instr.size() - off);
}
int rv = mf.write(instr.data() + off, to_write);
SOLID_CHECK(rv == to_write);
off += to_write;
++idx;
}while(off < instr.size());
}
SOLID_CHECK(mf.size() == static_cast<int64_t>(instr.size()));
mf.seek(0, SeekBeg);
{
std::string outstr;
size_t idx = 0;
constexpr int buf_cp = 10 * 1024;
char buf[10 * 1024];
bool fwd;
do{
int to_read = static_cast<int>(_read_size_vec[idx % _read_size_vec.size()]);
SOLID_CHECK(to_read < buf_cp);
int rv = mf.read(buf, to_read);
if(rv > 0){
SOLID_CHECK(rv <= to_read);
outstr.append(buf, rv);
}
fwd = (rv == to_read);
}while(fwd);
SOLID_CHECK(outstr == instr);
}
}
}//namespace
int test_memory_file(int argc, char *argv[]){
for(int j = 0; j < 1; ++j){
for(int i = 0; i < 127; ++i){
int c = (i + j) % 127;
if(isprint(c) and !isblank(c)){
pattern += static_cast<char>(c);
}
}
}
int size = 1000000;
if(argc >= 2){
size = atoi(argv[1]);
}
const std::vector<size_t> write_size_vec = {10, 100, 1000, 2000, 4000, 6000, 8000, 10000};
const std::vector<size_t> read_size_vec = {11, 111, 1111, 2222, 44444, 6666, 8888, 11111};
test_read_write(size, write_size_vec, read_size_vec);
return 0;
}