-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathtest-clp_s-ffi_sfa_reader.cpp
More file actions
141 lines (116 loc) · 4.87 KB
/
test-clp_s-ffi_sfa_reader.cpp
File metadata and controls
141 lines (116 loc) · 4.87 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
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <ystdlib/error_handling/Result.hpp>
#include "../src/clp/ReadOnlyMemoryMappedFile.hpp"
#include "../src/clp_s/ffi/sfa/ClpArchiveReader.hpp"
#include "clp_s_test_utils.hpp"
#include "TestOutputCleaner.hpp"
namespace {
using clp::ReadOnlyMemoryMappedFile;
using clp_s::ffi::sfa::ClpArchiveReader;
using ystdlib::error_handling::Result;
using ystdlib::error_handling::success;
constexpr std::string_view cSfaReaderLogsDirectory{"test_log_files"};
constexpr std::string_view cSfaReaderArchiveOutputDirectory{"test-clp_s-ffi_sfa-reader-archive"};
constexpr std::string_view cInputNoFloats{"test_no_floats_sorted.jsonl"};
constexpr std::string_view cInputFloatTimestamp{"test_search_float_timestamp.jsonl"};
auto get_tests_dir() -> std::filesystem::path {
std::filesystem::path const current_file_path{__FILE__};
return current_file_path.parent_path();
}
auto get_log_local_path(std::string_view const log_name) -> std::filesystem::path {
return get_tests_dir() / cSfaReaderLogsDirectory / log_name;
}
auto get_archive_output_root_dir() -> std::filesystem::path {
return get_tests_dir() / cSfaReaderArchiveOutputDirectory;
}
auto generate_single_file_archive(std::filesystem::path const& log_path) -> std::filesystem::path {
auto const root_output_dir{get_archive_output_root_dir()};
std::filesystem::create_directories(root_output_dir);
auto const output_dir{root_output_dir / log_path.stem().string()};
auto const archive_stats = compress_archive(
log_path.string(),
output_dir.string(),
std::nullopt,
false,
true,
false
);
REQUIRE(false == archive_stats.empty());
return output_dir / archive_stats.front().get_id();
}
auto get_num_lines(std::filesystem::path const& path) -> uint64_t {
std::ifstream input_file{path};
REQUIRE(input_file.is_open());
uint64_t num_lines{0};
std::string line;
while (std::getline(input_file, line)) {
++num_lines;
}
return num_lines;
}
auto assert_reader_matches_expected(
ClpArchiveReader const& reader,
std::string const& expected_file_name,
uint64_t expected_event_count
) -> void {
auto const event_count{reader.get_event_count()};
REQUIRE(event_count == expected_event_count);
auto const file_names{reader.get_file_names()};
auto const& file_infos{reader.get_file_infos()};
REQUIRE(1 == file_names.size());
REQUIRE(1 == file_infos.size());
auto const& file_name{file_names.front()};
auto const& file_info{file_infos.front()};
REQUIRE(expected_file_name == file_name);
REQUIRE(expected_file_name == file_info.get_file_name());
REQUIRE(0 == file_info.get_start_index());
REQUIRE(expected_event_count == file_info.get_end_index());
REQUIRE(expected_event_count == file_info.get_event_count());
}
auto create_reader_from_path(std::filesystem::path const& archive_path)
-> Result<ClpArchiveReader> {
return ClpArchiveReader::create(archive_path.string());
}
auto create_reader_from_bytes(std::filesystem::path const& archive_path)
-> Result<ClpArchiveReader> {
auto const mapped_archive{
YSTDLIB_ERROR_HANDLING_TRYX(ReadOnlyMemoryMappedFile::create(archive_path.string()))
};
auto const view{mapped_archive.get_view()};
REQUIRE(false == view.empty());
return ClpArchiveReader::create(std::vector<char>{view.begin(), view.end()});
}
auto run_single_log_file_test(
std::filesystem::path const& archive_path,
std::string const& expected_file_name,
uint64_t expected_event_count
) -> Result<void> {
auto const r_path{YSTDLIB_ERROR_HANDLING_TRYX(create_reader_from_path(archive_path))};
assert_reader_matches_expected(r_path, expected_file_name, expected_event_count);
auto const r_bytes{YSTDLIB_ERROR_HANDLING_TRYX(create_reader_from_bytes(archive_path))};
assert_reader_matches_expected(r_bytes, expected_file_name, expected_event_count);
return success();
}
} // namespace
TEST_CASE("clp_s_ffi_sfa_reader", "[clp-s][ffi][sfa]") {
TestOutputCleaner const test_cleanup{{get_archive_output_root_dir().string()}};
auto const log_file_name{GENERATE(cInputNoFloats, cInputFloatTimestamp)};
auto const log_path{get_log_local_path(log_file_name)};
REQUIRE(std::filesystem::exists(log_path));
auto const archive_path{generate_single_file_archive(log_path)};
REQUIRE(std::filesystem::exists(archive_path));
auto const expected_event_count{get_num_lines(log_path)};
auto const test_result{
run_single_log_file_test(archive_path, log_path.string(), expected_event_count)
};
REQUIRE(false == test_result.has_error());
}