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

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

View File

@ -47,11 +47,6 @@ void __AppendValue(SerializeSharedData* shared, Unitype u){
} }
addc('"'); addc('"');
break; break;
case Char:
addc('\'');
addc(u.Char);
addc('\'');
break;
case Bool: case Bool:
StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); StringBuilder_append_cptr(b, u.Bool ? "true" : "false");
break; break;

View File

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

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

@ -20,7 +20,7 @@ extern "C" {
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ 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);\ 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) ({\ #define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\ clock_t start=clock();\
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ 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 "std.h"
#include "errors.h" #include "errors.h"
#include "mystr.h"
const char* errname(err_t err){ char* errname(err_t err){
switch(err){ switch(err){
case SUCCESS: return "SUCCESS"; case SUCCESS: return "SUCCESS";
case ERR_MAXLENGTH: return "ERR_MAXLENGTH"; 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){ #define ERRMSG_MAXLENGTH 1024
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)); char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){
exit(err); 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;
} }
else printf("\e[93m[%s:%d %s] throwed SUCCESS as an error",srcfile,line,funcname);
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 _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); void Maybe_free(Maybe e){
exit(255); 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" { extern "C" {
#endif #endif
#include "std.h"
#include "types.h"
typedef enum err_t { 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_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR
} err_t; } 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) ; char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
void _throwstr(const char* errmesg, const char* srcfile, int line, const char* funcname); char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname);
#pragma GCC diagnostic ignored "-Wint-conversion"
#define throw(E) \ typedef struct Maybe{
CHOOSE(IFTYPE(E,int), _throwint(E,__FILE__,__LINE__,__func__), \ Unitype value;
CHOOSE(IFTYPE(E,char[]), _throwstr(E,__FILE__,__LINE__,__func__), \ char* errmsg;
printf("\e[31m[%s:%d/%s] UNKNOWN ERROR\n",__FILE__,__LINE__,__func__))) } 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 #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) //copies str content to new char pointer value (adding '\0' at the end)
char* string_cpToCptr(string str){ char* string_cpToCptr(string str){
char* cptr=malloc(str.length*sizeof(char)+1);
if(str.length==0) return NULL; if(str.length==0) return NULL;
char* cptr=malloc(str.length*sizeof(char)+1);
cptr[str.length]=0; cptr[str.length]=0;
while(str.length-->0) while(str.length-->0)
cptr[str.length]=str.ptr[str.length]; 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> //creates new string which is reversed variant of <s>
string string_reverse(string s){ string string_reverse(string s){
if(s.length==0) return s;
string r={malloc(s.length), s.length}; string r={malloc(s.length), s.length};
for(uint32 i=0; i<s.length; i++) for(uint32 i=0; i<s.length; i++)
r.ptr[i]=s.ptr[s.length-i-1]; r.ptr[i]=s.ptr[s.length-i-1];

View File

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

View File

@ -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){ void printuni(Unitype v){
switch (v.type) { char* s=malloc(SPRINT_BUFSIZE);
case Null: printf("{Null}");break; sprintuni(s,v);
case Float64: printf("{%s : %lf}",my_type_name(v.type),v.Float64);break; fputs(s, stdout);
case Char: printf("{%s : '%c'}",my_type_name(v.type),v.Char);break; free(s);
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;
}
} }

View File

@ -34,7 +34,6 @@ typedef struct Unitype{
int64 Int64; int64 Int64;
uint64 UInt64; uint64 UInt64;
double Float64; double Float64;
char Char;
bool Bool; bool Bool;
void* VoidPtr; void* VoidPtr;
}; };
@ -51,6 +50,7 @@ static const Unitype UniFalse={.Bool=false,.type=Bool};
//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 Unitype_free(Unitype u);
void printuni(Unitype v); void printuni(Unitype v);
void sprintuni(char* s, Unitype v);
#if __cplusplus #if __cplusplus
} }

View File

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

View File

@ -63,15 +63,6 @@
<ClInclude Include="tests\tests.h"> <ClInclude Include="tests\tests.h">
<Filter>Файлы заголовков</Filter> <Filter>Файлы заголовков</Filter>
</ClInclude> </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>
<ItemGroup> <ItemGroup>
<ClCompile Include="Autoarr\Autoarr.c"> <ClCompile Include="Autoarr\Autoarr.c">

View File

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

View File

@ -4,7 +4,6 @@
const char text[]= const char text[]=
"message: {\n" "message: {\n"
" bool: false;" " bool: false;"
" char: 'v';"
" int: -2515;" " int: -2515;"
" uint:#comment!\n 0u;" " uint:#comment!\n 0u;"
" double: 965.557f;#another comment!\n" " double: 965.557f;#another comment!\n"
@ -33,18 +32,21 @@ void test_dtsod(){
optime(__func__,1,({ optime(__func__,1,({
printf("\e[96m-------------[test_dtsod]-------------\n"); printf("\e[96m-------------[test_dtsod]-------------\n");
Hashtable* dtsod; Hashtable* dtsod;
char* s=cptr_copy(text);
optime("deserialize",1,(dtsod=DtsodV24_deserialize(s))); optime("deserialize",1,({
free(s); tryLast(DtsodV24_deserialize(text),r)
dtsod=r.value.VoidPtr;
}));
print_dtsod(dtsod); print_dtsod(dtsod);
char* s;
optime("serialize",1,(s=DtsodV24_serialize(dtsod))); optime("serialize",1,(s=DtsodV24_serialize(dtsod)));
Hashtable_free(dtsod); Hashtable_free(dtsod);
printf("\e[92m%s",s); printf("\e[92m%s",s);
optime("reserialize",10,({ optime("reserialize",10,({
dtsod=DtsodV24_deserialize(s); tryLast(DtsodV24_deserialize(s),r)
dtsod=r.value.VoidPtr;
free(s); free(s);
s=DtsodV24_serialize(dtsod); s=DtsodV24_serialize(dtsod);
Hashtable_free(dtsod); Hashtable_free(dtsod);

View File

@ -39,7 +39,10 @@ void printrowgraph(Hashtable* ht){
void fill(Hashtable* ht){ void fill(Hashtable* ht){
for(uint32 i=0;i<100000;i++){ for(uint32 i=0;i<100000;i++){
char* key=malloc(12); 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)); Hashtable_add(ht,key,Uni(UInt64,i));
} }
} }
@ -48,7 +51,10 @@ Unitype gett(Hashtable* ht){
char* key=malloc(12); char* key=malloc(12);
Unitype u; Unitype u;
for(uint32 i=0;i<100000;i++){ 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); u=Hashtable_get(ht,key);
} }
free(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"); printf("\e[96m-------------[test_string]-------------\n");
char c[]="0123456789abcdef"; char c[]="0123456789abcdef";
string s=string_cpFromCharPtr(c); 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"); if(s.length!=16) throw("string created with incorrect length");
char* p=string_cpToCptr(s); char* p=string_cpToCptr(s);
printf("\e[92mstring_cpToCptr() -> \"%s\"\n",p); printf("\e[94mstring_cpToCptr() -> \e[92m\"%s\"\n",p);
free(p); free(p);
free(s.ptr); free(s.ptr);
})); }));

View File

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