-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.hpp
77 lines (66 loc) · 2.27 KB
/
test_util.hpp
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
#pragma once
#include <algorithm>
#include <fstream>
#include <hpp_proto/json_serializer.hpp>
#include <hpp_proto/pb_serializer.hpp>
#include <ranges>
#include <span>
#include <string>
#include <vector>
inline std::string read_file(const std::string &filename) {
std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(static_cast<std::string::size_type>(in.tellg()));
in.seekg(0, std::ios::beg);
in.read(contents.data(), static_cast<std::streamsize>(contents.size()));
return contents;
}
std::array<char, 2> to_hex(hpp::proto::concepts::byte_type auto c) {
static const char qmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
const auto uc = static_cast<unsigned char>(c);
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
return {qmap[uc >> 4U], qmap[uc & 0x0FU]};
}
std::string to_hex(hpp::proto::concepts::contiguous_byte_range auto const &data) {
std::string result;
result.resize(data.size() * 2);
std::size_t index = 0;
for (auto b : data) {
std::ranges::copy(to_hex(b), &result[index]);
index += 2;
}
return result;
}
template <hpp::proto::compile_time_string str>
constexpr auto operator""_bytes_view() {
hpp::proto::bytes_literal<str> data;
return hpp::proto::bytes_view{data.data(), data.size()};
}
template <hpp::proto::compile_time_string str>
constexpr auto operator""_bytes() {
return static_cast<std::vector<std::byte>>(hpp::proto::bytes_literal<str>{});
}
// NOLINTBEGIN(cert-dcl58-cpp)
namespace std {
template <glz::detail::glaze_t T>
inline std::ostream &operator<<(ostream &os, const T &v) {
#if !defined(HPP_PROTO_DISABLE_GLAZE)
return os << hpp::proto::write_json(v).value();
#else
return os;
#endif
}
inline std::ostream &operator<<(std::ostream &os, const vector<byte> &bytes) { return os << to_hex(bytes); }
inline std::ostream &operator<<(std::ostream &os, span<const byte> bytes) { return os << to_hex(bytes); }
template <typename T>
inline std::ostream &operator<<(ostream &os, const vector<T> &c) {
os << '[';
if (!c.empty()) {
std::for_each(c.begin(), c.end(), [&os](const T &v) { os << ", " << v; });
}
os << ']';
return os;
}
} // namespace std
// NOLINTEND(cert-dcl58-cpp)