|
1 | 1 | #include <catch2/catch_test_macros.hpp> |
2 | 2 |
|
| 3 | +#include "../../../../tools/memory_injector.hpp" |
3 | 4 | #include <exercises/hackerrank/warmup/time_conversion.h> |
4 | 5 | #include <filesystem> |
5 | 6 | #include <fstream> |
6 | 7 | #include <nlohmann/json.hpp> |
7 | | -#include <vector> |
8 | 8 |
|
9 | 9 | using json = nlohmann::json; |
10 | 10 |
|
11 | 11 | TEST_CASE("time_conversion JSON Test Cases", |
12 | 12 | "[hackerrank] [jsontestcase] [warmup]") { |
13 | | - std::filesystem::path cwd = std::filesystem::current_path(); |
14 | | - std::string path = |
15 | | - cwd.string() + |
16 | | - "/unit/lib/hackerrank/warmup/time_conversion.testcases.json"; |
17 | 13 |
|
18 | | - INFO("time_conversion JSON test cases FILE: " << path); |
| 14 | +#ifdef __linux__ |
| 15 | + SECTION("time_conversion Failure handling during Out of Memory (OOM)") { |
| 16 | + MemoryInjector::enable_oom_fault(); |
19 | 17 |
|
20 | | - std::ifstream f(path); |
21 | | - json data = json::parse(f); |
| 18 | + const char *input = "12:01:00PM"; |
22 | 19 |
|
23 | | - for (auto testcase : data) { |
24 | | - char *result = HACKERRANK_WARMUP_timeConversion( |
25 | | - testcase["input"].get<std::string>().c_str()); |
| 20 | + REQUIRE(HACKERRANK_WARMUP_timeConversion(input) == nullptr); |
26 | 21 |
|
27 | | - std::string result_as_string(result); |
| 22 | + // Clean up the state after finishing the test section |
| 23 | + MemoryInjector::disable_fault(); |
| 24 | + } |
| 25 | +#endif |
| 26 | + |
| 27 | + SECTION("Normal memory allocation succeeds") { |
| 28 | + std::filesystem::path cwd = std::filesystem::current_path(); |
| 29 | + std::string path = |
| 30 | + cwd.string() + |
| 31 | + "/unit/lib/hackerrank/warmup/time_conversion.testcases.json"; |
| 32 | + |
| 33 | + INFO("time_conversion JSON test cases FILE: " << path); |
| 34 | + |
| 35 | + std::ifstream f(path); |
| 36 | + json data = json::parse(f); |
28 | 37 |
|
29 | | - free(result); |
| 38 | + for (auto testcase : data) { |
| 39 | + char *result = HACKERRANK_WARMUP_timeConversion( |
| 40 | + testcase["input"].get<std::string>().c_str()); |
30 | 41 |
|
31 | | - CHECK(result_as_string == testcase["expected"]); |
| 42 | + std::string result_as_string(result); |
| 43 | + |
| 44 | + free(result); |
| 45 | + |
| 46 | + CHECK(result_as_string == testcase["expected"]); |
| 47 | + } |
32 | 48 | } |
33 | | -} |
34 | 49 |
|
35 | | -TEST_CASE("time_conversion edge cases", "[hackerrank] [helper] [warmup]") { |
36 | | - CHECK(HACKERRANK_WARMUP_timeConversion(nullptr) == nullptr); |
| 50 | + SECTION("time_conversion edge cases", "[hackerrank] [helper] [warmup]") { |
| 51 | + CHECK(HACKERRANK_WARMUP_timeConversion(nullptr) == nullptr); |
37 | 52 |
|
38 | | - CHECK(HACKERRANK_WARMUP_timeConversion("") == nullptr); |
| 53 | + CHECK(HACKERRANK_WARMUP_timeConversion("") == nullptr); |
39 | 54 |
|
40 | | - CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr); |
| 55 | + CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr); |
| 56 | + } |
41 | 57 | } |
0 commit comments