forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstring_test.cpp
97 lines (74 loc) · 2.85 KB
/
pstring_test.cpp
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
/*
30 unit tests testing pstring library ( a string library)
located in include/pstring.cpp
include/pstring.h
include/pUnitTest.h
include/pUnitTest.cpp
*/
#include <cstdio>
#include "pUnitTest.h"
#include "pstring.h"
bool summary_flag = false;
unsigned long int tpass = 0, tfail = 0;
using string = algo::String;
int main(int argc, const char * argv[])
{
// Versions n things
printf("String version: %s\n", string::version());
printf("UTest version: %s\n", UTest::version());
UTest u("PstringLib");
u.summary(summary_flag);
// string
printf("\nTesting string -----\n");
u.init("string");
const char * _ctest = " \tfoo \r\n";
string ttest = _ctest;
u.test("cstring ctor", ttest.length() == 12);
string other = std::move(ttest);
u.test("move ctor", other.length() == 12 && ttest.length() == 0);
ttest = std::move(other);
u.test("move assignment", other.length() == 0 && ttest.length() == 12);
other = ttest;
u.test("copy assignment", other.length() == 12 && ttest.length() == 12);
ttest = "this is a string";
u.test("length is 16", ttest.length() == 16 && ttest.size() == 16);
u.test("substr", ttest.substr(10, 3) == string("str"));
u.test("charfind", ttest.find('s') == 3);
u.test("charfind (not found)", ttest.find('z') == -1);
string string_upper = ttest.upper();
string string_lower = string_upper.lower();
u.test("upper and lower", string_upper == string("THIS IS A STRING") && string_lower == string("this is a string"));
string x = "foo";
string y = "bar";
string z = x + "baz" + y;
u.test("concat", z.length() == 9 && memcmp(z.c_str(), "foobazbar", 10) == 0);
x = y = "foo";
u.test("foo == foo", (x == y));
u.test("foo > foo", !(x > y));
u.test("foo >= foo", (x >= y));
u.test("foo < foo", !(x < y));
u.test("foo <= foo", (x <= y));
x = "bar";
u.test("bar == foo", !(x == y));
u.test("bar > foo", !(x > y));
u.test("bar >= foo", !(x >= y));
u.test("bar < foo", (x < y));
u.test("bar <= foo", (x <= y));
u.test("foo == bar", !(y == x));
u.test("foo > bar", (y > x));
u.test("foo >= bar", (y >= x));
u.test("foo < bar", !(y < x));
u.test("foo <= bar", !(y <= x));
u.test("subscript x[0]", x[0] == 'b');
u.test("subscript x[1]", x[1] == 'a');
u.test("subscript x[2]", x[2] == 'r');
u.test("subscript terminator x[3]", x[3] == 0);
x = "this is a big string ###foobarbaz### this is a big string";
x = x.replace(21, 15, "foo bar baz");
u.test("replace", x.length() == 53 && memcmp(x.c_str(), "this is a big string foo bar baz this is a big string", 54) == 0);
tpass += u.pass_count();
tfail += u.fail_count();
u.report();
printf("\nTotals: pass: %ld, fail: %ld\n", tpass, tfail);
return 0; // Done. Yay!
}