20 #include <CUnit/CUnit.h> 25 #include "libfocunit.h" 30 static char*
const testFileName = (
char*) 0x34;
32 File* getFileWithText(
const char* text) {
33 char* fileText = g_strdup(text);
37 result->tokens = tokenize(fileText,
"^");
38 result->fileName = testFileName;
44 Licenses* getNLicensesWithText(
int count, ...) {
45 GArray* licenseArray = g_array_new(TRUE, FALSE,
sizeof(
License));
47 va_start(texts, count);
48 for (
int i = 0; i < count; i++) {
49 char* text = g_strdup(va_arg(texts,
char*));
52 license.shortname = g_strdup_printf(
"%d-testLic", i);
53 license.tokens = tokenize(text,
"^" );
55 g_array_append_val(licenseArray, license);
60 return buildLicenseIndexes(licenseArray, 1, 0);
63 void file_free(
File* file) {
64 g_array_free(file->tokens, TRUE);
68 void matchesArray_free(GArray* matches) {
69 for (guint i = 0; i < matches->len; i++) {
73 g_array_free(matches, TRUE);
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);
81 return ( (match_getStart(match) == start) &&
82 (match_getEnd(match) == end) &&
83 (match->license->refId == refId) );
86 void test_findAllMatchesDisjoint() {
87 File* file = getFileWithText(
"^e^a^b^c^d^e");
88 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
90 GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
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))
99 matchesArray_free(matches);
101 licenses_free(licenses);
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);
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))
118 matchesArray_free(matches);
120 licenses_free(licenses);
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);
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))
134 matchesArray_free(matches);
136 licenses_free(licenses);
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);
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))
150 matchesArray_free(matches);
152 licenses_free(licenses);
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);
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))
167 matchesArray_free(matches);
169 licenses_free(licenses);
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);
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))
182 matchesArray_free(matches);
184 licenses_free(licenses);
187 void test_formatMatchArray() {
190 .text = (
DiffPoint) { .start = 1, .length = 2 },
191 .search = (
DiffPoint) { .start = 3, .length = 4 },
195 .text = (
DiffPoint) { .start = 1, .length = 0 },
196 .search = (
DiffPoint) { .start = 3, .length = 4 },
200 .text = (
DiffPoint) { .start = 2, .length = 2 },
201 .search = (
DiffPoint) { .start = 3, .length = 0 },
205 .text = (
DiffPoint) { .start = 4, .length = 0 },
206 .search = (
DiffPoint) { .start = 3, .length = 0 },
210 GArray* matchInfo = g_array_new(TRUE, FALSE,
sizeof(
DiffMatchInfo));
212 g_array_append_val(matchInfo, diff1);
213 result = formatMatchArray(matchInfo);
214 FO_ASSERT_STRING_EQUAL(result,
"t[1+2] a s[3+4]");
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]");
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]");
228 g_array_free(matchInfo, TRUE);
233 Match* _matchWithARank(
int type,
double rank) {
234 Match* result = malloc(
sizeof(Match));
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));
242 result->ptr.full = malloc(
sizeof(
DiffPoint));
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;
256 matchInfo.diffType = NULL;
257 matchInfo.text.start =
start;
258 matchInfo.text.length = end -
start;
260 g_array_append_val(result->ptr.diff->matchedInfo, matchInfo);
262 result->license = license;
266 void test_filterMatches() {
267 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(Match*));
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);
274 g_array_append_val(matches, match1);
275 g_array_append_val(matches, match2);
277 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
285 licenses_free(licenses);
288 void test_filterMatchesEmpty() {
289 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(Match*));
291 GArray* filteredMatches = filterNonOverlappingMatches(matches);
293 FO_ASSERT_EQUAL(filteredMatches->len, 0);
295 matchesArray_free(filteredMatches);
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);
305 g_array_append_val(matches, match1);
306 g_array_append_val(matches, match2);
308 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
316 licenses_free(licenses);
319 void test_filterMatchesWithTwoGroups() {
320 GArray* matches = g_array_new(TRUE, FALSE,
sizeof(Match*));
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);
328 g_array_append_val(matches, match1);
329 g_array_append_val(matches, match2);
330 g_array_append_val(matches, match3);
332 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
340 licenses_free(licenses);
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);
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);
357 GArray* filteredMatches = filterNonOverlappingMatches(matches);
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);
365 licenses_free(licenses);
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);
381 CU_ASSERT_EQUAL(state, testState);
382 CU_ASSERT_EQUAL(file->fileName, testFileName);
383 FO_ASSERT_TRUE(expectOnNo);
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);
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);
410 CU_ASSERT_EQUAL(state, testState);
411 CU_ASSERT_EQUAL(file->fileName, testFileName);
421 File* file = getFileWithText(
"^e^a^b^c^d^e");
422 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"d");
424 GArray* matches = findAllMatchesBetween(file, licenses, 20, 1, 0);
426 processMatches(testState, file, matches, expectedCallbacks);
428 matchesArray_free(matches);
430 licenses_free(licenses);
433 void test_processMatchesIgnores() {
445 .onBeginOutput = noop,
446 .onBetweenIndividualOutputs = noop,
450 doProcessTest(&expectedCallbacks);
453 void test_processMatchesUsesOnAllIfDefined() {
465 .onBeginOutput = noop,
466 .onBetweenIndividualOutputs = noop,
470 doProcessTest(&expectedCallbacks);
473 void test_processMatchesUsesOnFullIfOnAllNotDefined() {
484 .onBeginOutput = noop,
485 .onBetweenIndividualOutputs = noop,
489 doProcessTest(&expectedCallbacks);
492 void test_processMatchesUsesOnNoOnNoMatches() {
503 .onBeginOutput = noop,
504 .onBetweenIndividualOutputs = noop,
508 GArray* matches = g_array_new(FALSE, FALSE, 1);
510 File* file = getFileWithText(
"^e^a^b^c^d^e");
511 processMatches(testState, file, matches, &expectedCallbacks);
514 g_array_free(matches, TRUE);
517 void test_processMatchesUsesOnAllForNoMatches() {
529 .onBeginOutput = noop,
530 .onBetweenIndividualOutputs = noop,
534 GArray* matches = g_array_new(FALSE, FALSE, 1);
536 File* file = getFileWithText(
"^e^a^b^c^d^e");
537 processMatches(testState, file, matches, &expectedCallbacks);
540 g_array_free(matches, TRUE);
543 void test_matchComparatorSameLicenseFullVsDiff() {
544 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
546 Match* match1 = _matchWithARankStartAndEnd(MATCH_TYPE_FULL, 100.0, 1, 2, licensePtr);
547 Match* match2 = _matchWithARankStartAndEnd(MATCH_TYPE_DIFF, 100.0, 1, 2, licensePtr);
549 CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0);
550 CU_ASSERT_TRUE(match_partialComparator(match2, match1) < 0);
554 licenses_free(licenses);
557 void test_matchComparatorDifferentLicensesNonIncluded() {
558 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
563 CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0);
564 CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
568 licenses_free(licenses);
571 void test_matchComparatorDifferentLicensesIncluded() {
572 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
578 CU_ASSERT_TRUE(match_partialComparator(match1, match2) < 0);
579 CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
580 CU_ASSERT_TRUE(match_partialComparator(match1, match3) < 0);
581 CU_ASSERT_TRUE(match_partialComparator(match3, match1) > 0);
582 CU_ASSERT_TRUE(match_partialComparator(match2, match3) > 0);
583 CU_ASSERT_TRUE(match_partialComparator(match3, match2) > 0);
588 licenses_free(licenses);
591 void test_matchComparatorIncludedSameLicenseNotComparable() {
592 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
597 CU_ASSERT_TRUE(match_partialComparator(match1, match2) == 0);
598 CU_ASSERT_TRUE(match_partialComparator(match2, match1) == 0);
602 licenses_free(licenses);
605 void test_matchComparatorIncludedSameLicenseComparedByRank() {
606 Licenses* licenses = getNLicensesWithText(3,
"a",
"b^c",
"a^b");
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);
612 CU_ASSERT_TRUE(match_partialComparator(match1, match2) < 0);
613 CU_ASSERT_TRUE(match_partialComparator(match2, match1) > 0);
614 CU_ASSERT_TRUE(match_partialComparator(match3, match1) > 0);
615 CU_ASSERT_TRUE(match_partialComparator(match1, match3) < 0);
616 CU_ASSERT_TRUE(match_partialComparator(match3, match2) > 0);
617 CU_ASSERT_TRUE(match_partialComparator(match2, match3) < 0);
622 licenses_free(licenses);
625 void test_matchComparatorIncludedSameLicenseBiggerMatch() {
626 Licenses* licenses = getNLicensesWithText(3,
"a^b^c^d",
"b^c^d",
"a^b");
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);
632 CU_ASSERT_TRUE(match_partialComparator(match1, match2) > 0);
633 CU_ASSERT_TRUE(match_partialComparator(match2, match1) < 0);
634 CU_ASSERT_TRUE(match_partialComparator(match1, match3) > 0);
635 CU_ASSERT_TRUE(match_partialComparator(match3, match1) < 0);
636 CU_ASSERT_TRUE(match_partialComparator(match3, match2) < 0);
637 CU_ASSERT_TRUE(match_partialComparator(match2, match3) > 0);
642 licenses_free(licenses);
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},
Store the results of a regex match.
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