Files
lab0.1_letscontinue/str_test.c
2025-09-27 03:54:38 +03:00

63 lines
2.0 KiB
C

#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.sz == 0, "initial size should be 0");
my_assert(s.ptr == str_data(&s), "str_data должна вернуть то же самое что хранится в ptr поле");
str_deinit(&s);
// TEST str_init_data
str_init_data(&s, "hello");
my_assert(s.sz == strlen("hello"), "str_init_data, неправильный размер строки");
my_assert(strcmp(str_data(&s), "hello") == 0, "не правильная строка '%s' != 'hello'", str_data(&s));
// TEST str_copy
str s2;
s2 = str_copy(&s);
my_assert(s2.sz == s.sz,
"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 не должны указывать на одну строку. Указатели строк должны быть разными");
// TEST str_append
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_shrink
str_shrink(&s, 5);
my_assert(s.sz == 5, "str_shrink неправильный размер строки");
my_assert(strcmp(str_data(&s), "hello") == 0, "str_shrink не правильная строка");
printf("[+] ALL_TESTS_PASSED\n");
}
int
main(int argc, char *argv[])
{
test_it();
return 0;
}