From 511be406bee0ce7ea973625a66aa9486c51ca42b Mon Sep 17 00:00:00 2001 From: Kwarde Date: Fri, 25 Jul 2025 11:04:41 +0200 Subject: [PATCH] Add tests for existing functions (except file functions) --- src/tests.asm | 336 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 336 insertions(+) diff --git a/src/tests.asm b/src/tests.asm index 547986b..16adcc6 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -234,6 +234,8 @@ section .rodata printfStr2 db "%x | %16x | %016x | %#16x | %#016x\n%X | %016X | %16X | %#016X | %#16X\n",EOS addTest(printf_ppadd, "printf(''%p | %16p | %016p | %#16p | %#016p\n%p | %016p | %16p | %#016p | %#16p\n'', 80181775710, [... same arg 9 more times])") printfStr3 db "%p | %16p | %016p | %#16p | %#016p\n%p | %016p | %16p | %#016p | %#16p\n",EOS + addTest(printf_null, "printf(''NULL pointer (%%s): %s\n'', itoa(128, 9, 0, 0))") + printfStr4 db "NULL pointer (%%s): %s\n",EOS ; STRINGS FOR USE IN STRING FUNCTIOns str1 db "Hello, World!",EOS;13 str2 db "Hello, world!",EOS;13 @@ -248,8 +250,69 @@ section .rodata addTest(strlen3, "strlen(str3)") addTest(strlen4, "strlen(str4)") addTest(strlen5, "strlen(str5)") + ;strcpy() + addTestHeader(_strcpy, "strcpy") + addTest(strcpy1, "strcpy(strBuff1, str1, 64)") + addTest(strcpy2, "strcpy(strBuff1, str5, 64)") + addTest(strcpy3, "strcpy(strBuff1, str5, 14)") + ;strcat() + addTestHeader(_strcat, "strcat") + addTest(strcat1, "strcat(strbuff2, str1, 64)") + addTest(strcat2, "strcat(strBuff2, str3, 64)") + addTest(strcat3, "strcat(strBuff2, str5, 64)") + addTest(strcat4, "strcat(strbuff1, str4, 1)") + ;strclr() + addTestHeader(_strclr, "strclr") + addTest(strclr1_1, "strcpy(strBuff1, str1, 64)") + addTest(strclr1_2, "puts(strBuff1)") + addTest(strclr1_3, "strclr(strBuff1, 64)") + addTest(strclr1_4, "puts(strbuff1)") + addTest(strclr2_1, "strclr(strBuff1, 2)") + addTest(strclr2_2, "puts(strBuff1[2])") + ;strcmp() + addTestHeader(_strcmp, "strcmp") + addTest(strcmp1, "strcmp(str1, str2)") ;8 + addTest(strcmp2, "strcmp(str1, str3)") ;6 + addTest(strcmp3_1, "strcpy(strBuff1, str1, 64)") + addTest(strcmp3_2, "strcmp(str1, strBuff1)") ;0 + addTest(strcmp4_1, "strcat(strBuff1, str1, 64)") + addTest(strcmp4_2, "strcmp(str1, strBuff1)") ;13 + addTest(strcmp5, "strcmp(strBuff1, str1)") ;14 + addTest(strcmp6_1, "strclr(strBuff1, 64)") + addTest(strcmp6_2, "strcmp(str1, strBuff1)") ;0 + ;atoi() + addTestHeader(_atoi, "atoi") + addTest(atoi1, "atoi(''079168045'')") ;79168045 + addTest(atoi2, "atoi(''-079168045'')") ;-79168045 + addTest(atoi3, "atoi(''-0xABCDEF'')") ;0 + addTest(atoi4, "atoi(''0xabC0D9efF'')") ;46104682239 + addTest(atoi5, "atoi(''0o77716254'')") ;16751788 + addTest(atoi6, "atoi(''0o77718254'')") ;0 + addTest(atoi7, "atoi(''0b00010111'')") ;23 + addTest(atoi8, "atoi(''0b02010111'')") ;0 + addTest(atoi9, "atoi(''0xabcdefg'')") ;0 + atoiStr1 db "079168045",EOS + atoiStr2 db "-079168045",EOS + atoiStr3 db "-0xABCDEF",EOS + atoiStr4 db "0xabC0D9efF",EOS + atoiStr5 db "0o77716254",EOS + atoiStr6 db "0o77718254",EOS + atoiStr7 db "0b00010111",EOS + atoiStr8 db "0b02010111",EOS + atoiStr9 db "0xabcdefg",EOS + ;itoa() + addTestHeader(_itoa, "itoa") + addTest(itoa1, "// See printf outputs") + ;utoa() + addTestHeader(_utoa, "utoa") + addTest(utoa1, "// See printf outputs") + ;fopen() + ;fclose() + ;fexist() section .bss + strBuff1 resb 64 + strBuff2 resb 64 section .text global _start @@ -688,6 +751,18 @@ _start: call printf add rsp, SIZE_QWORD * 5 assert_eq(178) ;Len of TEST 2 + 4 (since %p always uses prefix 0x, this test has that two times extra => 4 chars) + + ; TEST 4 (null) + printTest(printf_null) + mov rdi, 128 + mov rsi, 9 + mov rdx, 0 + mov rcx, 0 + call itoa + lea rdi, [rel printfStr4] + mov rsi, rax + call printf + assert_eq(26) %endif ;;; @@ -736,6 +811,267 @@ _start: assert_eq(43) %endif +;--- strcpy() +%if TEST_strcpy + printTestHeader(_strcpy) + + ; TEST 1: strcpy(strBuff1, str1, 64) + printTest(strcpy1) + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 64 + call strcpy + lea rdi, [rel strBuff1] + assert_eq(rdi) + lea rdi, [rel strBuff1] + call puts + + ; TEST 2: strcpy(strBuff1, str5, 64) + printTest(strcpy2) + lea rdi, [rel strBuff1] + lea rsi, [rel str5] + mov rdx, 64 + call strcpy + lea rdi, [rel strBuff1] + assert_eq(rdi) + lea rdi, [rel strBuff1] + call puts + + ; TEST 3: strcpy(strBuff1, str5, 14) + printTest(strcpy3) + lea rdi, [rel strBuff1] + lea rsi, [rel str5] + mov rdx, 14 + call strcpy + lea rdi, [rel strBuff1] + assert_eq(rdi) + lea rdi, [rel strBuff1] + call puts +%endif + +;--- strcat() +%if TEST_strcat + printTestHeader(_strcat) + + ; TEST 1: strcat(strBuff2, str1, 64) + printTest(strcat1) + lea rdi, [rel strBuff2] + lea rsi, [rel str1] + mov rdx, 64 + call strcat + assert_eq(strBuff2) + lea rdi, [rel strBuff2] + call puts + + ; TEST 2: strcat(strBuff2, str3, 64) + printTest(strcat2) + lea rdi, [rel strBuff2] + lea rsi, [rel str3] + mov rdx, 64 + call strcat + assert_eq(strBuff2) + lea rdi, [rel strBuff2] + call puts + + ; TEST 3: strcat(strBuff2, str5, 64) + printTest(strcat3) + lea rdi, [rel strBuff2] + lea rsi, [rel str5] + mov rdx, 64 + call strcat + assert_eq(strBuff2) + lea rdi, [rel strBuff2] + call puts + + ; TEST 4: strcat(strBuff1, str4, 1) + ;printTest(strcat4) + ;lea rdi, [rel strBuff1] + ;lea rsi, [rel str4] + ;mov rdx, 1 + ;call strcat ;SIGSEGV + ;assert_eq(strBuff1) + ;lea rdi, [rel strBuff1] + ;call puts +%endif + +;--- strclr() +%if TEST_strclr + printTestHeader(_strclr) + + ; TEST 1 + printTest(strclr1_1) + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 64 + call strcpy + printTest(strclr1_2) + lea rdi, [rel strBuff1] + call puts + printTest(strclr1_3) + lea rdi, [rel strBuff1] + mov rsi, 64 + call strclr + printTest(strclr1_4) + lea rdi, [rel strBuff1] + call puts + assert_eq(1) ;1 char printed, NL + + ; TEST 2 + printTest(strclr1_1) + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 64 + call strcpy + printTest(strclr1_2) + lea rdi, [rel strBuff1] + call puts + printTest(strclr2_1) + lea rdi, [rel strBuff1] + mov rsi, 2 + call strclr + printTest(strclr1_4) + lea rdi, [rel strBuff1] + call puts + assert_eq(1) + printTest(strclr2_2) + lea rdi, [rel strBuff1] + add rdi, 2 + call puts + assert_eq(12) +%endif + +;--- strcmp() +%if TEST_strcmp + printTestHeader(_strcmp) + + ; TEST 1: strcmp(str1, str2) + printTest(strcmp1) + lea rdi, [rel str1] + lea rsi, [rel str2] + call strcmp + assert_eq(8) + + ; TEST 2: strcmp(str1, str3) + printTest(strcmp2) + lea rdi, [rel str1] + lea rsi, [rel str3] + call strcmp + assert_eq(6) + + ; TEST 3: strcmp(str1, strBuff1) (eq) + printTest(strcmp3_1) + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 64 + call strcpy + printTest(strcmp3_2) + lea rdi, [rel str1] + lea rsi, [rel strBuff1] + call strcmp + assert_eq(0) + + ; TEST 4: strcmp(str1, strBuff1) (strBuff1 longer) + printTest(strcmp4_1) + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + mov rdx, 64 + call strcat + printTest(strcmp4_2) + lea rdi, [rel str1] + lea rsi, [rel strBuff1] + call strcmp + assert_eq(13) + + ; TEST 5: strcmp(strBuff1, str1) + printTest(strcmp5) + lea rdi, [rel strBuff1] + lea rsi, [rel str1] + call strcmp + assert_eq(14) + + ; TEST 6: strcmp(str1, strBuff1) (strBuff1 empty) + printTest(strcmp6_1) + lea rdi, [rel strBuff1] + mov rsi, 64 + call strclr + printTest(strcmp6_2) + lea rdi, [rel str1] + lea rsi, [rel strBuff1] + call strcmp + assert_eq(0) +%endif + +;--- atoi() +%if TEST_atoi + printTestHeader(_atoi) + + ; TEST 1 + printTest(atoi1) + lea rdi, [rel atoiStr1] + call atoi + assert_u_eq(79168045) + + ; TEST 2 + printTest(atoi2) + lea rdi, [rel atoiStr2] + call atoi + assert_eq(-79168045) + + ; TEST 3 + printTest(atoi3) + lea rdi, [rel atoiStr3] + call atoi + assert_u_eq(0) + + ; TEST 4 + printTest(atoi4) + lea rdi, [rel atoiStr4] + call atoi + assert_u_eq(46104682239) + + ; TEST 5 + printTest(atoi5) + lea rdi, [rel atoiStr5] + call atoi + assert_u_eq(16751788) + + ; TEST 6 + printTest(atoi6) + lea rdi, [rel atoiStr6] + call atoi + assert_u_eq(0) + + ; TEST 7 + printTest(atoi7) + lea rdi, [rel atoiStr7] + call atoi + assert_u_eq(23) + + ; TEST 8 + printTest(atoi8) + lea rdi, [rel atoiStr8] + call atoi + assert_u_eq(0) + + ; TEST 9 + printTest(atoi9) + lea rdi, [rel atoiStr9] + call atoi + assert_u_eq(0) +%endif + +;--- itoa() +%if TEST_itoa + printTestHeader(_itoa) + printTest(itoa1) +%endif + +;--- utoa() +%if TEST_utoa + printTestHeader(_utoa) + printTest(utoa1) +%endif + ;;; ;;; TEST RESULTS ;;;