Add strnlen test

This commit is contained in:
apio 2022-10-16 16:00:14 +02:00
parent d62eb6c791
commit 0a46feb162
4 changed files with 34 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#pragma once
#ifndef __TEST_H_
#define __TEST_H_
#include <stdbool.h>
int printf(const char*, ...);
@ -13,15 +14,15 @@ int printf(const char*, ...);
return true; \
} while (0)
#define TEST_FAIL() \
#define TEST_FAIL(expr) \
do { \
printf("no\n"); \
printf("no (%s)\n", #expr); \
return false; \
} while (0)
#define EXPECT(expr) \
do { \
if (!(expr)) { TEST_FAIL(); } \
if (!(expr)) { TEST_FAIL(expr); } \
} while (0)
#define EXPECT_EQ(a, b) EXPECT((a) == (b))
@ -34,3 +35,5 @@ int printf(const char*, ...);
do { \
if (!test_##name()) return 1; \
} while (0)
#endif

View File

@ -4,7 +4,7 @@ DESTDIR := $(LUNA_ROOT)/initrd/bin
build:
@mkdir -p $(TESTDIR)/bin
$(LUNA_ROOT)/tools/sync-libc.sh
$(CC) $(TESTDIR)/string.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc
$(CC) $(TESTDIR)/string.c $(TESTDIR)/Test.c -I$(LUNA_ROOT)/tests -o $(TESTDIR)/bin/test-libc -Wall -Wextra -Wno-stringop-overread -Werror
install:
$(LUNA_ROOT)/tools/clean.sh

View File

@ -1,9 +1,11 @@
#include "Test.h"
DEFINE_TEST(strlen);
DEFINE_TEST(strnlen);
int main()
{
START_TEST_CASE(string.h);
RUN_TEST(strlen);
RUN_TEST(strnlen);
}

View File

@ -19,3 +19,26 @@ DEFINE_TEST(strlen)
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));
TEST_SUCCESS();
}