#include "../../unity/unity.h" #include #include #include /* ==================== Setup / Teardown ==================== */ void setUp(void) { /* Nothing specific to set up before each test */ } void tearDown(void) { /* Nothing specific to clean up after each test */ } /* ==================== Helper Functions ==================== */ /* Allocate a dummy fileinfo for testing */ static struct fileinfo* create_dummy_file(const char *name) { struct fileinfo *f = malloc(sizeof(struct fileinfo)); if (!f) return NULL; f->name = malloc(strlen(name) + 1); if (!f->name) { free(f); return NULL; } strcpy(f->name, name); return f; } /* ==================== Tests ==================== */ void test_clear_files_no_files(void) { /* Edge case: no files in sorted_file */ cwd_n_used = 0; sorted_file = NULL; cwd_some_quoted = true; any_has_acl = true; inode_number_width = 5; clear_files(); TEST_ASSERT_EQUAL_UINT(0, cwd_n_used); TEST_ASSERT_FALSE(cwd_some_quoted); TEST_ASSERT_FALSE(any_has_acl); TEST_ASSERT_EQUAL_INT(0, inode_number_width); } void test_clear_files_single_file(void) { cwd_n_used = 1; sorted_file = malloc(sizeof(struct fileinfo*) * cwd_n_used); sorted_file[0] = create_dummy_file("file1"); cwd_some_quoted = true; any_has_acl = true; inode_number_width = 3; clear_files(); TEST_ASSERT_EQUAL_UINT(0, cwd_n_used); TEST_ASSERT_FALSE(cwd_some_quoted); TEST_ASSERT_FALSE(any_has_acl); TEST_ASSERT_EQUAL_INT(0, inode_number_width); } void test_clear_files_multiple_files(void) { cwd_n_used = 3; sorted_file = malloc(sizeof(struct fileinfo*) * cwd_n_used); sorted_file[0] = create_dummy_file("file1"); sorted_file[1] = create_dummy_file("file2"); sorted_file[2] = create_dummy_file("file3"); cwd_some_quoted = true; any_has_acl = true; inode_number_width = 8; block_size_width = 4; owner_width = 10; clear_files(); TEST_ASSERT_EQUAL_UINT(0, cwd_n_used); TEST_ASSERT_FALSE(cwd_some_quoted); TEST_ASSERT_FALSE(any_has_acl); TEST_ASSERT_EQUAL_INT(0, inode_number_width); TEST_ASSERT_EQUAL_INT(0, block_size_width); TEST_ASSERT_EQUAL_INT(0, owner_width); } void test_clear_files_edge_values(void) { /* All globals set to nonzero / true before call */ cwd_n_used = 2; sorted_file = malloc(sizeof(struct fileinfo*) * cwd_n_used); sorted_file[0] = create_dummy_file("a"); sorted_file[1] = create_dummy_file("b"); cwd_some_quoted = true; any_has_acl = true; inode_number_width = 99; block_size_width = 99; nlink_width = 99; owner_width = 99; group_width = 99; author_width = 99; scontext_width = 99; major_device_number_width = 99; minor_device_number_width = 99; file_size_width = 99; clear_files(); TEST_ASSERT_EQUAL_UINT(0, cwd_n_used); TEST_ASSERT_FALSE(cwd_some_quoted); TEST_ASSERT_FALSE(any_has_acl); TEST_ASSERT_EQUAL_INT(0, inode_number_width); TEST_ASSERT_EQUAL_INT(0, block_size_width); TEST_ASSERT_EQUAL_INT(0, nlink_width); TEST_ASSERT_EQUAL_INT(0, owner_width); TEST_ASSERT_EQUAL_INT(0, group_width); TEST_ASSERT_EQUAL_INT(0, author_width); TEST_ASSERT_EQUAL_INT(0, scontext_width); TEST_ASSERT_EQUAL_INT(0, major_device_number_width); TEST_ASSERT_EQUAL_INT(0, minor_device_number_width); TEST_ASSERT_EQUAL_INT(0, file_size_width); } /* ==================== Main ==================== */ int main(void) { UNITY_BEGIN(); RUN_TEST(test_clear_files_no_files); RUN_TEST(test_clear_files_single_file); RUN_TEST(test_clear_files_multiple_files); RUN_TEST(test_clear_files_edge_values); return UNITY_END(); }