Skip to content

Commit 81ebddd

Browse files
author
Gonzalo Diaz
committed
[WIP] coverage increased due new malloc() interceptor.
1 parent 94892a1 commit 81ebddd

1 file changed

Lines changed: 47 additions & 18 deletions

File tree

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,68 @@
1+
// 3rd party libs
12
#include <catch2/catch_test_macros.hpp>
23

4+
#include <nlohmann/json.hpp>
5+
6+
// local libs
7+
#if defined(__linux__)
8+
#include "../../../../tools/memory_injector.hpp"
9+
#endif
310
#include <exercises/hackerrank/warmup/staircase.h>
11+
12+
// std libs
413
#include <filesystem>
514
#include <fstream>
615
#include <nlohmann/json.hpp>
716
#include <vector>
817

918
using json = nlohmann::json;
1019

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;
1524

16-
INFO("staircase JSON test cases FILE: " << path);
25+
MemoryInjector::enable_oom_fault();
26+
REQUIRE(HACKERRANK_WARMUP_staircaseCalculate(input) == nullptr);
27+
MemoryInjector::disable_fault();
1728

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();
2032

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
2338

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";
2543

26-
std::vector<std::string> result_as_vector;
44+
INFO("staircase JSON test cases FILE: " << path);
2745

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);
3148

32-
HACKERRANK_WARMUP_freeStaircase(result, input);
49+
for (auto testcase : data) {
50+
auto input = static_cast<int>(testcase["input"]);
3351

34-
CHECK(result_as_vector == testcase["expected"]);
52+
char **result = HACKERRANK_WARMUP_staircaseCalculate(input);
3553

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+
}
3867
}
3968
}

0 commit comments

Comments
 (0)