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