10 #include <CUnit/CUnit.h>
15 #include "libfocunit.h"
20 static char*
const testFileName = (
char*) 0x34;
22 File* getFileWithText(
const char* text) {
23 char* fileText = g_strdup(text);
27 result->tokens = tokenize(fileText,
"^");
28 result->fileName = testFileName;
34 Licenses* getNLicensesWithText(
int count, ...) {
35 GArray* licenseArray = g_array_new(TRUE, FALSE,
sizeof(
License));
37 va_start(texts, count);
38 for (
int i = 0; i < count; i++) {
39 char* text = g_strdup(va_arg(texts,
char*));
42 license.shortname = g_strdup_printf(
"%d-testLic", i);
43 license.tokens = tokenize(text,
"^" );
45 g_array_append_val(licenseArray,
license);
50 return buildLicenseIndexes(licenseArray, 1, 0);
53 void file_free(
File* file) {
54 g_array_free(file->tokens, TRUE);
58 void matchesArray_free(GArray* matches) {
59 for (guint i = 0; i < matches->len; i++) {
63 g_array_free(matches, TRUE);
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);
72 (match_getEnd(
match) == end) &&
73 (
match->license->refId == refId) );
76 void test_findAllMatchesDisjoint() {
77 File* file = getFileWithText(
"^e^a^b^c^d^e");
78 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
80 GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
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))
89 matchesArray_free(matches);
91 licenses_free(licenses);
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);
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))
108 matchesArray_free(matches);
110 licenses_free(licenses);
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);
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))
124 matchesArray_free(matches);
126 licenses_free(licenses);
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);
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))
140 matchesArray_free(matches);
142 licenses_free(licenses);
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);
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))
157 matchesArray_free(matches);
159 licenses_free(licenses);
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);
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))
172 matchesArray_free(matches);
174 licenses_free(licenses);
177 void test_formatMatchArray() {
180 .text = (
DiffPoint) { .start = 1, .length = 2 },
181 .search = (
DiffPoint) { .start = 3, .length = 4 },
185 .text = (
DiffPoint) { .start = 1, .length = 0 },
186 .search = (
DiffPoint) { .start = 3, .length = 4 },
190 .text = (
DiffPoint) { .start = 2, .length = 2 },
191 .search = (
DiffPoint) { .start = 3, .length = 0 },
195 .text = (
DiffPoint) { .start = 4, .length = 0 },
196 .search = (
DiffPoint) { .start = 3, .length = 0 },
200 GArray* matchInfo = g_array_new(TRUE, FALSE,
sizeof(
DiffMatchInfo));
202 g_array_append_val(matchInfo, diff1);
203 result = formatMatchArray(matchInfo);
204 FO_ASSERT_STRING_EQUAL(result,
"t[1+2] a s[3+4]");
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]");
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]");
218 g_array_free(matchInfo, TRUE);
223 Match* _matchWithARank(
int type,
double rank) {
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));
232 result->ptr.full = malloc(
sizeof(
DiffPoint));
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;
246 matchInfo.diffType = NULL;
247 matchInfo.text.start =
start;
248 matchInfo.text.length = end -
start;
250 g_array_append_val(result->ptr.diff->matchedInfo, matchInfo);
256 void test_filterMatches() {
257 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(
Match*));
259 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
261 Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 50.0, 0, 4,
license);
262 Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 60.0, 0, 6,
license);
264 g_array_append_val(matches, match1);
265 g_array_append_val(matches, match2);
267 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
275 licenses_free(licenses);
278 void test_filterMatchesEmpty() {
279 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(
Match*));
281 GArray* filteredMatches = filterNonOverlappingMatches(matches);
283 FO_ASSERT_EQUAL(filteredMatches->len, 0);
285 matchesArray_free(filteredMatches);
288 void test_filterMatches2() {
289 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(
Match*));
290 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
292 Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 50.0, 0, 4,
license);
293 Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 0.0, 0, 6,
license);
295 g_array_append_val(matches, match1);
296 g_array_append_val(matches, match2);
298 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
306 licenses_free(licenses);
309 void test_filterMatchesWithTwoGroups() {
310 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(
Match*));
312 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
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);
318 g_array_append_val(matches, match1);
319 g_array_append_val(matches, match2);
320 g_array_append_val(matches, match3);
322 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
330 licenses_free(licenses);
333 void test_filterMatchesWithBadGroupingAtFirstPass() {
334 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(
Match*));
335 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
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);
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);
347 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
355 licenses_free(licenses);
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);
371 CU_ASSERT_EQUAL(state, testState);
372 CU_ASSERT_EQUAL(file->fileName, testFileName);
373 FO_ASSERT_TRUE(expectOnNo);
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);
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);
400 CU_ASSERT_EQUAL(state, testState);
401 CU_ASSERT_EQUAL(file->fileName, testFileName);
412 File* file = getFileWithText(
"^e^a^b^c^d^e");
413 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
415 GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
417 processMatches(testState, file, matches, expectedCallbacks);
419 matchesArray_free(matches);
421 licenses_free(licenses);
424 void test_processMatchesIgnores() {
436 .onBeginOutput = noop,
437 .onBetweenIndividualOutputs = noop,
441 doProcessTest(&expectedCallbacks);
444 void test_processMatchesUsesOnAllIfDefined() {
456 .onBeginOutput = noop,
457 .onBetweenIndividualOutputs = noop,
461 doProcessTest(&expectedCallbacks);
464 void test_processMatchesUsesOnFullIfOnAllNotDefined() {
475 .onBeginOutput = noop,
476 .onBetweenIndividualOutputs = noop,
480 doProcessTest(&expectedCallbacks);
483 void test_processMatchesUsesOnNoOnNoMatches() {
494 .onBeginOutput = noop,
495 .onBetweenIndividualOutputs = noop,
499 GArray* matches = g_array_new(FALSE, FALSE, 1);
501 File* file = getFileWithText(
"^e^a^b^c^d^e");
502 processMatches(testState, file, matches, &expectedCallbacks);
505 g_array_free(matches, TRUE);
508 void test_processMatchesUsesOnAllForNoMatches() {
520 .onBeginOutput = noop,
521 .onBetweenIndividualOutputs = noop,
525 GArray* matches = g_array_new(FALSE, FALSE, 1);
527 File* file = getFileWithText(
"^e^a^b^c^d^e");
528 processMatches(testState, file, matches, &expectedCallbacks);
531 g_array_free(matches, TRUE);
534 void test_matchComparatorSameLicenseFullVsDiff() {
535 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
537 Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr);
538 Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 100.0, 1, 2, licensePtr);
540 CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0);
541 CU_ASSERT_TRUE(match_partialComparator(match2, match1) < 0);
545 licenses_free(licenses);
548 void test_matchComparatorDifferentLicensesNonIncluded() {
549 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
554 CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0);
555 CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
559 licenses_free(licenses);
562 void test_matchComparatorDifferentLicensesIncluded() {
563 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
569 CU_ASSERT_TRUE(match_partialComparator(match1, match2) < 0);
570 CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
571 CU_ASSERT_TRUE(match_partialComparator(match1, match3) < 0);
572 CU_ASSERT_TRUE(match_partialComparator(match3, match1) > 0);
573 CU_ASSERT_TRUE(match_partialComparator(match2, match3) > 0);
574 CU_ASSERT_TRUE(match_partialComparator(match3, match2) > 0);
579 licenses_free(licenses);
582 void test_matchComparatorIncludedSameLicenseNotComparable() {
583 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
588 CU_ASSERT_TRUE(match_partialComparator(match1, match2) == 0);
589 CU_ASSERT_TRUE(match_partialComparator(match2, match1) == 0);
593 licenses_free(licenses);
596 void test_matchComparatorIncludedSameLicenseComparedByRank() {
597 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
603 CU_ASSERT_TRUE(match_partialComparator(match1, match2) < 0);
604 CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
605 CU_ASSERT_TRUE(match_partialComparator(match3, match1) > 0);
606 CU_ASSERT_TRUE(match_partialComparator(match1, match3) < 0);
607 CU_ASSERT_TRUE(match_partialComparator(match3, match2) > 0);
608 CU_ASSERT_TRUE(match_partialComparator(match2, match3) < 0);
613 licenses_free(licenses);
616 void test_matchComparatorIncludedSameLicenseBiggerMatch() {
617 Licenses* licenses = getNLicensesWithText(3,
"a^b^c^d",
"b^c^d",
"a^b");
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);
623 CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0);
624 CU_ASSERT_TRUE(match_partialComparator(match2, match1) < 0);
625 CU_ASSERT_TRUE(match_partialComparator(match1, match3) > 0);
626 CU_ASSERT_TRUE(match_partialComparator(match3, match1) < 0);
627 CU_ASSERT_TRUE(match_partialComparator(match3, match2) < 0);
628 CU_ASSERT_TRUE(match_partialComparator(match2, match3) > 0);
633 licenses_free(licenses);
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},
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>....
Store the results of a regex match.