Files
scuffedcraft/tests/main.c
2026-07-15 14:33:05 +03:00

105 lines
3.1 KiB
C

#include <string.h>
#include <stdlib.h>
#include "cmocka/cmocka.h"
#include "DynString.h"
void test_DynString_alloc(void** state) {
const char* c_str = "Hello";
const size_t c_str_len = strlen(c_str);
DynString dstr = DynString_alloc(c_str, c_str_len);
assert_memory_equal(dstr.data, c_str, c_str_len + 1);
assert_true(dstr.len == c_str_len);
assert_true(dstr.capacity == c_str_len);
free(dstr.data);
}
void test_DynString_free(void** state) {
DynString dstr = DynString_alloc("Hello", strlen("Hello"));
DynString_free(&dstr);
assert_null(dstr.data);
assert_true(dstr.len == 0);
assert_true(dstr.capacity == 0);
}
void test_DynString_equal(void** state) {
DynString s1 = DynString_alloc("Hello", strlen("Hello"));
DynString s2 = DynString_alloc("Hello", strlen("Hello"));
assert_true(DynString_equal(&s1, &s2));
DynString_free(&s1);
DynString_free(&s2);
s1 = DynString_alloc("Hello", strlen("Hello"));
s2 = DynString_alloc("Hell0", strlen("Hell0"));
assert_false(DynString_equal(&s1, &s2));
DynString_free(&s1);
DynString_free(&s2);
s1 = DynString_alloc("Hello World", strlen("Hello World"));
s2 = DynString_alloc("Hello", strlen("Hello"));
assert_false(DynString_equal(&s1, &s2));
DynString_free(&s1);
DynString_free(&s2);
}
void test_DynString_equal_c_str(void** state) {
DynString s1 = DynString_alloc("Hello", strlen("Hello"));
char* s2 = "Hello";
assert_true(DynString_equal_c_str(&s1, s2));
DynString_free(&s1);
s1 = DynString_alloc("Hello", strlen("Hello"));
s2 = "Hell0";
assert_false(DynString_equal_c_str(&s1, s2));
DynString_free(&s1);
s1 = DynString_alloc("Hello World", strlen("Hello World"));
s2 = "Hello";
assert_false(DynString_equal_c_str(&s1, s2));
DynString_free(&s1);
}
void test_DynString_concat_c_str(void** state) {
const char* dstr_c_str = "Hello";
const char* c_str = " World";
const size_t combined_length = strlen(dstr_c_str) + strlen(c_str);
DynString dstr = DynString_alloc(dstr_c_str, strlen("Hello"));
DynString_concat_c_str(&dstr, c_str, strlen(c_str));
assert_memory_equal(dstr.data, "Hello World", strlen("Hello World"));
assert_true(dstr.len == combined_length);
assert_true(dstr.capacity == combined_length + 1);
DynString_free(&dstr);
}
void test_DynString_c_str(void** state) {
const char* c_str = "Hello";
DynString dstr = DynString_alloc(c_str, strlen(c_str));
assert_string_equal(c_str, DynString_c_str(&dstr));
DynString_concat_c_str(&dstr, " World", strlen(" World"));
assert_string_equal("Hello World", DynString_c_str(&dstr));
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_DynString_alloc),
cmocka_unit_test(test_DynString_free),
cmocka_unit_test(test_DynString_equal),
cmocka_unit_test(test_DynString_equal_c_str),
cmocka_unit_test(test_DynString_concat_c_str),
cmocka_unit_test(test_DynString_c_str),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}