FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_match.c
1 /*
2 Authors: Daniele Fognini, Andreas Wuerl, Marion Deveaud
3 Copyright (C) 2013-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <CUnit/CUnit.h>
21 #include <stdarg.h>
22 #include <match.h>
23 #include <monk.h>
24 
25 #include "libfocunit.h"
26 
27 #include "match.h"
28 #include "license.h"
29 
30 static char* const testFileName = (char*) 0x34;
31 
32 File* getFileWithText(const char* text) {
33  char* fileText = g_strdup(text);
34 
35  File* result = malloc(sizeof(File));
36  result->id = 42;
37  result->tokens = tokenize(fileText, "^");
38  result->fileName = testFileName;
39  g_free(fileText);
40 
41  return result;
42 }
43 
44 Licenses* getNLicensesWithText(int count, ...) {
45  GArray* licenseArray = g_array_new(TRUE, FALSE, sizeof(License));
46  va_list texts;
47  va_start(texts, count);
48  for (int i = 0; i < count; i++) {
49  char* text = g_strdup(va_arg(texts, char*));
51  license.refId = i;
52  license.shortname = g_strdup_printf("%d-testLic", i);
53  license.tokens = tokenize(text, "^" );
54 
55  g_array_append_val(licenseArray, license);
56  g_free(text);
57  }
58  va_end(texts);
59 
60  return buildLicenseIndexes(licenseArray, 1, 0);
61 }
62 
63 void file_free(File* file) {
64  g_array_free(file->tokens, TRUE);
65  free(file);
66 }
67 
68 void matchesArray_free(GArray* matches) {
69  for (guint i = 0; i < matches->len; i++) {
70  Match* match = g_array_index(matches, Match*, i);
71  match_free(match);
72  }
73  g_array_free(matches, TRUE);
74 }
75 
76 int _matchEquals(Match* match, long refId, size_t start, size_t end) {
77  FO_ASSERT_EQUAL((int) match->license->refId, (int) refId);
78  FO_ASSERT_EQUAL((int) match_getStart(match), (int) start);
79  FO_ASSERT_EQUAL((int) match_getEnd(match), (int) end);
80 
81  return ( (match_getStart(match) == start) &&
82  (match_getEnd(match) == end) &&
83  (match->license->refId == refId) );
84 }
85 
86 void test_findAllMatchesDisjoint() {
87  File* file = getFileWithText("^e^a^b^c^d^e");
88  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "d");
89 
90  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
91 
92  CU_ASSERT_EQUAL(matches->len, 3);
93  if (matches->len == 3) {
94  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 0), 0, 1, 2))
95  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 1), 1, 2, 4))
96  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 2), 2, 4, 5))
97  }
98 
99  matchesArray_free(matches);
100  file_free(file);
101  licenses_free(licenses);
102 }
103 
104 void test_findDiffsAtBeginning() {
105  File* file = getFileWithText("^e^a^b^c^d^e");
106  Licenses* licenses = getNLicensesWithText(2, "a", "e^b^c^d^e");
107  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 2);
108 
109  FO_ASSERT_EQUAL(matches->len, 2);
110  if (matches->len == 2) {
111  Match* expectedDiff = g_array_index(matches, Match*, 0);
112  FO_ASSERT_TRUE(_matchEquals(expectedDiff, 1, 0, 6));
113  FO_ASSERT_EQUAL_FATAL(expectedDiff->type, MATCH_TYPE_DIFF);
114  CU_ASSERT_EQUAL(expectedDiff->ptr.diff->matchedInfo->len, 3);
115  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 1), 0, 1, 2))
116  }
117 
118  matchesArray_free(matches);
119  file_free(file);
120  licenses_free(licenses);
121 }
122 
123 void test_findAllMatchesWithDiff() {
124  File* file = getFileWithText("a^b^c^d^e^f");
125  Licenses* licenses = getNLicensesWithText(4, "a^c^d", "a^b^d^e", "d", "e^f");
126  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
127 
128  FO_ASSERT_EQUAL(matches->len, 2);
129  if (matches->len == 2) {
130  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 0), 1, 0, 5))
131  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 1), 3, 4, 6))
132  }
133 
134  matchesArray_free(matches);
135  file_free(file);
136  licenses_free(licenses);
137 }
138 
139 void test_findAllMatchesTwoGroups() {
140  File* file = getFileWithText("a^b^c^d^e^f^g");
141  Licenses* licenses = getNLicensesWithText(6, "a^b", "a^b^c^d", "d", "e", "f", "e^f^g");
142  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
143 
144  FO_ASSERT_EQUAL(matches->len, 2);
145  if (matches->len == 2) {
146  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 0), 1, 0, 4))
147  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 1), 5, 4, 7))
148  }
149 
150  matchesArray_free(matches);
151  file_free(file);
152  licenses_free(licenses);
153 }
154 
155 void test_findAllMatchesTwoGroupsWithDiff() {
156  File* file = getFileWithText("a^b^c^d^e^f^g");
157  Licenses* licenses = getNLicensesWithText(6, "a^b", "a^b^c^e", "d", "e", "f", "e^f^g");
158  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
159 
160  FO_ASSERT_EQUAL(matches->len, 3);
161  if (matches->len == 3) {
162  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 0), 1, 0, 5))
163  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 1), 2, 3, 4))
164  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 2), 5, 4, 7))
165  }
166 
167  matchesArray_free(matches);
168  file_free(file);
169  licenses_free(licenses);
170 }
171 
172 void test_findAllMatchesAllIncluded() {
173  File* file = getFileWithText("a^b^c^d");
174  Licenses* licenses = getNLicensesWithText(3, "a^b^c^d", "b^c", "d");
175  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
176 
177  FO_ASSERT_EQUAL(matches->len, 1);
178  if (matches->len == 1) {
179  FO_ASSERT_TRUE(_matchEquals(g_array_index(matches, Match*, 0), 0, 0, 4))
180  }
181 
182  matchesArray_free(matches);
183  file_free(file);
184  licenses_free(licenses);
185 }
186 
187 void test_formatMatchArray() {
188  DiffMatchInfo diff1 = (DiffMatchInfo){
189  .diffType = "a",
190  .text = (DiffPoint) { .start = 1, .length = 2 },
191  .search = (DiffPoint) { .start = 3, .length = 4 },
192  };
193  DiffMatchInfo diff2 = (DiffMatchInfo){
194  .diffType = "b",
195  .text = (DiffPoint) { .start = 1, .length = 0 },
196  .search = (DiffPoint) { .start = 3, .length = 4 },
197  };
198  DiffMatchInfo diff3 = (DiffMatchInfo){
199  .diffType = "b",
200  .text = (DiffPoint) { .start = 2, .length = 2 },
201  .search = (DiffPoint) { .start = 3, .length = 0 },
202  };
203  DiffMatchInfo diff4 = (DiffMatchInfo){
204  .diffType = "b",
205  .text = (DiffPoint) { .start = 4, .length = 0 },
206  .search = (DiffPoint) { .start = 3, .length = 0 },
207  };
208 
209  char* result;
210  GArray* matchInfo = g_array_new(TRUE, FALSE, sizeof(DiffMatchInfo));
211 
212  g_array_append_val(matchInfo, diff1);
213  result = formatMatchArray(matchInfo);
214  FO_ASSERT_STRING_EQUAL(result, "t[1+2] a s[3+4]");
215  free(result);
216 
217  g_array_append_val(matchInfo, diff2);
218  result = formatMatchArray(matchInfo);
219  FO_ASSERT_STRING_EQUAL(result, "t[1+2] a s[3+4], t[1] b s[3+4]");
220  free(result);
221 
222  g_array_append_val(matchInfo, diff3);
223  g_array_append_val(matchInfo, diff4);
224  result = formatMatchArray(matchInfo);
225  FO_ASSERT_STRING_EQUAL(result, "t[1+2] a s[3+4], t[1] b s[3+4], t[2+2] b s[3], t[4] b s[3]");
226  free(result);
227 
228  g_array_free(matchInfo, TRUE);
229 }
230 
231 // match initialized with just enough to run _getRank() and _free()
232 // if type == MATCH_TYPE_FULL then _getRank() == 100 irrespective of the rank variable
233 Match* _matchWithARank(int type, double rank) {
234  Match* result = malloc(sizeof(Match));
235 
236  result->type = type;
237  if (type == MATCH_TYPE_DIFF) {
238  result->ptr.diff = malloc(sizeof(DiffResult));
239  result->ptr.diff->rank = rank;
240  result->ptr.diff->matchedInfo = g_array_new(TRUE, FALSE, sizeof(DiffMatchInfo));
241  } else {
242  result->ptr.full = malloc(sizeof(DiffPoint));
243  }
244  return result;
245 }
246 
247 // match initialized with just enough to have
248 // _getRank() == rank, _getStart() == start, _getEnd() == end and working _free()
249 Match* _matchWithARankStartAndEnd(int type, double rank, size_t start, size_t end, License* license) {
250  Match* result = _matchWithARank(type, rank);
251  if (type == MATCH_TYPE_FULL) {
252  result->ptr.full->start = start;
253  result->ptr.full->length = end - start;
254  } else {
255  DiffMatchInfo matchInfo;
256  matchInfo.diffType = NULL;
257  matchInfo.text.start = start;
258  matchInfo.text.length = end - start;
259  matchInfo.search = (DiffPoint){0,0};
260  g_array_append_val(result->ptr.diff->matchedInfo, matchInfo);
261  }
262  result->license = license;
263  return result;
264 }
265 
266 void test_filterMatches() {
267  GArray* matches = g_array_new(TRUE, FALSE, sizeof(Match*));
268 
269  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "d");
270  License* license = &g_array_index(licenses->licenses, License, 0);
271  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 50.0, 0, 4, license);
272  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 60.0, 0, 6, license);
273 
274  g_array_append_val(matches, match1);
275  g_array_append_val(matches, match2);
276 
277  GArray* filteredMatches = filterNonOverlappingMatches(matches);
278 
279  FO_ASSERT_EQUAL(filteredMatches->len, 1);
280  if (filteredMatches->len == 1) {
281  CU_ASSERT_EQUAL(g_array_index(filteredMatches, Match*, 0), match2);
282  matchesArray_free(filteredMatches);
283  }
284 
285  licenses_free(licenses);
286 }
287 
288 void test_filterMatchesEmpty() {
289  GArray* matches = g_array_new(TRUE, FALSE, sizeof(Match*));
290 
291  GArray* filteredMatches = filterNonOverlappingMatches(matches);
292 
293  FO_ASSERT_EQUAL(filteredMatches->len, 0);
294 
295  matchesArray_free(filteredMatches);
296 }
297 
298 void test_filterMatches2() {
299  GArray* matches = g_array_new(TRUE, FALSE, sizeof(Match*));
300  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "d");
301  License* license = &g_array_index(licenses->licenses, License, 0);
302  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 50.0, 0, 4, license);
303  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 0.0, 0, 6, license);
304 
305  g_array_append_val(matches, match1);
306  g_array_append_val(matches, match2);
307 
308  GArray* filteredMatches = filterNonOverlappingMatches(matches);
309 
310  FO_ASSERT_EQUAL(filteredMatches->len, 1);
311  if (filteredMatches->len == 1) {
312  CU_ASSERT_EQUAL(g_array_index(filteredMatches, Match*, 0), match2);
313  matchesArray_free(filteredMatches);
314  }
315 
316  licenses_free(licenses);
317 }
318 
319 void test_filterMatchesWithTwoGroups() {
320  GArray* matches = g_array_new(TRUE, FALSE, sizeof(Match*));
321 
322  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "d");
323  License* license = &g_array_index(licenses->licenses, License, 0);
324  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 50.0, 0, 4, license);
325  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 60.0, 0, 6, license);
326  Match* match3 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 70.0, 4, 6, license);
327 
328  g_array_append_val(matches, match1);
329  g_array_append_val(matches, match2);
330  g_array_append_val(matches, match3);
331 
332  GArray* filteredMatches = filterNonOverlappingMatches(matches);
333 
334  FO_ASSERT_EQUAL(filteredMatches->len, 1);
335  if (filteredMatches->len == 1) {
336  CU_ASSERT_EQUAL(g_array_index(filteredMatches, Match*, 0), match3);
337  matchesArray_free(filteredMatches);
338  }
339 
340  licenses_free(licenses);
341 }
342 
343 void test_filterMatchesWithBadGroupingAtFirstPass() {
344  GArray* matches = g_array_new(TRUE, FALSE, sizeof(Match*));
345  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "d");
346  License* license = &g_array_index(licenses->licenses, License, 0);
347  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 95.0, 0, 10, license);
348  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 99.0, 5, 10, license);
349  Match* match3 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 95.0, 5, 9, license);
350  Match* match4 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 95.0, 5, 14, license);
351 
352  g_array_append_val(matches, match1);
353  g_array_append_val(matches, match2);
354  g_array_append_val(matches, match3);
355  g_array_append_val(matches, match4);
356 
357  GArray* filteredMatches = filterNonOverlappingMatches(matches);
358 
359  FO_ASSERT_EQUAL(filteredMatches->len, 1);
360  if (filteredMatches->len == 1) {
361  CU_ASSERT_EQUAL(g_array_index(filteredMatches, Match*, 0), match2);
362  matchesArray_free(filteredMatches);
363  }
364 
365  licenses_free(licenses);
366 }
367 
368 MonkState* testState = (MonkState*) 0x17;
369 
370 int expectOnAll;
371 int onAll(MonkState* state, const File* file, const GArray* matches) {
372  FO_ASSERT_PTR_NOT_NULL(matches);
373  CU_ASSERT_EQUAL(state, testState);
374  CU_ASSERT_EQUAL(file->fileName, testFileName);
375  FO_ASSERT_TRUE(expectOnAll);
376  return 1;
377 }
378 
379 int expectOnNo;
380 int onNo(MonkState* state, const File* file) {
381  CU_ASSERT_EQUAL(state, testState);
382  CU_ASSERT_EQUAL(file->fileName, testFileName);
383  FO_ASSERT_TRUE(expectOnNo);
384  return 1;
385 }
386 
387 int expectOnFull;
388 int onFull(MonkState* state, const File* file, const License* license, const DiffMatchInfo* matchInfo) {
389  FO_ASSERT_PTR_NOT_NULL(license);
390  FO_ASSERT_PTR_NOT_NULL(matchInfo);
391  CU_ASSERT_EQUAL(state, testState);
392  CU_ASSERT_EQUAL(file->fileName, testFileName);
393  FO_ASSERT_TRUE(expectOnFull);
394  return 1;
395 }
396 
397 int expectOnDiff;
398 int onDiff(MonkState* state, const File* file, const License* license, const DiffResult* diffResult) {
399  FO_ASSERT_PTR_NOT_NULL(license);
400  FO_ASSERT_PTR_NOT_NULL(diffResult);
401  CU_ASSERT_EQUAL(state, testState);
402  CU_ASSERT_EQUAL(file->fileName, testFileName);
403  FO_ASSERT_TRUE(expectOnDiff);
404  return 1;
405 }
406 
407 int doIgnore;
408 
409 int ignore(MonkState* state, const File* file) {
410  CU_ASSERT_EQUAL(state, testState);
411  CU_ASSERT_EQUAL(file->fileName, testFileName);
412  return doIgnore;
413 }
414 
415 int noop(MonkState* state) {
416  return 1;
417 }
418 
419 void doProcessTest(MatchCallbacks* expectedCallbacks)
420 {
421  File* file = getFileWithText("^e^a^b^c^d^e");
422  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "d");
423 
424  GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
425 
426  processMatches(testState, file, matches, expectedCallbacks);
427 
428  matchesArray_free(matches);
429  file_free(file);
430  licenses_free(licenses);
431 }
432 
433 void test_processMatchesIgnores() {
434  doIgnore = 1;
435  expectOnAll = 0;
436  expectOnDiff = 0;
437  expectOnFull = 0;
438  expectOnNo = 0;
439  MatchCallbacks expectedCallbacks =
440  { .ignore = ignore,
441  .onAll = onAll,
442  .onDiff = onDiff,
443  .onFull = onFull,
444  .onNo = onNo,
445  .onBeginOutput = noop,
446  .onBetweenIndividualOutputs = noop,
447  .onEndOutput = noop
448  };
449 
450  doProcessTest(&expectedCallbacks);
451 }
452 
453 void test_processMatchesUsesOnAllIfDefined() {
454  doIgnore = 0;
455  expectOnAll = 1;
456  expectOnDiff = 0;
457  expectOnFull = 0;
458  expectOnNo = 0;
459  MatchCallbacks expectedCallbacks =
460  { .ignore = ignore,
461  .onAll = onAll,
462  .onDiff = onDiff,
463  .onFull = onFull,
464  .onNo = onNo,
465  .onBeginOutput = noop,
466  .onBetweenIndividualOutputs = noop,
467  .onEndOutput = noop
468  };
469 
470  doProcessTest(&expectedCallbacks);
471 }
472 
473 void test_processMatchesUsesOnFullIfOnAllNotDefined() {
474  doIgnore = 0;
475  expectOnAll = 0;
476  expectOnDiff = 0;
477  expectOnFull = 1;
478  expectOnNo = 0;
479  MatchCallbacks expectedCallbacks =
480  { .ignore = ignore,
481  .onDiff = onDiff,
482  .onFull = onFull,
483  .onNo = onNo,
484  .onBeginOutput = noop,
485  .onBetweenIndividualOutputs = noop,
486  .onEndOutput = noop
487  };
488 
489  doProcessTest(&expectedCallbacks);
490 }
491 
492 void test_processMatchesUsesOnNoOnNoMatches() {
493  doIgnore = 0;
494  expectOnAll = 0;
495  expectOnDiff = 0;
496  expectOnFull = 0;
497  expectOnNo = 1;
498  MatchCallbacks expectedCallbacks =
499  { .ignore = ignore,
500  .onDiff = onDiff,
501  .onFull = onFull,
502  .onNo = onNo,
503  .onBeginOutput = noop,
504  .onBetweenIndividualOutputs = noop,
505  .onEndOutput = noop
506  };
507 
508  GArray* matches = g_array_new(FALSE, FALSE, 1);
509 
510  File* file = getFileWithText("^e^a^b^c^d^e");
511  processMatches(testState, file, matches, &expectedCallbacks);
512 
513  file_free(file);
514  g_array_free(matches, TRUE);
515 }
516 
517 void test_processMatchesUsesOnAllForNoMatches() {
518  doIgnore = 0;
519  expectOnAll = 1;
520  expectOnDiff = 0;
521  expectOnFull = 0;
522  expectOnNo = 0;
523  MatchCallbacks expectedCallbacks =
524  { .ignore = ignore,
525  .onAll = onAll,
526  .onDiff = onDiff,
527  .onFull = onFull,
528  .onNo = onNo,
529  .onBeginOutput = noop,
530  .onBetweenIndividualOutputs = noop,
531  .onEndOutput = noop
532  };
533 
534  GArray* matches = g_array_new(FALSE, FALSE, 1);
535 
536  File* file = getFileWithText("^e^a^b^c^d^e");
537  processMatches(testState, file, matches, &expectedCallbacks);
538 
539  file_free(file);
540  g_array_free(matches, TRUE);
541 }
542 
543 void test_matchComparatorSameLicenseFullVsDiff() {
544  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "a^b");
545  License* licensePtr = (License*) licenses->licenses->data;
546  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr);
547  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 100.0, 1, 2, licensePtr);
548 
549  CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0); // full > diff
550  CU_ASSERT_TRUE(match_partialComparator(match2, match1) < 0); // diff < full
551 
552  match_free(match1);
553  match_free(match2);
554  licenses_free(licenses);
555 }
556 
557 void test_matchComparatorDifferentLicensesNonIncluded() {
558  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "a^b");
559  License* licensePtr = (License*) licenses->licenses->data;
560  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr+1);
561  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr+2);
562 
563  CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0); // match1 >= match2
564  CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0); // match2 >= match1
565 
566  match_free(match1);
567  match_free(match2);
568  licenses_free(licenses);
569 }
570 
571 void test_matchComparatorDifferentLicensesIncluded() {
572  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "a^b");
573  License* licensePtr = (License*) licenses->licenses->data;
574  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr);
575  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 100.0, 1, 3, licensePtr+2);
576  Match* match3 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 100.0, 1, 8, licensePtr+2);
577 
578  CU_ASSERT_TRUE(match_partialComparator(match1, match2) < 0); // match1.license < match2.license
579  CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0); // match2.license > match1.license
580  CU_ASSERT_TRUE(match_partialComparator(match1, match3) < 0); // match1.license < match3.license
581  CU_ASSERT_TRUE(match_partialComparator(match3, match1) > 0); // match3.license > match1.license
582  CU_ASSERT_TRUE(match_partialComparator(match2, match3) > 0); // match2 rank >= match3 rank
583  CU_ASSERT_TRUE(match_partialComparator(match3, match2) > 0); // match3 rank >= match2 rank
584 
585  match_free(match1);
586  match_free(match2);
587  match_free(match3);
588  licenses_free(licenses);
589 }
590 
591 void test_matchComparatorIncludedSameLicenseNotComparable() {
592  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "a^b");
593  License* licensePtr = (License*) licenses->licenses->data;
594  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr);
595  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 100.0, 4, 8, licensePtr+2);
596 
597  CU_ASSERT_TRUE(match_partialComparator(match1, match2) == 0); // start(match2) > end(match1)
598  CU_ASSERT_TRUE(match_partialComparator(match2, match1) == 0); // start(match2) > end(match1)
599 
600  match_free(match1);
601  match_free(match2);
602  licenses_free(licenses);
603 }
604 
605 void test_matchComparatorIncludedSameLicenseComparedByRank() {
606  Licenses* licenses = getNLicensesWithText(3, "a", "b^c", "a^b");
607  License* licensePtr = (License*) licenses->licenses->data;
608  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr);
609  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 90.0, 1, 8, licensePtr+2);
610  Match* match3 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 99.0, 1, 8, licensePtr+2);
611 
612  CU_ASSERT_TRUE(match_partialComparator(match1, match2) < 0); //match2.license > match1.license
613  CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
614  CU_ASSERT_TRUE(match_partialComparator(match3, match1) > 0); //match3.license > match1.license
615  CU_ASSERT_TRUE(match_partialComparator(match1, match3) < 0);
616  CU_ASSERT_TRUE(match_partialComparator(match3, match2) > 0); //match3 > match2
617  CU_ASSERT_TRUE(match_partialComparator(match2, match3) < 0);
618 
619  match_free(match1);
620  match_free(match2);
621  match_free(match3);
622  licenses_free(licenses);
623 }
624 
625 void test_matchComparatorIncludedSameLicenseBiggerMatch() {
626  Licenses* licenses = getNLicensesWithText(3, "a^b^c^d", "b^c^d", "a^b");
627  License* licensePtr = (License*) licenses->licenses->data;
628  Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 0, 4, licensePtr);
629  Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 0, 3, licensePtr+1);
630  Match* match3 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 0, 2, licensePtr+2);
631 
632  CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0); //license2 included in license1
633  CU_ASSERT_TRUE(match_partialComparator(match2, match1) < 0);
634  CU_ASSERT_TRUE(match_partialComparator(match1, match3) > 0); //license3 included in license1
635  CU_ASSERT_TRUE(match_partialComparator(match3, match1) < 0);
636  CU_ASSERT_TRUE(match_partialComparator(match3, match2) < 0); //full match2 overlap match3
637  CU_ASSERT_TRUE(match_partialComparator(match2, match3) > 0);
638 
639  match_free(match1);
640  match_free(match2);
641  match_free(match3);
642  licenses_free(licenses);
643 }
644 
645 CU_TestInfo match_testcases[] = {
646  {"Testing match of all licenses with disjoint full matches:", test_findAllMatchesDisjoint},
647  {"Testing match of all licenses with diff at beginning", test_findDiffsAtBeginning},
648  {"Testing match of all licenses with diffs:", test_findAllMatchesWithDiff},
649  {"Testing match of all licenses with included full matches:", test_findAllMatchesAllIncluded},
650  {"Testing match of all licenses with two included group:", test_findAllMatchesTwoGroups},
651  {"Testing match of all licenses with two included group and diffs:", test_findAllMatchesTwoGroupsWithDiff},
652  {"Testing formatting the diff information output:", test_formatMatchArray},
653  {"Testing filtering matches:", test_filterMatches},
654  {"Testing filtering matches empty:", test_filterMatchesEmpty},
655  {"Testing filtering matches with a full match:", test_filterMatches2},
656  {"Testing filtering matches with two groups:", test_filterMatchesWithTwoGroups},
657  {"Testing filtering matches with bad grouping at first pass:", test_filterMatchesWithBadGroupingAtFirstPass},
658  {"Testing matches processor does nothing if ignore callback is true:", test_processMatchesIgnores},
659  {"Testing matches processor uses on all if defined:", test_processMatchesUsesOnAllIfDefined},
660  {"Testing matches processor uses on full if on all not defined:", test_processMatchesUsesOnFullIfOnAllNotDefined},
661  {"Testing matches processor uses on no if no matches:", test_processMatchesUsesOnNoOnNoMatches},
662  {"Testing matches processor uses on all if defined and no matches:", test_processMatchesUsesOnAllForNoMatches},
663  {"Testing matches comparator:", test_matchComparatorSameLicenseFullVsDiff},
664  {"Testing matches comparator different licenses not included one in the other:", test_matchComparatorDifferentLicensesNonIncluded},
665  {"Testing matches comparator different licenses included one in the other:", test_matchComparatorDifferentLicensesIncluded},
666  {"Testing matches comparator included same licence not comparable:", test_matchComparatorIncludedSameLicenseNotComparable},
667  {"Testing matches comparator included same license compared by rank:", test_matchComparatorIncludedSameLicenseComparedByRank},
668  {"Testing matches comparator included same license bigger match:", test_matchComparatorIncludedSameLicenseBiggerMatch},
669  CU_TEST_INFO_NULL
670 };
Definition: monk.h:78
Definition: match.h:31
Definition: monk.h:55
Store the results of a regex match.
Definition: scanners.hpp:39
Definition: monk.h:72
Definition: nomos.h:439
Definition: monk.h:66
Definition: diff.h:25
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>. The application passed in should match the script name in /etc/init.d
Definition: pkgConfig.php:1225