|
1 | 1 | #include <catch2/catch_test_macros.hpp> |
2 | 2 |
|
3 | | -#include "../../../../tools/memory_injector.hpp" |
4 | 3 | #include <exercises/hackerrank/warmup/plus_minus.h> |
5 | 4 | #include <iostream> |
6 | 5 | #include <stdexcept> |
|
12 | 11 | using json = nlohmann::json; |
13 | 12 |
|
14 | 13 | TEST_CASE("plusMinus JSON Test Cases", "[hackerrank] [jsontestcase] [warmup]") { |
| 14 | + std::filesystem::path cwd = std::filesystem::current_path(); |
| 15 | + std::string path = |
| 16 | + cwd.string() + "/unit/lib/hackerrank/warmup/plus_minus.testcases.json"; |
15 | 17 |
|
16 | | - // SECTION("Failure handling during Out of Memory (OOM)") { |
17 | | - // MemoryInjector::enable_oom_fault(); |
| 18 | + INFO("plusMinus JSON test cases FILE: " << path); |
18 | 19 |
|
19 | | - // int input_size = 3; |
20 | | - // int input_array[] = {1, -2, 0}; |
| 20 | + std::ifstream f(path); |
| 21 | + json data = json::parse(f); |
21 | 22 |
|
22 | | - // REQUIRE(HACKERRANK_WARMUP_plusMinusCalculate(input_size, input_array) == |
23 | | - // nullptr); |
| 23 | + for (auto testcase : data) { |
| 24 | + auto input_size = static_cast<int>(testcase["input"].size()); |
| 25 | + std::vector<int> input_vector = testcase["input"]; |
| 26 | + const int *input_array = input_vector.data(); |
24 | 27 |
|
25 | | - // // Clean up the state after finishing the test section |
26 | | - // MemoryInjector::disable_fault(); |
27 | | - // } |
| 28 | + char **result = |
| 29 | + HACKERRANK_WARMUP_plusMinusCalculate(input_size, input_array); |
28 | 30 |
|
29 | | - SECTION("Normal memory allocation succeeds") { |
30 | | - // Reset the injector state before every section running |
31 | | - // MemoryInjector::disable_fault(); |
| 31 | + std::vector<std::string> result_as_vector; |
32 | 32 |
|
33 | | - std::filesystem::path cwd = std::filesystem::current_path(); |
34 | | - std::string path = |
35 | | - cwd.string() + "/unit/lib/hackerrank/warmup/plus_minus.testcases.json"; |
36 | | - |
37 | | - INFO("plusMinus JSON test cases FILE: " << path); |
38 | | - |
39 | | - std::ifstream f(path); |
40 | | - json data = json::parse(f); |
41 | | - |
42 | | - for (auto testcase : data) { |
43 | | - auto input_size = static_cast<int>(testcase["input"].size()); |
44 | | - std::vector<int> input_vector = testcase["input"]; |
45 | | - const int *input_array = input_vector.data(); |
46 | | - |
47 | | - char **result = |
48 | | - HACKERRANK_WARMUP_plusMinusCalculate(input_size, input_array); |
49 | | - |
50 | | - std::vector<std::string> result_as_vector; |
51 | | - |
52 | | - for (int i = 0; i < HACKERRANK_WARMUP_PLUSMINUS_LIMIT_ANSWERS; i++) { |
53 | | - result_as_vector.emplace_back(result[i]); |
54 | | - } |
55 | | - HACKERRANK_WARMUP_freePlusMinus( |
56 | | - result, HACKERRANK_WARMUP_PLUSMINUS_LIMIT_ANSWERS); |
| 33 | + for (int i = 0; i < HACKERRANK_WARMUP_PLUSMINUS_LIMIT_ANSWERS; i++) { |
| 34 | + result_as_vector.emplace_back(result[i]); |
| 35 | + free(result[i]); |
| 36 | + } |
| 37 | + free(result); |
57 | 38 |
|
58 | | - CHECK(result_as_vector == testcase["expected"]); |
| 39 | + CHECK(result_as_vector == testcase["expected"]); |
59 | 40 |
|
60 | | - // Just call void function, to collect coverage |
61 | | - HACKERRANK_WARMUP_plusMinus(input_size, input_array); |
62 | | - } |
| 41 | + // Just call void function, to collect coverage |
| 42 | + HACKERRANK_WARMUP_plusMinus(input_size, input_array); |
63 | 43 | } |
64 | 44 | } |
0 commit comments