From 34e03fd9ecff6eae7f3002b29787f753a9abf1d5 Mon Sep 17 00:00:00 2001 From: Sven Michael Klose Date: Sat, 25 Dec 2021 09:13:43 +0100 Subject: [PATCH] Add test of strnlen(). --- targettest/Makefile | 4 ++++ targettest/strnlen.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 targettest/strnlen.c diff --git a/targettest/Makefile b/targettest/Makefile index f3694335c..44933949a 100644 --- a/targettest/Makefile +++ b/targettest/Makefile @@ -160,6 +160,7 @@ EXELIST_c64 = \ scanf-test \ ser-test \ strdup-test \ + strnlen \ stroserror-test \ strqtok-test \ tinyshell \ @@ -190,6 +191,7 @@ EXELIST_vic20 = \ rename-test \ scanf-test \ strdup-test \ + strnlen \ stroserror-test \ strqtok-test \ tinyshell \ @@ -222,6 +224,7 @@ EXELIST_apple2 = \ seek \ ser-test \ strdup-test \ + strnlen \ stroserror-test \ strqtok-test \ tinyshell \ @@ -257,6 +260,7 @@ EXELIST_atari = \ seek \ ser-test \ strdup-test \ + strnlen \ stroserror-test \ strqtok-test \ tinyshell \ diff --git a/targettest/strnlen.c b/targettest/strnlen.c new file mode 100644 index 000000000..d07b7628c --- /dev/null +++ b/targettest/strnlen.c @@ -0,0 +1,32 @@ +#include +#include +#include + +const char * str = "0123456789"; + +void +check (size_t result, size_t expected) +{ + if (result != expected) { + printf ("Expected strnlen() to return %d, got %d.\n", + expected, result); + exit (EXIT_FAILURE); + } +} + +int +main (void) +{ + size_t maxlen = strlen (str); + size_t result; + size_t expected; + + for (expected = 0; expected < maxlen; expected++) + check (strnlen (str, expected), expected); + + check (strnlen (str, maxlen << 1), maxlen); + + printf ("strnlen() OK.\n"); + + return EXIT_SUCCESS; +}