Tests: Add tests for memset, memcpy and memchr

This commit is contained in:
apio 2022-10-22 18:33:28 +02:00
parent 766b6d26c8
commit bfbe8e847b
2 changed files with 74 additions and 0 deletions

View File

@ -1,5 +1,9 @@
#include "Test.h"
// string.h
DEFINE_TEST(memset);
DEFINE_TEST(memcpy);
DEFINE_TEST(memchr);
DEFINE_TEST(strlen);
DEFINE_TEST(strnlen);
DEFINE_TEST(strcspn);
@ -8,6 +12,7 @@ DEFINE_TEST(strchr);
DEFINE_TEST(strrchr);
DEFINE_TEST(strpbrk);
// stdlib.h
DEFINE_TEST(atoi);
DEFINE_TEST(atol);
DEFINE_TEST(atoll);
@ -18,6 +23,9 @@ DEFINE_TEST(calloc);
int main()
{
START_TEST_CASE(string.h);
RUN_TEST(memset);
RUN_TEST(memcpy);
RUN_TEST(memchr);
RUN_TEST(strlen);
RUN_TEST(strnlen);
RUN_TEST(strcspn);

View File

@ -1,6 +1,72 @@
#include "Test.h"
#include <string.h>
DEFINE_TEST(memset)
{
START_TEST(memset);
char test[10];
char* ptr = memset(test, 0, 10);
EXPECT_EQ(ptr, test);
for (int i = 0; i < 10; i++) { EXPECT_EQ(test[i], 0); }
ptr = memset(test, 42, 10);
EXPECT_EQ(ptr, test);
for (int i = 0; i < 10; i++) { EXPECT_EQ(test[i], 42); }
TEST_SUCCESS();
}
DEFINE_TEST(memcpy)
{
START_TEST(memcpy);
char buf[20] = "Nothing is going on";
const char* str = "Something is going!";
char* ptr = memcpy(buf, str, 20);
EXPECT_EQ(ptr, buf);
for (int i = 0; i < 20; i++) { EXPECT_EQ(buf[i], str[i]); }
const char* new = "Well...";
ptr = memcpy(buf, new, 7);
EXPECT_EQ(ptr, buf);
for (int i = 0; i < 7; i++) { EXPECT_EQ(buf[i], new[i]); }
for (int i = 7; i < 20; i++) { EXPECT_EQ(buf[i], str[i]); }
TEST_SUCCESS();
}
DEFINE_TEST(memchr)
{
START_TEST(memchr);
char buf[20] = "abcdefghijklmnopqrs";
char* ptr = memchr(buf, 'z', 20);
EXPECT_EQ(ptr, NULL);
ptr = memchr(buf, 'd', 20);
EXPECT_EQ(ptr, buf + 3);
ptr = memchr(buf, 's', 20);
EXPECT_EQ(ptr, buf + 18);
TEST_SUCCESS();
}
DEFINE_TEST(strlen)
{
START_TEST(strlen);