|
| 1 | +// 3rd party libs |
1 | 2 | #include <catch2/catch_test_macros.hpp> |
2 | 3 |
|
| 4 | +#include <nlohmann/json.hpp> |
| 5 | + |
| 6 | +// local libs |
| 7 | +#if defined(__linux__) |
| 8 | +#include "../../../../tools/memory_injector.hpp" |
| 9 | +#endif |
3 | 10 | #include <exercises/hackerrank/warmup/staircase.h> |
| 11 | + |
| 12 | +// std libs |
4 | 13 | #include <filesystem> |
5 | 14 | #include <fstream> |
6 | 15 | #include <nlohmann/json.hpp> |
7 | 16 | #include <vector> |
8 | 17 |
|
9 | 18 | using json = nlohmann::json; |
10 | 19 |
|
11 | | -TEST_CASE("staircase", "[warmup]") { |
12 | | - std::filesystem::path cwd = std::filesystem::current_path(); |
13 | | - std::string path = |
14 | | - cwd.string() + "/unit/lib/hackerrank/warmup/staircase.testcases.json"; |
| 20 | +TEST_CASE("staircase JSON Test Cases", "[hackerrank] [jsontestcase] [warmup]") { |
| 21 | +#if defined(__linux__) |
| 22 | + SECTION("staircase: Failure handling during Out of Memory (OOM)") { |
| 23 | + int input = 4; |
15 | 24 |
|
16 | | - INFO("staircase JSON test cases FILE: " << path); |
| 25 | + MemoryInjector::enable_oom_fault(); |
| 26 | + REQUIRE(HACKERRANK_WARMUP_staircaseCalculate(input) == nullptr); |
| 27 | + MemoryInjector::disable_fault(); |
17 | 28 |
|
18 | | - std::ifstream f(path); |
19 | | - json data = json::parse(f); |
| 29 | + MemoryInjector::fail_on_allocation_number(2); |
| 30 | + REQUIRE(HACKERRANK_WARMUP_staircaseCalculate(input) == nullptr); |
| 31 | + MemoryInjector::disable_fault(); |
20 | 32 |
|
21 | | - for (auto testcase : data) { |
22 | | - auto input = static_cast<int>(testcase["input"]); |
| 33 | + MemoryInjector::enable_oom_fault(); |
| 34 | + REQUIRE_NOTHROW(HACKERRANK_WARMUP_staircase(input)); |
| 35 | + MemoryInjector::disable_fault(); |
| 36 | + } |
| 37 | +#endif |
23 | 38 |
|
24 | | - char **result = HACKERRANK_WARMUP_staircaseCalculate(input); |
| 39 | + SECTION("plusMinus: Normal memory allocation succeeds") { |
| 40 | + std::filesystem::path cwd = std::filesystem::current_path(); |
| 41 | + std::string path = |
| 42 | + cwd.string() + "/unit/lib/hackerrank/warmup/staircase.testcases.json"; |
25 | 43 |
|
26 | | - std::vector<std::string> result_as_vector; |
| 44 | + INFO("staircase JSON test cases FILE: " << path); |
27 | 45 |
|
28 | | - for (int i = 0; i < input; i++) { |
29 | | - result_as_vector.emplace_back(result[i]); |
30 | | - } |
| 46 | + std::ifstream f(path); |
| 47 | + json data = json::parse(f); |
31 | 48 |
|
32 | | - HACKERRANK_WARMUP_freeStaircase(result, input); |
| 49 | + for (auto testcase : data) { |
| 50 | + auto input = static_cast<int>(testcase["input"]); |
33 | 51 |
|
34 | | - CHECK(result_as_vector == testcase["expected"]); |
| 52 | + char **result = HACKERRANK_WARMUP_staircaseCalculate(input); |
35 | 53 |
|
36 | | - // Just call void function, to collect coverage |
37 | | - HACKERRANK_WARMUP_staircase(input); |
| 54 | + std::vector<std::string> result_as_vector; |
| 55 | + |
| 56 | + for (int i = 0; i < input; i++) { |
| 57 | + result_as_vector.emplace_back(result[i]); |
| 58 | + } |
| 59 | + |
| 60 | + HACKERRANK_WARMUP_freeStaircase(result, input); |
| 61 | + |
| 62 | + CHECK(result_as_vector == testcase["expected"]); |
| 63 | + |
| 64 | + // Just call void function, to collect coverage |
| 65 | + HACKERRANK_WARMUP_staircase(input); |
| 66 | + } |
38 | 67 | } |
39 | 68 | } |
0 commit comments