1 // sourcefile_test.h written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
7 #include "../../src/sourcefile.h"
8 #include "../../src/diff.h"
9 #include "../../src/loc.h"
11 void test_sourcefile_initialize() {
12 SourceFile *sf = ohcount_sourcefile_new("foo.rb");
13 assert(strcmp("foo.rb", sf->filepath) == 0);
14 assert(strcmp("rb", sf->ext) == 0);
15 assert(strcmp("foo.rb", sf->filename) == 0);
16 assert(strncmp("", sf->filepath, sf->dirpath) == 0);
17 assert(sf->contents == NULL);
18 ohcount_sourcefile_free(sf);
20 sf = ohcount_sourcefile_new("foo/bar.rb");
21 assert(strcmp("foo/bar.rb", sf->filepath) == 0);
22 assert(strcmp("rb", sf->ext) == 0);
23 assert(strcmp("bar.rb", sf->filename) == 0);
24 assert(strncmp("foo/", sf->filepath, sf->dirpath) == 0);
25 assert(sf->contents == NULL);
26 ohcount_sourcefile_free(sf);
29 void test_sourcefile_language_breakdowns() {
30 SourceFile *sf = ohcount_sourcefile_new("foo.rb");
31 ohcount_sourcefile_set_contents(sf, "x = 5");
32 ParsedLanguageList *list = ohcount_sourcefile_get_parsed_language_list(sf);
33 assert(strcmp("ruby", list->head->pl->name) == 0);
34 assert(strcmp("x = 5", list->head->pl->code) == 0);
35 ohcount_sourcefile_free(sf);
38 void test_sourcefile_diff() {
39 SourceFile *old = ohcount_sourcefile_new("foo.c");
40 ohcount_sourcefile_set_contents(old, "int i;");
41 SourceFile *new = ohcount_sourcefile_new("foo.c");
42 ohcount_sourcefile_set_contents(new, "int j;");
43 LocDelta *delta1 = ohcount_loc_delta_new("c", 1, 1, 0, 0, 0, 0);
44 LocDelta *delta2 = ohcount_sourcefile_calc_loc_delta(old, "c", new);
45 assert(ohcount_loc_delta_is_equal(delta1, delta2));
46 LocDeltaList *list1 = ohcount_loc_delta_list_new();
47 ohcount_loc_delta_list_add_loc_delta(list1, delta1);
48 LocDeltaList *list2 = ohcount_sourcefile_diff(old, new);
49 assert(list1->head != NULL);
50 assert(list2->head != NULL);
51 assert(list1->head->next == NULL);
52 assert(list2->head->next == NULL);
53 assert(ohcount_loc_delta_is_equal(list1->head->delta, list2->head->delta));
54 ohcount_sourcefile_free(old);
55 ohcount_sourcefile_free(new);
56 ohcount_loc_delta_free(delta1);
57 ohcount_loc_delta_free(delta2);
58 ohcount_loc_delta_list_free(list1);
59 ohcount_loc_delta_list_free(list2);
62 void test_sourcefile_calc_diff2() {
63 SourceFile *old = ohcount_sourcefile_new("foo.html");
64 ohcount_sourcefile_set_contents(old,
66 " <script type='text/javascript'>\n"
69 " <style type=\"text/css\">\n"
71 " /* css_comment */\n"
75 SourceFile *new = ohcount_sourcefile_new("foo.html");
76 ohcount_sourcefile_set_contents(new,
78 " <script type='text/javascript'>\n"
81 " <style type=\"text/css\">\n"
83 " /* different css_comment */\n"
87 LocDeltaList *list = ohcount_sourcefile_diff(old, new);
88 assert(strcmp(list->head->delta->language, "html") == 0);
89 assert(strcmp(list->head->next->delta->language, "javascript") == 0);
90 assert(strcmp(list->head->next->next->delta->language, "css") == 0);
91 LocDelta *delta1 = ohcount_loc_delta_new("javascript", 1, 1, 0, 0, 0, 0);
92 LocDelta *delta2 = ohcount_loc_delta_list_get_loc_delta(list, "javascript");
93 assert(ohcount_loc_delta_is_equal(delta1, delta2));
94 ohcount_loc_delta_free(delta1);
95 delta1 = ohcount_loc_delta_new("css", 0, 0, 1, 1, 0, 0);
96 delta2 = ohcount_loc_delta_list_get_loc_delta(list, "css");
97 assert(ohcount_loc_delta_is_equal(delta1, delta2));
98 ohcount_sourcefile_free(old);
99 ohcount_sourcefile_free(new);
100 ohcount_loc_delta_list_free(list);
101 ohcount_loc_delta_free(delta1);
104 void test_sourcefile_diff_longer() {
105 SourceFile *old = ohcount_sourcefile_new("foo.c");
106 ohcount_sourcefile_set_contents(old,
112 SourceFile *new = ohcount_sourcefile_new("foo.c");
113 ohcount_sourcefile_set_contents(new,
119 LocDeltaList *list = ohcount_sourcefile_diff(old, new);
120 LocDelta *delta1 = ohcount_loc_delta_new("c", 2, 2, 0, 0, 0, 0);
121 LocDelta *delta2 = ohcount_loc_delta_list_get_loc_delta(list, "c");
122 assert(ohcount_loc_delta_is_equal(delta1, delta2));
123 ohcount_sourcefile_free(old);
124 ohcount_sourcefile_free(new);
125 ohcount_loc_delta_list_free(list);
126 ohcount_loc_delta_free(delta1);
129 void test_sourcefile_diff_very_long() {
131 char *a = malloc(len);
136 SourceFile *old = ohcount_sourcefile_new("foo.c");
137 ohcount_sourcefile_set_contents(old, a);
138 strncpy(a, "int = 1;\n", strlen("int = 1;\n"));
139 SourceFile *new = ohcount_sourcefile_new("foo.c");
140 ohcount_sourcefile_set_contents(new, a);
141 LocDeltaList *list = ohcount_sourcefile_diff(old, new);
142 // 2 lines added, 1 removed... strange but thats the expectation
143 LocDelta *delta1 = ohcount_loc_delta_new("c", 2, 1, 0, 0, 0, 0);
144 LocDelta *delta2 = ohcount_loc_delta_list_get_loc_delta(list, "c");
145 assert(ohcount_loc_delta_is_equal(delta1, delta2));
146 ohcount_sourcefile_free(old);
147 ohcount_sourcefile_free(new);
148 ohcount_loc_delta_list_free(list);
149 ohcount_loc_delta_free(delta1);
152 void test_sourcefile_calc_diff() {
154 ohcount_calc_diff("", "", &added, &removed);
156 assert(removed == 0);
157 ohcount_calc_diff("a", "a", &added, &removed);
159 assert(removed == 0);
160 ohcount_calc_diff("a\n", "a\n", &added, &removed);
162 assert(removed == 0);
163 ohcount_calc_diff("", "a\n", &added, &removed);
165 assert(removed == 0);
166 ohcount_calc_diff("a\n", "", &added, &removed);
168 assert(removed == 1);
169 ohcount_calc_diff("a\n", "b\n", &added, &removed);
171 assert(removed == 1);
172 ohcount_calc_diff("a\nb\nc\n", "a\nc\nd\n", &added, &removed);
174 assert(removed == 1);
186 "Hello, World!\n", // 10 times
197 "Hello, World!\n", // 11 times
201 assert(removed == 0);
204 void test_sourcefile_list_language_facts() {
205 SourceFileList *sfl = ohcount_sourcefile_list_new();
206 ohcount_sourcefile_list_add_directory(sfl, "../gestalt_files/win32_enough/");
207 LocList *list = ohcount_sourcefile_list_analyze_languages(sfl);
208 assert(ohcount_loc_list_filecount(list) == 2);
209 Loc *loc = ohcount_loc_list_get_loc(list, "c");
210 assert(loc->code == 2);
211 assert(loc->comments == 2);
212 assert(loc->blanks == 2);
213 ohcount_sourcefile_list_free(sfl);
214 ohcount_loc_list_free(list);
217 void test_sourcefile_list_no_symlink_dir() {
218 SourceFileList *sfl = ohcount_sourcefile_list_new();
219 ohcount_sourcefile_list_add_directory(sfl, "../symlink_test_dir");
220 LocList *list = ohcount_sourcefile_list_analyze_languages(sfl);
221 assert(ohcount_loc_list_filecount(list) == 0);
222 ohcount_sourcefile_list_free(sfl);
223 ohcount_loc_list_free(list);
228 char *tmp_file_from_buf(const char *buf);
230 void test_tmp_dir() {
231 char buf[] = "This is just some bogus text.";
232 char *tmp_path = tmp_file_from_buf(buf);
234 SourceFileList *list = ohcount_sourcefile_list_new();
235 ohcount_sourcefile_list_add_directory(list, "/tmp");
237 SourceFileList *iter = list->head;
239 for (; iter != NULL; iter = iter->next) {
240 if (strcmp(iter->sf->filepath, tmp_path) == 0) {
249 void all_sourcefile_tests() {
250 test_sourcefile_initialize();
251 test_sourcefile_language_breakdowns();
252 test_sourcefile_diff();
253 test_sourcefile_calc_diff2();
254 test_sourcefile_diff_longer();
255 test_sourcefile_diff_very_long();
256 test_sourcefile_calc_diff();
258 test_sourcefile_list_language_facts();
259 test_sourcefile_list_no_symlink_dir();