-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelper.hpp
More file actions
38 lines (35 loc) · 972 Bytes
/
helper.hpp
File metadata and controls
38 lines (35 loc) · 972 Bytes
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
#pragma once
#include <functional>
#include <optional>
#include <source_location>
#include <sstream>
#include <string_view>
template<typename T, typename U>
void assert_equal(
const T& left,
const U& right,
const std::source_location& loc = std::source_location::current()
) {
if (left != right) {
std::ostringstream message;
message << left << " != " << right;
message << " in " << loc.function_name() << " " << loc.file_name() << ":" << loc.line();
throw std::runtime_error{message.str()};
}
}
template<typename T>
void assert_exception(
const std::function<void(void)>& code,
std::optional<std::string_view> message = std::nullopt
) {
std::optional<T> exception;
try {
code();
} catch (const T& e) {
exception = e;
}
assert_equal(exception.has_value(), true);
if (message) {
assert_equal(std::string_view{exception.value().what()}, message.value());
}
}