Tests: Add tests for strchrnul, strdup and strndup

This commit is contained in:
apio 2022-10-22 20:36:28 +02:00
parent cf94ca2a4e
commit 6d4d2579ab
2 changed files with 68 additions and 0 deletions

View File

@ -11,8 +11,11 @@ DEFINE_TEST(strnlen);
DEFINE_TEST(strcspn); DEFINE_TEST(strcspn);
DEFINE_TEST(strspn); DEFINE_TEST(strspn);
DEFINE_TEST(strchr); DEFINE_TEST(strchr);
DEFINE_TEST(strchrnul);
DEFINE_TEST(strrchr); DEFINE_TEST(strrchr);
DEFINE_TEST(strpbrk); DEFINE_TEST(strpbrk);
DEFINE_TEST(strdup);
DEFINE_TEST(strndup);
// stdlib.h // stdlib.h
DEFINE_TEST(atoi); DEFINE_TEST(atoi);
@ -35,8 +38,11 @@ int main()
RUN_TEST(strcspn); RUN_TEST(strcspn);
RUN_TEST(strspn); RUN_TEST(strspn);
RUN_TEST(strchr); RUN_TEST(strchr);
RUN_TEST(strchrnul);
RUN_TEST(strrchr); RUN_TEST(strrchr);
RUN_TEST(strpbrk); RUN_TEST(strpbrk);
RUN_TEST(strdup);
RUN_TEST(strndup);
START_TEST_CASE(stdlib.h); START_TEST_CASE(stdlib.h);
RUN_TEST(atoi); RUN_TEST(atoi);

View File

@ -1,4 +1,5 @@
#include "Test.h" #include "Test.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
DEFINE_TEST(memset) DEFINE_TEST(memset)
@ -229,6 +230,27 @@ DEFINE_TEST(strchr)
TEST_SUCCESS(); TEST_SUCCESS();
} }
DEFINE_TEST(strchrnul)
{
START_TEST(strchrnul);
const char* str = "Hello, world!";
char* ptr = strchrnul(str, 'l');
EXPECT_EQ(ptr, str + 2);
ptr = strchrnul(str, 'u');
EXPECT_EQ(ptr, str + 13);
ptr = strchrnul(str, '!');
EXPECT_EQ(ptr, str + 12);
TEST_SUCCESS();
}
DEFINE_TEST(strrchr) DEFINE_TEST(strrchr)
{ {
START_TEST(strrchr); START_TEST(strrchr);
@ -271,5 +293,45 @@ DEFINE_TEST(strpbrk)
EXPECT_EQ(ptr, NULL); EXPECT_EQ(ptr, NULL);
TEST_SUCCESS();
}
DEFINE_TEST(strdup)
{
START_TEST(strdup);
const char* orig = "Well hello there!";
char* copy = strdup(orig);
if (!copy) { TEST_NOT_SURE(copy); }
EXPECT(memcmp(orig, copy, 17) == 0);
free(copy);
TEST_SUCCESS();
}
DEFINE_TEST(strndup)
{
START_TEST(strndup);
const char* orig = "Well hello there!";
char* copy = strndup(orig, 17);
if (!copy) { TEST_NOT_SURE(copy); }
EXPECT(memcmp(orig, copy, 17) == 0);
free(copy);
copy = strndup(orig, 12);
if (!copy) { TEST_NOT_SURE(copy); }
EXPECT_NOT_EQ(memcmp(orig, copy, 14),
0); // FIXME: This is undefined behaviour and the memory could also be by chance identical.
free(copy);
TEST_SUCCESS(); TEST_SUCCESS();
} }