Luna/tests/libc/string.c

219 lines
3.3 KiB
C
Raw Normal View History

2022-10-16 13:31:58 +00:00
#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();
}
2022-10-16 13:31:58 +00:00
DEFINE_TEST(strlen)
{
START_TEST(strlen);
const char* str = "Hello, World!";
size_t len = strlen(str);
EXPECT_EQ(len, 13);
char null[] = {'\0'};
len = strlen(null);
EXPECT_EQ(len, 0);
2022-10-16 14:00:14 +00:00
TEST_SUCCESS();
}
DEFINE_TEST(strnlen)
{
START_TEST(strnlen);
const char* str = "What is going on?";
size_t len = strnlen(str, 20);
EXPECT_EQ(len, 17);
len = strnlen(str, 15);
EXPECT_EQ(len, 15);
char buf[] = {'H', 'e', 'l', 'l', 'o'};
len = strnlen(buf, sizeof(buf));
EXPECT_EQ(len, sizeof(buf));
2022-10-22 09:57:25 +00:00
TEST_SUCCESS();
}
DEFINE_TEST(strcspn)
{
START_TEST(strcspn);
const char* str = "This string has vowels";
const char* vowels = "aeiou";
size_t len = strcspn(str, vowels);
EXPECT_EQ(len, 2);
str = "WWWWW";
len = strcspn(str, vowels);
EXPECT_EQ(len, 5);
2022-10-22 10:36:31 +00:00
TEST_SUCCESS();
}
DEFINE_TEST(strspn)
{
START_TEST(strspn);
const char* str = "This is a test string";
const char* accept = "This ";
size_t len = strspn(str, accept);
EXPECT_EQ(len, 8);
str = "WWWWW";
len = strspn(str, accept);
EXPECT_EQ(len, 0);
str = "This is hi";
len = strspn(str, accept);
EXPECT_EQ(len, 10);
TEST_SUCCESS();
}
DEFINE_TEST(strchr)
{
START_TEST(strchr);
const char* str = "Hello, world!";
char* ptr = strchr(str, 'l');
EXPECT_EQ(ptr, str + 2);
ptr = strchr(str, 'u');
EXPECT_EQ(ptr, NULL);
ptr = strchr(str, '!');
EXPECT_EQ(ptr, str + 12);
TEST_SUCCESS();
}
DEFINE_TEST(strrchr)
{
START_TEST(strrchr);
const char* str = "Hello, world!";
char* ptr = strrchr(str, 'l');
EXPECT_EQ(ptr, str + 10);
ptr = strrchr(str, 'u');
EXPECT_EQ(ptr, NULL);
ptr = strrchr(str, '!');
EXPECT_EQ(ptr, str + 12);
2022-10-22 16:09:43 +00:00
TEST_SUCCESS();
}
DEFINE_TEST(strpbrk)
{
START_TEST(strpbrk);
const char* str = "Hello, world!";
const char* vowels = "aeiou";
char* ptr = strpbrk(str, vowels);
EXPECT_EQ(ptr, str + 1);
str = "There are more vowels";
ptr = strpbrk(str, vowels);
EXPECT_EQ(ptr, str + 2);
str = "zzzzzz";
ptr = strpbrk(str, vowels);
EXPECT_EQ(ptr, NULL);
2022-10-16 13:31:58 +00:00
TEST_SUCCESS();
}