Skip to content

Commit a358cd3

Browse files
author
Gonzalo Diaz
committed
[WIP] coverage increased due new malloc() interceptor.
1 parent ad6507c commit a358cd3

1 file changed

Lines changed: 35 additions & 19 deletions

File tree

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
#include <catch2/catch_test_macros.hpp>
22

3+
#include "../../../../tools/memory_injector.hpp"
34
#include <exercises/hackerrank/warmup/time_conversion.h>
45
#include <filesystem>
56
#include <fstream>
67
#include <nlohmann/json.hpp>
7-
#include <vector>
88

99
using json = nlohmann::json;
1010

1111
TEST_CASE("time_conversion JSON Test Cases",
1212
"[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";
1713

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

20-
std::ifstream f(path);
21-
json data = json::parse(f);
18+
const char *input = "12:01:00PM";
2219

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

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

29-
free(result);
38+
for (auto testcase : data) {
39+
char *result = HACKERRANK_WARMUP_timeConversion(
40+
testcase["input"].get<std::string>().c_str());
3041

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+
}
3248
}
33-
}
3449

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

38-
CHECK(HACKERRANK_WARMUP_timeConversion("") == nullptr);
53+
CHECK(HACKERRANK_WARMUP_timeConversion("") == nullptr);
3954

40-
CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr);
55+
CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr);
56+
}
4157
}

0 commit comments

Comments
 (0)