From c7a3fb10b5c5156c1b69160f59da624b99e4fcfb Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 16 Feb 2022 20:54:50 +0300 Subject: [PATCH] 81 --- DtsodC/Makefile | 17 ++--- DtsodC/src/Autoarr/Autoarr2_declare.h | 4 ++ DtsodC/src/Autoarr/Autoarr2_define.h | 6 ++ DtsodC/src/Hashtable/Hashtable.c | 93 +++++++++++++------------- DtsodC/src/Hashtable/Hashtable.h | 26 +++++++- DtsodC/src/Hashtable/hash.c | 2 +- DtsodC/src/base/base.h | 1 + DtsodC/src/base/mystr.c | 30 +++++++++ DtsodC/src/base/mystr.h | 14 ++++ DtsodC/src/base/types.h | 2 + DtsodC/src/main.c | 5 +- DtsodC/src/tests/slimak.h | 55 ---------------- DtsodC/src/tests/test_autoarr2.c | 28 ++++---- DtsodC/src/tests/test_hashtable.c | 46 +++++++++++++ DtsodC/src/tests/test_searchtree.c | 94 ++++++++++++++------------- DtsodC/src/tests/tests.c | 7 +- DtsodC/src/tests/tests.h | 8 ++- 17 files changed, 260 insertions(+), 178 deletions(-) create mode 100644 DtsodC/src/base/mystr.c create mode 100644 DtsodC/src/base/mystr.h delete mode 100644 DtsodC/src/tests/slimak.h create mode 100644 DtsodC/src/tests/test_hashtable.c diff --git a/DtsodC/Makefile b/DtsodC/Makefile index cf9f299..ccc9e58 100644 --- a/DtsodC/Makefile +++ b/DtsodC/Makefile @@ -7,7 +7,7 @@ all: clear_c build test clear_c: clear clear_bin: - @echo -e "\e[36m-------------[clear_bin]---------------\e[0m" + @echo -e '\e[96m--------------[clear_bin]--------------\e[0m' touch $(OUTDIR)_.com rm $(OUTDIR)*.com @@ -15,15 +15,16 @@ clang: CMP=clang clang: all CMPARGS= -Wall $(SRC) -o $(OUTFILE) -build: - @echo -e "\e[36m-------------[build]---------------\e[0m" +build: + @echo -e '\n\e[96m----------------[build]----------------\e[0m' $(CMP) -O1 -flto $(CMPARGS) build_dbg: - @echo -e "\e[36m-------------[build_dbg]---------------\e[0m" - $(CMP) -O0 $(CMPARGS).dbg + @echo -e '\n\e[96m--------------[build_dbg]--------------\e[0m' + $(CMP) -O0 -g $(CMPARGS).dbg test: - @echo -e "\e[36m-------------[test]----------------\e[0m" + @echo -e '\n\e[96m----------------[test]-----------------\e[0m' $(OUTFILE) valgrind: clear_c build_dbg - @echo -e "\e[36m-----------[valgrind]--------------\e[0m" - valgrind -s --read-var-info=yes --fullpath-after=DtsodC/ $(OUTFILE).dbg \ No newline at end of file + @echo -e '\n\e[96m--------------[valgrind]---------------\e[0m' + valgrind -s --read-var-info=yes --track-origins=yes --fullpath-after=DtsodC/ \ + --leak-check=full --show-leak-kinds=all $(OUTFILE).dbg diff --git a/DtsodC/src/Autoarr/Autoarr2_declare.h b/DtsodC/src/Autoarr/Autoarr2_declare.h index f36e08f..39e975f 100644 --- a/DtsodC/src/Autoarr/Autoarr2_declare.h +++ b/DtsodC/src/Autoarr/Autoarr2_declare.h @@ -9,6 +9,7 @@ struct Autoarr2_##type;\ typedef struct {\ void (*add)(struct Autoarr2_##type* ar, type element);\ type (*get)(struct Autoarr2_##type* ar, uint32 index);\ + type* (*getptr)(struct Autoarr2_##type* ar, uint32 index);\ void (*set)(struct Autoarr2_##type* ar, uint32 index, type element);\ void (*clear)(struct Autoarr2_##type* ar);\ } __functions_list_t_##type;\ @@ -24,6 +25,7 @@ typedef struct Autoarr2_##type{\ \ void __Autoarr2_add_##type(Autoarr2_##type* ar, type element);\ type __Autoarr2_get_##type(Autoarr2_##type* ar, uint32 index);\ +type* __Autoarr2_getptr_##type(Autoarr2_##type* ar, uint32 index);\ void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element);\ void __Autoarr2_clear_##type(Autoarr2_##type* ar);\ Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_block_length); @@ -34,6 +36,8 @@ Autoarr2_##type __Autoarr2_create_##type(uint16 max_blocks_count, uint16 max_blo autoarr->functions->add(autoarr, element) #define Autoarr2_get(autoarr, index)\ autoarr->functions->get(autoarr,index) +#define Autoarr2_getptr(autoarr, index)\ + autoarr->functions->getptr(autoarr,index) #define Autoarr2_set(autoarr, index, element)\ autoarr->functions->set(autoarr, index, element) #define Autoarr2_clear(autoarr)\ diff --git a/DtsodC/src/Autoarr/Autoarr2_define.h b/DtsodC/src/Autoarr/Autoarr2_define.h index 2a5973e..fd7aa46 100644 --- a/DtsodC/src/Autoarr/Autoarr2_define.h +++ b/DtsodC/src/Autoarr/Autoarr2_define.h @@ -25,6 +25,11 @@ type __Autoarr2_get_##type(Autoarr2_##type* ar, uint32 index){\ return ar->values[index/ar->max_block_length][index%ar->max_block_length];\ }\ \ +type* __Autoarr2_getptr_##type(Autoarr2_##type* ar, uint32 index){\ + if(index>=Autoarr2_length(ar)) throw(ERR_WRONGINDEX);\ + return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\ +}\ +\ void __Autoarr2_set_##type(Autoarr2_##type* ar, uint32 index, type element){\ if(index>=Autoarr2_length(ar)) throw(ERR_WRONGINDEX);\ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ @@ -42,6 +47,7 @@ void __Autoarr2_clear_##type(Autoarr2_##type* ar){\ __functions_list_t_##type __functions_list_##type={\ &__Autoarr2_add_##type,\ &__Autoarr2_get_##type,\ + &__Autoarr2_getptr_##type,\ &__Autoarr2_set_##type,\ &__Autoarr2_clear_##type\ };\ diff --git a/DtsodC/src/Hashtable/Hashtable.c b/DtsodC/src/Hashtable/Hashtable.c index 195ce16..331ab76 100644 --- a/DtsodC/src/Hashtable/Hashtable.c +++ b/DtsodC/src/Hashtable/Hashtable.c @@ -2,16 +2,34 @@ define_Autoarr2(KeyValuePair) +// amount of rows +static const uint16 HT_HEIGHTS[]={61,631,3889,19441,65521}; + + Hashtable* Hashtable_create(){ Hashtable* ht=malloc(sizeof(Hashtable)); - ht->hein=HT_HEIGHTS[0]; + ht->hein=0; ht->rows=malloc(HT_HEIGHTS[0]*sizeof(Autoarr2(KeyValuePair))); - for(uint16 i;irows[i]=Autoarr2_create(KeyValuePair,4,16); return ht; } -static Autoarr2(KeyValuePair)* getrow(Hashtable* ht,char* key){ +void Hashtable_free(Hashtable* ht){ + for(uint16 i=0;ihein];i++){ + Autoarr2(KeyValuePair)* ar=(Autoarr2(KeyValuePair)*)(ht->rows+i); + for(uint32 i=0;ikey); + Autoarr2_clear(ar); + } + free(ht->rows); + free(ht); +} + +uint32 Hashtable_height(Hashtable* ht){ return HT_HEIGHTS[ht->hein]; } + +Autoarr2(KeyValuePair)* getrow(Hashtable* ht, char* key){ + uint16 rown=HT_HEIGHTS[ht->hein]%ihash(key); if(rown>=HT_HEIGHTS[ht->hein]){ ht->rows=realloc(ht->rows,HT_HEIGHTS[++ht->hein]*sizeof(Autoarr2(KeyValuePair))); @@ -21,47 +39,28 @@ static Autoarr2(KeyValuePair)* getrow(Hashtable* ht,char* key){ return ht->rows+rown; } -void Hashtable_free(Hashtable* ht){ - for(uint16 i=0;ihein];i++) - Autoarr2_clear(((Autoarr2(KeyValuePair)*)(ht->rows+i))); - free(ht->rows); - free(ht); +//copies string and value to new KeyValuePair +KeyValuePair cpair(char* key, Unitype value){ + return (KeyValuePair){.key=mystrcpy(key),.value=value}; } -void Hashtable_add_pair(Hashtable* ht, KeyValuePair pair){ - Autoarr2_add(getrow(ht,pair.key),pair); + +void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){ + Autoarr2_add(getrow(ht,p.key),p); +} +void Hashtable_add(Hashtable* ht, char* key, Unitype u){ + Hashtable_add_pair(ht,cpair(key,u)); } -//returns length of string (including \0) -uint32 mystrlen(char* str){ - uint32 len=0; - while(*(str++)) len++; - return len++; -} - -//allocates new char[] and copies src there -char* mystrcpy(char* src){ - uint32 len=mystrlen(src); - char* dst=malloc(len*sizeof(char)); - while(--len<0) - dst[len]=src[len]; - return dst; -} - -//compares two strings, NullPtr-friendly -bool mystrcmp(char* key0, char* key1){ - if(!key0) return key1 ? 0 : 1; - else if(!key1) return 0; - while(*key0&&*key1){ - if(*key0!=*key1) return 0; - key0++; - key1++; +//returns null or pointer to value in hashtable +Unitype* Hashtable_getptr(Hashtable* ht, char* key){ + Autoarr2(KeyValuePair)* ar=getrow(ht,key); + uint32 arlen=Autoarr2_length(ar); + for(uint32 i=0;ikey)) return &p->value; } - return 1; -} - -void Hashtable_add(Hashtable* ht, char* key, Unitype value){ - Hashtable_add_pair(ht,(KeyValuePair){key,value}); + return NULL; } Unitype Hashtable_get(Hashtable* ht, char* key){ @@ -73,16 +72,18 @@ Unitype Hashtable_get(Hashtable* ht, char* key){ } return (Unitype){.type=Null,.VoidPtr=NULL}; } - KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){ - return (KeyValuePair){ - .key=key, - .value=Hashtable_get(ht,key) - }; + return cpair(key,Hashtable_get(ht,key)); } - -bool Hashtable_try_get(Hashtable* ht, char* key,Unitype* output){ +bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ Unitype u=Hashtable_get(ht,key); *output=u; return u.type==Null; } + +void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){ + if(Hashtable_try_get(ht,p.key, NULL)){ + + } +} +void Hashtable_set(Hashtable* ht, char* key, Unitype u){ Hashtable_set_pair(ht,cpair(key,u)); } diff --git a/DtsodC/src/Hashtable/Hashtable.h b/DtsodC/src/Hashtable/Hashtable.h index b0764ab..6b000aa 100644 --- a/DtsodC/src/Hashtable/Hashtable.h +++ b/DtsodC/src/Hashtable/Hashtable.h @@ -9,6 +9,9 @@ typedef struct KeyValuePair{ Unitype value; } KeyValuePair; +//copies string and value to new KeyValuePair +KeyValuePair cpair(char* key, Unitype value); + declare_Autoarr2(KeyValuePair) typedef struct Hashtable{ @@ -16,5 +19,24 @@ typedef struct Hashtable{ Autoarr2(KeyValuePair)* rows; // Autoarr[height] } Hashtable; -// amount of rows -const uint16 HT_HEIGHTS[]={61,631,3889,19441,65521}; +Hashtable* Hashtable_create(); +void Hashtable_free(Hashtable* ht); + +uint32 Hashtable_height(Hashtable* ht); + +//copies string and value to new KeyValuePair +KeyValuePair cpair(char* key, Unitype value); + + +void Hashtable_add_pair(Hashtable* ht, KeyValuePair p); +void Hashtable_add(Hashtable* ht, char* key, Unitype u); + +//returns null or pointer to value in hashtable +Unitype* Hashtable_getptr(Hashtable* ht, char* key); + +Unitype Hashtable_get(Hashtable* ht, char* key); +KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key); +bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); + +void Hashtable_set_pair(Hashtable* ht, KeyValuePair p); +void Hashtable_set(Hashtable* ht, char* key, Unitype u); \ No newline at end of file diff --git a/DtsodC/src/Hashtable/hash.c b/DtsodC/src/Hashtable/hash.c index 42e2c25..32ff34f 100644 --- a/DtsodC/src/Hashtable/hash.c +++ b/DtsodC/src/Hashtable/hash.c @@ -3,7 +3,7 @@ uint32 ihash(char *str){ uint32 hash=5381; for (char c=*str;c;c=*(++str)) - hash=((hash<<5)+hash)+c; //hash=hash*33^c + hash=((hash<<5)+hash)+c; return hash; } diff --git a/DtsodC/src/base/base.h b/DtsodC/src/base/base.h index 20104d1..20bafb6 100644 --- a/DtsodC/src/base/base.h +++ b/DtsodC/src/base/base.h @@ -3,6 +3,7 @@ #include "std.h" #include "types.h" #include "errors.h" +#include "mystr.h" // sleep function based on std nanosleep() void fsleep(float sec); diff --git a/DtsodC/src/base/mystr.c b/DtsodC/src/base/mystr.c new file mode 100644 index 0000000..915ee6e --- /dev/null +++ b/DtsodC/src/base/mystr.c @@ -0,0 +1,30 @@ +#include "base.h" + +//returns length of string (including \0) +uint32 mystrlen(char* str){ + uint32 len=0; + while(*(str++)) len++; + return ++len; +} + +//allocates new char[] and copies src there +char* mystrcpy(char* src){ + uint32 len=mystrlen(src);dbg(len); + char* dst=malloc(len*sizeof(char)); + while(len-->0) + dst[len]=src[len]; + printf("dst: %s\n",dst); + return dst; +} + +//compares two strings, NullPtr-friendly +bool mystrcmp(char* key0, char* key1){ + if(!key0) return key1 ? 0 : 1; + else if(!key1) return 0; + while(*key0&&*key1){ + if(*key0!=*key1) return 0; + key0++; + key1++; + } + return 1; +} diff --git a/DtsodC/src/base/mystr.h b/DtsodC/src/base/mystr.h new file mode 100644 index 0000000..b1fe233 --- /dev/null +++ b/DtsodC/src/base/mystr.h @@ -0,0 +1,14 @@ +#pragma once + +#include "types.h" + + +//returns length of string (including \0) +uint32 mystrlen(char* str); + + +//allocates new char[] and copies src there +char* mystrcpy(char* src); + +//compares two strings, NullPtr-friendly +bool mystrcmp(char* key0, char* key1); \ No newline at end of file diff --git a/DtsodC/src/base/types.h b/DtsodC/src/base/types.h index 64fabd2..30cba80 100644 --- a/DtsodC/src/base/types.h +++ b/DtsodC/src/base/types.h @@ -40,3 +40,5 @@ typedef struct Unitype{ void* VoidPtr; }; } Unitype; + +#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} diff --git a/DtsodC/src/main.c b/DtsodC/src/main.c index 93f0c1d..4c11d95 100644 --- a/DtsodC/src/main.c +++ b/DtsodC/src/main.c @@ -1,10 +1,13 @@ #include "base/base.h" #include "tests/tests.h" #include "Autoarr/Autoarr2.h" +#include "Hashtable/Hashtable.h" int main(){ setlocale(LC_ALL, "en-US.Unicode"); printf("\e[92mdtsod parser in c language!\e[97m\n"); - optime({test_all();}); + //optime("test_all",1,{test_all();}); + test_hashtable(); + printf("\e[0m\n"); return 0; } diff --git a/DtsodC/src/tests/slimak.h b/DtsodC/src/tests/slimak.h deleted file mode 100644 index 11ba386..0000000 --- a/DtsodC/src/tests/slimak.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include "../base/base.h" - -const wchar_t* slimak = -U" ▄▄▄ \n" -"▄▀░▄░▀▄ \n" -"█░█▄▀░█ \n" -"█░▀▄▄▀█▄█▄▀ \n" -"▄▄█▄▄▄▄███▀ \n" -" ▄▄▄ \n" -" █▀▄▄░▀█ \n" -"█░█░▄▀░░█ \n" -"█░▀▄▄▄▀░█▄█▄▄▀ \n" -"▄▄█▄▄▄▄███▀▀ \n" -" ▄▄▄ \n" -" █▀▄▄░▀█ \n" -"█░█░▄▀░░█ \n" -"█░▀▄▄▄▀░█▄█▄▄▀ \n" -"▄▄█▄▄▄▄████▀▀ \n" -" ▄▄▄ \n" -" ▄▀░▄░▀▄ \n" -" █░█▄▀░█ \n" -" █░▀▄▄▀█▄█▄▀ \n" -" ▄▄█▄▄▄▄███▀ \n" -" ▄▄▄ \n" -" █▀▄▄░▀█ \n" -" █░█░▄▀░░█ \n" -" █░▀▄▄▄▀░█▄█▄▄▀ \n" -" ▄▄█▄▄▄▄███▀▀ \n" -" ▄▄▄ \n" -" █▀▄▄░▀█ \n" -" █░█░▄▀░░█ \n" -" █░▀▄▄▄▀░█▄█▄▄▀ \n" -" ▄▄█▄▄▄▄████▀▀ \n" -" ▄▄▄ \n" -" ▄▀░▄░▀▄ \n" -" █░█▄▀░█ \n" -" █░▀▄▄▀█▄█▄▀ \n" -" ▄▄█▄▄▄▄███▀ \n" -" ▄▄▄ \n" -" █▀▄▄░▀█ \n" -" █░█░▄▀░░█ \n" -" █░▀▄▄▄▀░█▄█▄▄▀\n" -" ▄▄█▄▄▄▄███▀▀ \n" -" ▄▄▄ \n" -" █▀▄▄░▀█ \n" -" █░█░▄▀░░█ \n" -" █░▀▄▄▄▀░█▄█▄▄▀\n" -" ▄▄█▄▄▄▄████▀▀ \n" -" ▄▄▄ \n" -" ▄▀░▄░▀▄ \n" -" █░█▄▀░█ \n" -" █░▀▄▄▀█▄█▄▀\n" -" ▄▄█▄▄▄▄███▀ \n"; \ No newline at end of file diff --git a/DtsodC/src/tests/test_autoarr2.c b/DtsodC/src/tests/test_autoarr2.c index 541eff3..27d665c 100644 --- a/DtsodC/src/tests/test_autoarr2.c +++ b/DtsodC/src/tests/test_autoarr2.c @@ -2,7 +2,7 @@ #include "../Autoarr/Autoarr2.h" static void printautoarr(Autoarr2(uint16)* ar){ - printf("\e[94mAUTOARR:%lu\n" + printf("\e[94mAutoarr2(uint16): %lu\n" " max_blocks_count: %u\n" " blocks_count: %u\n" " max_block_length: %u\n" @@ -35,16 +35,18 @@ static void printallval(Autoarr2(uint16)* ar){ } void test_autoarr2(){ - printf("\e[96m------------[test_autoarr2]-------------\n"); - Autoarr2(uint16) ar=Autoarr2_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); - Autoarr2_clear(((&ar))); - printf("\e[92mautoarr cleared\n"); + optime("test_autoarr2",1,({ + printf("\e[96m------------[test_autoarr2]------------\n"); + Autoarr2(uint16) ar=Autoarr2_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); + Autoarr2_clear(((&ar))); + printf("\e[92mautoarr cleared\n"); + })); } diff --git a/DtsodC/src/tests/test_hashtable.c b/DtsodC/src/tests/test_hashtable.c new file mode 100644 index 0000000..5e91d07 --- /dev/null +++ b/DtsodC/src/tests/test_hashtable.c @@ -0,0 +1,46 @@ +#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 hashtable_fill(Hashtable* ht){ + char* key=malloc(20); + for(uint32 i=0;i<255;i++){ + sprintf(key,"key__%u",i); + Hashtable_add(ht,key,Uni(UInt32,i)); + } + free(key); +} + +void hashtable_printval(Hashtable* ht){ + char* key=malloc(20); + for(uint32 i=0;i<255;i++){ + sprintf(key,"key__%u",i); + printuni(Hashtable_get(ht,key)); + printf(" "); + } + free(key); +} + + +void test_hashtable(void){ + optime("test_hashtable",1,({ + printf("\e[96m-----------[test_hashtable]------------\n"); + Hashtable* ht=Hashtable_create(); + print_hashtable(ht); + hashtable_fill(ht); + printf("\e[92mhashtable filled\n\e[90m"); + hashtable_printval(ht); + Hashtable_free(ht); + printf("\n\e[92mhashtable freed\n"); + })); +} diff --git a/DtsodC/src/tests/test_searchtree.c b/DtsodC/src/tests/test_searchtree.c index 4d402dd..0b83b59 100644 --- a/DtsodC/src/tests/test_searchtree.c +++ b/DtsodC/src/tests/test_searchtree.c @@ -20,50 +20,52 @@ void printstnode(STNode* node){ } void test_searchtree(){ - printf("\e[96m-----------[test_searchtree]-----------\n"); - STNode* node=STNode_create(); - printf("\e[92mnode created\n"); - printf("push:\e[94m\n "); - Unitype u={.type=Int16,.Int16=-3}; - printuni(u); - ST_push(node,"type", u); - printf(" -> type\n "); - u=(Unitype){.type=Int16,.Int16=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=Float,.Float=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"); + 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=Int16,.Int16=-3}; + printuni(u); + ST_push(node,"type", u); + printf(" -> type\n "); + u=(Unitype){.type=Int16,.Int16=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=Float,.Float=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"); + })); } diff --git a/DtsodC/src/tests/tests.c b/DtsodC/src/tests/tests.c index a50b4d1..ae49ab5 100644 --- a/DtsodC/src/tests/tests.c +++ b/DtsodC/src/tests/tests.c @@ -29,7 +29,8 @@ void printuni(Unitype v){ } void test_all(void){ - optime({test_searchtree();}); - optime({test_autoarr2();}); - printf("\e[96m---------------------------------------\n"); + test_searchtree(); + test_autoarr2(); + test_hashtable(); + printf("\e[96m---------------------------------------\e[0m\n"); } \ No newline at end of file diff --git a/DtsodC/src/tests/tests.h b/DtsodC/src/tests/tests.h index cc41a0a..6d1ae9f 100644 --- a/DtsodC/src/tests/tests.h +++ b/DtsodC/src/tests/tests.h @@ -7,12 +7,14 @@ void printuni(Unitype v); void test_all(void); void test_searchtree(void); void test_autoarr2(void); +void test_hashtable(void); // executes codeblock and prints execution time // should be used like optime({foo();}), because just optime(foo()) works slower -#define optime(codeblock) ({\ +#define optime(opname,repeats,codeblock) ({\ clock_t start=clock();\ - (codeblock);\ + for(uint64 ___OPREP=0;___OPREP