GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/StringUtils/TESTS/TEST_STRING_FUNCTION/main.cpp Lines: 163 163 100.0 %
Date: 2023-10-11 10:52:07 Branches: 322 330 97.6 %

Line Branch Exec Source
1
2
/***************************************
3
	Auteur : Pierre Aubert
4
	Mail : pierre.aubert@lapp.in2p3.fr
5
	Licence : CeCILL-C
6
****************************************/
7
8
#include <iostream>
9
#include <vector>
10
#include "phoenix_check.h"
11
12
#include "string_function.h"
13
14
///Check string lower expression
15
/**	@param testName : name of the test
16
 * 	@param strValue : string to be tested
17
 * 	@param strReference : reference string
18
 * 	@return true is both strings are equal, false otherwise
19
*/
20
12
bool checkString(const std::string & testName, const std::string & strValue, const std::string & strReference){
21
12
	return phoenix_check(testName, strValue, strReference);
22
}
23
24
///Test the find in list string filename function
25
/**	@return true on success, false otherwise
26
*/
27
1
bool testFindInListString(){
28
1
	bool b(true);
29
30
2
	std::list<std::string> listStrRef;
31
1
	b &= !findInListString(listStrRef, "");
32
1
	listStrRef.push_back("one");
33
1
	b &= !findInListString(listStrRef, "");
34
1
	b &= !findInListString(listStrRef, "other");
35
1
	b &= findInListString(listStrRef, "one");
36
1
	listStrRef.push_back("two");
37
1
	b &= !findInListString(listStrRef, "other");
38
1
	b &= findInListString(listStrRef, "two");
39
1
	listStrRef.push_back("three");
40
1
	b &= !findInListString(listStrRef, "other");
41
1
	b &= findInListString(listStrRef, "two");
42
1
	b &= findInListString(listStrRef, "three");
43
44
1
	std::vector<std::string> vecStrRef;
45
1
	b &= !findInVectorString(vecStrRef, "");
46
1
	vecStrRef.push_back("one");
47
1
	b &= !findInVectorString(vecStrRef, "");
48
1
	b &= !findInVectorString(vecStrRef, "other");
49
1
	b &= findInVectorString(vecStrRef, "one");
50
1
	vecStrRef.push_back("two");
51
1
	b &= !findInVectorString(vecStrRef, "other");
52
1
	b &= findInVectorString(vecStrRef, "two");
53
1
	vecStrRef.push_back("three");
54
1
	b &= !findInVectorString(vecStrRef, "other");
55
1
	b &= findInVectorString(vecStrRef, "two");
56
1
	b &= findInVectorString(vecStrRef, "three");
57
58
1
	b &= eraseLastCharsInStr("", "afe") == "";
59
60

1
	b &= replaceStrInStr("", "", "") == "";
61

1
	b &= replaceStrInStr("", "o", "") == "";
62

1
	b &= replaceStrInStr("o", "", "") == "";
63
64
1
	phoenix_functionOk("testFindInListString", b);
65
2
	return b;
66
}
67
68
///Test the string filename function
69
/**	@return true on success, false otherwise
70
*/
71
1
bool testStringFunction(){
72
1
	bool b(true);
73
2
	std::string testString("some string to be used");
74
75

1
	b &= checkString("Test replaceCharInStr", replaceCharInStr(testString, 'o', "O"), "sOme string tO be used");
76

1
	b &= checkString("Test replaceCharsInStr", replaceCharsInStr(testString, "ot", 'X'), "sXme sXring XX be used");
77

1
	b &= checkString("Test replaceStrInStr", replaceStrInStr(testString, "string", "text"), "some text to be used");
78

1
	b &= checkString("Test copyStr", copyStr("Some string", 2lu, 4lu), "me s");
79
80

1
	b &= checkString("Test eraseCharInStr", eraseCharInStr(testString, 'o'), "sme string t be used");
81

1
	b &= checkString("Test eraseCharInStr", eraseCharInStr("", 'o'), "");
82

1
	b &= checkString("Test eraseCharsInStr", eraseCharsInStr(testString, "o"), "sme string t be used");
83

1
	b &= checkString("Test eraseCharsInStr", eraseCharsInStr(testString, "oxi"), "sme strng t be used");
84

1
	b &= checkString("Test eraseCharsInStr", eraseCharsInStr("", "o"), "");
85

1
	b &= checkString("Test replaceCharsInStr", replaceCharsInStr("", "o", 'd'), "");
86

1
	b &= checkString("Test replaceCharsInStr", replaceCharsInStr("", "", 'd'), "");
87

1
	b &= checkString("Test replaceCharsInStr", replaceCharsInStr("o", "", 'd'), "o");
88
89
2
	std::vector<std::string> vecStrRef;
90
1
	vecStrRef.push_back("one");
91
1
	vecStrRef.push_back("two");
92
1
	vecStrRef.push_back("three");
93
94
3
	std::list<std::string> listStr(cutStringList("one two three", ' '));
95
3
	std::vector<std::string> vecStr(cutStringVector("one two three", ' '));
96
97
3
	std::list<std::string> listSpaceStr(cutStringOnSpacesList("one two three"));
98
2
	std::vector<std::string> vecSpaceStr(cutStringOnSpacesVector("one two three"));
99
100
1
	std::list<std::string>::iterator itList(listStr.begin());
101
1
	std::list<std::string>::iterator itSpaceList(listSpaceStr.begin());
102
1
	std::vector<std::string>::iterator itVec(vecStr.begin());
103
1
	std::vector<std::string>::iterator itSpaceVec(vecSpaceStr.begin());
104
1
	std::vector<std::string>::iterator itVecRef(vecStrRef.begin());
105
106



4
	while(itList != listStr.end() && itVec != vecStr.end() && itVecRef != vecStrRef.end() && itSpaceList != listSpaceStr.end() && itSpaceVec != vecSpaceStr.end()){
107

3
		b &= *itList == *itVec && *itVec ==* itVecRef;
108

3
		b &= *itSpaceList == *itVec && *itSpaceVec == *itVec;
109
110
3
		++itList;
111
3
		++itVec;
112
3
		++itVecRef;
113
3
		++itSpaceList;
114
3
		++itSpaceVec;
115
	}
116
117
1
	b &= !findInString("", 'a');
118
1
	b &= findInString("abc", 'a');
119
1
	b &= findInString("abc", 'b');
120
1
	b &= findInString("abc", 'c');
121
1
	b &= !findInString("abc", 'd');
122
123
1
	b &= !findCharsInString("", "");
124
1
	b &= !findCharsInString("", "a");
125
1
	b &= !findCharsInString("abc", "");
126
127
1
	b &= findCharsInString("abc", "a");
128
1
	b &= findCharsInString("abc", "b");
129
1
	b &= findCharsInString("abc", "c");
130
1
	b &= !findCharsInString("abc", "d");
131
132
1
	b &= findCharsInString("abc", "xa");
133
1
	b &= findCharsInString("abc", "xb");
134
1
	b &= findCharsInString("abc", "xc");
135
1
	b &= !findCharsInString("abc", "xd");
136
137
1
	b &= findCharsInString("abc", "ax");
138
1
	b &= findCharsInString("abc", "bx");
139
1
	b &= findCharsInString("abc", "cx");
140
1
	b &= !findCharsInString("abc", "dx");
141
142
1
	b &= countNbChar("", 'o') == 0lu;
143
1
	b &= countNbChar("some thing to count", 'o') == 3lu;
144
1
	b &= countNbChar("some thing to count", 'w') == 0lu;
145
146
1
	b &= isSameBegining("", "");
147
1
	b &= !isSameBegining("", "d");
148
1
	b &= isSameBegining("start", "start");
149
1
	b &= isSameBegining("start", "st");
150
1
	b &= !isSameBegining("st", "start");
151
152
153
1
	phoenix_functionOk("testStringFunction", b);
154
2
	return b;
155
}
156
157
///Check the replaceVectorStrInStr function
158
/**	@param inputStr : input string
159
 * 	@param vecPattern : vector of patterns
160
 * 	@param replace : substitution string
161
 * 	@param strReference : expected result
162
 * 	@return true on success, false otherwise
163
*/
164
5
bool checkReplaceVectorStrInStr(const std::string & inputStr, const std::vector<std::string> & vecPattern, const std::string & replace,
165
				const std::string & strReference)
166
{
167
5
	std::string res(replaceVectorStrInStr(inputStr, vecPattern, replace));
168
10
	return phoenix_check("", res, strReference);
169
}
170
171
///Test the replaceVectorStrInStr function
172
/**	@return true on success, false otherwise
173
*/
174
1
bool testReplaceVectorStrInStr(){
175
1
	bool b(true);
176
1
	std::vector<std::string> vecStrPattern;
177

1
	b &= checkReplaceVectorStrInStr("", vecStrPattern, "", "");
178

1
	b &= checkReplaceVectorStrInStr("something", vecStrPattern, "", "something");
179
1
	vecStrPattern.push_back("key");
180

1
	b &= checkReplaceVectorStrInStr("", vecStrPattern, "", "");
181

1
	b &= checkReplaceVectorStrInStr("convert value to key", vecStrPattern, "value", "convert value to value");
182
1
	vecStrPattern.push_back("value");
183

1
	b &= checkReplaceVectorStrInStr("convert value to key", vecStrPattern, "thing", "convert thing to thing");
184
185
1
	phoenix_functionOk("testReplaceVectorStrInStr", b);
186
2
	return b;
187
}
188
189
///Check the replaceVectorStrInStr function
190
/**	@param inputStr : input string
191
 * 	@param vecPattern : list of patterns
192
 * 	@param replace : substitution string
193
 * 	@param strReference : expected result
194
 * 	@return true on success, false otherwise
195
*/
196
5
bool checkReplaceListStrInStr(const std::string & inputStr, const std::list<std::string> & vecPattern, const std::string & replace,
197
				const std::string & strReference)
198
{
199
5
	std::string res(replaceListStrInStr(inputStr, vecPattern, replace));
200
10
	return phoenix_check("", res, strReference);
201
}
202
203
///Test the replaceVectorStrInStr function
204
/**	@return true on success, false otherwise
205
*/
206
1
bool testReplaceListStrInStr(){
207
1
	bool b(true);
208
1
	std::list<std::string> vecStrPattern;
209

1
	b &= checkReplaceListStrInStr("", vecStrPattern, "", "");
210

1
	b &= checkReplaceListStrInStr("something", vecStrPattern, "", "something");
211
1
	vecStrPattern.push_back("key");
212

1
	b &= checkReplaceListStrInStr("", vecStrPattern, "", "");
213

1
	b &= checkReplaceListStrInStr("convert value to key", vecStrPattern, "value", "convert value to value");
214
1
	vecStrPattern.push_back("value");
215

1
	b &= checkReplaceListStrInStr("convert value to key", vecStrPattern, "thing", "convert thing to thing");
216
217
1
	phoenix_functionOk("testReplaceListStrInStr", b);
218
2
	return b;
219
}
220
221
///Test the phoenix_charToString function
222
/**	@return true on success, false otherwise
223
*/
224
1
bool testCharToString(){
225
1
	bool b(true);
226
1
	b &= phoenix_charToString("some string") == "some string";
227
1
	b &= phoenix_charToString(NULL) == "";
228
1
	phoenix_functionOk("testCharToString", b);
229
1
	return b;
230
}
231
232
///Test the phoenix_escapeStr function
233
/**	@return true on success, false otherwise
234
*/
235
1
bool testEscapeString(){
236
1
	bool b(true);
237

1
	b &= phoenix_check("", phoenix_escapeStr("some string with escape's \"char\"", " '\"", "\\"), "some\\ string\\ with\\ escape\\'s\\ \\\"char\\\"");
238
1
	phoenix_functionOk("testEscapeString", b);
239
1
	return b;
240
}
241
242
///Test the phoenix_getCommonBegining function
243
/**	@return true on success, false otherwise
244
*/
245
1
bool testGetCommonBegning(){
246
1
	bool b(true);
247
1
	b &= phoenix_getCommonBegining("", "") == "";
248
1
	b &= phoenix_getCommonBegining("someString", "") == "";
249
1
	b &= phoenix_getCommonBegining("", "someString") == "";
250
1
	b &= phoenix_getCommonBegining("someString", "someOtherString") == "some";
251
1
	b &= phoenix_getCommonBegining("someString", "AndsomeOtherString") == "";
252
253
1
	phoenix_functionOk("testGetCommonBegning", b);
254
1
	return b;
255
}
256
257
1
int main(int argc, char** argv){
258
1
	bool b(true);
259
1
	b &= testStringFunction();
260
1
	b &= testFindInListString();
261
1
	b &= testReplaceVectorStrInStr();
262
1
	b &= testReplaceListStrInStr();
263
1
	b &= testCharToString();
264
1
	b &= testEscapeString();
265
1
	b &= testGetCommonBegning();
266
1
	phoenix_functionOk("final", b);
267
1
	return b - 1;
268
}
269
270