1
0
mirror of https://github.com/cc65/cc65.git synced 2026-01-22 17:16:21 +00:00

Add strlen and strnlen unit tests

This commit is contained in:
Colin Leroy-Mira
2025-07-03 23:43:04 +02:00
parent 54a2410b5a
commit fcbc253bf9
2 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
#include <string.h>
#include "unittest.h"
#define SHORT_STR "abcdefghijklmnopqrstuvwxyz"
#define MID_STR_LEN 700 /* Two pages and something */
TEST
{
char *src;
int i;
/* Long enough for the whole string */
ASSERT_IsTrue(strlen("") == 0, "strlen(\"\") != 0");
ASSERT_IsTrue(strlen(SHORT_STR) == 26, "strlen(\""SHORT_STR"\") != 26");
src = malloc(MID_STR_LEN+1);
ASSERT_IsTrue(src != NULL, "Could not allocate source string");
memset(src, 'a', MID_STR_LEN-1);
src[MID_STR_LEN] = '\0';
ASSERT_IsTrue(strlen(src) == MID_STR_LEN, "strlen(\"700 chars\") != 700");
}
ENDTEST

View File

@@ -0,0 +1,29 @@
#include <string.h>
#include "unittest.h"
#define SHORT_STR "abcdefghijklmnopqrstuvwxyz"
#define MID_STR_LEN 700 /* Two pages and something */
TEST
{
char *src;
int i;
/* Long enough for the whole string */
ASSERT_IsTrue(strnlen("", 0) == 0, "strnlen(\"\", 0) != 0");
ASSERT_IsTrue(strnlen("", 10) == 0, "strnlen(\"\", 10) != 0");
ASSERT_IsTrue(strnlen(SHORT_STR, 0) == 0, "strnlen(\""SHORT_STR"\", 0) != 0");
ASSERT_IsTrue(strnlen(SHORT_STR, 10) == 10, "strnlen(\""SHORT_STR"\", 10) != 10");
ASSERT_IsTrue(strnlen(SHORT_STR, 26) == 26, "strnlen(\""SHORT_STR"\", 26) != 26");
ASSERT_IsTrue(strnlen(SHORT_STR, 50) == 26, "strnlen(\""SHORT_STR"\", 50) != 26");
src = malloc(MID_STR_LEN+1);
ASSERT_IsTrue(src != NULL, "Could not allocate source string");
memset(src, 'a', MID_STR_LEN-1);
src[MID_STR_LEN] = '\0';
ASSERT_IsTrue(strnlen(src, 0) == 0, "strnlen(src, 0) != 0");
ASSERT_IsTrue(strnlen(src, 10) == 10, "strnlen(src, 10) != 10");
ASSERT_IsTrue(strnlen(src, 260) == 260, "strnlen(src, 260) != 260");
ASSERT_IsTrue(strnlen(src, MID_STR_LEN+1) == MID_STR_LEN, "strnlen(src, MID_STR_LEN+1) != MID_STR_LEN");
}
ENDTEST