This is from a Codewars challenge so I'll leave out unnecessary code from this function. Basically if n <= 1, I need to return an empty string.
char* sc(int n)
{
if(n > 1) {
//irrelevant code
}
//return strdup("");
return "";
}
If the value being passed is -1, when I use return ""; I get a "Test crashed" message but when I use return strdup(""); I pass the test. Just wondering why this is?
Edit: This is how the function is called. It is only the last test that fails.
#include <criterion/criterion.h>
#include <string.h>
char* sc(int);
void dotest(int n, const char* expect)
{
char* actual = sc(n);
cr_expect(!strcmp(actual, expect), "Expected: '%s', got: '%s'\n", expect, actual);
free(actual);
}
Test(the_multiply_function, should_pass_all_the_tests_provided) {
dotest(2,"Aa~ Pa! Aa!");
dotest(6, "Aa~ Aa~ Aa~ Aa~ Aa~ Pa! Aa!");
dotest(7, "Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Pa!");
dotest(10, "Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Aa~ Pa!");
dotest(1, "");
dotest(-1, "");
}