File size: 3,798 Bytes
78d2150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "../../unity/unity.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

/* ==================== 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();
}