forked from mcauley-penney/mmv-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_set.c
175 lines (135 loc) · 4.26 KB
/
test_set.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "../src/set.c"
#include "../src/utils.c"
#include "unity.h"
#include <stdbool.h>
void setUp(void)
{
}
void tearDown(void)
{
}
/* tied to implementation details */
void test_set_alloc(void)
{
struct Set *test_set = set_alloc(10);
TEST_ASSERT_EQUAL_UINT(10, test_set->map_capacity);
set_destroy(test_set);
test_set = set_alloc(0);
TEST_ASSERT_EQUAL_UINT(0, test_set->map_capacity);
set_destroy(test_set);
}
void test_set_begin(void)
{
int *i;
char *cur_str, *test_str = "TEST STRING\0";
struct Set *test_set = set_alloc(10);
set_insert(test_str, test_set, true);
i = set_begin(test_set);
cur_str = *get_set_pos(test_set, i);
TEST_ASSERT_EQUAL_STRING(test_str, cur_str);
}
void test_set_next(void)
{
int *i;
char *cur_str, *test_str1 = "TEST STRING1\0", *test_str2 = "TEST STRING2\0";
struct Set *test_set = set_alloc(10);
set_insert(test_str1, test_set, true);
set_insert(test_str2, test_set, true);
i = set_begin(test_set);
i = set_next(i);
cur_str = *get_set_pos(test_set, i);
TEST_ASSERT_EQUAL_STRING(test_str2, cur_str);
}
void test_set_end(void)
{
int *i;
char *cur_str, *test_str1 = "TEST STRING1\0", *test_str2 = "TEST STRING2\0";
struct Set *test_set = set_alloc(10);
set_insert(test_str1, test_set, true);
set_insert(test_str2, test_set, true);
for (i = set_begin(test_set); i < set_end(test_set) - 1; i = set_next(i))
;
cur_str = *get_set_pos(test_set, i);
TEST_ASSERT_EQUAL_STRING(test_str2, cur_str);
}
void test_set_insert_uniq_str(void)
{
char *cur_str, *test_str = "TEST STRING\0";
struct Set *test_set = set_alloc(1);
int insert_outcome = set_insert(test_str, test_set, true);
TEST_ASSERT_EQUAL_INT(1, insert_outcome);
set_destroy(test_set);
}
void test_set_insert_dup_str(void)
{
char *cur_str, *test_str = "TEST STRING\0";
struct Set *test_set = set_alloc(2);
int insert_outcome = set_insert(test_str, test_set, true);
TEST_ASSERT_EQUAL_INT(1, insert_outcome);
insert_outcome = set_insert(test_str, test_set, true);
TEST_ASSERT_EQUAL_INT(0, insert_outcome);
set_destroy(test_set);
}
void test_set_init_empty(void)
{
// case: NULL because no arguments
int argc = 0;
char *empty_argv[] = {NULL};
struct Set *test_set = set_init(false, argc, empty_argv, false);
TEST_ASSERT_NULL(test_set);
}
void test_set_init_max(void)
{
// case: NULL because too many arguments
int argc = MAX_OPS + 1;
char *empty_argv[] = {NULL};
struct Set *test_set = set_init(false, argc, empty_argv, false);
TEST_ASSERT_NULL(test_set);
}
void test_set_init_onedupe_notrack(void)
{
// case: two keys, one removed duplicate, no sentinal value
int *i, argc = 2;
char *cur_str, *dupe_argv[] = {"TEST STRING", "TEST STRING"};
struct Set *test_set = set_init(false, argc, dupe_argv, false);
// make sure first string is present
i = set_begin(test_set);
cur_str = *get_set_pos(test_set, i);
TEST_ASSERT_EQUAL_STRING("TEST STRING", cur_str);
// make sure second string has been removed
i = set_next(i);
cur_str = *get_set_pos(test_set, i);
TEST_ASSERT_NULL(cur_str);
set_destroy(test_set);
}
void test_set_init_onedupe_track(void)
{
// case: two keys, one removed duplicate, sentinal value
int *i, argc = 2;
char *cur_str, *dupe_argv[] = {"TEST STRING", "TEST STRING"};
struct Set *test_set = set_init(false, argc, dupe_argv, false);
// make sure second string's key is sentinal value using predicate
test_set = set_init(false, argc, dupe_argv, true);
i = set_begin(test_set);
i = set_next(i);
TEST_ASSERT_FALSE(is_valid_key(i));
set_destroy(test_set);
}
int main(void)
{
UNITY_BEGIN();
// Set private helper functions
RUN_TEST(test_set_alloc);
RUN_TEST(test_set_insert_uniq_str);
RUN_TEST(test_set_insert_dup_str);
// Set iteration
RUN_TEST(test_set_begin);
RUN_TEST(test_set_next);
RUN_TEST(test_set_end);
// Set initialization
RUN_TEST(test_set_init_empty);
RUN_TEST(test_set_init_max);
RUN_TEST(test_set_init_onedupe_notrack);
RUN_TEST(test_set_init_onedupe_track);
return UNITY_END();
}