56 const string myname =
"test_StringManipulator: ";
57 cout << myname <<
"Starting test with copy = " << (
copy ?
"true" :
"false") <<
endl;
59 cout << myname <<
"NDEBUG must be off." <<
endl;
62 string line =
"-----------------------------";
65 cout << myname <<
"Checking float to string." <<
endl;
74 cout << myname <<
"Checking fill char." <<
endl;
76 cfill = StringManipulator::getFill<int>(vint);
77 cout << myname <<
" int " << vint <<
": " << cfill <<
endl;
78 assert( cfill ==
'0' );
80 cfill = StringManipulator::getFill<int>(vint);
81 cout << myname <<
" int " << vint <<
": " << cfill <<
endl;
82 assert( cfill ==
'-' );
84 cfill = StringManipulator::getFill<float>(vflo);
85 cout << myname <<
" float " << vflo <<
": " << cfill <<
endl;
86 assert( cfill ==
'_' );
89 vector<string> raws = {
93 "abc*SUB*ghi_*NEG*_*POS*.txt" 95 vector<string> exps = {
99 "abcdefghi_-123_123.txt" 101 vector<string> expws = {
105 "abc__defghi_--123_00123.txt" 108 cout << myname <<
"Testing no width with " << raws.size() <<
" strings." <<
endl;
110 for (
Index istr=0; istr<raws.size(); ++istr ) {
111 string raw = raws[istr];
112 string exp = exps[istr];
115 assert( sman.logLevel() == 0 );
116 sman.setLogLevel(logLevel);
117 assert( sman.logLevel() == logLevel );
118 sman.replace(
"*SUB*",
"def");
119 sman.replace(
"*POS*", 123);
120 sman.replace(
"*NEG*", -123);
121 cout << myname <<
" " 122 <<
setw(w) << raw <<
": " 123 <<
setw(w) << val <<
" ?= " 125 assert( raw != exp );
126 assert( sman.str() == exp );
127 if ( !
copy ) assert( val == exp );
130 cout << myname <<
"Testing width 5 with " << raws.size() <<
" strings." <<
endl;
131 for (
Index istr=0; istr<raws.size(); ++istr ) {
132 string raw = raws[istr];
133 string exp = expws[istr];
136 sman.setLogLevel(logLevel);
137 sman.replaceFixedWidth(
"*SUB*",
"def", 5);
138 sman.replaceFixedWidth(
"*POS*", 123, 5);
139 sman.replaceFixedWidth(
"*NEG*", -123, 5);
140 cout << myname <<
" " 141 <<
setw(w) << raw <<
": " 142 <<
setw(w) << val <<
" ?= " 144 assert( raw != exp );
145 assert( sman.str() == exp );
146 if ( !
copy ) assert( val == exp );
149 cout << myname <<
"Test split." <<
endl;
154 strs.push_back(
"a,bb,ccc");
155 splits1.push_back({
"a",
"bb",
"ccc"});
156 splits2.push_back({
"a",
"bb",
"ccc"});
157 strs.push_back(
",a,bb,ccc,");
158 splits1.push_back({
"a",
"bb",
"ccc"});
159 splits2.push_back({
"",
"a",
"bb",
"ccc",
""});
160 strs.push_back(
"a,,bb,ccc");
161 splits1.push_back({
"a",
"bb",
"ccc"});
162 splits2.push_back({
"a",
"",
"bb",
"ccc"});
163 for (
Index itst=0; itst<strs.size(); ++itst ) {
164 string str = strs[itst];
165 string seps = sepss[itst];
167 sman.setLogLevel(logLevel);
168 cout << myname <<
" " << str <<
endl;
169 assert(
areEqual(sman.split(seps), splits1[itst]) );
170 assert(
areEqual(sman.split(seps,
true), splits2[itst]) );
173 cout << myname <<
"Test pattern split." <<
endl;
177 strs.push_back(
"where did {bob,sally,kim} go?");
178 splits1.push_back({
"where did bob go?",
"where did sally go?",
"where did kim go?"});
179 strs.push_back(
"where did {bob,sal,kim} {go,stay}?");
180 splits1.push_back({
"where did bob go?",
"where did bob stay?",
181 "where did sal go?",
"where did sal stay?",
182 "where did kim go?",
"where did kim stay?"});
183 for (
Index itst=0; itst<strs.size(); ++itst ) {
184 string str = strs[itst];
185 string spat = spats[itst];
187 cout << myname <<
" " << str <<
", " << spat <<
endl;
188 assert(
areEqual(sman.patternSplit(spat), splits1[itst]) );
191 cout << myname <<
"Check int conversion." <<
endl;
192 vector<std::pair<string, int>> ichks;
194 ichks.push_back({
"123", 123});
195 ichks.push_back({
"+123", 123});
196 ichks.push_back({
"-123", -123});
197 ichks.push_back({
"xx", badInt});
198 ichks.push_back({
"", badInt});
199 ichks.push_back({
"--123", badInt});
200 ichks.push_back({
"", badInt});
201 for (
auto vv : ichks ) {
203 cout << myname <<
setw(10) << vv.first <<
": " << vv.second <<
" ?= " << iman <<
endl;
204 assert( iman == vv.second );
207 cout << myname <<
"Check unsigned int conversion." <<
endl;
208 vector<std::pair<string, unsigned int>> uchks;
209 unsigned int badUInt = 9999;
210 uchks.push_back({
"123", 123});
211 uchks.push_back({
"+123", 123});
212 uchks.push_back({
"-123", badUInt});
213 uchks.push_back({
"xx", badUInt});
214 uchks.push_back({
"", badUInt});
215 uchks.push_back({
"--123", badUInt});
216 uchks.push_back({
"", badUInt});
217 for (
auto vv : uchks ) {
219 cout << myname <<
setw(10) << vv.first <<
": " << vv.second <<
" ?= " << iman <<
endl;
220 assert( iman == vv.second );
223 cout << myname <<
"Check float conversion." <<
endl;
224 vector<std::pair<string, float>> fchks;
225 float badFloat = -999.9;
226 fchks.push_back({
"1.23", 1.23});
227 fchks.push_back({
"+1.23", 1.23});
228 fchks.push_back({
"-1.23", -1.23});
229 fchks.push_back({
"xx", badFloat});
230 fchks.push_back({
"", badFloat});
231 fchks.push_back({
"--1.23", badFloat});
232 fchks.push_back({
"1.23e1", 12.3});
233 fchks.push_back({
"1.23x", badFloat});
234 fchks.push_back({
"", badFloat});
235 for (
auto vv : fchks ) {
237 float xman = sman.toFloat(badFloat);
238 cout << myname <<
setw(10) << vv.first <<
": " << vv.second <<
" ?= " << xman <<
endl;
239 assert(
int(1000*xman) ==
int(1000*(vv.second)) );
240 bool foundBad =
int(1000*xman) ==
int(1000*badFloat);
241 assert( sman.isFloat() != foundBad );
244 cout << myname <<
"Done." <<
endl;
static std::string floatToString(float val, int prec, bool trunc, std::string sdot="", std::string smin="")
bool checkEqual(string s1, string s2)
unsigned int toUnsignedInt(unsigned int valbad=0) const
bool areEqual(const StringVector &lhs, const StringVector &rhs)
Q_EXPORT QTSManip setw(int w)
int toInt(int valbad=0) const
std::vector< string > StringVector
void line(double t, double *p, double &x, double &y, double &z)
std::vector< StringVector > StringVV
QTextStream & endl(QTextStream &s)