FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_highlight.c
1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <CUnit/CUnit.h>
22 
23 #include "highlight.h"
24 #include "string_operations.h"
25 #include "diff.h"
26 
27 void _callConvertToAbsolutePositions(char* text, char* search, GArray* diffMatchInfo) {
28  char* testText = g_strdup(text);
29  char* testSearch = g_strdup(search);
30 
31  GArray* textTokens = tokenize(testText, "." );
32  GArray* searchTokens = tokenize(testSearch, "." );
33 
34  convertToAbsolutePositions(diffMatchInfo, textTokens, searchTokens);
35 
36  tokens_free(textTokens);
37  tokens_free(searchTokens);
38  g_free(testText);
39  g_free(testSearch);
40 }
41 
42 void _appendToDiffMatchInfo(GArray* diffMatchInfo,
43  size_t textPosition, size_t textCount, size_t searchPosition,
44  size_t searchCount) {
45 
46  DiffMatchInfo toAppend;
47  toAppend.diffType = "a";
48  toAppend.search = (DiffPoint){.start = searchPosition, .length = searchCount};
49  toAppend.text = (DiffPoint){.start = textPosition, .length = textCount};
50 
51  g_array_append_val(diffMatchInfo, toAppend);
52 }
53 
54 //TODO move to utils and generalize
55 int _CU_ASSERT_EQUAL(size_t actual, size_t expected, char * error) {
56  CU_ASSERT_EQUAL(actual, expected);
57  if (actual != expected)
58  printf(error, actual, expected);
59  return actual == expected;
60 }
61 
62 void _assertDiffMatchInfo(GArray* diffMatchInfo, GArray* expectedDiffMatchInfo) {
63  CU_ASSERT_EQUAL(diffMatchInfo->len, expectedDiffMatchInfo->len );
64  if (diffMatchInfo->len == expectedDiffMatchInfo->len) {
65  for (size_t i = 0; i < diffMatchInfo->len; i++) {
66  DiffMatchInfo extracted = g_array_index(diffMatchInfo, DiffMatchInfo, i);
67  DiffMatchInfo expected = g_array_index(expectedDiffMatchInfo, DiffMatchInfo, i);
68  _CU_ASSERT_EQUAL(extracted.search.start, expected.search.start, "ss %zu != %zu\n");
69  _CU_ASSERT_EQUAL(extracted.search.length, expected.search.length, "sl %zu != %zu\n");
70  _CU_ASSERT_EQUAL(extracted.text.start, expected.text.start, "ts %zu != %zu\n");
71  _CU_ASSERT_EQUAL(extracted.text.length, expected.text.length, "tl %zu != %zu\n");
72  }
73  }
74 }
75 
76 void test_convertToAbsolute() {
77  GArray* diffMatchInfo = g_array_new(TRUE, FALSE, sizeof(DiffMatchInfo));
78  GArray* expectedDiffMatchInfo = g_array_new(TRUE, FALSE, sizeof(DiffMatchInfo));
79 
80  char* text = "A.a.bcd.e.f.";
81  char* search = "...a.bc.e.f.";
82 
83  _appendToDiffMatchInfo(diffMatchInfo, 1, 3, 1, 2);
84  _appendToDiffMatchInfo(expectedDiffMatchInfo, 2, 7, 5, 4);
85 
86  _appendToDiffMatchInfo(diffMatchInfo, 4, 0, 0, 2);
87  _appendToDiffMatchInfo(expectedDiffMatchInfo, 10, 0, 3, 4);
88 
89  _appendToDiffMatchInfo(diffMatchInfo, 0, 0, 0, 1);
90  _appendToDiffMatchInfo(expectedDiffMatchInfo, 0, 0, 3, 1);
91 
92  _callConvertToAbsolutePositions(text, search, diffMatchInfo);
93 
94  _assertDiffMatchInfo(diffMatchInfo, expectedDiffMatchInfo);
95 
96  g_array_free(diffMatchInfo, TRUE);
97  g_array_free(expectedDiffMatchInfo, TRUE);
98 }
99 
100 void test_getFullHighlightFor() {
101  char* text = g_strdup("...a.aa..b.a.c");
102 
103  GArray* tokens = tokenize(text, ".");
104 
105  DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 3);
106 
107  _CU_ASSERT_EQUAL(fullHighlight.start, 5, "start %zu!=%zu\n");
108  _CU_ASSERT_EQUAL(fullHighlight.length, 7, "length %zu!=%zu\n");
109 
110  g_free(text);
111  tokens_free(tokens);
112 }
113 
114 void test_getFullHighlightFor_2() {
115  char* text = g_strdup("...a.aa..b.a.c");
116 
117  GArray* tokens = tokenize(text, ".");
118 
119  DiffPoint fullHighlight = getFullHighlightFor(tokens, 1, 0);
120 
121  _CU_ASSERT_EQUAL(fullHighlight.start, 5, "start %zu!=%zu\n");
122  _CU_ASSERT_EQUAL(fullHighlight.length, 0, "length %zu!=%zu\n");
123 
124  g_free(text);
125  tokens_free(tokens);
126 }
127 
128 CU_TestInfo highlight_testcases[] = {
129  {"Testing conversion to absolute positions:", test_convertToAbsolute},
130  {"Testing extracting full highlight:", test_getFullHighlightFor},
131  {"Testing extracting full highlight with empty:", test_getFullHighlightFor_2},
132  CU_TEST_INFO_NULL
133 };
Definition: diff.h:25