-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.h
97 lines (79 loc) · 2.7 KB
/
test_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
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
//
// Created by nbdy on 25.09.21.
//
#ifndef BINFMT__TEST_COMMON_H_
#define BINFMT__TEST_COMMON_H_
#include <cstdint>
#include <ctime>
#include <fstream>
#include "gtest/gtest.h"
#include "binfmt.h"
#define TEST_MAX_ENTRIES 2000
#define TEST_DIRECTORY "/tmp/binfmt_test"
#define TEST_BINARY_FILE "/tmp/binfmt_test/test_binary"
#define TEST_BINARY_FILE_IN_NON_EXISTENT_DIRECTORY "/tmp/this_directory_probably_does_not_exist/test_binary"
struct TestBinaryEntry {
uint32_t m_u32Number;
};
using TestBinaryHeader = binfmt::BinaryFileHeaderBase;
using TestBinaryEntryContainer = binfmt::BinaryEntryContainer<TestBinaryEntry>;
using TestBinaryFile = binfmt::BinaryFile<TestBinaryHeader, TestBinaryEntry, TestBinaryEntryContainer>;
int generateRandomInteger() {
return std::rand() % 10000000; // NOLINT(cert-msc50-cpp)
}
TestBinaryEntry generateRandomTestEntry() {
return TestBinaryEntry{
static_cast<uint32_t>(generateRandomInteger()),
};
}
TestBinaryEntryContainer generateRandomTestEntryContainer() {
return TestBinaryEntryContainer(generateRandomTestEntry());
}
int appendRandomAmountOfEntries(TestBinaryFile& f, int max = 20) {
int r = std::rand() % max; // NOLINT(cert-msc50-cpp)
for (int i = 0; i < r; i++) {
EXPECT_EQ(f.append(generateRandomTestEntryContainer()), binfmt::ErrorCode::OK);
}
return r;
}
std::vector<TestBinaryEntryContainer>
appendRandomAmountOfEntriesV(TestBinaryFile& f, int max = 20) {
int x = std::rand() % max; // NOLINT(cert-msc50-cpp)
std::vector<TestBinaryEntryContainer> r;
for (int i = 0; i < x; i++) {
auto v = generateRandomTestEntryContainer();
r.push_back(v);
EXPECT_EQ(f.append(v), binfmt::ErrorCode::OK);
}
return r;
}
template <typename BinaryFileType>
std::vector<TestBinaryEntryContainer>
appendExactAmountOfEntriesV(BinaryFileType &f, uint32_t count = 42) {
std::vector<TestBinaryEntryContainer> r;
for (int i = 0; i < count; i++) {
auto v = generateRandomTestEntryContainer();
r.push_back(v);
EXPECT_EQ(f.append(v), binfmt::ErrorCode::OK);
}
return r;
}
TestBinaryFile getRandomTestFile() {
TestBinaryFile r("/tmp/test.bin", TestBinaryHeader {});
EXPECT_TRUE(std::filesystem::exists(r.getPath()));
return r;
}
TestBinaryFile getRandomTestEntryLimitedFile() {
TestBinaryFile r("/tmp/test.bin", TestBinaryHeader {0, 0, TEST_MAX_ENTRIES});
EXPECT_TRUE(std::filesystem::exists(r.getPath()));
return r;
}
template <typename BinaryFileType> void cleanupTestFile(BinaryFileType& f) {
f.deleteFile();
EXPECT_FALSE(std::filesystem::exists(f.getPath()));
}
void cleanup(const std::string &FilePath) {
EXPECT_TRUE(std::filesystem::remove(FilePath));
EXPECT_FALSE(std::filesystem::exists(FilePath));
}
#endif // BINFMT__TEST_COMMON_H_