FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_highlight.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 <string.h>
11 #include <CUnit/CUnit.h>
12 
13 #include "highlight.h"
14 #include "string_operations.h"
15 #include "diff.h"
16 
17 void _callConvertToAbsolutePositions(char* text, char* search, GArray* diffMatchInfo) {
18  char* testText = g_strdup(text);
19  char* testSearch = g_strdup(search);
20 
21  GArray* textTokens = tokenize(testText, "." );
22  GArray* searchTokens = tokenize(testSearch, "." );
23 
24  convertToAbsolutePositions(diffMatchInfo, textTokens, searchTokens);
25 
26  tokens_free(textTokens);
27  tokens_free(searchTokens);
28  g_free(testText);
29  g_free(testSearch);
30 }
31 
32 void _appendToDiffMatchInfo(GArray* diffMatchInfo,
33  size_t textPosition, size_t textCount, size_t searchPosition,
34  size_t searchCount) {
35 
36  DiffMatchInfo toAppend;
37  toAppend.diffType = "a";
38  toAppend.search = (DiffPoint){.start = searchPosition, .length = searchCount};
39  toAppend.text = (DiffPoint){.start = textPosition, .length = textCount};
40 
41  g_array_append_val(diffMatchInfo, toAppend);
42 }
43 
44 //TODO move to utils and generalize
45 int _CU_ASSERT_EQUAL(size_t actual, size_t expected, char * error) {
46  CU_ASSERT_EQUAL(actual, expected);
47  if (actual != expected)
48  printf(error, actual, expected);
49  return actual == expected;
50 }
51 
52 void _assertDiffMatchInfo(GArray* diffMatchInfo, GArray* expectedDiffMatchInfo) {
53  CU_ASSERT_EQUAL(diffMatchInfo->len, expectedDiffMatchInfo->len );
54  if (diffMatchInfo->len == expectedDiffMatchInfo->len) {
55  for (size_t i = 0; i < diffMatchInfo->len; i++) {
56  DiffMatchInfo extracted = g_array_index(diffMatchInfo, DiffMatchInfo, i);
57  DiffMatchInfo expected = g_array_index(expectedDiffMatchInfo, DiffMatchInfo, i);
58  _CU_ASSERT_EQUAL(extracted.search.start, expected.search.start, "ss %zu != %zu\n");
59  _CU_ASSERT_EQUAL(extracted.search.length, expected.search.length, "sl %zu != %zu\n");
60  _CU_ASSERT_EQUAL(extracted.text.start, expected.text.start, "ts %zu != %zu\n");
61  _CU_ASSERT_EQUAL(extracted.text.length, expected.text.length, "tl %zu != %zu\n");
62  }
63  }
64 }
65 
66 void test_convertToAbsolute() {
67  GArray* diffMatchInfo = g_array_new(TRUE, FALSE, sizeof(DiffMatchInfo));
68  GArray* expectedDiffMatchInfo = g_array_new(TRUE, FALSE, sizeof(DiffMatchInfo));
69 
70  char* text = "A.a.bcd.e.f.";
71  char* search = "...a.bc.e.f.";
72 
73  _appendToDiffMatchInfo(diffMatchInfo, 1, 3, 1, 2);
74  _appendToDiffMatchInfo(expectedDiffMatchInfo, 2, 7, 5, 4);
75 
76  _appendToDiffMatchInfo(diffMatchInfo, 4, 0, 0, 2);
77  _appendToDiffMatchInfo(expectedDiffMatchInfo, 10, 0, 3, 4);
78 
79  _appendToDiffMatchInfo(diffMatchInfo, 0, 0, 0, 1);
80  _appendToDiffMatchInfo(expectedDiffMatchInfo, 0, 0, 3, 1);
81 
82  _callConvertToAbsolutePositions(text, search, diffMatchInfo);
83 
84  _assertDiffMatchInfo(diffMatchInfo, expectedDiffMatchInfo);
85 
86  g_array_free(diffMatchInfo, TRUE);
87  g_array_free(expectedDiffMatchInfo, TRUE);
88 }
89 
90 void test_getFullHighlightFor() {
91  char* text = g_strdup("...a.aa..b.a.c");
92 
93  GArray* tokens = tokenize(text, ".");
94 
95  DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 3);
96 
97  _CU_ASSERT_EQUAL(fullHighlight.start, 5, "start %zu!=%zu\n");
98  _CU_ASSERT_EQUAL(fullHighlight.length, 7, "length %zu!=%zu\n");
99 
100  g_free(text);
101  tokens_free(tokens);
102 }
103 
104 void test_getFullHighlightFor_2() {
105  char* text = g_strdup("...a.aa..b.a.c");
106 
107  GArray* tokens = tokenize(text, ".");
108 
109  DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 0);
110 
111  _CU_ASSERT_EQUAL(fullHighlight.start, 5, "start %zu!=%zu\n");
112  _CU_ASSERT_EQUAL(fullHighlight.length, 0, "length %zu!=%zu\n");
113 
114  g_free(text);
115  tokens_free(tokens);
116 }
117 
118 CU_TestInfo highlight_testcases[] = {
119  {"Testing conversion to absolute positions:", test_convertToAbsolute},
120  {"Testing extracting full highlight:", test_getFullHighlightFor},
121  {"Testing extracting full highlight with empty:", test_getFullHighlightFor_2},
122  CU_TEST_INFO_NULL
123 };
Definition: diff.h:14