File: Substring_test.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (437 lines) | stat: -rw-r--r-- 12,673 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/CONCEPT/classTest.h>
#include <BALLTestConfig.h>

///////////////////////////
#include <BALL/DATATYPE/string.h>
#include <cstring>
#include <string>
///////////////////////////

START_TEST(String)

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

using namespace BALL;
using std::string;

// Substring class....
Substring* ptr = 0;
CHECK(Substring() throw())
	ptr = new Substring;
	TEST_NOT_EQUAL(ptr, 0)
RESULT

CHECK(~Substring() throw())
	delete ptr;
RESULT

CHECK(Substring(const Substring& substring, bool deep = true) throw())
	String s = "abcdef";
	Substring sub(s);
	Substring sub2(sub);
	TEST_EQUAL(sub2, s)
RESULT

const char* char1 = "ABCDEF";
const String abcdef = "abcdef";
const String ABCDEF = "ABCDEF";
Substring empty_sub;
const Substring abcdef_sub(abcdef, 0, 6);
const Substring ABCDEF_sub(ABCDEF, 0, 6);
Substring test_sub1(ABCDEF, 0, 6);
Substring test_sub2;
String test_string;
String s5 = "abcdef";
String s4;

CHECK(Substring(const String& string, Index from = 0, Size len = String::EndPos))
	Substring sub(s5, 1);
	TEST_EQUAL(sub, "bcdef")
	Substring sub2(s5, 2, 1);
	TEST_EQUAL(sub2, "c")
	Substring sub3(s5, 1, 2);
	TEST_EQUAL(sub3, "bc")
	Substring sub4(s5, -2, 1);
	TEST_EQUAL(sub4, "e")
	Substring* sub5;
	TEST_EXCEPTION(Exception::IndexOverflow, sub5 = new Substring(s5, 6); delete sub5)
	TEST_EXCEPTION(Exception::IndexOverflow, sub5 = new Substring(s5, 0, 7); delete sub5)
	TEST_EXCEPTION(Exception::IndexUnderflow, sub5 = new Substring(s5, -10); delete sub5)
RESULT

CHECK(Substring::~Substring())
	Substring* sub = new Substring(s5);
	delete sub;
RESULT

CHECK(BALL_CREATE_DEEP(Substring))
	Substring sub(s5);
	Substring* sub2 = (Substring*)sub.create(true);
	TEST_EQUAL(sub, *sub2)
	delete sub2;
	sub2 = (Substring*)sub.create(false);
	TEST_EQUAL(sub, *sub2)
	delete sub2;
RESULT

CHECK(void destroy() throw())
	String test_destroy("abcdefghij");
	Substring sub(test_destroy, 3, 3);
	TEST_EQUAL(sub.isBound(), true)
	TEST_EQUAL(sub.toString(), "def")
	sub.destroy();
	TEST_EQUAL(sub.isBound(), false)
	TEST_EQUAL(test_destroy, "abcghij")
RESULT

CHECK(operator String() const)
	Substring sub(s5);
	String s6 = (String)sub;
	TEST_EQUAL(s6, s5)
	Substring empty_sub(s5, 0, 0);
	s6 = (String)empty_sub;
	TEST_EQUAL(s6, "")
RESULT

CHECK(String toString() const)
	Substring sub(s5);
	String s6 = sub.toString();
	TEST_EQUAL(s6, s5)
RESULT

CHECK(Substring& bind(const String& string, Index from = 0, Size len = String::EndPos))
	Substring sub;
	sub.bind(ABCDEF, 1, 1);
	TEST_EQUAL(sub, "B")
RESULT

CHECK(Substring& bind(const Substring& substring, Index from = 0, Size len = String::EndPos))
	Substring sub;
	sub.bind(ABCDEF, 1, 1);
	TEST_EQUAL(sub, "B")
RESULT

CHECK(void unbind() throw())
	Substring sub;
	sub.bind(test_sub1, 1, 1);
	sub.unbind();
	TEST_EQUAL(sub.getBoundString(), 0)
RESULT

CHECK(String* getBoundString() throw())
	Substring sub;
	sub.bind(test_sub1, 1, 1);
	TEST_EQUAL(sub.getBoundString(), &ABCDEF)
RESULT

CHECK(const String* getBoundString() const throw())
	String* p = test_sub1.getBoundString();
	TEST_EQUAL(p, &ABCDEF)
RESULT

CHECK(void set(const String& string))
	test_string = "";
	test_sub1.bind(test_string);
	test_sub1.set(ABCDEF);
	TEST_EQUAL(test_string, "ABCDEF")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.set(ABCDEF))	
RESULT

CHECK(void set(const Substring& s))
	test_string = "";
	test_sub1.bind(test_string);
	test_sub2.bind(ABCDEF);
	test_sub1.set(test_sub2);
	TEST_EQUAL(test_string, "ABCDEF")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.set(test_sub1))	
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub1.set(empty_sub))	
RESULT

CHECK(void set(const char* char_ptr, Size size = String::EndPos))
	String s = "AB12CDEF";
	Substring sub(s, 2, 2);
	TEST_EQUAL(sub, "12")
	sub.set("test");
	TEST_EQUAL(s, "ABtestCDEF")
	TEST_EXCEPTION(Exception::NullPointer, test_sub1.set(0))	
	TEST_EXCEPTION(Exception::SizeUnderflow, test_sub1.set("test", 0))	
	TEST_EQUAL(test_string, "ABCDEF")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.set(s))	
RESULT

CHECK(bool operator == (const String& string) const)
	test_string = "";
	test_sub1.bind(test_string);
	test_sub1 = ABCDEF;
	TEST_EQUAL(test_string, "ABCDEF")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub = ABCDEF)	
RESULT

CHECK(const Substring& operator = (const Substring& substring))
	test_string = "";
	test_sub1.bind(test_string);
	test_sub1 = ABCDEF_sub;
	TEST_EQUAL(test_string, "ABCDEF")
	test_sub1.unbind();
	test_sub2.bind(ABCDEF);
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub1 = test_sub2)
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub2 = test_sub1)
	test_sub2.unbind();
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub2 = test_sub1)
RESULT

CHECK(const Substring& operator = (const char* char_ptr))
	test_string = "";
	test_sub1.bind(test_string);
	test_sub1 = char1;
	TEST_EQUAL(test_string, "ABCDEF")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub = char1)	
	TEST_EXCEPTION(Exception::NullPointer, test_sub1 = 0)	
RESULT

CHECK(char* c_str())
	test_string = "abcdef";
	test_sub1.bind(test_string);
	test_sub1.c_str()[0] = 'A';
	TEST_EQUAL(test_string, "Abcdef")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.c_str()[0] = 'A')	
RESULT

CHECK(const char* c_str() const)
	const char*	c1 = ABCDEF_sub.c_str();
	const char*	c2 = "ABCDEF";
	TEST_EQUAL(strcmp(c1, c2), 0)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.c_str())	
RESULT

CHECK(Index getFirstIndex() const)
	String temp = "AAAA";
	test_sub1.bind(temp, 2, 1);
	TEST_EQUAL(test_sub1.getFirstIndex(), 2)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.getFirstIndex())	
RESULT

CHECK(Index getLastIndex() const)
	String temp = "AAAA";
	test_sub1.bind(temp, 1, 2);
	TEST_EQUAL(test_sub1.getLastIndex(), 2)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.getLastIndex())	
RESULT

CHECK(Size size() const throw())
	TEST_EQUAL(ABCDEF_sub.size(), 6)
	TEST_EQUAL(empty_sub.size(), 0)
RESULT

CHECK(char& operator [] (Index index))
	test_string = "ABCDEF";
	test_sub1.bind(test_string);
	test_sub1[0] = 'a';
	test_sub1[5] = 'f';
	TEST_EQUAL(test_string, "aBCDEf")
	test_sub1[-1] = 'F';
	TEST_EQUAL(test_string, "aBCDEF")
	TEST_EXCEPTION(Exception::IndexOverflow, test_sub1[ 6] = 'F')
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub[0] = 'F')	
RESULT

CHECK(char operator [] (Index index) const)
	const char c = abcdef_sub[0];
	TEST_EQUAL(c, 'a')
	TEST_EQUAL(abcdef_sub[-1], 'f')
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub[0])	
RESULT

CHECK(Substring& toLower())
	test_string = abcdef + ABCDEF + "1";
	test_sub1.bind(test_string);
	TEST_EQUAL(test_sub1.toLower(), "abcdefabcdef1")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.toLower())	
RESULT

CHECK(Substring& toUpper())
	test_string = abcdef + ABCDEF + "1";
	test_sub1.bind(test_string);
	TEST_EQUAL(test_sub1.toUpper(), "ABCDEFABCDEF1")
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.toUpper())	
RESULT

CHECK(bool isBound() const throw())
	test_sub1.unbind();
	TEST_EQUAL(test_sub1.isBound(), false)
	test_sub1.bind(ABCDEF, 1, 1);
	TEST_EQUAL(test_sub1.isBound(), true)
RESULT

CHECK(bool isEmpty() const throw())
	test_string = "";
	test_sub1.bind(test_string);
	test_string = "A";
	TEST_EQUAL(test_sub1.isEmpty(), false)
	test_sub1.unbind();
	TEST_EQUAL(test_sub1.isEmpty(), true)
RESULT

CHECK(bool operator == (const Substring& substring) const)
	test_sub1.unbind();
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub1 == test_sub2)	
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub2 == test_sub1)	
	test_sub1.bind(ABCDEF, 0, 5);
	test_sub2.bind(ABCDEF, 0, 5);
	TEST_EQUAL(test_sub1 == test_sub2, true)
	test_sub1.bind(ABCDEF, 0, 3);
	TEST_EQUAL(test_sub1 == test_sub2, false)
	test_string = "XXX";
	test_sub2.bind(test_string);
	TEST_EQUAL(test_sub1 == test_sub2, false)
RESULT

CHECK(bool operator != (const Substring& substring) const)
	test_sub1.unbind();
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub1 != test_sub2)	
	TEST_EXCEPTION(Substring::UnboundSubstring,  test_sub2 != test_sub1)	
	test_sub1.bind(ABCDEF, 0, 5);
	test_sub2.bind(ABCDEF, 0, 5);
	TEST_EQUAL(test_sub1 != test_sub2, false)
	test_sub1.bind(ABCDEF, 0, 3);
	TEST_EQUAL(test_sub1 != test_sub2, true)
	test_string = "XXX";
	test_sub2.bind(test_string);
	TEST_EQUAL(test_sub1 != test_sub2, true)
RESULT

CHECK(const Substring& operator = (const String& string))
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 6);
	TEST_EQUAL(test_sub1 == ABCDEF, true)
	test_string = "ABCDE";
	TEST_EQUAL(test_sub1 == ABCDEF, false)
	test_sub1.unbind();
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub1 == ABCDEF)	
RESULT

CHECK(friend bool operator != (const String& string, const Substring& substring))
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 6);
	TEST_EQUAL(test_sub1 != ABCDEF, false)
	test_string = "ABCDE";
	TEST_EQUAL(test_sub1 != ABCDEF, true)
	test_sub1.unbind();
	TEST_EXCEPTION(Substring::UnboundSubstring, test_sub1 != ABCDEF)	
RESULT

CHECK(friend bool operator == (const String& string, const Substring& substring))
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 6);
	TEST_EQUAL(ABCDEF == test_sub1, true)
	test_string = "ABCDE";
	TEST_EQUAL(ABCDEF == test_sub1, false)
	TEST_EXCEPTION(Substring::UnboundSubstring, ABCDEF == empty_sub)	
RESULT

CHECK(bool operator != (const String& string) const)
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 6);
	TEST_EQUAL(ABCDEF != test_sub1, false)
	test_string = "ABCDE";
	TEST_EQUAL(ABCDEF != test_sub1, true)
	TEST_EXCEPTION(Substring::UnboundSubstring, ABCDEF != empty_sub)	
RESULT

CHECK(bool operator == (const char* char_ptr) const)
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 6);
	TEST_EQUAL(test_sub1 == char1, true)
	test_string = "ABCDE";
	test_sub1.bind(test_string);
	TEST_EQUAL(test_sub1 == char1, false)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub == char1)	
	char* c = 0;
	TEST_EXCEPTION(Exception::NullPointer, test_sub1 == c)	
RESULT

CHECK(bool operator != (const char* char_ptr) const)
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 6);
	TEST_EQUAL(test_sub1 != char1, false)
	test_string = "ABCDE";
	test_sub1.bind(test_string);
	TEST_EQUAL(test_sub1 != char1, true)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub != char1)	
	char* c = 0;
	TEST_EXCEPTION(Exception::NullPointer, test_sub1 != c)	
RESULT

CHECK(bool operator == (char c) const)
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 1);
	char c = 'A';
	TEST_EQUAL(test_sub1 == c, true)
	test_string = "ABCDE";
	test_sub1.bind(test_string);
	TEST_EQUAL(test_sub1 == c, false)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub == c)	
RESULT

CHECK(bool operator != (char c) const)
	test_string = "XABCDEFG";
	test_sub1.bind(test_string, 1, 1);
	char c = 'A';
	TEST_EQUAL(test_sub1 != c, false)
	test_string = "ABCDE";
	test_sub1.bind(test_string);
	TEST_EQUAL(test_sub1 != c, true)
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub != c)	
RESULT

String filename;
using std::ofstream;
using std::ios;

CHECK(friend std::ostream& operator << (std::ostream& s, const Substring& substring) throw())
	NEW_TMP_FILE(filename)
	std::ofstream outstr(filename.c_str(), std::ios::out);
	test_sub1.bind(ABCDEF, 1, 4);
	outstr << test_sub1;
	outstr.close();
	TEST_FILE(filename.c_str(), BALL_TEST_DATA_PATH(Substring_test2.txt))
	std::ofstream outstr2(filename.c_str(), std::ios::out);
	outstr2 << empty_sub;
	outstr2.close();
RESULT

CHECK(bool isValid() const throw())
	TEST_EQUAL(test_sub1.isValid(), true)
	test_sub1.unbind();
	TEST_EQUAL(test_sub1.isValid(), false)
	test_sub1.bind(test_string, -1, 1);
	TEST_EQUAL(test_sub1.isValid(), true)
RESULT

CHECK(void dump(std::ostream& s = std::cout, Size depth = 0) const)
	NEW_TMP_FILE(filename)
	std::ofstream outfile(filename.c_str(), ios::out);
	test_sub1.bind(ABCDEF, 1, 4);
	test_sub1.dump(outfile);
	outfile.close();
	TEST_FILE_REGEXP(filename.c_str(), BALL_TEST_DATA_PATH(Substring_test.txt))
	std::ofstream outfile2(filename.c_str(), ios::out);
	TEST_EXCEPTION(Substring::UnboundSubstring, empty_sub.dump(outfile2))
	outfile2.close();
RESULT

CHECK(void clear() throw())
	Substring sub(s5, 1);
	TEST_EQUAL(sub, "bcdef")
	sub.clear();
	TEST_EQUAL(sub.isBound(), false)
	TEST_EXCEPTION(Substring::UnboundSubstring, sub.c_str())
RESULT
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
END_TEST