refactoring

This commit is contained in:
2023-06-08 15:15:38 +06:00
parent a983df1ac6
commit c5585bbb0c
88 changed files with 922 additions and 1071 deletions

View File

@@ -7,7 +7,7 @@ i32 main(){
kt_initKerepTypes();
kt_endInit();
test_all();
kt_free();
kt_deinit();
kprintf("\e[0m\n");
return 0;
}

View File

@@ -1,6 +1,6 @@
#include "tests.h"
void _test_allocator(allocator_t* al){
void _test_allocator(allocator_ptr al){
void* ptr=allocator_alloc(al, 1);
allocator_free(al, ptr);
ptr=allocator_alloc(al, 5);
@@ -30,18 +30,18 @@ void test_allocators(){
optime("test CstdAllocator", 10000,
CstdAllocator al;
CstdAllocator_construct(&al);
_test_allocator((allocator_t*)&al);
_test_allocator((allocator_ptr)&al);
);
optime("test LinearAllocator", 10000,
LinearAllocator al;
LinearAllocator_construct(&al, 4096);
_test_allocator((allocator_t*)&al);
_test_allocator((allocator_ptr)&al);
LinearAllocator_destruct(&al);
);
optime("test StackingAllocator", 10000,
StackingAllocator al;
StackingAllocator_construct(&al, 4096);
_test_allocator((allocator_t*)&al);
_test_allocator((allocator_ptr)&al);
StackingAllocator_destruct(&al);
);
}

View File

@@ -16,7 +16,7 @@ i64 _autoarrVsVector(u16 chunkCount, u16 chunkLength){
t=Autoarr_get(ar, op_i));
optime("vector_get", count,
t=vec[op_i]);
Autoarr_free(ar, true);
Autoarr_destruct(ar, true);
return t;
}

View File

@@ -47,7 +47,7 @@ void test_autoarr(){
resetar(ar);
kprintf("\e[92mautoarr values reset\n");
printallval(ar);
Autoarr_free(ar, true);
Autoarr_destruct(ar, true);
kprintf("\e[92mautoarr deleted\n");
);
}

View File

@@ -151,4 +151,4 @@ void test_cptr(){
// TODO cptr_replace
);
}
}

View File

@@ -52,7 +52,7 @@ void test_dtsod(){
tryLast(DtsodV24_serialize(dtsod),r,;)
s=r.value.VoidPtr;
);
DtsodV24_free(dtsod);
DtsodV24_destruct(dtsod);
kprintf("\e[92m%s",s);
optime("reserialize",10,
@@ -61,9 +61,9 @@ void test_dtsod(){
free(s);
tryLast(DtsodV24_serialize(dtsod),rr,;)
s=rr.value.VoidPtr;
DtsodV24_free(dtsod);
DtsodV24_destruct(dtsod);
);
free(s);
);
}
}

View File

@@ -33,7 +33,7 @@ char data[]="iojihiojopijiugbjmoihftytryfdrh";
} \
kprintf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS); \
); \
Autoarr_free(hashes, true); \
Autoarr_destruct(hashes, true); \
kprintf("\e[96m--------------------------------------\n"); \
}
@@ -43,4 +43,4 @@ void test_hash_functions(){
test_hashfunc(u32, hash_crc32);
test_hashfunc(u32, hash_sdbm32);
);
}
}

View File

@@ -13,7 +13,7 @@ void print_hashtable(Hashtable* ht){
ht->rows);
}
void printrowgraph(Hashtable* ht){
void printrowgraph(allocator_ptr al, Hashtable* ht){
kprintf("\e[94mrow length graph:\n");
u16 lgs_l=1000;
u32 lgs[lgs_l];
@@ -26,36 +26,33 @@ void printrowgraph(Hashtable* ht){
}
for(u32 i=0; i<lgs_l; i++)
if(lgs[i]>0) {
char* str0=char_multiply(' ',i>=100?0:(i>=10?1:2));
char* str1=char_multiply(' ',lgs[i]>=100?0:(lgs[i]>=10?1:2));
char* str2=char_multiply('#',lgs[i]/100);
char* str0=char_multiply(al, ' ',i>=100?0:(i>=10?1:2));
char* str1=char_multiply(al, ' ',lgs[i]>=100?0:(lgs[i]>=10?1:2));
char* str2=char_multiply(al, '#',lgs[i]/100);
kprintf("\e[94m length: \e[96m%u %s \e[94mfrequency: \e[96m%u %s \e[90m%s\n",i,str0,lgs[i],str1,str2);
free(str0);
free(str1);
free(str2);
allocator_free(al, str2);
allocator_free(al, str1);
allocator_free(al, str0);
}
}
char* genkey(u32 i){
char* key=malloc(12);
IFMSC(
sprintf_s(key,12,"key_%u",i),
sprintf(key,"key_%u",i)
);
char* genkey(allocator_ptr al, u32 i){
char* key=allocator_alloc(al, 16);
sprintf_s(key,16,"key_%u",i);
return key;
}
void fill(Hashtable* ht){
void fill(allocator_ptr al, Hashtable* ht){
for(u32 i=0;i<100000;i++)
Hashtable_add(ht,genkey(i),UniUInt64(i));
Hashtable_add(ht,genkey(al, i), UniUInt64(i));
}
Unitype gett(Hashtable* ht){
Unitype gett(allocator_ptr al, Hashtable* ht){
Unitype u;
for(u32 i=0;i<100000;i++){
char* key=genkey(i);
char* key=genkey(al, i);
u=Hashtable_get(ht,key);
free(key);
allocator_free(al, key);
}
return u;
}
@@ -64,14 +61,18 @@ Unitype gett(Hashtable* ht){
void test_hashtable(){
optime("test_hashtable",1,
kprintf("\e[96m-----------[test_hashtable]-----------\n");
StackingAllocator _al;
allocator_ptr al=(allocator_ptr)&_al;
StackingAllocator_construct(&_al, 4096);
Hashtable* ht=Hashtable_create();
kprintf("\e[92mhashtable created\n");
print_hashtable(ht);
optime("fill",1,fill(ht));
optime("get",1,gett(ht));
printrowgraph(ht);
optime("fill",1,fill(al, ht));
optime("get",1,gett(al, ht));
printrowgraph(al, ht);
print_hashtable(ht);
Hashtable_free(ht);
Hashtable_destruct(ht);
kprintf("\e[92mhashtable freed\n");
StackingAllocator_destruct(&_al);
);
}

View File

@@ -1,9 +1,12 @@
#include "../src/Hashtable/KeyValuePair.h"
EXPORT void CALL test_marshalling(char* text, KVPair** kptr){
KVPair* k=malloc(sizeof(KVPair));
CstdAllocator _al;
allocator_ptr al=(allocator_ptr)&_al;
CstdAllocator_construct(&_al);
KVPair* k=allocator_alloc(al, sizeof(KVPair));
k->key="message";
char* tc=cptr_copy(text);
char* tc=cptr_copy(al, text);
Unitype u=UniHeapPtr(char,tc);
k->value=u;
*kptr=k;

View File

@@ -1,45 +1,43 @@
#include "tests.h"
#include "../src/random/krandom.h"
#include "../src/random/random.h"
#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO) { \
#define test_alg(ALG, STATE_TYPE, EXPECTED_FROM_ZERO) { \
kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n"); \
void* s= ALG##_init(0); \
u##VALUE_SIZE r=ALG##_next(s); \
STATE_TYPE##_state state; \
STATE_TYPE##_construct(&state, 0); \
u64 rn=ALG##_next(&state); \
kprintf("\e[97m next from zero seed:"); \
if(r!=EXPECTED_FROM_ZERO){ \
kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r); \
if(rn != EXPECTED_FROM_ZERO){ \
kprintf("\e[91m %lu\n", rn); \
throw(ERR_UNEXPECTEDVAL); \
} \
kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
ALG##_free(s); \
s= ALG##_initFromTime(); \
r=ALG##_next(s); \
ALG##_free(s); \
kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r); \
kprintf("\e[92m %lu\n", rn); \
STATE_TYPE##_construct(&state, random_seedFromTime()); \
rn=ALG##_next(&state); \
kprintf("\e[97m next from time seed:\e[92m %lu\n", rn); \
}
void test_rng_algorithms(){
optime("test_rng_algorithms",1,
kprintf("\e[96m--------[test_rng_algorithms]---------\n");
// for ALG32
// xoroshiro64
test_alg(xoroshiro64star, 32, 932574677ULL)
test_alg(xoroshiro64starstar, 32, 3183060286ULL)
// xoshiro128
test_alg(xoshiro128plus, 32, 3918949401ULL)
test_alg(xoshiro128plusplus, 32, 1179900579ULL)
test_alg(xoshiro128starstar, 32, 3737715805ULL)
// for ALG64
// xoroshiro128
test_alg(xoroshiro128plus, 64, 4778832803ULL)
test_alg(xoroshiro128plusplus, 64, 626373238705583ULL)
test_alg(xoroshiro128starstar, 64, 11897572417920ULL)
// xoshiro256
test_alg(xoshiro256plus, 64, 15757075719729598363ULL)
test_alg(xoshiro256plusplus, 64, 5987356902031041503ULL)
test_alg(xoshiro256starstar, 64, 11091344671253066420ULL)
// splitmix64
test_alg(splitmix64, 64, 16294208416658607535ULL)
);
}
// optime("test_rng_algorithms",1,
kprintf("\e[96m--------[test_rng_algorithms]---------\n");
// splitmix64
test_alg(splitmix64, splitmix64, 16294208416658607535ULL)
// 32-bit
// xoroshiro64
test_alg(xoroshiro64star, xoroshiro64, 932574677ULL)
test_alg(xoroshiro64starstar, xoroshiro64, 3183060286ULL)
// xoshiro128
test_alg(xoshiro128plus, xoshiro128, 3918949401ULL)
test_alg(xoshiro128plusplus, xoshiro128, 1179900579ULL)
test_alg(xoshiro128starstar, xoshiro128, 3737715805ULL)
// 64-bit
// xoroshiro128
test_alg(xoroshiro128plus, xoroshiro128, 4778832803ULL)
test_alg(xoroshiro128plusplus, xoroshiro128, 626373238705583ULL)
test_alg(xoroshiro128starstar, xoroshiro128, 11897572417920ULL)
// xoshiro256
test_alg(xoshiro256plus, xoshiro256, 15757075719729598363ULL)
test_alg(xoshiro256plusplus, xoshiro256, 5987356902031041503ULL)
test_alg(xoshiro256starstar, xoshiro256, 11091344671253066420ULL)
// );
}

View File

@@ -34,10 +34,10 @@ void test_safethrow(){
Maybe e=test_maybe();
kprintf("\e[94mthrow_error:\n\e[92m");
printMaybe(e);
Maybe_free(e);
Maybe_destruct(e);
kprintf("\e[94mthrow_errcode:\n\e[92m");
e=a();
printMaybe(e);
Maybe_free(e);
Maybe_destruct(e);
);
}

View File

@@ -25,6 +25,9 @@ void printstnode(STNode* node){
void test_searchtree(){
optime("test_searchtree",1,
kprintf("\e[96m-----------[test_searchtree]----------\n");
LinearAllocator _al;
LinearAllocator_construct(&_al, 1024);
allocator_ptr al=(allocator_ptr)&_al;
STNode* node=STNode_create();
kprintf("\e[92mnode created\n");
kprintf("push:\e[94m\n ");
@@ -44,7 +47,7 @@ void test_searchtree(){
printuni(u);
ST_push(node,"channel_id", u);
kprintf(" -> channel_id\n ");
u=UniHeapPtr(char, cptr_copy("32.2004"));
u=UniHeapPtr(char, cptr_copy(al, "32.2004"));
printuni(u);
ST_push(node,"message_id", u);
kprintf(" -> message_id\n ");
@@ -68,7 +71,8 @@ void test_searchtree(){
kprintf("\n");
kprintf("\e[92mfirst node: ");
printstnode(node);
STNode_free(node);
STNode_destruct(node);
kprintf("\e[92mnode deleted\n");
LinearAllocator_destruct(&_al);
);
}

View File

@@ -11,4 +11,4 @@ void test_string(){
kprintf("\e[94mstring_extract() -> \e[92m\"%s\"\n",p);
free(p);
);
}
}

View File

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

View File

@@ -24,20 +24,20 @@ void test_type_system();
static inline void test_all(){
kprintf("\e[97mkerep tests are starting!\n");
optime(__func__, 1,
// test_type_system();
test_allocators();
/*test_cptr();
test_string();
test_safethrow();
test_searchtree();
test_autoarr();
test_hash_functions();
test_hashtable();
test_dtsod();
test_autoarrVsVector();
test_rng_algorithms();
test_kprint_colors();
test_kprint();
test_type_system();*/
// test_kprint_colors();
// test_kprint();
// test_safethrow();
// test_rng_algorithms();
// test_cptr();
// test_string();
// test_searchtree();
// test_autoarr();
// test_autoarrVsVector();
// test_hash_functions();
// test_hashtable();
// test_dtsod();*/
kprintf("\e[96m--------------------------------------\e[0m\n");
);
}