1
0
Fork 0
mirror of https://github.com/tailix/libclayer.git synced 2024-11-20 11:06:24 -05:00

Add tests and fix code

This commit is contained in:
Alex Kotov 2022-12-27 16:59:03 +04:00
parent 8197462089
commit 4850c3ee4f
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 53 additions and 2 deletions

View file

@ -67,8 +67,12 @@ char *LIBCLAYER(strchr)(const char *s, int c)
int LIBCLAYER(strcmp)(const char *s1, const char *s2)
{
for (; *s1; ++s1, ++s2) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
return 0;
for (;;) {
const char c1 = *s1++;
const char c2 = *s2++;
if (c1 != c2) return c1 < c2 ? -1 : 1;
if (!c1) return 0;
}
}
char *LIBCLAYER(strcpy)(char *dest, const char *src)

View file

@ -13,6 +13,12 @@ static char aaa[COUNT], bbb[COUNT];
#define memchr LIBCLAYER(memchr)
#define memset LIBCLAYER(memset)
#define strcat LIBCLAYER(strcat)
#define strchr LIBCLAYER(strchr)
#define strcmp LIBCLAYER(strcmp)
#define strcpy LIBCLAYER(strcpy)
#define strlen LIBCLAYER(strlen)
void test_main()
{
{ // memcmp
@ -50,4 +56,45 @@ void test_main()
for (size_t i = 0; i < COUNT / 2; ++i) assert(aaa[i] == 'a');
for (size_t i = COUNT / 2; i < COUNT; ++i) assert(aaa[i] == 'b');
}
{ // strcat
aaa[0] = 'f'; aaa[1] = 'o'; aaa[2] = 'o'; aaa[3] = '\0';
strcat(aaa, "bar");
for (const char *e = "foobar", *g = aaa; *e; ++e, ++g) assert(*g == *e);
assert(aaa[6] == '\0');
}
{ // strchr
const char *const s = "foobar";
assert(strchr(s, 'f') == &s[0]);
assert(strchr(s, 'b') == &s[3]);
assert(strchr(s, 'r') == &s[5]);
assert(strchr(s, 'x') == NULL);
assert(strchr(s, 'F') == NULL);
}
{ // strcmp
assert(strcmp("", "") == 0);
assert(strcmp("", "a") == -1);
assert(strcmp("a", "") == 1);
assert(strcmp("a", "a") == 0);
assert(strcmp("a", "aa") == -1);
assert(strcmp("aa", "a") == 1);
assert(strcmp("aa", "ab") == -1);
assert(strcmp("ab", "aa") == 1);
assert(strcmp("aa", "aa") == 0);
}
{ // strcpy
assert(strcpy(aaa, "foobar") == aaa);
for (const char *e = "foobar", *g = aaa; *e; ++e, ++g) assert(*g == *e);
assert(aaa[6] == '\0');
}
{ // strlen
assert(strlen("") == 0);
assert(strlen("a") == 1);
assert(strlen("aa") == 2);
assert(strlen("aaaaaaaaaa") == 10);
}
}