-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.h
60 lines (49 loc) · 1.85 KB
/
common.h
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
#pragma once
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include "const.h"
#include "debug.h"
#include "utils/logging.h"
#define CHECK_RESULT(expected, actual, length, fd) \
do { \
if (memcmp(expected, actual, length) != 0) { \
madfs::debug::print_file(fd); \
std::cerr << "expected: \""; \
for (int i = 0; i < length; ++i) putc(expected[i], stderr); \
std::cerr << "\"\n"; \
std::cerr << "actual : \""; \
for (int i = 0; i < length; ++i) putc(actual[i], stderr); \
std::cerr << "\"\n"; \
assert(false); \
} \
} while (0)
#define ASSERT(x) PANIC_IF(!(x), "assertion failed: " #x)
static const char* get_filepath() {
const char* res = "test.txt";
if (char* pmem_path = std::getenv("PMEM_PATH"); pmem_path) {
static char path[PATH_MAX];
strcpy(path, pmem_path);
strcat(path, "/test.txt");
res = path;
}
fprintf(stderr, "filepath: %s\n", res);
return res;
}
constexpr std::string_view chars =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static void fill_buff(char* buff, int num_elem, int init = 0) {
std::generate(buff, buff + num_elem,
[i = init]() mutable { return chars[i++ % chars.length()]; });
}
static std::string random_string(int length) {
std::string str;
str.reserve(length);
for (int i = 0; i < length; ++i)
str.push_back(chars[rand() % chars.length()]);
return str;
}