diff --git a/tests/Test.h b/tests/Test.h index 0430c7e5..5d69714e 100644 --- a/tests/Test.h +++ b/tests/Test.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef __TEST_H_ +#define __TEST_H_ #include 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)) @@ -33,4 +34,6 @@ int printf(const char*, ...); #define RUN_TEST(name) \ do { \ if (!test_##name()) return 1; \ - } while (0) \ No newline at end of file + } while (0) + +#endif \ No newline at end of file diff --git a/tests/libc/Makefile b/tests/libc/Makefile index 3f6e9ce5..dc0dd772 100644 --- a/tests/libc/Makefile +++ b/tests/libc/Makefile @@ -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 diff --git a/tests/libc/Test.c b/tests/libc/Test.c index b97475b0..517c5e9a 100644 --- a/tests/libc/Test.c +++ b/tests/libc/Test.c @@ -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); } \ No newline at end of file diff --git a/tests/libc/string.c b/tests/libc/string.c index a603dcc6..f3d1bf2f 100644 --- a/tests/libc/string.c +++ b/tests/libc/string.c @@ -17,5 +17,28 @@ DEFINE_TEST(strlen) EXPECT_EQ(len, 0); + 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(); } \ No newline at end of file