FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_diff.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2014 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 <glib.h>
12 #include <stdarg.h>
13 
14 #include "hash.h"
15 #include "string_operations.h"
16 #include "diff.h"
17 #include "match.h"
18 #include "monk.h"
19 
20 int token_search_diff(char* text, char* search, unsigned int maxAllowedDiff,
21  size_t expectedMatchCount, size_t expectedAdditionsCount, size_t expectedRemovalsCount, ...) {
22 
23  int result;
24  result = 0;
25 
26  char* textCopy = g_strdup(text);
27  char* searchCopy = g_strdup(search);
28 
29  GArray* tokenizedText = tokenize(textCopy, "^");
30  GArray* tokenizedSearch = tokenize(searchCopy, "^");
31 
32  GArray* matched = g_array_new(TRUE, FALSE, sizeof (size_t));
33  GArray* additions = g_array_new(TRUE, FALSE, sizeof (size_t));
34  GArray* removals = g_array_new(TRUE, FALSE, sizeof (size_t));
35 
36  size_t textStartPosition = 0;
37  DiffResult* diffResult = findMatchAsDiffs(tokenizedText, tokenizedSearch, textStartPosition, 0, maxAllowedDiff, 1);
38  if(expectedAdditionsCount + expectedMatchCount + expectedRemovalsCount == 0) {
39  CU_ASSERT_PTR_NULL(diffResult);
40  result = diffResult != NULL;
41  goto end;
42  } else {
43  CU_ASSERT_PTR_NOT_NULL(diffResult);
44  if (!diffResult) {
45  printf("null result unexpected\n");
46  result = 0;
47  goto end;
48  }
49  }
50  GArray* matchedInfo = diffResult->matchedInfo;
51 
52  size_t matchedCount = 0;
53  size_t additionCount = 0;
54  size_t removalCount = 0;
55 
56  for (size_t i=0; i<matchedInfo->len; i++) {
57  DiffMatchInfo diffInfo = g_array_index(matchedInfo, DiffMatchInfo, i);
58  if (strcmp(diffInfo.diffType, DIFF_TYPE_ADDITION) == 0) {
59  additionCount += diffInfo.text.length;
60  for (size_t j=diffInfo.text.start;
61  j<diffInfo.text.start + diffInfo.text.length;
62  j++)
63  g_array_append_val(additions, j);
64  }
65  if (strcmp(diffInfo.diffType, DIFF_TYPE_MATCH) == 0) {
66  for (size_t j=diffInfo.text.start;
67  j<diffInfo.text.start + diffInfo.text.length;
68  j++)
69  g_array_append_val(matched, j);
70  matchedCount += diffInfo.text.length;
71  }
72  if (strcmp(diffInfo.diffType, DIFF_TYPE_REMOVAL) == 0) {
73  g_array_append_val(removals, diffInfo.text.start);
74  removalCount ++;
75  }
76  if (strcmp(diffInfo.diffType, DIFF_TYPE_REPLACE) == 0) {
77  additionCount += diffInfo.text.length;
78  for (size_t j=diffInfo.text.start;
79  j<diffInfo.text.start + diffInfo.text.length;
80  j++)
81  g_array_append_val(additions, j);
82  removalCount++;
83  g_array_append_val(removals, diffInfo.text.start);
84  }
85  }
86  CU_ASSERT_EQUAL(matchedCount, expectedMatchCount);
87  CU_ASSERT_EQUAL(additionCount, expectedAdditionsCount);
88  CU_ASSERT_EQUAL(removalCount, expectedRemovalsCount);
89  CU_ASSERT_EQUAL(matched->len, expectedMatchCount);
90  CU_ASSERT_EQUAL(additions->len, expectedAdditionsCount);
91  CU_ASSERT_EQUAL(removals->len, expectedRemovalsCount);
92 
93  va_list argptr;
94  va_start(argptr, expectedRemovalsCount);
95 
96  if (expectedMatchCount == matchedCount) {
97  if (expectedAdditionsCount == additionCount) {
98  if (expectedRemovalsCount == removalCount) {
99  size_t i;
100  size_t actual;
101  size_t expected;
102  for (i = 0; i < expectedMatchCount; i++) {
103  expected = va_arg(argptr, int);
104  actual = g_array_index(matched, size_t, i);
105  CU_ASSERT_EQUAL(actual, expected);
106  if (actual != expected) {
107  printf("ASSERT ERROR: findMatchAsDiffs(\"%s\", \"%s\"):\n", text, search);
108  printf("matched[%ld] == %ld != %ld\n", i, actual, expected);
109  goto end;
110  }
111  }
112  for (i = 0; i < expectedAdditionsCount; i++) {
113  expected = va_arg(argptr, int);
114  actual = g_array_index(additions, size_t, i);
115  CU_ASSERT_EQUAL(actual, expected);
116  if (actual != expected) {
117  printf("ASSERT ERROR: findMatchAsDiffs(\"%s\", \"%s\"):\n", text, search);
118  printf("additions[%ld] == %ld != %ld\n", i, actual, expected);
119  goto end;
120  }
121  }
122  for (i = 0; i < expectedRemovalsCount; i++) {
123  expected = va_arg(argptr, int);
124  actual = g_array_index(removals, size_t, i);
125  CU_ASSERT_EQUAL(actual, expected);
126  if (actual != expected) {
127  printf("ASSERT ERROR: findMatchAsDiffs(\"%s\", \"%s\"):\n", text, search);
128  printf("removals[%ld] == %ld != %ld\n", i, actual, expected);
129  goto end;
130  }
131  }
132  } else
133  printf("ASSERT ERROR: findMatchAsDiffs(\"%s\", \"%s\"): removals(%zu) != expected(%ld)\n",
134  text, search, removalCount, expectedRemovalsCount);
135  } else
136  printf("ASSERT ERROR: findMatchAsDiffs(\"%s\", \"%s\"): additions(%zu) != expected(%ld)\n",
137  text, search, additionCount, expectedAdditionsCount);
138  } else
139  printf("ASSERT ERROR: findMatchAsDiffs(\"%s\", \"%s\"): matched(%zu) != expected(%ld)\n",
140  text, search, matchedCount, expectedMatchCount);
141 
142  result = 1;
143 end:
144 
145  va_end(argptr);
146 
147  if (diffResult)
148  diffResult_free(diffResult);
149  g_array_free(matched, TRUE);
150  g_array_free(additions, TRUE);
151  g_array_free(removals, TRUE);
152 
153  g_array_free(tokenizedText, TRUE);
154  g_array_free(tokenizedSearch, TRUE);
155 
156  g_free(textCopy);
157  g_free(searchCopy);
158 
159  return result;
160 }
161 
162 void test_token_search_diffs() {
163  // simple matches
164  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^foo^^bar^", "one",
165  5,
166  1, 0, 0,
167  0));
168  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^foo^^bar^", "bar",
169  5,
170  1, 0, 0,
171  4));
172  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^foo^^bar^", "two",
173  5,
174  1, 0, 0,
175  1));
176  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^foo^^bar^", "3^foo",
177  5,
178  2, 0, 0,
179  2, 3));
180  CU_ASSERT_FALSE(token_search_diff("^one^^two^^3^^foo^^bar^", "",
181  5,
182  0, 0, 0));
183 
184  // not matches
185  CU_ASSERT_FALSE(token_search_diff("^one^^two^^3^^foo^^bar^", "one^^foo^^bar^^3",
186  2,
187  0, 0, 0));
188  CU_ASSERT_FALSE(token_search_diff("^one^^two^^3^^bar", "one^^3^3^^bar^^z^d^5",
189  2,
190  0, 0, 0));
191  CU_ASSERT_FALSE(token_search_diff("one^two^^three^^bas", "one^^3^3^^bar^^z",
192  2,
193  0, 0, 0));
194 
195  // simple additions
196  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^foo^^bar^", "one^^foo^^bar",
197  5,
198  3, 2, 0,
199  0, 3, 4, // matched
200  1, 2 // additions
201  ));
202 
203  // simple removals
204  CU_ASSERT_TRUE(token_search_diff("^one^^3^^bar^z", "one^^3^3^^5^^bar^^y^^z",
205  5,
206  4, 0, 2,
207  0, 1, 2, 3, // matched
208  2, 3 // removals
209  ));
210 
211  // mixed additions and removals
212  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^bar^z", "one^^3^3^^bar^^z",
213  5,
214  4, 1, 1,
215  0, 2, 3, 4, // matched
216  1, // additions
217  3 // removals
218  ));
219 
220  CU_ASSERT_TRUE(token_search_diff("^one^^two^^1^2^3^4^5^z", "one^^1^2^3^4^5^4^z",
221  5,
222  7, 1, 1,
223  0, 2, 3, 4, 5, 6, 7, // matched
224  1, // additions
225  7 // removals
226  ));
227 
228  CU_ASSERT_TRUE(token_search_diff("^one^^3^^bar^5^e", "one^^3^bar^^4^e",
229  5,
230  4, 1, 1,
231  0, 1, 2, 4, // matched
232  3, // additions
233  3 // removals
234  ));
235 
236  CU_ASSERT_TRUE(token_search_diff("^one^^3^^bar^5^z", "one^^3^bar^^4^a^^z",
237  5,
238  4, 1, 1,
239  0, 1, 2, 4, // matched
240  3, // additions
241  3 // removals
242  ));
243 
244  CU_ASSERT_TRUE(token_search_diff("^one^^3^^bar^5^6^z", "one^^3^bar^^4^^z",
245  5,
246  4, 2, 1,
247  0, 1, 2, 5, // matched
248  3, 4, // additions
249  3 // removals
250  ));
251 
252  CU_ASSERT_TRUE(token_search_diff("^one^^two^^3^^bar", "one^^3^3^^bar^^z",
253  2,
254  3, 1, 2,
255  0, 2, 3, // matched
256  1, // additions
257  3, 4 // removals
258  ));
259 
260  // simple replace
261  CU_ASSERT_TRUE(token_search_diff("^foo^^one^two^three^^bar", "foo^1^two^three^bar",
262  5,
263  4, 1, 1,
264  0, 2, 3, 4, // matched
265  1, 1, // additions
266  1 // removals
267  ));
268 
269  CU_ASSERT_TRUE(token_search_diff("^foo^^one^bar^two^three^^bar", "foo^1^two^three^bar",
270  5,
271  4, 2, 1,
272  0, 3, 4, 5, // matched
273  1, 2, // additions
274  1 // removals
275  ));
276 
277  CU_ASSERT_TRUE(token_search_diff("^foo^^one^two^three^^bar", "foo^1^2^two^three^bar",
278  5,
279  4, 1, 1,
280  0, 2, 3, 4, // matched
281  1, // additions
282  1 // removals
283  ));
284 }
285 
286 int token_search(char* text, char* search, size_t expectedStart) {
287  char* textCopy = g_strdup(text);
288  char* searchCopy = g_strdup(search);
289 
290  GArray* tokenizedText = tokenize(textCopy, "^");
291  GArray* tokenizedSearch = tokenize(searchCopy, "^");
292 
293  size_t matchStart = 0;
294  size_t textStartPosition = 0;
295  DiffResult* diffResult = findMatchAsDiffs(tokenizedText, tokenizedSearch, textStartPosition, 0, 0, 1);
296 
297  int matched = 0;
298 
299  if (diffResult) {
300  matched = diffResult->matchedInfo->len == 1;
301  matchStart = g_array_index(diffResult->matchedInfo, DiffPoint, 0).start;
302  diffResult_free(diffResult);
303  }
304  CU_ASSERT_EQUAL(expectedStart, matchStart);
305 
306  g_array_free(tokenizedText, TRUE);
307  g_array_free(tokenizedSearch, TRUE);
308  g_free(textCopy);
309  g_free(searchCopy);
310 
311  return matched;
312 }
313 
314 void test_token_search() {
315  CU_ASSERT_TRUE(token_search("^one^^two^^3^^foo^^bar^", "one", 0));
316  CU_ASSERT_TRUE(token_search("^one^^two^^3^^foo^^bar^", "bar", 4));
317  CU_ASSERT_TRUE(token_search("^one^^two^^3^^foo^^bar^", "two", 1));
318  CU_ASSERT_TRUE(token_search("^one^^two^^3^^foo^^bar^", "3^foo", 2));
319 
320  CU_ASSERT_FALSE(token_search("^^", "one", 0));
321  CU_ASSERT_FALSE(token_search("^one^", "^^", 0));
322 
323  CU_ASSERT_FALSE(token_search("^one^^two^^3^^foo^^bar^", "3^^foo^two", 0));
324 
325  CU_ASSERT_FALSE(token_search("^3^one^^two^^3^^foo^^bar^", "3^^foo", 0));
326  CU_ASSERT_TRUE(token_search("one^^two^^3^^foo^^bar^", "3^^foo", 2));
327 }
328 
329 void test_matchNTokens(){
330  char* text = g_strdup("a.b.c.d.e.f.g");
331  char* search = g_strdup("a.b.c.d.E.E.f.g");
332 
333  GArray* textTokens = tokenize(text,".");
334  GArray* searchTokens = tokenize(search,".");
335 
336  CU_ASSERT_TRUE(matchNTokens(textTokens, 1, textTokens->len,
337  searchTokens, 1, searchTokens->len,
338  2));
339 
340  CU_ASSERT_TRUE(matchNTokens(textTokens, 5, textTokens->len,
341  searchTokens, 6, searchTokens->len,
342  1));
343 
344  CU_ASSERT_FALSE(matchNTokens(textTokens, 1, textTokens->len,
345  searchTokens, 1, searchTokens->len,
346  7));
347 
348  g_array_free(textTokens, TRUE);
349  g_array_free(searchTokens, TRUE);
350  g_free(text);
351  g_free(search);
352 }
353 
354 void test_matchNTokensCorners(){
355  char* empty = g_strdup("....");
356  char* search = g_strdup("a.b.c.d.E.E.f.g");
357 
358  GArray* emptyTokens = tokenize(empty,".");
359  GArray* secondTokens = tokenize(search,".");
360 
361  CU_ASSERT_FALSE(matchNTokens(emptyTokens, 0, emptyTokens->len,
362  secondTokens, 1, secondTokens->len,
363  2));
364 
365  CU_ASSERT_FALSE(matchNTokens(emptyTokens, 5, emptyTokens->len,
366  secondTokens, 6, secondTokens->len,
367  1));
368 
369  CU_ASSERT_FALSE(matchNTokens(secondTokens, 0, secondTokens->len,
370  emptyTokens, 0, emptyTokens->len,
371  1));
372 
373  g_array_free(emptyTokens, TRUE);
374  g_array_free(secondTokens, TRUE);
375  g_free(empty);
376  g_free(search);
377 }
378 
379 int _test_lookForAdditions(char* text, char* search,
380  int textPosition, int searchPosition, int maxAllowedDiff, int minTrailingMatches,
381  size_t expectedTextPosition, size_t expectedSearchPosition) {
382  char* testText = g_strdup(text);
383  char* testSearch = g_strdup(search);
384 
385  GArray* textTokens = tokenize(testText, "^");
386  GArray* searchTokens = tokenize(testSearch, "^");
387 
388  DiffMatchInfo result;
389  int ret = lookForDiff(textTokens, searchTokens,
390  textPosition, searchPosition, maxAllowedDiff, minTrailingMatches,
391  &result);
392 
393  if (ret) {
394  if (result.search.start != expectedSearchPosition) {
395  printf("adds(%s,%s): result.search.start == %zu != %ld\n", text, search,
396  result.search.start, expectedSearchPosition);
397  }
398  if (result.text.start != expectedTextPosition) {
399  printf("adds(%s,%s): result.text.start == %zu != %ld\n", text, search,
400  result.text.start, expectedTextPosition);
401  }
402 
403  CU_ASSERT_TRUE(result.search.start == expectedSearchPosition);
404  CU_ASSERT_TRUE(result.text.start == expectedTextPosition);
405  }
406 
407  g_array_free(textTokens, TRUE);
408  g_array_free(searchTokens, TRUE);
409  g_free(testText);
410  g_free(testSearch);
411 
412  return ret;
413 }
414 
415 int _test_lookForRemovals(char* text, char* search,
416  int textPosition, int searchPosition, int maxAllowedDiff, int minTrailingMatches,
417  size_t expectedTextPosition, size_t expectedSearchPosition) {
418  char* testText = g_strdup(text);
419  char* testSearch = g_strdup(search);
420 
421  GArray* textTokens = tokenize(testText, "^");
422  GArray* searchTokens = tokenize(testSearch, "^");
423 
424  DiffMatchInfo result;
425  int ret = lookForDiff(textTokens, searchTokens,
426  textPosition, searchPosition, maxAllowedDiff, minTrailingMatches,
427  &result);
428 
429  if (ret) {
430  if (result.search.start != expectedSearchPosition) {
431  printf("rems(%s,%s): result.search.start == %zu != %ld\n", text, search,
432  result.search.start, expectedSearchPosition);
433  }
434  if (result.text.start != expectedTextPosition) {
435  printf("rems(%s,%s): result.text.start == %zu != %ld\n", text, search,
436  result.text.start, expectedTextPosition);
437  }
438 
439  CU_ASSERT_TRUE(result.search.start == expectedSearchPosition);
440  CU_ASSERT_TRUE(result.text.start == expectedTextPosition);
441  }
442 
443  g_array_free(textTokens, TRUE);
444  g_array_free(searchTokens, TRUE);
445  g_free(testText);
446  g_free(testSearch);
447  return ret;
448 }
449 
450 void test_lookForReplacesNotOverflowing() {
451  int max = MAX_ALLOWED_DIFF_LENGTH+1;
452  int length = max + 1;
453  char* testText = malloc((length)*2+1);
454  char* testSearch = malloc((length)*2+1);
455 
456  char* ptr1 =testSearch;
457  char* ptr2 =testText;
458  for (int i = 0; i<length; i++) {
459  *ptr1='a';
460  *ptr2='b';
461  ptr1++;
462  ptr2++;
463  *ptr1='^';
464  *ptr2='^';
465  ptr1++;
466  ptr2++;
467  }
468  int matchPosition = length;
469  *(testSearch + 2*(matchPosition-1))='m';
470  *(testText + 2)='m';
471  *ptr1 = '\0';
472  *ptr2 = '\0';
473 
474  GArray* textTokens = tokenize(testText, "^");
475  GArray* searchTokens = tokenize(testSearch, "^");
476 
477  DiffMatchInfo result;
478  CU_ASSERT_FALSE(lookForDiff(textTokens, searchTokens,
479  0, 0, max, 1, &result));
480 
481  g_array_free(textTokens, TRUE);
482  g_array_free(searchTokens, TRUE);
483  free(testText);
484  free(testSearch);
485 }
486 
487 int _test_lookForReplaces(char* text, char* search,
488  int textPosition, int searchPosition, int maxAllowedDiff, int minTrailingMatches,
489  size_t expectedTextPosition, size_t expectedSearchPosition) {
490  char* testText = g_strdup(text);
491  char* testSearch = g_strdup(search);
492 
493  GArray* textTokens = tokenize(testText, "^");
494  GArray* searchTokens = tokenize(testSearch, "^");
495 
496  DiffMatchInfo result;
497  int ret = lookForDiff(textTokens, searchTokens,
498  textPosition, searchPosition, maxAllowedDiff, minTrailingMatches, &result);
499 
500  if (ret) {
501  if (result.search.start != expectedSearchPosition) {
502  printf("replS(%s,%s): result.search.start == %zu != %ld\n", text, search,
503  result.search.start, expectedSearchPosition);
504  }
505  if (result.text.start != expectedTextPosition) {
506  printf("replS(%s,%s): result.text.start == %zu != %ld\n", text, search,
507  result.text.start, expectedTextPosition);
508  }
509 
510  CU_ASSERT_TRUE(result.search.start == expectedSearchPosition);
511  CU_ASSERT_TRUE(result.text.start == expectedTextPosition);
512  }
513 
514  g_array_free(textTokens, TRUE);
515  g_array_free(searchTokens, TRUE);
516  g_free(testText);
517  g_free(testSearch);
518 
519  return ret;
520 }
521 
522 void test_lookForAdditions() {
523  CU_ASSERT_TRUE(_test_lookForAdditions(
524  "one^two",
525  "two",
526  0, 0, 5, 1,
527  1, 0));
528 
529  CU_ASSERT_FALSE(_test_lookForAdditions(
530  "one^two^three^four^five",
531  "five",
532  0, 0, 2, 1,
533  0, 0));
534 
535  CU_ASSERT_FALSE(_test_lookForAdditions(
536  "one^two^three^four",
537  "one",
538  1, 0, 6, 1,
539  1, 0));
540 
541  CU_ASSERT_TRUE(_test_lookForAdditions(
542  "1^d^a^test_starts_here^two^three",
543  "v^test_starts_here^^three",
544  4, 2, 5, 1,
545  5, 2));
546 
547  CU_ASSERT_FALSE(_test_lookForAdditions(
548  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
549  "v^test_starts_here^^eight",
550  4, 2, 10, 1,
551  4, 2));
552 
553  CU_ASSERT_FALSE(_test_lookForAdditions(
554  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
555  "v^test_starts_here^^seven",
556  4, 2, 2, 1,
557  4, 2));
558 }
559 
560 void test_lookForRemovals() {
561  CU_ASSERT_TRUE(_test_lookForRemovals(
562  "two",
563  "one^two",
564  0, 0, 5, 1,
565  0, 1));
566 
567  CU_ASSERT_FALSE(_test_lookForRemovals(
568  "five",
569  "one^two^three^four^five",
570  0, 0, 2, 1,
571  0, 0));
572 
573  CU_ASSERT_FALSE(_test_lookForRemovals(
574  "five",
575  "five^two^three^four^five",
576  0, 1, 2, 1,
577  0, 1));
578 
579  CU_ASSERT_TRUE(_test_lookForRemovals(
580  "1^d^a^test_starts_here^three",
581  "v^test_starts_here^two^three",
582  4, 2, 5, 1,
583  4, 3));
584 
585  CU_ASSERT_FALSE(_test_lookForRemovals(
586  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
587  "v^test_starts_here^^eight",
588  4, 2, 10, 1,
589  4, 2));
590 
591  CU_ASSERT_FALSE(_test_lookForRemovals(
592  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
593  "v^test_starts_here^^seven",
594  4, 2, 2, 1,
595  4, 2));
596 }
597 
598 void test_lookForReplaces1() {
599  CU_ASSERT_TRUE(_test_lookForReplaces(
600  "one^two",
601  "eins^two",
602  0, 0, 5, 1,
603  1, 1));
604 
605  CU_ASSERT_TRUE(_test_lookForReplaces(
606  "one^two^three",
607  "eins^three",
608  0, 0, 5, 1,
609  2, 1));
610 
611  CU_ASSERT_FALSE(_test_lookForReplaces(
612  "one^two^three^four^five",
613  "eins^five",
614  0, 0, 2, 1,
615  0, 0));
616 
617  CU_ASSERT_TRUE(_test_lookForReplaces(
618  "1^d^a^test_starts_here^one^three",
619  "v^test_starts_here^two^three",
620  4, 2, 5, 1,
621  5, 3));
622 
623  CU_ASSERT_FALSE(_test_lookForReplaces(
624  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
625  "v^test_starts_here^^eight",
626  4, 2, 10, 1,
627  4, 2));
628 
629  CU_ASSERT_FALSE(_test_lookForReplaces(
630  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
631  "v^test_starts_here^^seven",
632  4, 2, 2, 1,
633  4, 2));
634 
635  CU_ASSERT_TRUE(_test_lookForReplaces(
636  "one^two",
637  "eins^two",
638  0, 0, 5, 1,
639  1, 1));
640 
641  CU_ASSERT_TRUE(_test_lookForReplaces(
642  "one^three",
643  "eins^zwei^three",
644  0, 0, 5, 1,
645  1, 2));
646 
647  CU_ASSERT_FALSE(_test_lookForReplaces(
648  "one^five",
649  "eins^zwei^drei^vier^five",
650  0, 0, 2, 1,
651  0, 0));
652 
653  CU_ASSERT_TRUE(_test_lookForReplaces(
654  "1^d^a^test_starts_here^one^three",
655  "v^test_starts_here^two^three",
656  4, 2, 5, 1,
657  5, 3));
658 
659  CU_ASSERT_FALSE(_test_lookForReplaces(
660  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
661  "v^test_starts_here^^eight",
662  4, 2, 10, 1,
663  4, 2));
664 
665  CU_ASSERT_FALSE(_test_lookForReplaces(
666  "1^d^a^test_starts_here^two^three^four^five^six^seven^",
667  "v^test_starts_here^^seven",
668  4, 2, 2, 1,
669  4, 2));
670 }
671 
672 void test_lookForReplaces2() {
673  //some tests in which replace search order is important
674  CU_ASSERT_TRUE(_test_lookForReplaces(
675  "0^a^a^a^1^2^3^4^1^5",
676  "0^b^b^b^4^1^5",
677  3, 3, 5, 1,
678  4, 5)); // match token is "1"
679  CU_ASSERT_FALSE(token_search_diff(
680  "0^a^a^a^1^2^3^4^1^5",
681  "0^b^b^b^4^1^5",
682  3,
683  0, 0, 0));
684 }
685 
686 CU_TestInfo diff_testcases[] = {
687  {"Testing token search:", test_token_search},
688  {"Testing token diff functions, additions:", test_lookForAdditions},
689  {"Testing token diff functions, removals:", test_lookForRemovals},
690  {"Testing token diff functions, replaces:", test_lookForReplaces1},
691  {"Testing token diff functions, replaces complex cases:", test_lookForReplaces2},
692  {"Testing token diff functions, replaces correctly handles max diff: ", test_lookForReplacesNotOverflowing},
693  {"Testing token diff functions, matchNTokens:", test_matchNTokens},
694  {"Testing token diff functions, matchNTokens corner cases:", test_matchNTokensCorners},
695  {"Testing token search_diffs:", test_token_search_diffs},
696  CU_TEST_INFO_NULL
697 };
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
Definition: libfossagent.c:295
Definition: diff.h:14