Testing: add tests for string_ends_with function

This commit is contained in:
Shahram Najm 2022-03-14 15:15:05 +00:00
parent b4b4f7b4ae
commit a2cb76c7ec
2 changed files with 21 additions and 5 deletions

View File

@ -148,15 +148,15 @@ int string_to_long(const char* input, long* output)
return GRIB_SUCCESS;
}
/* Return 1 if str1 ends with str2, 0 otherwise */
int string_ends_with(const char* str1, const char* str2)
/* Return 1 if 's' ends with 'suffix', 0 otherwise */
int string_ends_with(const char* s, const char* suffix)
{
const size_t len1 = strlen(str1);
const size_t len2 = strlen(str2);
const size_t len1 = strlen(s);
const size_t len2 = strlen(suffix);
if (len2 > len1)
return 0;
if (strcmp(&str1[len1 - len2], str2) == 0)
if (strcmp(&s[len1 - len2], suffix) == 0)
return 1;
return 0;
}

View File

@ -1527,6 +1527,21 @@ static void test_trimming()
assert( strcmp(pE, "Apostle In Triumph")==0 );
}
static void test_string_ends_with()
{
printf("Testing: test_string_ends_with...\n");
assert( string_ends_with("GRIB2.tmpl", "tmpl") == 1 );
assert( string_ends_with("GRIB2.tmpl", ".tmpl") == 1 );
assert( string_ends_with("", "") == 1 );
assert( string_ends_with(".", ".") == 1 );
assert( string_ends_with("Bam", "") == 1 );
assert( string_ends_with("GRIB2.tmpl", "tmp") == 0 );
assert( string_ends_with("GRIB2.tmpl", "tmpl0") == 0 );
assert( string_ends_with("GRIB2.tmpl", "1.tmpl") == 0 );
assert( string_ends_with("GRIB2.tmpl", " ") == 0 );
}
static void test_gribex_mode()
{
grib_context* c = grib_context_get_default();
@ -1544,6 +1559,7 @@ int main(int argc, char** argv)
/*printf("Doing unit tests. ecCodes version = %ld\n", grib_get_api_version());*/
test_trimming();
test_string_ends_with();
test_get_git_sha1();
test_get_build_date();