Files
lab1_letscontinue/str_test.c
2025-11-15 13:11:21 +03:00

123 lines
3.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "str.h"
#define my_assert(cond, reason, ...) do { \
if (!(cond)) { \
printf("[-] ERROR " reason "\n", ##__VA_ARGS__); \
printf("\t%s:%d " #cond "\n", __FILE_NAME__, __LINE__); \
exit(1); \
} \
} while (0)
void
test_it()
{
str s;
//TEST str_init
str_init(&s);
my_assert(s.len == 0,
"initial size should be 0");
my_assert(s.ptr == str_data(&s),
"str_data, должна вернуть то же самое что хранится в ptr поле");
str_deinit(&s);
// TEST str_init_data
printf("testing str_init_data...\n");
str_init_data(&s, "hello");
my_assert(s.len == strlen("hello"),
"str_init_data, неправильный размер строки");
my_assert(str_data(&s) != NULL, "str_data вернула NULL, а должна указатель на строку");
my_assert(strcmp(str_data(&s), "hello") == 0,
"str_init_data, не правильная строка '%s' != 'hello'", str_data(&s));
// TEST str_set
printf("testing str_set...\n");
str_set(&s, "");
my_assert(s.len == 0,
"str_set, нулевая строка, неверный размер");
my_assert(strcmp(str_data(&s), "") == 0,
"str_set, нулевая строка, не верное содержимое %s", str_data(&s));
char *lazyfox = "The quick brown fox jumps over the lazy dog";
str_set(&s, lazyfox);
my_assert(s.len == strlen(lazyfox),
"str_set, lazyfox s.len error");
my_assert(strcmp(str_data(&s), lazyfox) == 0,
"str_set, нулевая строка, не верное содержимое %s", str_data(&s));
// TEST str_copy
printf("testing str_copy...\n");
str s2;
s2 = str_copy(&s);
my_assert(s2.len == s.len,
"str_copy, размеры строк разные!");
my_assert(strcmp(str_data(&s), str_data(&s2)) == 0,
"str_copy, получились разные строки '%s' != '%s'", str_data(&s), str_data(&s2));
my_assert(s.ptr != s2.ptr,
"str_copy, s.ptr и s2.ptr не должны указывать на одну строку. Указатели строк должны быть разными");
str_deinit(&s2);
// TEST str_append
printf("testing str_append...\n");
str_set(&s, "hello");
str_append(&s, " world");
my_assert(strcmp(s.ptr, "hello world") == 0,
"str_append, неправильная строка %s", s.ptr);
str_append(&s, "loooooooooooooooooooooooooooooooooooooooooooooooong str");
my_assert(strcmp(s.ptr, "hello worldloooooooooooooooooooooooooooooooooooooooooooooooong str") == 0,
"str_append, вторая проверка, неправильная строка");
// TEST str_append_n
str_set(&s, "hello ");
str_append_n(&s, lazyfox, 9);
my_assert(strcmp(str_data(&s), "hello The quick") == 0,
"str_append_n, неверная строка %s", str_data(&s));
// TEST str_shrink
str_shrink(&s, 5);
my_assert(s.len == 5,
"str_shrink, неправильный размер строки");
my_assert(strcmp(str_data(&s), "hello") == 0,
"str_shrink, не правильная строка");
str_deinit(&s);
// TEST str_find
str_init_data(&s, "hello");
my_assert(str_find(&s, "hello") == 0,
"str_find, не могу найти `hello` в строке `hello`");
my_assert(str_find(&s, "llo") == 2,
"str_find, не могу найти `llo` в строке `hello`");
my_assert(str_find(&s, "world") == -1,
"str_find, могу найти `world` в строке `hello`");
//TEST str_sub
str_set(&s, lazyfox);
s2 = str_sub(&s, 4, 5);
my_assert(strcmp(str_data(&s2), "quick") == 0,
"str_sub, не правильная строка %s", str_data(&s2));
str_deinit(&s2);
//TEST str_replace
str_set(&s, "foo bar baz foo");
str_replace(&s, "foo", "test");
my_assert(strcmp(s.ptr, "test bar baz test") == 0,
"str_replace, не верное значение %s", s.ptr);
str_deinit(&s);
printf("[+] ALL_TESTS_PASSED\n");
}
int
main(int argc, char *argv[])
{
test_it();
return 0;
}