HelenOS sources
This source file includes following definitions.
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
- PCUT_TEST
#define _REALLY_WANT_STRING_H
#include <string.h>
#include <pcut/pcut.h>
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wstringop-truncation"
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#pragma GCC diagnostic ignored "-Wstringop-overread"
#endif
PCUT_INIT;
PCUT_TEST_SUITE(string);
PCUT_TEST(strcpy)
{
const char *s = "hello";
char buf[7];
size_t i;
char *p;
for (i = 0; i < 7; i++)
buf[i] = 'X';
p = strcpy(buf, s);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strncpy_zero)
{
const char *s = "hello";
char buf[1];
char *p;
buf[0] = 'X';
p = strncpy(buf, s, 0);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'X');
}
PCUT_TEST(strncpy_long)
{
const char *s = "hello";
char buf[5];
size_t i;
char *p;
for (i = 0; i < 5; i++)
buf[i] = 'X';
p = strncpy(buf, s, 4);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'X');
}
PCUT_TEST(strncpy_just)
{
const char *s = "hello";
char buf[6];
size_t i;
char *p;
for (i = 0; i < 6; i++)
buf[i] = 'X';
p = strncpy(buf, s, 5);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == 'X');
}
PCUT_TEST(strncpy_just_over)
{
const char *s = "hello";
char buf[7];
size_t i;
char *p;
for (i = 0; i < 7; i++)
buf[i] = 'X';
p = strncpy(buf, s, 6);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strncpy_over)
{
const char *s = "hello";
char buf[8];
size_t i;
char *p;
for (i = 0; i < 8; i++)
buf[i] = 'X';
p = strncpy(buf, s, 7);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == '\0');
PCUT_ASSERT_TRUE(buf[7] == 'X');
}
PCUT_TEST(strcat)
{
char buf[7];
const char *s = "cde";
size_t i;
char *p;
buf[0] = 'a';
buf[1] = 'b';
buf[2] = '\0';
for (i = 3; i < 7; i++)
buf[i] = 'X';
p = strcat(buf, s);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'a');
PCUT_ASSERT_TRUE(buf[1] == 'b');
PCUT_ASSERT_TRUE(buf[2] == 'c');
PCUT_ASSERT_TRUE(buf[3] == 'd');
PCUT_ASSERT_TRUE(buf[4] == 'e');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strncat_zero)
{
const char *s = "cde";
char buf[4];
char *p;
buf[0] = 'a';
buf[1] = 'b';
buf[2] = '\0';
buf[3] = 'X';
p = strncat(buf, s, 0);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'a');
PCUT_ASSERT_TRUE(buf[1] == 'b');
PCUT_ASSERT_TRUE(buf[2] == '\0');
PCUT_ASSERT_TRUE(buf[3] == 'X');
}
PCUT_TEST(strncat_long)
{
const char *s = "cde";
char buf[6];
size_t i;
char *p;
buf[0] = 'a';
buf[1] = 'b';
buf[2] = '\0';
for (i = 3; i < 6; i++)
buf[i] = 'X';
p = strncat(buf, s, 2);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'a');
PCUT_ASSERT_TRUE(buf[1] == 'b');
PCUT_ASSERT_TRUE(buf[2] == 'c');
PCUT_ASSERT_TRUE(buf[3] == 'd');
PCUT_ASSERT_TRUE(buf[4] == '\0');
PCUT_ASSERT_TRUE(buf[5] == 'X');
}
PCUT_TEST(strncat_just)
{
const char *s = "cde";
char buf[7];
size_t i;
char *p;
buf[0] = 'a';
buf[1] = 'b';
buf[2] = '\0';
for (i = 3; i < 7; i++)
buf[i] = 'X';
p = strncat(buf, s, 3);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'a');
PCUT_ASSERT_TRUE(buf[1] == 'b');
PCUT_ASSERT_TRUE(buf[2] == 'c');
PCUT_ASSERT_TRUE(buf[3] == 'd');
PCUT_ASSERT_TRUE(buf[4] == 'e');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strncat_just_over)
{
const char *s = "cde";
char buf[7];
size_t i;
char *p;
buf[0] = 'a';
buf[1] = 'b';
buf[2] = '\0';
for (i = 3; i < 7; i++)
buf[i] = 'X';
p = strncat(buf, s, 4);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'a');
PCUT_ASSERT_TRUE(buf[1] == 'b');
PCUT_ASSERT_TRUE(buf[2] == 'c');
PCUT_ASSERT_TRUE(buf[3] == 'd');
PCUT_ASSERT_TRUE(buf[4] == 'e');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strncat_over)
{
const char *s = "cde";
char buf[7];
size_t i;
char *p;
buf[0] = 'a';
buf[1] = 'b';
buf[2] = '\0';
for (i = 3; i < 7; i++)
buf[i] = 'X';
p = strncat(buf, s, 5);
PCUT_ASSERT_TRUE(p == buf);
PCUT_ASSERT_TRUE(buf[0] == 'a');
PCUT_ASSERT_TRUE(buf[1] == 'b');
PCUT_ASSERT_TRUE(buf[2] == 'c');
PCUT_ASSERT_TRUE(buf[3] == 'd');
PCUT_ASSERT_TRUE(buf[4] == 'e');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strcmp_same)
{
PCUT_ASSERT_TRUE(strcmp("apples\0#", "apples\0$") == 0);
}
PCUT_TEST(strcmp_less_than)
{
PCUT_ASSERT_TRUE(strcmp("apples", "oranges") < 0);
}
PCUT_TEST(strcmp_greater_than)
{
PCUT_ASSERT_TRUE(strcmp("oranges", "apples") > 0);
}
PCUT_TEST(strcmp_prefix)
{
PCUT_ASSERT_TRUE(strcmp("apple", "apples") < 0);
}
PCUT_TEST(strcoll)
{
PCUT_ASSERT_TRUE(strcoll("apples\0#", "apples\0$") == 0);
PCUT_ASSERT_TRUE(strcoll("apples", "oranges") < 0);
PCUT_ASSERT_TRUE(strcoll("oranges", "apples") > 0);
PCUT_ASSERT_TRUE(strcoll("apple", "apples") < 0);
}
PCUT_TEST(strncmp_zero)
{
PCUT_ASSERT_TRUE(strncmp("apple", "orange", 0) == 0);
}
PCUT_TEST(strncmp_long)
{
PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 2) == 0);
}
PCUT_TEST(strncmp_just)
{
PCUT_ASSERT_TRUE(strncmp("apple", "apricot", 3) < 0);
}
PCUT_TEST(strncmp_over)
{
PCUT_ASSERT_TRUE(strncmp("dart", "tart", 3) < 0);
}
PCUT_TEST(strxfrm_null)
{
size_t n;
n = strxfrm(NULL, "hello", 0);
PCUT_ASSERT_INT_EQUALS(5, n);
}
PCUT_TEST(strxfrm_long)
{
const char *s = "hello";
char buf[5];
size_t i;
size_t n;
for (i = 0; i < 5; i++)
buf[i] = 'X';
n = strxfrm(buf, s, 4);
PCUT_ASSERT_INT_EQUALS(5, n);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'X');
}
PCUT_TEST(strxfrm_just)
{
const char *s = "hello";
char buf[6];
size_t i;
size_t n;
for (i = 0; i < 6; i++)
buf[i] = 'X';
n = strxfrm(buf, s, 5);
PCUT_ASSERT_INT_EQUALS(5, n);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == 'X');
}
PCUT_TEST(strxfrm_just_over)
{
const char *s = "hello";
char buf[7];
size_t i;
size_t n;
for (i = 0; i < 7; i++)
buf[i] = 'X';
n = strxfrm(buf, s, 6);
PCUT_ASSERT_INT_EQUALS(5, n);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
}
PCUT_TEST(strxfrm_over)
{
const char *s = "hello";
char buf[8];
size_t i;
size_t n;
for (i = 0; i < 8; i++)
buf[i] = 'X';
n = strxfrm(buf, s, 7);
PCUT_ASSERT_INT_EQUALS(5, n);
PCUT_ASSERT_TRUE(buf[0] == 'h');
PCUT_ASSERT_TRUE(buf[1] == 'e');
PCUT_ASSERT_TRUE(buf[2] == 'l');
PCUT_ASSERT_TRUE(buf[3] == 'l');
PCUT_ASSERT_TRUE(buf[4] == 'o');
PCUT_ASSERT_TRUE(buf[5] == '\0');
PCUT_ASSERT_TRUE(buf[6] == 'X');
PCUT_ASSERT_TRUE(buf[7] == 'X');
}
PCUT_TEST(strchr_nullchar)
{
const char *s = "abcabc";
char *p;
p = strchr(s, '\0');
PCUT_ASSERT_TRUE((const char *)p == &s[6]);
}
PCUT_TEST(strchr_found)
{
const char *s = "abcabc";
char *p;
p = strchr(s, 'b');
PCUT_ASSERT_TRUE((const char *)p == &s[1]);
}
PCUT_TEST(strchr_not_found)
{
const char *s = "abcabc";
char *p;
p = strchr(s, 'd');
PCUT_ASSERT_TRUE(p == NULL);
}
PCUT_TEST(strcspn_empty_str)
{
size_t n;
n = strcspn("", "abc");
PCUT_ASSERT_INT_EQUALS(0, n);
}
PCUT_TEST(strcspn_empty_set)
{
size_t n;
n = strcspn("abc", "");
PCUT_ASSERT_INT_EQUALS(3, n);
}
PCUT_TEST(strcspn_regular)
{
size_t n;
n = strcspn("baBAba", "AB");
PCUT_ASSERT_INT_EQUALS(2, n);
}
PCUT_TEST(strpbrk_empty_string)
{
const char *p;
p = strpbrk("", "abc");
PCUT_ASSERT_NULL(p);
}
PCUT_TEST(strpbrk_empty_set)
{
const char *p;
p = strpbrk("abc", "");
PCUT_ASSERT_NULL(p);
}
PCUT_TEST(strpbrk_regular)
{
const char *s = "baBAba";
char *p;
p = strpbrk(s, "ab");
PCUT_ASSERT_TRUE((const char *)p == s);
}
PCUT_TEST(strrchr_nullchar)
{
const char *s = "abcabc";
char *p;
p = strrchr(s, '\0');
PCUT_ASSERT_TRUE((const char *)p == &s[6]);
}
PCUT_TEST(strrchr_found)
{
const char *s = "abcabc";
char *p;
p = strrchr(s, 'b');
PCUT_ASSERT_TRUE((const char *)p == &s[4]);
}
PCUT_TEST(strrchr_not_found)
{
const char *s = "abcabc";
char *p;
p = strrchr(s, 'd');
PCUT_ASSERT_TRUE(p == NULL);
}
PCUT_TEST(strspn_empty_str)
{
size_t n;
n = strspn("", "abc");
PCUT_ASSERT_INT_EQUALS(0, n);
}
PCUT_TEST(strspn_empty_set)
{
size_t n;
n = strspn("abc", "");
PCUT_ASSERT_INT_EQUALS(0, n);
}
PCUT_TEST(strspn_regular)
{
size_t n;
n = strspn("baBAba", "ab");
PCUT_ASSERT_INT_EQUALS(2, n);
}
PCUT_TEST(strstr_empty)
{
const char *str = "abcabcabcdabc";
char *p;
p = strstr(str, "");
PCUT_ASSERT_TRUE((const char *) p == str);
}
PCUT_TEST(strstr_found)
{
const char *str = "abcabcabcdabc";
char *p;
p = strstr(str, "abcd");
PCUT_ASSERT_TRUE((const char *) p == &str[6]);
}
PCUT_TEST(strstr_notfound)
{
const char *str = "abcabcabcdabc";
char *p;
p = strstr(str, "abcde");
PCUT_ASSERT_NULL(p);
}
PCUT_TEST(strtok)
{
char str[] = ":a::b;;;$c";
char *t;
t = strtok(str, ":");
PCUT_ASSERT_TRUE(t == &str[1]);
PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "a"));
t = strtok(NULL, ";");
PCUT_ASSERT_TRUE(t == &str[3]);
PCUT_ASSERT_INT_EQUALS(0, strcmp(t, ":b"));
t = strtok(NULL, "$;");
PCUT_ASSERT_TRUE(t == &str[9]);
PCUT_ASSERT_INT_EQUALS(0, strcmp(t, "c"));
t = strtok(NULL, "$");
PCUT_ASSERT_NULL(t);
}
PCUT_TEST(strerror_zero)
{
char *p;
p = strerror(0);
PCUT_ASSERT_NOT_NULL(p);
}
PCUT_TEST(strerror_errno)
{
char *p;
p = strerror(EINVAL);
PCUT_ASSERT_NOT_NULL(p);
}
PCUT_TEST(strerror_negative)
{
char *p;
p = strerror(-1);
PCUT_ASSERT_NOT_NULL(p);
}
PCUT_TEST(strlen_empty)
{
PCUT_ASSERT_INT_EQUALS(0, strlen(""));
}
PCUT_TEST(strlen_nonempty)
{
PCUT_ASSERT_INT_EQUALS(3, strlen("abc"));
}
PCUT_TEST(strnlen_empty_short)
{
PCUT_ASSERT_INT_EQUALS(0, strnlen("", 1));
}
PCUT_TEST(strnlen_empty_eq)
{
PCUT_ASSERT_INT_EQUALS(0, strnlen("", 0));
}
PCUT_TEST(strnlen_nonempty_short)
{
PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 5));
}
PCUT_TEST(strnlen_nonempty_just_short)
{
PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 4));
}
PCUT_TEST(strnlen_nonempty_eq)
{
PCUT_ASSERT_INT_EQUALS(3, strnlen("abc", 3));
}
PCUT_TEST(strnlen_nonempty_long)
{
PCUT_ASSERT_INT_EQUALS(2, strnlen("abc", 2));
}
PCUT_TEST(strdup_empty)
{
char *d = strdup("");
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == '\0');
free(d);
}
PCUT_TEST(strdup_nonempty)
{
char *d = strdup("abc");
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == 'a');
PCUT_ASSERT_TRUE(d[1] == 'b');
PCUT_ASSERT_TRUE(d[2] == 'c');
PCUT_ASSERT_TRUE(d[3] == '\0');
free(d);
}
PCUT_TEST(strndup_empty_short)
{
char *d = strndup("", 1);
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == '\0');
free(d);
}
PCUT_TEST(strndup_empty_eq)
{
char *d = strndup("", 0);
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == '\0');
free(d);
}
PCUT_TEST(strndup_nonempty_short)
{
#pragma GCC diagnostic push
#if defined(__GNUC__) && (__GNUC__ >= 11)
#pragma GCC diagnostic ignored "-Wstringop-overread"
#endif
char *d = strndup("abc", 5);
#pragma GCC diagnostic pop
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == 'a');
PCUT_ASSERT_TRUE(d[1] == 'b');
PCUT_ASSERT_TRUE(d[2] == 'c');
PCUT_ASSERT_TRUE(d[3] == '\0');
free(d);
}
PCUT_TEST(strndup_nonempty_eq)
{
char *d = strndup("abc", 3);
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == 'a');
PCUT_ASSERT_TRUE(d[1] == 'b');
PCUT_ASSERT_TRUE(d[2] == 'c');
PCUT_ASSERT_TRUE(d[3] == '\0');
free(d);
}
PCUT_TEST(strndup_nonempty_long)
{
char *d = strndup("abc", 2);
PCUT_ASSERT_NOT_NULL(d);
PCUT_ASSERT_TRUE(d[0] == 'a');
PCUT_ASSERT_TRUE(d[1] == 'b');
PCUT_ASSERT_TRUE(d[2] == '\0');
free(d);
}
PCUT_EXPORT(string);
HelenOS homepage, sources at GitHub