finally returned strings

This commit is contained in:
Timerix22 2022-04-17 00:38:05 +03:00
parent f6b51dbc6e
commit b4f2ca92c7
34 changed files with 247 additions and 276 deletions

View File

@ -12,7 +12,7 @@ define_Autoarr(float)
define_Autoarr(double)
define_Autoarr(Unitype)
// right func to clear array of unitype values
//right func to clear array of unitype values
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar){
for(uint32 blockI=0;blockI<ar->blocks_count-1;blockI++)
for(uint32 elemI=0;elemI<ar->max_block_length;elemI++)

View File

@ -19,7 +19,7 @@ declare_Autoarr(float)
declare_Autoarr(double)
declare_Autoarr(Unitype)
// right func to clear array of unitype values
//right func to clear array of unitype values
void Autoarr_Unitype_clear(Autoarr(Unitype)* ar);
#define Autoarr_foreach(ar,elem,codeblock)({\

View File

@ -25,6 +25,7 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){
uint8 i=0;
if(a==0){
Autoarr_add(b,'0');
return;
}
else if(a<0){
Autoarr_add(b,'-');
@ -44,6 +45,7 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
uint8 i=0;
if(a==0){
Autoarr_add(b,'0');
return;
}
char buf[24];
while(a!=0){
@ -51,13 +53,17 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){
a/=10;
}
string rev=string_reverse((string){buf,i});
printf("\e[95mrev:%s\n",string_cpToCptr(rev));
StringBuilder_append_string(b,rev);
free(rev.ptr);
}
void StringBuilder_append_double(StringBuilder* b, double a){
char buf[32];
sprintf(buf,"%lf",a);
IFWIN(
sprintf_s(buf,32,"%lf",a),
sprintf(buf,"%lf",a)
);
StringBuilder_append_cptr(b,buf);
}

View File

@ -1,24 +1,24 @@
#include "DtsodV24.h"
// returns UniNull if key not found
//returns UniNull if key not found
Unitype DtsodV24_get(Hashtable* dtsod, char* key){
return Hashtable_get(dtsod, key);
}
// adds or sets value
//adds or sets value
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
Unitype* val=Hashtable_getptr(dtsod, key);
if(val) *val=value;
else Hashtable_add(dtsod, key, value);
}
// checks for dtsod contains value or dont
//checks for dtsod contains value or dont
bool DtsodV24_contains(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key);
return val!=NULL;
}
// replaces value with UniNull if key exists in dtsod
//replaces value with UniNull if key exists in dtsod
bool DtsodV24_remove(Hashtable* dtsod, char* key){
Unitype* val=Hashtable_getptr(dtsod, key);
if (!val) return false;
@ -26,7 +26,7 @@ bool DtsodV24_remove(Hashtable* dtsod, char* key){
return true;
}
// frees memory including memory of elements (hashtables, autoarrs, etc.)
//frees memory including memory of elements (hashtables, autoarrs, etc.)
void DtsodV24_free(Hashtable* dtsod){
Hashtable_free(dtsod);
}

View File

@ -9,22 +9,22 @@ extern "C" {
//parses text to binary values
Maybe DtsodV24_deserialize(char* text);
// creates text representation of dtsod
//creates text representation of dtsod
char* DtsodV24_serialize(Hashtable* dtsod);
// returns value or UniNull if key not found
//returns value or UniNull if key not found
Unitype DtsodV24_get(Hashtable* dtsod, char* key);
// adds or sets value
//adds or sets value
void DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value);
// checks for dtsod contains value or dont
//checks for dtsod contains value or dont
bool DtsodV24_contains(Hashtable* dtsod, char* key);
// replaces value with UniNull if key exists in dtsod
//replaces value with UniNull if key exists in dtsod
bool DtsodV24_remove(Hashtable* dtsod, char* key);
// frees memory including memory of elements (hashtables, autoarrs, etc.)
//frees memory including memory of elements (hashtables, autoarrs, etc.)
void DtsodV24_free(Hashtable* dtsod);
#if __cplusplus

View File

@ -10,39 +10,39 @@ extern "C" {
#include "DtsodV24.h"
// parses text to binary values
//parses text to binary values
EXPORT void CALL kerep_DtsodV24_deserialize(char* text, Hashtable** output, char** errmsg){
Maybe r=DtsodV24_deserialize(text);
*errmsg= r.errmsg ? r.errmsg : NULL;
*output=r.value.VoidPtr;
}
// creates text representation of dtsod
//creates text representation of dtsod
EXPORT void CALL kerep_DtsodV24_serialize(Hashtable* dtsod, char** output){
*output=DtsodV24_serialize(dtsod);
}
// returns value or UniNull if key not found
//returns value or UniNull if key not found
EXPORT void CALL kerep_DtsodV24_get(Hashtable* dtsod, char* key, Unitype* output){
*output=DtsodV24_get(dtsod, key);
}
// adds or sets the value
//adds or sets the value
EXPORT void CALL kerep_DtsodV24_addOrSet(Hashtable* dtsod, char* key, Unitype value){
DtsodV24_addOrSet(dtsod, key, value);
}
// checks for dtsod contains value or dont
//checks for dtsod contains value or dont
EXPORT void CALL kerep_DtsodV24_contains(Hashtable* dtsod, char* key, bool* output){
*output=DtsodV24_contains(dtsod, key);
}
// replaces value with UniNull if key exists in dtsod
//replaces value with UniNull if key exists in dtsod
EXPORT void CALL kerep_DtsodV24_remove(Hashtable* dtsod, char* key, bool* output){
*output=DtsodV24_remove(dtsod, key);
}
// replaces value with UniNull if key exists in dtsod
//replaces value with UniNull if key exists in dtsod
EXPORT void CALL kerep_DtsodV24_free(Hashtable* dtsod){
DtsodV24_free(dtsod);
}

View File

@ -1,7 +1,7 @@
#include "DtsodV24.h"
#include "../Autoarr/StringBuilder.h"
// 65536 max length!
//65536 max length!
#define STRB_BC 64
#define STRB_BL 1024
@ -47,11 +47,6 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){
}
addc('"');
break;
case Char:
addc('\'');
addc(u.Char);
addc('\'');
break;
case Bool:
StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
break;

View File

@ -64,7 +64,7 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){
Hashtable_add_pair(ht,KVPair(key,u));
}
// returns null or pointer to value in hashtable
//returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);

View File

@ -9,18 +9,18 @@ extern "C" {
#include "KeyValuePair.h"
typedef struct Hashtable{
uint8 hein; // height=HT_HEIGHTS[hein]
uint8 hein; //height=HT_HEIGHTS[hein]
Autoarr(KeyValuePair)* rows; // Autoarr[height]
} Hashtable;
Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht);
// amount of rows
//amount of rows
uint32 Hashtable_height(Hashtable* ht);
// adds charptr and value to new KeyValuePair
// use cptr_copy() to create new string if needed
//adds charptr and value to new KeyValuePair
//use cptr_copy() to create new string if needed
#define KVPair(key,value) (KeyValuePair){key,value}
//
@ -30,14 +30,14 @@ uint32 Hashtable_height(Hashtable* ht);
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
//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);
// not implemented yet
//not implemented yet
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_set(Hashtable* ht, char* key, Unitype u);

View File

@ -3,13 +3,13 @@
define_Autoarr(KeyValuePair)
// proper way to clear a KVP
//proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p){
free(p.key);
Unitype_free(p.value);
}
// func for KVP array clearing
//func for KVP array clearing
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){
for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++)
for(uint16 elemI=0; elemI < ar->max_block_length; elemI++)

View File

@ -14,10 +14,10 @@ typedef struct KeyValuePair{
declare_Autoarr(KeyValuePair)
// proper way to clear a KVP
//proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p);
// func to clear KVP array
//func to clear KVP array
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar);
void printkvp(KeyValuePair p);

View File

@ -6,9 +6,9 @@ extern "C" {
#include "../base/base.h"
// djb2 hash function from http:// www.cse.yorku.ca/~oz/hash.html
//djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html
uint32 ihash(char *str);
// sdbm hash function
//sdbm hash function
uint64 lhash(char* str);
#if __cplusplus

View File

@ -3,7 +3,7 @@ TESTS=$(wildcard tests/*c) $(wildcard tests/**/*.c)
OUTDIR=bin
CMP=gcc
all: clear_c clear_bin build_test build_lib
all: build_test
clear_c:
clear
@ -19,11 +19,11 @@ clang: all
TEST_FILE=$(OUTDIR)/kerep_test.com
TEST_ARGS= -Wall -Wno-discarded-qualifiers $(SRC) $(TESTS) -o $(TEST_FILE)
OPT_ARGS= -O1 -flto
build_test:
build_test: clear_bin
@echo -e '\n\e[96m----------------[build_test]----------------\e[0m'
$(CMP) $(OPT_ARGS) $(TEST_ARGS)
build_test_dbg:
build_test_dbg: clear_bin
@echo -e '\n\e[96m--------------[build_test_dbg]--------------\e[0m'
$(CMP) -O0 -g $(TEST_ARGS).dbg
@ -40,6 +40,6 @@ LIB_FILE=kerep.so
LIB_ARGS= -Wall -Wno-discarded-qualifiers \
-O1 -fPIC -shared -Wl,-soname,$(LIB_FILE) \
$(SRC) $(TESTS) -o $(OUTDIR)/$(LIB_FILE)
build_lib:
build_lib: clear_bin
@echo -e '\n\e[96m-------------[build_lib]---------------\e[0m'
$(CMP) $(OPT_ARGS) $(LIB_ARGS)

View File

@ -7,7 +7,7 @@ extern "C" {
#include "../base/base.h"
typedef struct SearchTreeNode{
struct SearchTreeNode**** branches; // *STNode[8][8][4]
struct SearchTreeNode**** branches; //*STNode[8][8][4]
Unitype value;
} STNode;

View File

@ -1,24 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../Autoarr/Autoarr.h"
#include "StringFragment.h"
typedef Autoarr(int8) StringBuilder;
StringBuilder StringBuilder_create(uint16 max_blocks_count, uint16 max_block_length);
void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cptr(StringBuilder* b, char* s);
void StringBuilder_append_string(StringBuilder* b, StringFragment s);
void StringBuilder_append_int64(StringBuilder* b, int64 a);
void StringBuilder_append_uint64(StringBuilder* b, uint64 a);
void StringBuilder_append_double(StringBuilder* b, double a);
// returns StringFragment with '\0' at the end
StringFragment StringBuilder_build(StringBuilder* b);
#if __cplusplus
}
#endif

View File

@ -1,42 +0,0 @@
#include "StringFragment.h"
// copies <length> characters from <ptr+offset> to new StringFragment (adding '\0' at the end)
StringFragment StringFragment_extract(StringFragment str){
if(str.length==0) return stringNull;
StringFragment extr={
.offset=0,
.length=str.length,
.ptr=malloc(str.length+1)
};
str.ptr+=str.offset;
for(uint32 i=0; i<str.length; i++)
extr.ptr[i]=str.ptr[i];
extr.ptr[str.length]='\0';
return extr;
}
// compares two strings, NullPtr-friendly
bool StringFragment_compare(StringFragment str0, StringFragment str1){
if(str0.length!=str1.length) return false;
if(!str0.ptr) return str1.ptr ? false : true;
else if(!str1.ptr) return false;
str0.ptr+=str0.offset;
str1.ptr+=str1.offset;
while(str0.length-->0)
if(*str0.ptr++ != *str1.ptr++)
return false;
return true;
}
// creates new StringFragment which is reversed variant of <s>
StringFragment StringFragment_reverse(StringFragment s){
if(s.length==0) return s;
StringFragment r={
.offset=0,
.length=s.length,
.ptr=malloc(s.length)
};
for(uint32 i=0; i<s.length; i++)
r.ptr[i+s.offset]=s.ptr[s.length-i-1];
return r;
}

View File

@ -1,29 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
// part of string
typedef struct StringFragment{
char* ptr; // may be without '\0' at the end
uint32 offset;
uint32 length;
} StringFragment;
static const StringFragment stringNull={NULL,0,0};
// copies <length> characters from <ptr+offset> to new StringFragment (adding '\0' at the end)
StringFragment StringFragment_extract(StringFragment str);
// compares two strings, NullPtr-friendly
bool StringFragment_compare(StringFragment str0, StringFragment str1);
// creates new StringFragment which is reversed variant of <s>
StringFragment StringFragment_reverse(StringFragment s);
#if __cplusplus
}
#endif

View File

@ -10,7 +10,7 @@ extern "C" {
#include "mystr.h"
// executes codeblock and prints execution time
#ifdef CLOCK_REALTIME // non-standard high-precision clock
#ifdef CLOCK_REALTIME //non-standard high-precision clock
#define optime(opname,repeats,codeblock) ({\
struct timespec start, stop;\
clock_gettime(CLOCK_REALTIME, &start);\
@ -20,7 +20,7 @@ extern "C" {
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\
printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\
})
#else // standard clock which works worse then previous
#else //
#define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\

View File

@ -1,36 +0,0 @@
#include "base.h"
// returns length of string (without \0)
uint32 cptr_length(char* str){
uint32 len=0;
while(*(str++)) len++;
return len;
}
// allocates new char[] and copies src there
char* cptr_copy(char* src){
uint32 len=cptr_length(src)+1;
char* dst=malloc(len);
while(len-->0)
dst[len]=src[len];
return dst;
}
// compares two char buffers, NullPtr-friendly
bool cptr_compare(char* key0, char* key1){
if(!key0) return key1 ? false : true;
if(!key1) return false;
while(*key0&&*key1)
if(*key0++ != *key1++)
return false;
return true;
}
// multiplies char n times
char* char_multiply(char c, uint32 n){
char* rez=malloc(n+1);
rez[n]=0;
while(n-->0)
rez[n]=c;
return rez;
}

View File

@ -1,23 +0,0 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "types.h"
// returns length of string (without \0)
uint32 cptr_length(char* str);
// allocates new char[] and copies src there
char* cptr_copy(char* src);
// compares two char buffers, NullPtr-friendly
bool cptr_compare(char* key0, char* key1);
// multiplies char n times
char* char_multiply(char c, uint32 n);
#if __cplusplus
}
#endif

View File

@ -1,7 +1,8 @@
#include "std.h"
#include "errors.h"
#include "mystr.h"
const char* errname(err_t err){
char* errname(err_t err){
switch(err){
case SUCCESS: return "SUCCESS";
case ERR_MAXLENGTH: return "ERR_MAXLENGTH";
@ -14,14 +15,37 @@ const char* errname(err_t err){
}
}
void _throwint(int err, const char* srcfile, int line, const char* funcname){
if(err){ // SUCCESS=0 is not an error
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errname(err));
exit(err);
}
else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname);
#define ERRMSG_MAXLENGTH 1024
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
size_t bufsize=ERRMSG_MAXLENGTH;
char* rezult=malloc(bufsize);
IFWIN(
sprintf_s(rezult,bufsize,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg),
sprintf(rezult,"[%s:%d] %s() throwed error: %s",srcfile,line,funcname,errmsg)
);
return rezult;
}
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname){
printf("\e[91m[%s:%d %s] throwed error: %s\e[0m\n",srcfile,line,funcname,errmesg);
exit(255);
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
size_t bufsize=cptr_length(errmsg)+ERRMSG_MAXLENGTH;
char* rezult=malloc(bufsize);
IFWIN(
sprintf_s(rezult,bufsize,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname),
sprintf(rezult,"%s\n \\___[%s:%d] %s()",errmsg,srcfile,line,funcname)
);
free(errmsg);
return rezult;
}
void Maybe_free(Maybe e){
free(e.errmsg);
Unitype_free(e.value);
}
void printMaybe(Maybe e){
if(e.errmsg) printf("%s\n",e.errmsg);
else printuni(e.value);
}
char* __doNothing(char* a) {return a;}
char* __unknownErr() {return "UNKNOWN ERROR";}

View File

@ -4,20 +4,58 @@
extern "C" {
#endif
#include "std.h"
#include "types.h"
typedef enum err_t {
SUCCESS, // not an error
SUCCESS, //not an error
ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t;
const char* errname(err_t err);
char* errname(err_t err);
void _throwint(int err, const char* srcfile, int line, const char* funcname) ;
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname);
#pragma GCC diagnostic ignored "-Wint-conversion"
#define throw(E) \
CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \
CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \
printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__)))
char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
typedef struct Maybe{
Unitype value;
char* errmsg;
} Maybe;
static const Maybe MaybeNull={.value.type=Null, .value.VoidPtr=NULL,.errmsg=NULL};
void Maybe_free(Maybe e);
void printMaybe(Maybe e);
#define SUCCESS(REZLT) (Maybe){.errmsg=NULL, .value=REZLT}
#define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull}
#define __EXIT(ERRMSG) ({ printf("\e[91m%s\e[0m \n", ERRMSG); exit(128); })
char* __doNothing(char* a);
char* __unknownErr( );
#define __stringify_err(E) _Generic(\
(E),\
char*: __doNothing,\
int: errname,\
default: __unknownErr\
)(E)
#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
#define safethrow(E) __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)))
#define try(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
return _rezult;\
}else
#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\
_rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\
__EXIT(_rezult.errmsg);\
}else
#if __cplusplus
}

View File

@ -37,8 +37,8 @@ char* char_multiply(char c, uint32 n){
//copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCptr(string str){
char* cptr=malloc(str.length*sizeof(char)+1);
if(str.length==0) return NULL;
char* cptr=malloc(str.length*sizeof(char)+1);
cptr[str.length]=0;
while(str.length-->0)
cptr[str.length]=str.ptr[str.length];
@ -69,6 +69,7 @@ bool string_compare(string str0, string str1){
//creates new string which is reversed variant of <s>
string string_reverse(string s){
if(s.length==0) return s;
string r={malloc(s.length), s.length};
for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1];

View File

@ -10,6 +10,7 @@ extern "C" {
#include <stdbool.h>
#include <locale.h>
#include <time.h>
#include <setjmp.h>
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)

View File

@ -45,7 +45,7 @@ const char* my_type_name(my_type t){
}
}
// frees VoidPtr value or does nothing if type isn't pointer
//frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u){
switch (u.type) {
case Null:
@ -115,16 +115,33 @@ void Unitype_free(Unitype u){
}
}
#define SPRINT_BUFSIZE 64
void sprintuni(char* buf,Unitype v){
IFWIN(
switch (v.type) {
case Null: sprintf_s(buf, SPRINT_BUFSIZE, "{Null}");break;
case Float64: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
case Bool:
case UInt64: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
case Int64: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
case CharPtr: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);break;
default: sprintf_s(buf, SPRINT_BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
},
switch (v.type) {
case Null: sprintf(buf, "{Null}");break;
case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64);break;
case Bool:
case UInt64: sprintf(buf, "{%s : %lu}", my_type_name(v.type),v.UInt64);break;
case Int64: sprintf(buf, "{%s : %ld}", my_type_name(v.type),v.Int64);break;
case CharPtr: sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr);break;
default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break;
}
);
}
void printuni(Unitype v){
switch (v.type) {
case Null: printf("{Null}");break;
case Float64: printf("{%s : %lf}",my_type_name(v.type),v.Float64);break;
case Char: printf("{%s : '%c'}",my_type_name(v.type),v.Char);break;
case Bool:
case UInt64: printf("{%s : %lu}",my_type_name(v.type),v.UInt64);break;
case Int64: printf("{%s : %ld}",my_type_name(v.type),v.Int64);break;
case CharPtr: printf("{%s : \"%s\"}",my_type_name(v.type),(char*)v.VoidPtr);break;
default: printf("{%s : %p}",my_type_name(v.type),v.VoidPtr);break;
}
char* s=malloc(SPRINT_BUFSIZE);
sprintuni(s,v);
fputs(s, stdout);
free(s);
}

View File

@ -34,7 +34,6 @@ typedef struct Unitype{
int64 Int64;
uint64 UInt64;
double Float64;
char Char;
bool Bool;
void* VoidPtr;
};
@ -48,9 +47,10 @@ static const Unitype UniFalse={.Bool=false,.type=Bool};
#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL}
#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL}
// frees VoidPtr value or does nothing if type isn't pointer
//frees VoidPtr value or does nothing if type isn't pointer
void Unitype_free(Unitype u);
void printuni(Unitype v);
void sprintuni(char* s, Unitype v);
#if __cplusplus
}

View File

@ -75,27 +75,27 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir>
<OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<RunCodeAnalysis>true</RunCodeAnalysis>
<ManagedAssembly>false</ManagedAssembly>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir>
<OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<RunCodeAnalysis>true</RunCodeAnalysis>
<ManagedAssembly>false</ManagedAssembly>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<RunCodeAnalysis>true</RunCodeAnalysis>
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir>
<OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<ManagedAssembly>false</ManagedAssembly>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<RunCodeAnalysis>true</RunCodeAnalysis>
<OutDir>bin-$(Platform)\$(Configuration)\</OutDir>
<IntDir>obj-$(Platform)\$(Configuration)\</IntDir>
<OutDir>bin\$(Configuration)-$(Platform)\</OutDir>
<IntDir>obj\$(Configuration)-$(Platform)\</IntDir>
<ManagedAssembly>false</ManagedAssembly>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@ -204,9 +204,10 @@
<ClInclude Include="Autoarr\Autoarr.h" />
<ClInclude Include="Autoarr\Autoarr_declare.h" />
<ClInclude Include="Autoarr\Autoarr_define.h" />
<ClInclude Include="Autoarr\StringBuilder.h" />
<ClInclude Include="base\base.h" />
<ClInclude Include="base\cptr.h" />
<ClInclude Include="base\errors.h" />
<ClInclude Include="base\mystr.h" />
<ClInclude Include="base\std.h" />
<ClInclude Include="base\types.h" />
<ClInclude Include="DtsodParser\DtsodV24.h" />
@ -214,17 +215,16 @@
<ClInclude Include="Hashtable\Hashtable.h" />
<ClInclude Include="Hashtable\KeyValuePair.h" />
<ClInclude Include="SearchTree\SearchTree.h" />
<ClInclude Include="StringFragment\StringBuilder.h" />
<ClInclude Include="StringFragment\StringFragment.h" />
<ClInclude Include="tests\tests.h" />
<ClInclude Include="tests\test_marshalling.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Autoarr\Autoarr.c" />
<ClCompile Include="Autoarr\StringBuilder.c" />
<ClCompile Include="Autoarr\Autoarr_KVPair_exported.c" />
<ClCompile Include="Autoarr\Autoarr_Unitype_exported.c" />
<ClCompile Include="Autoarr\StringBuilder.c" />
<ClCompile Include="base\errors.c" />
<ClCompile Include="base\mystr.c" />
<ClCompile Include="base\types.c" />
<ClCompile Include="DtsodParser\DtsodV24.c" />
<ClCompile Include="DtsodParser\DtsodV24_deserialize.c" />
@ -234,13 +234,12 @@
<ClCompile Include="Hashtable\Hashtable.c" />
<ClCompile Include="Hashtable\KeyValuePair.c" />
<ClCompile Include="SearchTree\SearchTree.c" />
<ClCompile Include="StringFragment\StringBuilder.c" />
<ClCompile Include="StringFragment\StringFragment.c" />
<ClCompile Include="tests\main.c" />
<ClCompile Include="tests\test_autoarr.c" />
<ClCompile Include="tests\test_dtsod.c" />
<ClCompile Include="tests\test_hashtable.c" />
<ClCompile Include="tests\test_marshalling.c" />
<ClCompile Include="tests\test_safethrow.c" />
<ClCompile Include="tests\test_searchtree.c" />
<ClCompile Include="tests\test_string.c" />
</ItemGroup>

View File

@ -63,15 +63,6 @@
<ClInclude Include="tests\tests.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="base\cptr.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="StringFragment\StringBuilder.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="StringFragment\StringFragment.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Autoarr\Autoarr.c">

View File

@ -1,10 +1,11 @@
#include "tests.h"
void test_all(){
test_searchtree();
test_autoarr();
test_hashtable();
test_string();
test_safethrow();
test_searchtree();
test_hashtable();
test_dtsod();
printf("\e[96m---------------------------------------\e[0m\n");
}

View File

@ -4,12 +4,11 @@
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");
@ -33,18 +32,21 @@ 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);
optime("deserialize",1,({
tryLast(DtsodV24_deserialize(text),r)
dtsod=r.value.VoidPtr;
}));
print_dtsod(dtsod);
char* s;
optime("serialize",1,(s=DtsodV24_serialize(dtsod)));
Hashtable_free(dtsod);
printf("\e[92m%s",s);
optime("reserialize",10,({
dtsod=DtsodV24_deserialize(s);
tryLast(DtsodV24_deserialize(s),r)
dtsod=r.value.VoidPtr;
free(s);
s=DtsodV24_serialize(dtsod);
Hashtable_free(dtsod);

View File

@ -39,7 +39,10 @@ void printrowgraph(Hashtable* ht){
void fill(Hashtable* ht){
for(uint32 i=0;i<100000;i++){
char* key=malloc(12);
sprintf(key,"key__%u",i);
IFWIN(
sprintf_s(key,12,"key_%u",i),
sprintf(key,"key_%u",i)
);
Hashtable_add(ht,key,Uni(UInt64,i));
}
}
@ -48,7 +51,10 @@ Unitype gett(Hashtable* ht){
char* key=malloc(12);
Unitype u;
for(uint32 i=0;i<100000;i++){
sprintf(key,"key__%u",i);
IFWIN(
sprintf_s(key,12,"key_%u",i),
sprintf(key,"key_%u",i)
);
u=Hashtable_get(ht,key);
}
free(key);

43
tests/test_safethrow.c Normal file
View File

@ -0,0 +1,43 @@
#include "tests.h"
Maybe dont_throw(){
return SUCCESS(Uni(UInt64, 9/2));
}
Maybe throw_error(){
safethrow("test exception");
}
Maybe throw_errcode(){
safethrow(ERR_NULLPTR);
}
Maybe test_maybe(){
printf("\e[94mdont_throw returns \e[92m");
tryLast(dont_throw(),rez0)
printMaybe(rez0);
printf("\n");
try(throw_error(),rez1)
printMaybe(rez1);
throw("test_maybe failed");
return MaybeNull;
}
Maybe c(){ try(throw_errcode(),_) return MaybeNull; }
Maybe b(){ try(c(),_) return MaybeNull; }
Maybe a(){ try(b(),_) return MaybeNull; }
void test_safethrow(){
printf("\e[96m-----------[test_safethrow]-----------\n");
optime("test_safethrow", 1, ({
Maybe e=test_maybe();
printf("\e[94mthrow_error:\n\e[92m");
printMaybe(e);
Maybe_free(e);
printf("\e[94mthrow_errcode:\n\e[92m");
e=a();
printMaybe(e);
Maybe_free(e);
}));
}

View File

@ -6,10 +6,10 @@ void test_string(){
printf("\e[96m-------------[test_string]-------------\n");
char c[]="0123456789abcdef";
string s=string_cpFromCharPtr(c);
printf("\e[92m\"%s\" -> string_cpFromCharPtr()\n",c);
printf("\e[92m\"%s\" \e[94m-> string_cpFromCharPtr()\n",c);
if(s.length!=16) throw("string created with incorrect length");
char* p=string_cpToCptr(s);
printf("\e[92mstring_cpToCptr() -> \"%s\"\n",p);
printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p);
free(p);
free(s.ptr);
}));

View File

@ -9,3 +9,4 @@ void test_autoarr();
void test_hashtable();
void test_string();
void test_dtsod();
void test_safethrow();