OTWO-1213 Works around lost encoding in Ruby/C binding layer
[ohcount] / test / unit / sourcefile_test.h
1 // sourcefile_test.h written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
3
4 #include <string.h>
5 #include <assert.h>
6
7 #include "../../src/sourcefile.h"
8 #include "../../src/diff.h"
9 #include "../../src/loc.h"
10
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);
19
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);
27 }
28
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);
36 }
37
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);
60 }
61
62 void test_sourcefile_calc_diff2() {
63   SourceFile *old = ohcount_sourcefile_new("foo.html");
64   ohcount_sourcefile_set_contents(old,
65     "<html>\n"
66     "  <script type='text/javascript'>\n"
67     "    var i = 1;\n"
68     "  </script>\n"
69     "  <style type=\"text/css\">\n"
70     "    new_css_code\n"
71     "    /* css_comment */\n"
72     "  </style>\n"
73     "</html>"
74   );
75   SourceFile *new = ohcount_sourcefile_new("foo.html");
76   ohcount_sourcefile_set_contents(new,
77     "<html>\n"
78     "  <script type='text/javascript'>\n"
79     "    var i = 2;\n"
80     "  </script>\n"
81     "  <style type=\"text/css\">\n"
82     "    new_css_code\n"
83     "    /* different css_comment */\n"
84     "  </style>\n"
85     "</html>"
86   );
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);
102 }
103
104 void test_sourcefile_diff_longer() {
105   SourceFile *old = ohcount_sourcefile_new("foo.c");
106   ohcount_sourcefile_set_contents(old,
107     "int = 1;\n"
108     "int = 2;\n"
109     "int = 3;\n"
110     "int = 4;\n"
111   );
112   SourceFile *new = ohcount_sourcefile_new("foo.c");
113   ohcount_sourcefile_set_contents(new,
114     "int = 1;\n"
115     "int = 5;\n"
116     "int = 6;\n"
117     "int = 4;\n"
118   );
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);
127 }
128
129 void test_sourcefile_diff_very_long() {
130         int len = 5500000;
131         char *a = malloc(len);
132         memset(a, 'i', len);
133         a[len-1] = '\0';
134         a[len-2] = '\n';
135
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);
150 }
151
152 void test_sourcefile_calc_diff() {
153   int added, removed;
154   ohcount_calc_diff("", "", &added, &removed);
155   assert(added == 0);
156   assert(removed == 0);
157   ohcount_calc_diff("a", "a", &added, &removed);
158   assert(added == 0);
159   assert(removed == 0);
160   ohcount_calc_diff("a\n", "a\n", &added, &removed);
161   assert(added == 0);
162   assert(removed == 0);
163   ohcount_calc_diff("", "a\n", &added, &removed);
164   assert(added == 1);
165   assert(removed == 0);
166   ohcount_calc_diff("a\n", "", &added, &removed);
167   assert(added == 0);
168   assert(removed == 1);
169   ohcount_calc_diff("a\n", "b\n", &added, &removed);
170   assert(added = 1);
171   assert(removed == 1);
172   ohcount_calc_diff("a\nb\nc\n", "a\nc\nd\n", &added, &removed);
173   assert(added == 1);
174   assert(removed == 1);
175
176   ohcount_calc_diff(
177     "Hello, World!\n"
178     "Hello, World!\n"
179     "Hello, World!\n"
180     "Hello, World!\n"
181     "Hello, World!\n"
182     "Hello, World!\n"
183     "Hello, World!\n"
184     "Hello, World!\n"
185     "Hello, World!\n"
186     "Hello, World!\n", // 10 times
187     "Hello, World!\n"
188     "Hello, World!\n"
189     "Hello, World!\n"
190     "Hello, World!\n"
191     "Hello, World!\n"
192     "Hello, World!\n"
193     "Hello, World!\n"
194     "Hello, World!\n"
195     "Hello, World!\n"
196     "Hello, World!\n"
197     "Hello, World!\n", // 11 times
198     &added, &removed
199   );
200   assert(added == 1);
201   assert(removed == 0);
202 }
203
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);
215 }
216
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);
224 }
225
226 #define FALSE 0
227 #define TRUE 1
228 char *tmp_file_from_buf(const char *buf);
229
230 void test_tmp_dir() {
231         char buf[] = "This is just some bogus text.";
232         char *tmp_path = tmp_file_from_buf(buf);
233
234         SourceFileList *list = ohcount_sourcefile_list_new();
235         ohcount_sourcefile_list_add_directory(list, "/tmp");
236         int has_tmp = FALSE;
237         SourceFileList *iter = list->head;
238
239         for (; iter != NULL; iter = iter->next) {
240                 if (strcmp(iter->sf->filepath, tmp_path) == 0) {
241                         has_tmp = TRUE;
242                         break;
243                 }
244         }
245         assert(has_tmp);
246 }
247
248
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();
257
258   test_sourcefile_list_language_facts();
259   test_sourcefile_list_no_symlink_dir();
260         test_tmp_dir();
261 }