-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_table.c
More file actions
159 lines (115 loc) · 3.75 KB
/
check_table.c
File metadata and controls
159 lines (115 loc) · 3.75 KB
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
#include <check.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "table.h"
char *
make_temporary_file(char *template, int suffix_len, char *contents) {
char buf[PATH_MAX];
strcpy(buf, template);
int fd = mkstemps(buf, suffix_len);
if (fd != -1) {
FILE *f = fdopen(fd, "w");
if (NULL == f) {
perror("fdopen failed");
return NULL;
} else {
fputs(contents, f);
if (fclose(f) == EOF) {
perror("failed to close temp file");
return NULL;
}
return strdup(buf);
}
} else {
perror("mkstemp failed");
return NULL;
}
}
START_TEST(test_path_to_name) {
char *name = path_to_name("bar.tsv");
fail_unless(strcmp(name, "bar") == 0, "name is not \"bar\"");
}
END_TEST
START_TEST(test_path_to_name_full) {
char *name = path_to_name("/tmp/baz.tsv");
fail_unless(strcmp(name, "baz") == 0, "name is not \"baz\"");
}
END_TEST
START_TEST(test_path_to_name_bad_suffix) {
char *name = path_to_name("/tmp/wumpus.txt");
fail_unless(name == NULL, "name is not NULL");
}
END_TEST
START_TEST(test_read_column) {
column *col = read_column("foo");
fail_unless(strcmp(col->name, "foo") == 0, "name is not \"foo\"");
fail_unless(strcmp(col->type, "text") == 0, "type is not \"text\"");
fail_unless(col->next == NULL, "next is not NULL");
}
END_TEST
START_TEST(test_read_column_with_type) {
column *col = read_column("baz{int}");
fail_unless(strcmp(col->name, "baz") == 0, "name is not \"baz\"");
fail_unless(strcmp(col->type, "int") == 0, "type is not \"int\"");
fail_unless(col->next == NULL, "next is not NULL");
}
END_TEST
START_TEST(test_read_column_invalid_name) {
column *col = read_column("$$$");
fail_unless(col == NULL, "col is not NULL");
}
END_TEST
START_TEST(test_read_columns) {
char *path = make_temporary_file("/tmp/check_table.XXXXXX.tsv",
4,
"foo\tbar\none\ttwo");
column *cols = read_columns(path);
fail_unless(cols != NULL, "cols is NULL");
fail_unless(strcmp(cols->name, "foo") == 0, "cols->name is not \"foo\"");
fail_unless(strcmp(cols->type, "text") == 0, "cols->text is not \"text\"");
fail_unless(cols->next != NULL, "cols->next is NULL");
fail_unless(strcmp(cols->next->name, "bar") == 0,
"cols->name is not \"bar\"");
fail_unless(strcmp(cols->next->type, "text") == 0,
"cols->text is not \"text\"");
fail_unless(cols->next->next == NULL, "cols->next->next is not NULL");
}
END_TEST
Suite *
suite_table(void) {
Suite *ste = suite_create("suite: table");
TCase *tc1 = tcase_create("case: path_to_name");
TCase *tc2 = tcase_create("case: path_to_name full");
TCase *tc3 = tcase_create("case: path_to_name bad suffix");
TCase *tc4 = tcase_create("case: read_column");
TCase *tc5 = tcase_create("case: read_column with type");
TCase *tc6 = tcase_create("case: read_column invalid name");
TCase *tc7 = tcase_create("case: read_columns");
tcase_add_test(tc1, test_path_to_name);
suite_add_tcase(ste, tc1);
tcase_add_test(tc2, test_path_to_name_full);
suite_add_tcase(ste, tc2);
tcase_add_test(tc3, test_path_to_name_bad_suffix);
suite_add_tcase(ste, tc3);
tcase_add_test(tc4, test_read_column);
suite_add_tcase(ste, tc4);
tcase_add_test(tc5, test_read_column_with_type);
suite_add_tcase(ste, tc5);
tcase_add_test(tc6, test_read_column_invalid_name);
suite_add_tcase(ste, tc6);
tcase_add_test(tc7, test_read_columns);
suite_add_tcase(ste, tc7);
return ste;
}
int
main(void) {
int number_failed;
Suite *ste = suite_table();
SRunner *sr = srunner_create(ste);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed);
}