moved files from DTLib repo
This commit is contained in:
19
tests/main.c
Normal file
19
tests/main.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "../base/base.h"
|
||||
#include "tests.h"
|
||||
|
||||
void test_all(){
|
||||
/* test_searchtree();
|
||||
test_autoarr();
|
||||
test_hashtable();
|
||||
test_string(); */
|
||||
test_dtsod();
|
||||
printf("\e[96m---------------------------------------\e[0m\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
setlocale(LC_ALL, "en-US.Unicode");
|
||||
printf("\e[92mdtsod parser in c language!\e[97m\n");
|
||||
optime("test_all",1,{test_all();});
|
||||
printf("\e[0m\n");
|
||||
return 0;
|
||||
}
|
||||
52
tests/test_autoarr.c
Normal file
52
tests/test_autoarr.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "tests.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
|
||||
static void printautoarr(Autoarr(uint16)* ar){
|
||||
printf("\e[94mAutoarr(uint16): %lu\n"
|
||||
" max_blocks_count: %u\n"
|
||||
" blocks_count: %u\n"
|
||||
" max_block_length: %u\n"
|
||||
" block_length: %u\n"
|
||||
" max_length: %u\n"
|
||||
" length: %u\n",
|
||||
sizeof(Autoarr(uint16)),
|
||||
ar->max_blocks_count,
|
||||
ar->blocks_count,
|
||||
ar->max_block_length,
|
||||
ar->block_length,
|
||||
Autoarr_max_length(ar),
|
||||
Autoarr_length(ar));
|
||||
}
|
||||
|
||||
static void fillar(Autoarr(uint16)* ar){
|
||||
for (uint16 i=0;i<Autoarr_max_length(ar);i++)
|
||||
Autoarr_add(ar,i);
|
||||
}
|
||||
static void resetar(Autoarr(uint16)* ar){
|
||||
for (uint16 i=0;i<Autoarr_max_length(ar);i++)
|
||||
Autoarr_set(ar,i,Autoarr_max_length(ar)-i-1);
|
||||
}
|
||||
|
||||
static void printallval(Autoarr(uint16)* ar){
|
||||
printf("\e[90m");
|
||||
for (uint16 i=0;i<Autoarr_length(ar);i++)
|
||||
printf("%u ",Autoarr_get(ar,i));
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void test_autoarr(){
|
||||
optime("test_autoarr",1,({
|
||||
printf("\e[96m------------[test_autoarr]------------\n");
|
||||
Autoarr(uint16) ar=Autoarr_create(uint16,10,16);
|
||||
printf("\e[92mautoarr created\n");
|
||||
fillar(&ar);
|
||||
printf("\e[92mautoarr filled up\n");
|
||||
printautoarr(&ar);
|
||||
printallval(&ar);
|
||||
resetar(&ar);
|
||||
printf("\e[92mautoarr values reset\n");
|
||||
printallval(&ar);
|
||||
Autoarr_clear(((&ar)));
|
||||
printf("\e[92mautoarr cleared\n");
|
||||
}));
|
||||
}
|
||||
55
tests/test_dtsod.c
Normal file
55
tests/test_dtsod.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "tests.h"
|
||||
#include "../DtsodParser/DtsodV24.h"
|
||||
|
||||
const char text[]=
|
||||
"message: {\n"
|
||||
" bool: false;"
|
||||
" char: 'v';"
|
||||
" int: -2515;"
|
||||
" uint:#comment!\n 0u;"
|
||||
" double: 965.557f;#another comment!\n"
|
||||
" text: \"_$\\\"\\\\'''a ыыы000;2;=:%d;```\";\n"
|
||||
"}; ";
|
||||
|
||||
void print_dtsod(Hashtable* dtsod){
|
||||
printf("\e[92m");
|
||||
Hashtable_foreach(dtsod, p,({
|
||||
printkvp(p);
|
||||
if(p.value.type==HashtablePtr){
|
||||
printf(": {\n");
|
||||
Hashtable* sub=p.value.VoidPtr;
|
||||
Hashtable_foreach(sub, _p,({
|
||||
printf(" ");
|
||||
printkvp(_p);
|
||||
printf("\n");
|
||||
}));
|
||||
printf("}");
|
||||
}
|
||||
printf("\n");
|
||||
}));
|
||||
}
|
||||
|
||||
void test_dtsod(){
|
||||
optime(__func__,1,({
|
||||
printf("\e[96m-------------[test_dtsod]-------------\n");
|
||||
Hashtable* dtsod;
|
||||
char* s=cptr_copy(text);
|
||||
|
||||
optime("deserialize",1,(dtsod=DtsodV24_deserialize(s)));
|
||||
free(s);
|
||||
print_dtsod(dtsod);
|
||||
|
||||
optime("serialize",1,(s=DtsodV24_serialize(dtsod)));
|
||||
Hashtable_free(dtsod);
|
||||
printf("\e[92m%s",s);
|
||||
|
||||
optime("reserialize",10,({
|
||||
dtsod=DtsodV24_deserialize(s);
|
||||
free(s);
|
||||
s=DtsodV24_serialize(dtsod);
|
||||
Hashtable_free(dtsod);
|
||||
}));
|
||||
|
||||
free(s);
|
||||
}));
|
||||
}
|
||||
71
tests/test_hashtable.c
Normal file
71
tests/test_hashtable.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "tests.h"
|
||||
#include "../Hashtable/Hashtable.h"
|
||||
|
||||
void print_hashtable(Hashtable* ht){
|
||||
printf("\e[94mHashtable:%lu\n"
|
||||
" hein: %u\n"
|
||||
" height: %u\n"
|
||||
" rows: %p\n",
|
||||
sizeof(Hashtable),
|
||||
ht->hein,
|
||||
Hashtable_height(ht),
|
||||
ht->rows);
|
||||
}
|
||||
|
||||
void printrowgraph(Hashtable* ht){
|
||||
printf("\e[94mrow length graph:\n");
|
||||
uint16 lgs_l=1000;
|
||||
uint32 lgs[lgs_l];
|
||||
for(uint32 i=0; i<lgs_l; i++)
|
||||
lgs[i]=0;
|
||||
for(uint16 h=0;h<Hashtable_height(ht);h++){
|
||||
Autoarr(KeyValuePair)* ar=ht->rows+h;
|
||||
uint32 l=Autoarr_length(ar);
|
||||
lgs[l]++;
|
||||
}
|
||||
for(uint32 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);
|
||||
printf("\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);
|
||||
}
|
||||
}
|
||||
|
||||
void fill(Hashtable* ht){
|
||||
for(uint32 i=0;i<100000;i++){
|
||||
char* key=malloc(12);
|
||||
sprintf(key,"key__%u",i);
|
||||
Hashtable_add(ht,key,Uni(UInt64,i));
|
||||
}
|
||||
}
|
||||
|
||||
Unitype gett(Hashtable* ht){
|
||||
char* key=malloc(12);
|
||||
Unitype u;
|
||||
for(uint32 i=0;i<100000;i++){
|
||||
sprintf(key,"key__%u",i);
|
||||
u=Hashtable_get(ht,key);
|
||||
}
|
||||
free(key);
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
void test_hashtable(){
|
||||
optime("test_hashtable",1,({
|
||||
printf("\e[96m-----------[test_hashtable]------------\n");
|
||||
Hashtable* ht=Hashtable_create();
|
||||
printf("\e[92mhashtable created\n");
|
||||
print_hashtable(ht);
|
||||
optime("fill",1,fill(ht));
|
||||
optime("get",1,gett(ht));
|
||||
printrowgraph(ht);
|
||||
print_hashtable(ht);
|
||||
Hashtable_free(ht);
|
||||
printf("\e[92mhashtable freed\n");
|
||||
}));
|
||||
}
|
||||
71
tests/test_searchtree.c
Normal file
71
tests/test_searchtree.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "tests.h"
|
||||
#include "../SearchTree/SearchTree.h"
|
||||
|
||||
void printstnode(STNode* node){
|
||||
printf("\e[94mSTNode: %lu\n address: %p\n value: ",sizeof(STNode),node);
|
||||
printuni(node->value);
|
||||
// prints pointers to all existing branches
|
||||
printf("\n branches: %p\n", node->branches);
|
||||
if(node->branches) for(uint8 i=0;i<8;i++){
|
||||
printf(" \e[90m[%u]=%p\n",i,node->branches[i]);
|
||||
if(node->branches[i])
|
||||
for (uint8 ii = 0; ii < 8; ii++){
|
||||
printf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]);
|
||||
if(node->branches[i][ii])
|
||||
for (uint8 iii = 0; iii < 4; iii++)
|
||||
printf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void test_searchtree(){
|
||||
optime("test_searchtree",1,({
|
||||
printf("\e[96m-----------[test_searchtree]-----------\n");
|
||||
STNode* node=STNode_create();
|
||||
printf("\e[92mnode created\n");
|
||||
printf("push:\e[94m\n ");
|
||||
Unitype u={.type=Int64,.Int64=-3};
|
||||
printuni(u);
|
||||
ST_push(node,"type", u);
|
||||
printf(" -> type\n ");
|
||||
u=(Unitype){.type=Int64,.Int64=25};
|
||||
printuni(u);
|
||||
ST_push(node,"time", u);
|
||||
printf(" -> time\n ");
|
||||
u=(Unitype){.type=Double,.Double=-542.00600};
|
||||
printuni(u);
|
||||
ST_push(node,"author_id", u);
|
||||
printf(" -> author_id\n ");
|
||||
u=(Unitype){.type=Int64,.Int64=-31255};
|
||||
printuni(u);
|
||||
ST_push(node,"channel_id", u);
|
||||
printf(" -> channel_id\n ");
|
||||
u=(Unitype){.type=Double,.Double=32.2004};
|
||||
printuni(u);
|
||||
ST_push(node,"message_id", u);
|
||||
printf(" -> message_id\n ");
|
||||
u=(Unitype){.type=Int8Ptr,.VoidPtr=malloc(1)};
|
||||
printuni(u);
|
||||
ST_push(node,"text", u);
|
||||
printf(" -> text\n");
|
||||
printf("\e[92mpull:\e[94m");
|
||||
printf("\n type -> ");
|
||||
printuni(ST_pull(node,"type"));
|
||||
printf("\n time -> ");
|
||||
printuni(ST_pull(node,"time"));
|
||||
printf("\n author_id -> ");
|
||||
printuni(ST_pull(node,"author_id"));
|
||||
printf("\n channel_id -> ");
|
||||
printuni(ST_pull(node,"channel_id"));
|
||||
printf("\n message_id -> ");
|
||||
printuni(ST_pull(node,"message_id"));
|
||||
printf("\n text -> ");
|
||||
printuni(ST_pull(node,"text"));
|
||||
printf("\n");
|
||||
printf("\e[92mfirst node: ");
|
||||
printstnode(node);
|
||||
STNode_free(node);
|
||||
printf("\e[92mnode deleted\n");
|
||||
}));
|
||||
}
|
||||
16
tests/test_string.c
Normal file
16
tests/test_string.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "tests.h"
|
||||
#include "../base/mystr.h"
|
||||
|
||||
void test_string(){
|
||||
optime(__func__,1,({
|
||||
printf("\e[96m-------------[test_string]-------------\n");
|
||||
char c[]="0123456789abcdef";
|
||||
string s=string_cpFromCharPtr(c);
|
||||
printf("\e[92m\"%s\" -> string_cpFromCharPtr()\n",c);
|
||||
if(s.length!=16) throw("string created with incorrect length");
|
||||
char* p=string_cpToCharPtr(s);
|
||||
printf("\e[92mstring_cpToCharPtr() -> \"%s\"\n",p);
|
||||
free(p);
|
||||
free(s.ptr);
|
||||
}));
|
||||
}
|
||||
11
tests/tests.h
Normal file
11
tests/tests.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "../base/base.h"
|
||||
|
||||
void printuni(Unitype v);
|
||||
|
||||
void test_searchtree();
|
||||
void test_autoarr();
|
||||
void test_hashtable();
|
||||
void test_string();
|
||||
void test_dtsod();
|
||||
Reference in New Issue
Block a user