11#include < catch2/catch_test_macros.hpp>
22
3+ #include " ../../../../tools/memory_injector.hpp"
34#include < exercises/hackerrank/warmup/compare_triplets.h>
4- #include < iostream>
55#include < vector>
66
77#include < filesystem>
@@ -11,34 +11,59 @@ using json = nlohmann::json;
1111
1212TEST_CASE (" compareTriplets JSON Test Cases" ,
1313 " [hackerrank] [jsontestcase] [warmup]" ) {
14- std::filesystem::path cwd = std::filesystem::current_path ();
15- std::string path =
16- cwd.string () +
17- " /unit/lib/hackerrank/warmup/compare_triplets.testcases.json" ;
1814
19- INFO (" compareTriplets JSON test cases FILE: " << path);
15+ #ifdef __linux__
16+ SECTION (" compareTriplets: Failure handling during Out of Memory (OOM)" ) {
17+ MemoryInjector::enable_oom_fault ();
2018
21- std::ifstream f (path);
22- json data = json::parse (f);
23-
24- for (auto testcase : data) {
25- auto a_size = static_cast <int >(testcase[" a" ].size ());
26- std::vector<int > a_input_vector = testcase[" a" ];
19+ std::vector<int > a_input_vector = {1 , 2 , 3 , 4 };
20+ auto a_size = static_cast <int >(a_input_vector.size ());
2721 const int *a_input_array = a_input_vector.data ();
2822
29- auto b_size = static_cast < int >(testcase[ " b " ]. size ()) ;
30- std::vector< int > b_input_vector = testcase[ " b " ] ;
23+ std::vector< int > b_input_vector = { 4 , 3 , 2 , 1 } ;
24+ auto b_size = static_cast < int >(b_input_vector. size ()) ;
3125 const int *b_input_array = b_input_vector.data ();
3226 int result_count;
3327
34- int *result = HACKERRANK_WARMUP_compareTriplets (
35- a_size, a_input_array, b_size, b_input_array, &result_count);
28+ REQUIRE (HACKERRANK_WARMUP_compareTriplets (a_size, a_input_array, b_size,
29+ b_input_array,
30+ &result_count) == nullptr );
31+
32+ // Clean up the state after finishing the test section
33+ MemoryInjector::disable_fault ();
34+ }
35+ #endif
36+
37+ SECTION (" compareTriplets: Normal memory allocation succeeds" ) {
38+ std::filesystem::path cwd = std::filesystem::current_path ();
39+ std::string path =
40+ cwd.string () +
41+ " /unit/lib/hackerrank/warmup/compare_triplets.testcases.json" ;
42+
43+ INFO (" compareTriplets JSON test cases FILE: " << path);
44+
45+ std::ifstream f (path);
46+ json data = json::parse (f);
47+
48+ for (auto testcase : data) {
49+ auto a_size = static_cast <int >(testcase[" a" ].size ());
50+ std::vector<int > a_input_vector = testcase[" a" ];
51+ const int *a_input_array = a_input_vector.data ();
52+
53+ auto b_size = static_cast <int >(testcase[" b" ].size ());
54+ std::vector<int > b_input_vector = testcase[" b" ];
55+ const int *b_input_array = b_input_vector.data ();
56+ int result_count;
57+
58+ int *result = HACKERRANK_WARMUP_compareTriplets (
59+ a_size, a_input_array, b_size, b_input_array, &result_count);
3660
37- // Crear un vector a partir del array en C
38- std::vector<int > result_as_vector (result, result + result_count);
61+ // Crear un vector a partir del array en C
62+ std::vector<int > result_as_vector (result, result + result_count);
3963
40- CHECK (result_as_vector == testcase[" expected" ]);
64+ CHECK (result_as_vector == testcase[" expected" ]);
4165
42- free (result);
66+ free (result);
67+ }
4368 }
4469}
0 commit comments