-
Notifications
You must be signed in to change notification settings - Fork 246
/
TestStrBlob.cpp
175 lines (150 loc) · 4.98 KB
/
TestStrBlob.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
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
#include <iostream>
#include "StrBlob.h"
#include "StrBlobPtr.h"
#include "ConstStrBlobPtr.h"
void testStrBlob(StrBlob &sb) {
try {
sb.push_back("abc");
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
sb.pop_back();
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
*sb.begin() = "Change";
for (auto p = sb.begin(); ; ++p)
std::cout << "deref: " << *p << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
void testStrBlob(const StrBlob &sb) {
try {
std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
void testStrBlobPtr(StrBlobPtr &sbp) {
try {
*sbp = "Change2";
for (auto p = sbp; ; ++p)
std::cout << "deref: " << *p << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
}
int main() {
StrBlob sb1;
testStrBlob(sb1);
std::cout << std::endl;
StrBlob sb2{"Hello", "World"};
testStrBlob(sb2);
std::cout << std::endl;
std::cout << sb2[1] << std::endl;
try {
std::cout << sb2[100] << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
StrBlob sb3{"ABC", "DEF"};
StrBlob sb4 = sb3;
{
std::cout << (sb3 == sb4 ? "equal" : "not equal") << std::endl; // e
StrBlobPtr sbp1(sb3), sbp2(sb4), sbp3(sb4), sbp4(sb4, sb4.size());
std::cout << (sbp1 == sbp2 ? "equal" : "not equal") << std::endl; // n
std::cout << (sbp2 == sbp3 ? "equal" : "not equal") << std::endl; // e
std::cout << (sbp3 == sbp4 ? "equal" : "not equal") << std::endl; // n
ConstStrBlobPtr csbp1(sb3), csbp2(sb4), csbp3(sb4), csbp4(sb4, sb4.size());
std::cout << (csbp1 == csbp2 ? "equal" : "not equal") << std::endl; // n
std::cout << (csbp2 == csbp3 ? "equal" : "not equal") << std::endl; // e
std::cout << (csbp3 == csbp4 ? "equal" : "not equal") << std::endl; // n
std::cout << std::endl;
}
sb4.push_back("GHI");
testStrBlob(sb3);
std::cout << std::endl;
testStrBlob(sb4);
std::cout << std::endl;
const StrBlob csb1;
testStrBlob(csb1);
std::cout << std::endl;
const StrBlob csb2{"This", "Blob"};
testStrBlob(csb2);
std::cout << std::endl;
std::cout << csb2[1] << std::endl;
try {
std::cout << csb2[100] << std::endl;
} catch (std::out_of_range err) {
std::cerr << err.what() << " out of range" << std::endl;
} catch (std::exception err) {
std::cerr << err.what() << std::endl;
}
testStrBlob({"ppp", "qqq"});
std::cout << std::endl;
//testStrBlob({"mm", 1}); // Error
StrBlobPtr sbp1;
testStrBlobPtr(sbp1);
std::cout << std::endl;
StrBlobPtr sbp2(sb1);
testStrBlobPtr(sbp2);
std::cout << std::endl;
StrBlobPtr sbp3(sb1, sb1.size());
std::cout << (sbp3 == sbp2 ? "equal" : "not equal") << std::endl; // e
testStrBlobPtr(sbp3);
std::cout << std::endl;
StrBlobPtr sbp4(sb2);
testStrBlobPtr(sbp4);
std::cout << std::endl;
std::cout << sbp4[1] << std::endl;
StrBlobPtr sbp5(sb2, sb2.size());
testStrBlobPtr(sbp5);
std::cout << std::endl;
{
StrBlob s1{"a", "b"};
StrBlob s2{"a", "b", "c"};
StrBlob s3{"a", "b", "d"};
StrBlob s4{"b", "a", "b"};
StrBlob s5{"b", "a", "b"};
std::cout << (s1 < s2) << std::endl; // 1
std::cout << (s2 < s3) << std::endl; // 1
std::cout << (s3 < s4) << std::endl; // 1
std::cout << (s1 < s4) << std::endl; // 1
std::cout << (s1 > s2) << std::endl; // 0
std::cout << (s2 > s3) << std::endl; // 0
std::cout << (s3 > s4) << std::endl; // 0
std::cout << (s1 > s4) << std::endl; // 0
std::cout << (s4 < s5) << std::endl; // 0
std::cout << (s4 > s5) << std::endl; // 0
std::cout << std::endl;
StrBlobPtr p1(s1), p2(s1, s1.size());
StrBlobPtr p3(s2), p4(s2, s2.size());
StrBlobPtr p5(s2);
std::cout << (p1 < p2) << std::endl; // 1
std::cout << (p3 < p4) << std::endl; // 1
std::cout << (p1 < p3) << std::endl; // 0
std::cout << (p1 > p2) << std::endl; // 0
std::cout << (p3 > p4) << std::endl; // 0
std::cout << (p1 > p3) << std::endl; // 0
std::cout << (p3 < p5) << std::endl; // 0
std::cout << (p3 > p5) << std::endl; // 0
std::cout << std::endl;
}
{
StrBlob s{"abc", "defg", "hi"};
StrBlobPtr p(s);
std::cout << *p++ << std::endl;
std::cout << *p << std::endl;
std::cout << p->back() << std::endl;
ConstStrBlobPtr cp(s);
std::cout << *cp++ << std::endl;
std::cout << *cp << std::endl;
std::cout << cp->back() << std::endl;
}
return 0;
}