Luna/tests/libc/string.c

129 lines
1.8 KiB
C
Raw Normal View History

2022-10-16 13:31:58 +00:00
#include "Test.h"
#include <string.h>
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-16 13:31:58 +00:00
TEST_SUCCESS();
}