test_kprint

This commit is contained in:
timerix 2023-01-18 03:49:44 +06:00
parent ab761aa395
commit 169165ac5a
7 changed files with 103 additions and 29 deletions

View File

@ -72,15 +72,23 @@ typedef uint8 bool;
#define __count_args(\ #define __count_args(\
a0, a1, a2, a3, a4, a5, a6, a7,\ a0, a1, a2, a3, a4, a5, a6, a7 ,\
a8, a9, a10,a11,a12,a13,a14,a15,\ a8, a9, a10,a11,a12,a13,a14,a15,\
a16,a17,a18,a19,a20,a21,a22,a23,\ a16,a17,a18,a19,a20,a21,a22,a23,\
a24,a25,a26,a27,a28,a29,a30,a31,\ a24,a25,a26,a27,a28,a29,a30,a31,\
a32,...) a32 a32,a33,a34,a35,a36,a37,a38,a39,\
// Macro for counting variadic arguments a40,a41,a42,a43,a44,a45,a46,a47,\
a48,a49,a50,a51,a52,a53,a54,a55,\
a56,a57,a58,a59,a60,a61,a62,a63,\
a64,...) a64
// Macro for counting variadic arguments (max 64)
// (see usage in kprint.h) // (see usage in kprint.h)
#define count_args(ARGS...) __count_args(\ #define count_args(ARGS...) __count_args(\
ARGS,\ ARGS,\
64,63,62,61,60,59,58,57,\
56,55,54,53,52,51,50,49,\
48,47,46,45,44,43,42,41,\
40,39,38,37,36,35,34,33,\
32,31,30,29,28,27,26,25,\ 32,31,30,29,28,27,26,25,\
24,23,22,21,20,19,18,17,\ 24,23,22,21,20,19,18,17,\
16,15,14,13,12,11,10,9,\ 16,15,14,13,12,11,10,9,\

View File

@ -34,7 +34,14 @@ Maybe __next_toString(kp_fmt f, __kp_value_union* object){
return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f))); return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f)));
} }
Maybe check_argsN(uint8 n){
if(n%2 != 0) safethrow("kprint recieved non-even number of arguments",;);
if(n > 32) safethrow("kprint recieved >32 number of arguments",;);
return MaybeNull;
}
Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){ Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){
try(check_argsN(n), _,;);
n/=2; n/=2;
StringBuilder* strb=StringBuilder_create(); StringBuilder* strb=StringBuilder_create();
for(uint8 i=0; i<n; i++){ for(uint8 i=0; i<n; i++){
@ -47,6 +54,7 @@ Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){
} }
Maybe __kfprint(FILE* file, uint8 n, kp_fmt* formats, __kp_value_union* objects){ Maybe __kfprint(FILE* file, uint8 n, kp_fmt* formats, __kp_value_union* objects){
try(check_argsN(n), _,;);
n/=2; n/=2;
for(uint8 i=0; i<n; i++){ for(uint8 i=0; i<n; i++){
try(__next_toString(formats[i], &objects[i]),maybeStr,;); try(__next_toString(formats[i], &objects[i]),maybeStr,;);
@ -59,6 +67,7 @@ Maybe __kfprint(FILE* file, uint8 n, kp_fmt* formats, __kp_value_union* objects)
} }
void __kprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){ void __kprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){
tryLast(check_argsN(n), _);
n/=2; n/=2;
for(uint8 i=0; i<n; i++){ for(uint8 i=0; i<n; i++){
kp_fmt fmt=formats[i]; kp_fmt fmt=formats[i];

View File

@ -1,29 +1,11 @@
#include "tests.h" #include "tests.h"
void test_all(){
test_string();
test_safethrow();
test_searchtree();
test_autoarr();
test_autoarrVsVector();
test_hash_functions();
test_hashtable();
test_dtsod();
test_rng_algorithms();
test_kprint_colors();
kprintf("\e[96m--------------------------------------\e[0m\n");
}
int main(){ int main(){
if(!setlocale(LC_ALL, "C.UTF8")) if(!setlocale(LC_ALL, "C.UTF8"))
kprintf("\e[93msetlocale failed\n"); kprintf("\e[93msetlocale failed\n");
ktDescriptors_beginInit(); ktDescriptors_beginInit();
ktDescriptors_initKerepTypes(); ktDescriptors_initKerepTypes();
ktDescriptors_endInit(); ktDescriptors_endInit();
for(ktid id=0; id<ktid_last; id++){
ktDescriptor d=ktDescriptor_get(id);
kprintf("{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n",
d.id, d.name, d.size, d.freeMembers, d.toString);
}
kprintf("\e[97mkerep tests are starting!\n"); kprintf("\e[97mkerep tests are starting!\n");
optime("test_all",1,test_all()); optime("test_all",1,test_all());
ktDescriptors_free(); ktDescriptors_free();

50
tests/test_kprint.c Normal file
View File

@ -0,0 +1,50 @@
#include "tests.h"
#include "../src/kprint/kprint.h"
void test_kprint(){
//int
kprint(kp_fgCyan|
kp_i,-8888, kp_c,' ', kp_i,0, kp_c,' ', kp_i,1234567890987654321LL,kp_s,"\n");
//uint
kprint(kp_fgGreen|
kp_u|kp_post,-8888, kp_c|kp_post|kp_upper,' ', kp_u,0, kp_c,' ',
kp_u,1234567890987654321LL, kp_c,'\n');
//float
kprint(kp_bgGreenD|kp_fgRed|
kp_f,-4000.0109f, kp_c,' ', kp_f,-0.000020004f, kp_c,'\n',
kp_f,-1.0f, kp_c,' ', kp_f,0.0f, kp_c,' ', kp_f,1.0f, kp_c,'\n',
kp_f|kp_post,0.000020004f, kp_c,' ',
kp_f|kp_post|kp_upper,4000.0109f, kp_c,'\n');
//double
kprint(kp_bgBlueD|kp_fgRed|
kp_f,-4000.0109, kp_c,' ', kp_f,-0.000020004, kp_c,'\n',
kp_f,-1.0, kp_c,' ', kp_f,0.0, kp_c,' ', kp_f,1.0, kp_c,'\n',
kp_f|kp_post,0.000020004, kp_c,' ',
kp_f|kp_post|kp_upper,4000.0109, kp_c,'\n');
//hex
kprint(kp_bgBlack|kp_fgYellow|
kp_h,0, kp_c,' ', kp_h,1, kp_c,' ', kp_h,-1, kp_c,' ', kp_h,15, kp_c,'\n',
kp_h,4095, kp_c,' ', kp_h,-4095, kp_c,'\n',
kp_h,1234567890987654321LL, kp_c,'\n', kp_h,-1234567890987654321LL, kp_c,'\n');
kprint(
kp_h,-1.0f, kp_c,' ', kp_h,0.0f, kp_c,' ', kp_h,1.0f, kp_c,'\n',
kp_h, 0.00016f, kp_c,' ', kp_h,-115515.009f, kp_c,'\n');
kprint(
kp_h|kp_pre, 0.00016, kp_c,'\n',
kp_h|kp_pre|kp_upper,-115515.009, kp_c,'\n');
//bin
kprint(kp_fgBlue|
kp_b,0, kp_c,' ', kp_b,1, kp_c,' ', kp_b,-1, kp_c,' ', kp_b,15, kp_c,' ',
kp_b,4095, kp_c,' ', kp_b,-4095, kp_c,'\n',
kp_b,1234567890987654321LL, kp_c,'\n', kp_b,-1234567890987654321LL, kp_c,'\n');
kprint(
kp_b,-1.0f, kp_c,' ', kp_b,0.0f, kp_c,' ', kp_b,1.0f, kp_c,'\n',
kp_b, 0.00016f, kp_c,' ', kp_b,-115515.009f, kp_c,'\n');
kprint(
kp_b|kp_pre, 0.00016, kp_c,'\n',
kp_b|kp_pre|kp_upper,-115515.009, kp_c,'\n');
//string
kprint(kp_s|kp_fgYellow, "\n ooo \n",
kp_s|kp_fgWhite, "gg",
kp_fgGray|kp_bgBlack|kp_s, "\n");
}

View File

@ -5,11 +5,11 @@
#endif #endif
#define testColor(COLOR) \ #define testColor(COLOR) \
kprint_setColor(kp_bgBlack | kp_fg##COLOR);\ kprint_setColor(kp_bgBlack|kp_fg##COLOR);\
kprintf(#COLOR " ");\ kprintf(#COLOR " ");\
kprint_setColor(kp_bg##COLOR | kp_fgGray);\ kprint_setColor(kp_bg##COLOR|kp_fgGray);\
kprintf(#COLOR);\ kprintf(#COLOR);\
kprint_setColor(kp_bgBlack | kp_fgBlack);\ kprint_setColor(kp_bgBlack|kp_fgBlack);\
kprintf("\n"); kprintf("\n");
void test_kprint_colors(){ void test_kprint_colors(){
@ -44,7 +44,5 @@ void test_kprint_colors(){
testColor(Magenta); testColor(Magenta);
testColor(Cyan); testColor(Cyan);
testColor(White); testColor(White);
kprint_setColor(kp_bgBlack | kp_fgGray); kprint_setColor(kp_bgBlack|kp_fgGray);
}
kprint(kprint_fmtInt | kp_fgCyan, 8888, kprint_fmtString | kp_fgYellow, " ooo ", kprint_fmtFloat | kp_bgGreenD | kp_fgRed, 4.01, kprint_fmtString | kp_fgWhite, "\ngg\n");
}

9
tests/test_type_system.c Normal file
View File

@ -0,0 +1,9 @@
#include "tests.h"
void test_type_system(){
for(ktid id=0; id<ktid_last; id++){
ktDescriptor d=ktDescriptor_get(id);
kprintf("\e[37m{ id:%u name:%s size:%u freeMembers:%p toString:%p }\n",
d.id, d.name, d.size, d.freeMembers, d.toString);
}
}

View File

@ -13,9 +13,27 @@ void test_autoarr();
void test_hash_functions(); void test_hash_functions();
void test_hashtable(); void test_hashtable();
void test_dtsod(); void test_dtsod();
void test_kprint_colors();
void test_autoarrVsVector(); void test_autoarrVsVector();
void test_rng_algorithms(); void test_rng_algorithms();
void test_kprint_colors();
void test_kprint();
void test_type_system();
inline void test_all(){
test_type_system();
test_string();
test_safethrow();
test_searchtree();
test_autoarr();
test_autoarrVsVector();
test_hash_functions();
test_hashtable();
test_dtsod();
test_rng_algorithms();
test_kprint_colors();
test_kprint();
kprintf("\e[96m--------------------------------------\e[0m\n");
}
#define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) #define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T))