-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRunner.cpp
More file actions
74 lines (60 loc) · 2.09 KB
/
TestRunner.cpp
File metadata and controls
74 lines (60 loc) · 2.09 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
#include "config.h"
#include "TestRunner.h"
#include <memory>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/fmt/bundled/core.h>
#include <spdlog/fmt/bundled/color.h>
void initLogger()
{
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_st>(); // Console sink
auto logger = std::make_shared<spdlog::logger>(test_name, console_sink);
logger->set_pattern("[%^%n%$]: %v"); // log the name and the text
spdlog::set_default_logger(logger);
}
template<typename SolutionType>
void LocalTests<SolutionType>::runTests(const std::vector<TestCase>& testCases) {
for (size_t i = 0; i < testCases.size(); ++i) {
const auto& testCase = testCases[i];
SolutionType* solution = createSolution();
auto result = solution->TEST_FUNC(const_cast<test_arg_type&>(testCase.input));
printResult(result, testCase.expected, i + 1);
delete solution;
}
}
template<typename SolutionType>
template<typename T>
void LocalTests<SolutionType>::printResult(const T& result, const T& expected, int testNumber)
{
using namespace fmt;
bool passed = (result == expected);
std::string resultStr = passed ? "PASSED" : "FAILED";
auto color = passed ? terminal_color::green : terminal_color::red;
spdlog::info("Test case {}: {}", testNumber, format(fg(color) | emphasis::bold, resultStr));
if (!passed)
{
if constexpr (is_container<T>::value)
{
print("\t >> Expected: [{}]\n", join(expected, " "));
print("\t >> Received: [{}]\n\n", join(result, " "));
}
else
{
print("\t >> Expected: {}\n", expected);
print("\t >> Received: {}\n\n", result);
}
}
}
int main()
{
// Initialize the logger
initLogger();
// Declare the test cases vector
extern std::vector<TestCase> testCases;
// Run the tests and print the result
LocalTests<LeetCodeSolution> tests;
tests.runTests(testCases);
return 0;
}
// Explicit template instantiation
template class LocalTests<LeetCodeSolution>;