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:
22
test/val/lib_common_strlen.c
Normal file
22
test/val/lib_common_strlen.c
Normal 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
|
||||
29
test/val/lib_common_strnlen.c
Normal file
29
test/val/lib_common_strnlen.c
Normal 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
|
||||
Reference in New Issue
Block a user