Files
klibc/src/tests.asm

593 lines
11 KiB
NASM

%include "src/constants.asm"
; core.asm
extern exit
extern islower
extern isupper
; console.asm
extern print
extern puts
extern printf
; string.asm
extern strlen
extern strcpy
extern strcat
extern strclr
extern strcmp
; convert.asm
extern dec2str
extern udec2str
extern hex2str
section .rodata
TEST_print equ 1
TEST_puts equ 1
TEST_dec2str equ 1 ;includes udec2str
TEST_hex2str equ 1
TEST_printf equ 1
TEST_strlen equ 1
TEST_strcpy equ 1
TEST_strcat equ 1
TEST_strclr equ 1
TEST_islower equ 1
TEST_isupper equ 1
TEST_strcmp equ 1
str1 db "Hello, world!",EOS
str2 db "Hello, World!",EOS
str3 db "Hello world!",EOS
str4 db "Howdy environment!",EOS
str5 db "The quick brown fox jumps over the lazy dog",EOS
msgStrings db "PRINT str1-5:",NL,"str1: %s",NL,"str2: %s",NL,"str3: %s",NL,"str4: %s",NL,"str5: %s",NL,NL,EOS
; print()
msgPrint db NL,"TEST print()",NL,EOS
; puts()
msgPuts db NL,"TEST puts()",EOS
; [u]dec2str() / hex2str()
msgDec2str db NL,"TEST dec2str() / udec2str()",NL,TAB,"Use GDB ('x/s $rax' after function call)",NL,TAB,"No tests printed - see printf outputs :)",NL,EOS
msgHex2str db NL,"TEST hex2str()",NL,TAB,"Use GDB ('x/s $rax' after function call)",NL,TAB,"No tests printed - see printf outputs :)",NL,EOS
; printf()
msgPrintf db NL,"TEST printf()",NL,EOS
printf1 db TAB,"printf(",DQUO,"He%ll%o there%%%%%!%!%\n",DQUO,"): ",NL,TAB,TAB,EOS
printf1Str db "He%ll%o there%%%%%!%!%",NL,EOS
printf2 db TAB,"printf(",DQUO,"Are %s doing %s?\n",DQUO,", printf2Str1, printf2Str2): ",NL,TAB,TAB,EOS
printf2Str db "Are %s doing %s?",NL,EOS
printf2Str1 db "you",EOS
printf2Str2 db "okay",EOS
printf3 db TAB,"printf(",DQUO,"%c%c%c%c%c there %c%c %s%c\n",DQUO,"): ",NL,TAB,TAB,EOS
printf3Str db "%c%c%c%c%c there %c%c %s%c",NL,EOS
printf3C1 equ 'H'
printf3C2 equ 'e'
printf3C3 equ 'l'
printf3C4 equ 'l'
printf3C5 equ 'o'
printf3C6 equ 'm'
printf3C7 equ 'y'
printf3Str1 db "friend",EOS
printf3C8 equ '!'
printf4 db TAB,"printf(",DQUO,"%i|%d|%u , %i|%d|%u\n",DQUO,", -50, -50, -50, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF): ",NL,TAB,TAB,EOS
printf4Str db "%i|%d|%u , %i|%d|%u",NL,EOS
printf5 db TAB,"printf(",DQUO,"%x|%X , %x|%X\n",DQUO,", 0xabcdeffedcba, 0x069bc0e, 666, -1): ",NL,TAB,TAB,EOS
printf5Str db "%x|%X , %x|%X",NL,EOS
; strlen()
msgStrlen db NL,"TEST strlen()",NL,EOS
strlen1 db TAB,"strlen(str1): %d",NL,EOS
strlen2 db TAB,"strlen(str3): %d",NL,EOS
strlen3 db TAB,"strlen(str5): %d",NL,EOS
; strcpy()
msgStrcpy db NL,"TEST strcpy()",NL,EOS
strcpy1 db TAB,"strcpy(strBuff1, str1, 12): ",NL,TAB,TAB,EOS
strcpy2 db TAB,"strcpy(strBuff1, str1, 13): ",NL,TAB,TAB,EOS
strcpy3 db TAB,"strcpy(strBuff1, str1, 32): ",NL,TAB,TAB,EOS
strcpy4 db TAB,"strcpy(strBuff1, str5, 32): ",NL,TAB,TAB,EOS
; strcat()
msgStrcat db NL,"TEST strcat()",NL,EOS
strcat1 db TAB,"strcat(strBuff2, str1, 32): ",NL,TAB,TAB,EOS
strcat2 db TAB,"strcat(strBuff2, str5, 32): ",NL,TAB,TAB,EOS
;strclr()
msgStrclr db NL,"TEST strclr()",NL,EOS
strclr1a db TAB,"strcat(strBuff1, str5, 32)",NL,TAB,"strclr(strBuff1, 32)",NL,TAB,"print(strBuff1): ",NL,TAB,TAB,EOS
strclr1b db TAB,"print(strBuff1[3]): ",NL,TAB,TAB,EOS
strclr2 db TAB,"strcat(strBuff1, str5, 32)",NL,TAB,"strclr(strbuff1, 2)",NL,TAB,"print(strbuff1[3]): ",NL,TAB,TAB,EOS
; islower()
msgIslower db NL,"TEST islower()",NL,EOS
islower1 db TAB,"islower(str1[0]): %d",NL,EOS
islower2 db TAB,"islower(str1[1]): %d",NL,EOS
islower3 db TAB,"islower('d'): %d",NL,EOS
islower4 db TAB,"islower('D'): %d",NL,EOS
islower5 db TAB,"islower('!'): %d",NL,EOS
; isupper()
msgIsupper db NL,"TEST isupper()",NL,EOS
isupper1 db TAB,"isupper(str1[0]): %d",NL,EOS
isupper2 db TAB,"isupper(str1[1]): %d",NL,EOS
isupper3 db TAB,"isupper('d'): %d",NL,EOS
isupper4 db TAB,"isupper('D'): %d",NL,EOS
isupper5 db TAB,"isupper('!'): %d",NL,EOS
; strcmp()
msgStrcmp db NL,"TEST strcmp()",NL,EOS
strcmp1 db TAB,"strcmp(str1, str1): %d",NL,EOS
strcmp2 db TAB,"strcmp(str1, str2): %d",NL,EOS
strcmp3 db TAB,"- strcpy(strBuff1, str1)",NL,TAB,"- strcat(strBuff1, str2)",NL,TAB,"> strBuff1: %s",NL,TAB,"> str1: %s",NL,TAB,"strcmp(str1, strBuff1): %d",NL,EOS
strcmp4 db TAB,"strcmp(strBuff1, str1): %d",NL,EOS
strcmp5 db TAB,"- strclr(strBuff1, 32)",NL,TAB,"strcmp(strBuff1, str1): %d",NL,TAB,"strcmp(str1, strBuff1): %d",NL,EOS
strcmp6 db TAB,"strcmp(str1, str5): %d",NL,EOS
section .data
section .bss
strBuff1 resb 32
strBuff2 resb 32
section .text
global _start
_start:
push rbp
mov rbp, rsp
sub rsp, SIZE_QWORD
; PRINT STRINGS
lea rdi, [rel msgStrings]
lea rsi, [rel str1]
lea rdx, [rel str2]
lea rcx, [rel str3]
lea r8, [rel str4]
lea r9, [rel str5]
call printf
;---
;--- print()
;---
%if TEST_print
lea rdi, [rel msgPrint]
call print
%endif
;---
;--- puts()
;---
%if TEST_puts
lea rdi, [rel msgPuts]
call puts
%endif
;---
;--- dec2str() / udec2str()
;---
%if TEST_dec2str
lea rdi, [rel msgDec2str]
call print
; TEST 1
mov rdi, -569384
call dec2str
; TEST 2
mov rdi, -569384
call udec2str
; TEST 3
mov rdi, 0xFFFFFFFFFFFFFFFF
call dec2str
; TEST 4
mov rdi, 0xFFFFFFFFFFFFFFFF
call udec2str
%endif
;---
;--- hex2str()
;---
%if TEST_hex2str
lea rdi, [rel msgHex2str]
call print
; TEST 1
xor rdi, rdi
mov rsi, 0
call hex2str
mov rdi, -1
mov rsi, 0
call hex2str
; TEST 2
xor rdi, rdi
mov rsi, 1
call hex2str
mov rdi, -1
mov rsi, 1
call hex2str
; TEST 3
mov rdi, 0x123456789ABCDEF
mov rsi, 0
call hex2str
mov rdi, 0x123456789ABCDEF
mov rsi, 1
call hex2str
; TEST 4
mov rdi, 128
mov rsi, 0
call hex2str
%endif
;---
;--- printf()
;---
%if TEST_printf
lea rdi, [rel msgPrintf]
call printf
; TEST 1
lea rdi, [rel printf1]
call print
lea rdi, [rel printf1Str]
call printf
; TEST 2
lea rdi, [rel printf2]
call print
lea rdi, [rel printf2Str]
lea rsi, [rel printf2Str1]
lea rdx, [rel printf2Str2]
call printf
; TEST 3
lea rdi, [rel printf3]
call print
lea rdi, [rel printf3Str]
mov rsi, printf3C1
mov rdx, printf3C2
mov rcx, printf3C3
mov r8, printf3C4
mov r9, printf3C5
push printf3C8
push printf3Str1
push printf3C7
push printf3C6
call printf
add rsp, SIZE_QWORD * 4
; TEST 4
lea rdi, [rel printf4]
call print
lea rdi, [rel printf4Str]
mov rsi, -50
mov rdx, -50
mov rcx, -50
mov r8, 0xFFFFFFFFFFFFFFFF
mov r9, 0xFFFFFFFFFFFFFFFF
sub rsp, SIZE_QWORD
push 0xFFFFFFFFFFFFFFFF
call printf
add rsp, SIZE_QWORD
; TEST 5
lea rdi, [rel printf5]
call print
lea rdi, [rel printf5Str]
mov rsi, 0xabcdeffedcba
mov rdx, 0x069bc0e
mov rcx, 666
mov r8, -1
call printf
%endif
;---
;--- strlen()
;---
%if TEST_strlen
lea rdi, [rel msgStrlen]
call print
; TEST 1
lea rdi, [rel str1]
call strlen
lea rdi, [rel strlen1]
mov rsi, rax
call printf
; TEST 2
lea rdi, [rel str3]
call strlen
lea rdi, [rel strlen2]
mov rsi, rax
call printf
; TEST 3
lea rdi, [rel str5]
call strlen
lea rdi, [rel strlen3]
mov rsi, rax
call printf
%endif
;---
;--- strcpy()
;---
%if TEST_strcpy
lea rdi, [rel msgStrcpy]
call print
; TEST 1
lea rdi, [rel strcpy1]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
mov rdx, 12
call strcpy
mov rdi, rax
call puts
; TEST 2
lea rdi, [rel strcpy2]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
mov rdx, 13
call strcpy
mov rdi, rax
call puts
; TEST 3
lea rdi, [rel strcpy3]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
mov rdx, 32
call strcpy
mov rdi, rax
call puts
; TEST 4
lea rdi, [rel strcpy4]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str5]
mov rdx, 32
call strcpy
mov rdi, rax
call puts
%endif
;---
;--- strcat
;---
%if TEST_strcat
lea rdi, [rel msgStrcat]
call print
; TEST 1
lea rdi, [rel strcat1]
call print
lea rdi, [rel strBuff2]
lea rsi, [rel str1]
mov rcx, 32
call strcat
mov rdi, rax
call puts
; TEST 3
lea rdi, [rel strcat2]
call print
lea rdi, [rel strBuff2]
lea rsi, [rel str5]
mov rcx, 32
call strcat
mov rdi, rax
call puts
%endif
;---
;--- strclr
;---
%if TEST_strclr
lea rdi, [rel msgStrclr]
call print
; TEST 1
lea rdi, [rel strclr1a]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str5]
mov rdx, 32
call strcat
lea rdi, [rel strBuff1]
mov rsi, 32
call strclr
lea rdi, [rel strBuff1]
call puts
lea rdi, [rel strclr1b]
call print
lea rdi, [rel strBuff1]
add rdi, 3
call puts
; TEST 2
lea rdi, [rel strclr2]
call print
lea rdi, [rel strBuff1]
lea rsi, [rel str5]
mov rdx, 32
call strcat
lea rdi, [rel strBuff1]
mov rsi, 2
call strclr
;lea rdi, [rel strBuff1] ;will add output for this later < works as expected tho
;call puts
lea rdi, [rel strBuff1]
add rdi, 3
call puts
%endif
;---
;--- islower()
;---
%if TEST_islower
lea rdi, [rel msgIslower]
call print
; TEST 1
mov dil, byte [str1]
call islower
lea rdi, [rel islower1]
mov rsi, rax
call printf
; TEST 2
mov dil, byte [str1 + 1]
call islower
lea rdi, [rel islower2]
mov rsi, rax
call printf
; TEST 3
mov dil, 'd'
call islower
lea rdi, [rel islower3]
mov rsi, rax
call printf
; TEST 4
mov dil, 'D'
call islower
lea rdi, [rel islower4]
mov rsi, rax
call printf
; TEST 5
mov dil, '!'
call islower
lea rdi, [rel islower5]
mov rsi, rax
call printf
%endif
;---
;--- isupper()
;---
%if TEST_isupper
lea rdi, [rel msgIsupper]
call print
; TEST 1
mov dil, byte [str1]
call isupper
lea rdi, [rel isupper1]
mov rsi, rax
call printf
; TEST 2
mov dil, byte [str1 + 1]
call isupper
lea rdi, [rel isupper2]
mov rsi, rax
call printf
; TEST 3
mov dil, 'd'
call isupper
lea rdi, [rel isupper3]
mov rsi, rax
call printf
; TEST 4
mov dil, 'D'
call isupper
lea rdi, [rel isupper4]
mov rsi, rax
call printf
; TEST 5
mov dil, '!'
call isupper
lea rdi, [rel isupper5]
mov rsi, rax
call printf
%endif
;---
;--- strcmp()
;---
%if TEST_strcmp
lea rdi, [rel msgStrcmp]
call printf
; TEST 1
lea rdi, [rel str1]
lea rsi, [rel str1]
call strcmp
lea rdi, [rel strcmp1]
mov rsi, rax
call printf
; TEST 2
lea rdi, [rel str1]
lea rsi, [rel str2]
call strcmp
lea rdi, [rel strcmp2]
mov rsi, rax
call printf
; TEST 3
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcpy
lea rdi, [rel strBuff1]
lea rsi, [rel str2]
call strcat
lea rdi, [rel str1]
lea rsi, [rel strBuff1]
call strcmp
lea rdi, [rel strcmp3]
lea rsi, [rel strBuff1]
lea rdx, [rel str1]
mov rcx, rax
call printf
; TEST 4
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcmp
lea rdi, [rel strcmp4]
mov rsi, rax
call printf
; TEST 5
lea rdi, [rel strBuff1]
mov rsi, 32
call strclr
lea rdi, [rel strBuff1]
lea rsi, [rel str1]
call strcmp
mov rbx, rax
lea rdi, [rel str1]
lea rsi, [rel strBuff1]
call strcmp
lea rdi, [rel strcmp5]
mov rsi, rbx
mov rdx, rax
call printf
; TEST 6
lea rdi, [rel str1]
lea rsi, [rel str5]
call strcmp
lea rdi, [rel strcmp6]
mov rsi, rax
call printf
%endif
;---
;--- exit()
;---
leave
mov rdi, 0
call exit