/* * axl_string_tests.c — Unit tests for axl_string.h */ #include "axl_string.h" #include "axl_koan.h" #include "axl_memory.h" /* ------------------------------------------------------------------ */ /* axl_strlen */ /* ------------------------------------------------------------------ */ KOAN(strlen_empty) { i8* s = (i8*)""; ASSERT_UINT_EQ(0, axl_strlen(s)); } KOAN(strlen_simple) { i8* s = (i8*)"hello"; ASSERT_UINT_EQ(5, axl_strlen(s)); } KOAN(strlen_with_special_chars) { i8* s = (i8*)"hello\n\tworld"; ASSERT_UINT_EQ(12, axl_strlen(s)); } /* ------------------------------------------------------------------ */ /* axl_strcpy */ /* ------------------------------------------------------------------ */ KOAN(strcpy_basic) { i8* src = (i8*)"test"; i8* dst = (i8*)axl_malloc(10); ASSERT_NOT_NULL(dst); i8* result = axl_strcpy(dst, src); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("test", (const char*)dst); axl_free(dst); } KOAN(strcpy_empty) { i8* src = (i8*)""; i8* dst = (i8*)axl_malloc(10); ASSERT_NOT_NULL(dst); axl_memset(dst, 0xFF, 10); i8* result = axl_strcpy(dst, src); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("", (const char*)dst); axl_free(dst); } /* ------------------------------------------------------------------ */ /* axl_strncpy */ /* ------------------------------------------------------------------ */ KOAN(strncpy_exact) { i8* src = (i8*)"hello"; i8* dst = (i8*)axl_malloc(10); ASSERT_NOT_NULL(dst); i8* result = axl_strncpy(dst, src, 5); ASSERT_PTR_EQ(dst, result); ASSERT_TRUE(memcmp(dst, "hello", 5) == 0); axl_free(dst); } KOAN(strncpy_truncates) { i8* src = (i8*)"hello world"; i8* dst = (i8*)axl_malloc(10); ASSERT_NOT_NULL(dst); i8* result = axl_strncpy(dst, src, 5); ASSERT_PTR_EQ(dst, result); ASSERT_TRUE(memcmp(dst, "hello", 5) == 0); axl_free(dst); } KOAN(strncpy_pads_with_nulls) { i8* src = (i8*)"hi"; i8* dst = (i8*)axl_malloc(10); ASSERT_NOT_NULL(dst); axl_memset(dst, 0xFF, 10); i8* result = axl_strncpy(dst, src, 5); ASSERT_PTR_EQ(dst, result); ASSERT_TRUE(memcmp(dst, "hi\0\0\0", 5) == 0); axl_free(dst); } /* ------------------------------------------------------------------ */ /* axl_strcat */ /* ------------------------------------------------------------------ */ KOAN(strcat_basic) { i8* dst = (i8*)axl_malloc(20); ASSERT_NOT_NULL(dst); axl_strcpy(dst, (i8*)"hello "); i8* result = axl_strcat(dst, (i8*)"world"); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("hello world", (const char*)dst); axl_free(dst); } KOAN(strcat_to_empty) { i8* dst = (i8*)axl_malloc(20); ASSERT_NOT_NULL(dst); axl_strcpy(dst, (i8*)""); i8* result = axl_strcat(dst, (i8*)"test"); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("test", (const char*)dst); axl_free(dst); } /* ------------------------------------------------------------------ */ /* axl_strncat */ /* ------------------------------------------------------------------ */ KOAN(strncat_full) { i8* dst = (i8*)axl_malloc(20); ASSERT_NOT_NULL(dst); axl_strcpy(dst, (i8*)"hello "); i8* result = axl_strncat(dst, (i8*)"world", 5); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("hello world", (const char*)dst); axl_free(dst); } KOAN(strncat_partial) { i8* dst = (i8*)axl_malloc(20); ASSERT_NOT_NULL(dst); axl_strcpy(dst, (i8*)"hello "); i8* result = axl_strncat(dst, (i8*)"worldwide", 5); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("hello world", (const char*)dst); axl_free(dst); } KOAN(strncat_always_null_terminates) { i8* dst = (i8*)axl_malloc(20); ASSERT_NOT_NULL(dst); axl_strcpy(dst, (i8*)"test"); i8* result = axl_strncat(dst, (i8*)"123456789", 3); ASSERT_PTR_EQ(dst, result); ASSERT_STR_EQ("test123", (const char*)dst); ASSERT_INT_EQ(0, (int)dst[7]); // Ensure null at position 7 axl_free(dst); } /* ------------------------------------------------------------------ */ /* axl_strcmp */ /* ------------------------------------------------------------------ */ KOAN(strcmp_equal) { i8* s1 = (i8*)"hello"; i8* s2 = (i8*)"hello"; ASSERT_INT_EQ(0, axl_strcmp(s1, s2)); } KOAN(strcmp_less) { i8* s1 = (i8*)"abc"; i8* s2 = (i8*)"def"; ASSERT_TRUE(axl_strcmp(s1, s2) < 0); } KOAN(strcmp_greater) { i8* s1 = (i8*)"xyz"; i8* s2 = (i8*)"abc"; ASSERT_TRUE(axl_strcmp(s1, s2) > 0); } KOAN(strcmp_prefix) { i8* s1 = (i8*)"test"; i8* s2 = (i8*)"testing"; ASSERT_TRUE(axl_strcmp(s1, s2) < 0); } KOAN(strcmp_both_empty) { i8* s1 = (i8*)""; i8* s2 = (i8*)""; ASSERT_INT_EQ(0, axl_strcmp(s1, s2)); } KOAN(strcmp_first_empty) { i8* s1 = (i8*)""; i8* s2 = (i8*)"hello"; ASSERT_TRUE(axl_strcmp(s1, s2) < 0); } /* ------------------------------------------------------------------ */ /* Main */ /* ------------------------------------------------------------------ */ int main(void) { axl_init(); return koan_run_all(); }