R
RoSsIaCrIiLoIA
Do you like my strlcpy strlcat etc?
Thank you
/*---------------------------------------*/
.....
int main(void)
{
/*-------------------------------------{}*/
srand((unsigned)time(0));
test3(200000, 100, 100, strlcat, strlcat_m);
test1(3000000, 100, revstring, revstring_m );
return 0;
} /* main, endswith */
/* 25 + 25 + 10 = 60 0..59 */
char lettere(unsigned j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
return u[j%60];
}
void rand_phrase(char* a, size_t le)
{unsigned i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) goto l0;
la: a=lettere((unsigned)rand()); if(++i<le)goto la;
l0:
a=0;
}
/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
goto l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) goto le;
if(p2==searchme)
{r=1; goto le;}
if(p1!=phrase) goto l0;
le:
return r;
}
/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) goto l0;
}
return lgh;
} /* revstring */
size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{while( --sz && (*dst++ = *src++) );
--z;
if( sz )
{return z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) goto l0;
}
else if(src) goto l1;
return z-sz;
} /* strlcpy */
size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
return z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
return z + (src ? strlen(src): 0);
}
} /* strlcat */
int test1(cicli, len_s0, f0, f1)
unsigned cicli, len_s0;
size_t (*f0)(char*), (*f1)(char*);
{char *s0, *s1, *s2;
size_t i, k0, k1, h0, h1, h3, len_s1=len_s0;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||f0==0||f1==0)
return 0;
if(len_s0>100000)
return 0;
s0=malloc(len_s0);
if(s0==0) return 0;
if((s1=malloc(len_s1))==0)
{free(s0); return 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); return 0;}
i=0;
l0:
h0=rand()%len_s0;
rand_phrase(s0, h0);
strcpy(s1, s0);
if( (k0=f0(s0))!=(k1=f1(s1)) )
{l1:
printf("test failure:"
"s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s1, s0, s2, (unsigned)k0,
(unsigned)k1,(unsigned)strlen(s0),(unsigned)h0,(unsigned)h1,
(unsigned)i);
free(s0); free(s1); free(s2);
return 0;
}
if(strcmp(s0, s1)!=0)goto l1;
if(++i<cicli/50)goto l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) goto l34;
t0=time(0);
l2:
*s1=++*s1;
if(f0(s1)==1234567) ++i;
if(++i<cicli)goto l2;
t1=time(0);
printf("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
*s1=++*s1;
if(f1(s1)==1234567) ++i;
if(++i<cicli)goto l3;
t1=time(0);
printf("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2);
printf("Test success\n");
return 1;
}
/*************************************/
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
/* 25 + 25 + 10 = 60 0..59 */
char lettere(U j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}
void rand_phrase(char* a, size_t le)
{U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}
/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)
{r=1; G le;}
if(p1!=phrase) G l0;
le:
R r;
}
/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) G l0;
}
return lgh;
} /* revstring */
size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{W( --sz && (*dst++ = *src++) );
--z;
if( sz )
{R z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) G l0;
}
else if(src) G l1;
R z-sz;
} /* strlcpy */
size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
R z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
R z + (src ? strlen(src): 0);
}
} /* strlcat */
int test3(cicli, len_s0, len_s1, f0, f1)
U cicli, len_s0, len_s1;
size_t (*f0)(char*, const char*, size_t),
(*f1)(char*, const char*, size_t);
{char *s2, *s0, *s1, *s3;
size_t i, k0, k1, h0, h1, h3;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||len_s1==0||f0==0||f1==0)
R 0;
if(len_s0>100000||len_s1>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
if((s3=malloc(len_s0))==0)
{free(s0);free(s1); free(s2); R 0;}
i=0;
l0:
h0=rand()%len_s0; h1=rand()%len_s1;
h3=h0+1+rand()%(len_s0-h0);
rand_phrase(s0, h0); rand_phrase(s1, h1);
strcpy(s3, s0); strcpy(s2, s0);
if( (k0=f0(s0, s1, h3 )) !=
(k1=f1(s2, s1, h3 )) )
{l1:
P("test failure:"
"s3=%s# s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s3, s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1,
(U)i);
free(s0); free(s1); free(s2); free(s3);
R 0;
}
if(strcmp(s0, s2)!=0)G l1;
if(++i<cicli)G l0;
i=0;
//l34:
// h1=rand()%len_s1; rand_phrase(s1, h1);
// if(h1==0) G l34;
t0=time(0);
l2:
h0=rand()%len_s0; rand_phrase(s0, h3);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f0(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
h0=rand()%len_s0; rand_phrase(s0, h0);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f1(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2); free(s3);
P("Test success\n");
R 1;
}
Thank you
/*---------------------------------------*/
.....
int main(void)
{
/*-------------------------------------{}*/
srand((unsigned)time(0));
test3(200000, 100, 100, strlcat, strlcat_m);
test1(3000000, 100, revstring, revstring_m );
return 0;
} /* main, endswith */
/* 25 + 25 + 10 = 60 0..59 */
char lettere(unsigned j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
return u[j%60];
}
void rand_phrase(char* a, size_t le)
{unsigned i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) goto l0;
la: a=lettere((unsigned)rand()); if(++i<le)goto la;
l0:
a=0;
}
/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
goto l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) goto le;
if(p2==searchme)
{r=1; goto le;}
if(p1!=phrase) goto l0;
le:
return r;
}
/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) goto l0;
}
return lgh;
} /* revstring */
size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{while( --sz && (*dst++ = *src++) );
--z;
if( sz )
{return z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) goto l0;
}
else if(src) goto l1;
return z-sz;
} /* strlcpy */
size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
return z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
return z + (src ? strlen(src): 0);
}
} /* strlcat */
int test1(cicli, len_s0, f0, f1)
unsigned cicli, len_s0;
size_t (*f0)(char*), (*f1)(char*);
{char *s0, *s1, *s2;
size_t i, k0, k1, h0, h1, h3, len_s1=len_s0;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||f0==0||f1==0)
return 0;
if(len_s0>100000)
return 0;
s0=malloc(len_s0);
if(s0==0) return 0;
if((s1=malloc(len_s1))==0)
{free(s0); return 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); return 0;}
i=0;
l0:
h0=rand()%len_s0;
rand_phrase(s0, h0);
strcpy(s1, s0);
if( (k0=f0(s0))!=(k1=f1(s1)) )
{l1:
printf("test failure:"
"s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s1, s0, s2, (unsigned)k0,
(unsigned)k1,(unsigned)strlen(s0),(unsigned)h0,(unsigned)h1,
(unsigned)i);
free(s0); free(s1); free(s2);
return 0;
}
if(strcmp(s0, s1)!=0)goto l1;
if(++i<cicli/50)goto l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) goto l34;
t0=time(0);
l2:
*s1=++*s1;
if(f0(s1)==1234567) ++i;
if(++i<cicli)goto l2;
t1=time(0);
printf("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
*s1=++*s1;
if(f1(s1)==1234567) ++i;
if(++i<cicli)goto l3;
t1=time(0);
printf("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2);
printf("Test success\n");
return 1;
}
/*************************************/
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
/* 25 + 25 + 10 = 60 0..59 */
char lettere(U j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}
void rand_phrase(char* a, size_t le)
{U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}
/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)
{r=1; G le;}
if(p1!=phrase) G l0;
le:
R r;
}
/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) G l0;
}
return lgh;
} /* revstring */
size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{W( --sz && (*dst++ = *src++) );
--z;
if( sz )
{R z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) G l0;
}
else if(src) G l1;
R z-sz;
} /* strlcpy */
size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
R z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
R z + (src ? strlen(src): 0);
}
} /* strlcat */
int test3(cicli, len_s0, len_s1, f0, f1)
U cicli, len_s0, len_s1;
size_t (*f0)(char*, const char*, size_t),
(*f1)(char*, const char*, size_t);
{char *s2, *s0, *s1, *s3;
size_t i, k0, k1, h0, h1, h3;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||len_s1==0||f0==0||f1==0)
R 0;
if(len_s0>100000||len_s1>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
if((s3=malloc(len_s0))==0)
{free(s0);free(s1); free(s2); R 0;}
i=0;
l0:
h0=rand()%len_s0; h1=rand()%len_s1;
h3=h0+1+rand()%(len_s0-h0);
rand_phrase(s0, h0); rand_phrase(s1, h1);
strcpy(s3, s0); strcpy(s2, s0);
if( (k0=f0(s0, s1, h3 )) !=
(k1=f1(s2, s1, h3 )) )
{l1:
P("test failure:"
"s3=%s# s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s3, s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1,
(U)i);
free(s0); free(s1); free(s2); free(s3);
R 0;
}
if(strcmp(s0, s2)!=0)G l1;
if(++i<cicli)G l0;
i=0;
//l34:
// h1=rand()%len_s1; rand_phrase(s1, h1);
// if(h1==0) G l34;
t0=time(0);
l2:
h0=rand()%len_s0; rand_phrase(s0, h3);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f0(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
h0=rand()%len_s0; rand_phrase(s0, h0);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f1(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2); free(s3);
P("Test success\n");
R 1;
}