10 #include "../StringManipulator.h" 30 string prefix =
"Stringmanipulator: ";
32 cout << (s1 == s2 ?
" == " :
" != ");
38 string myname =
"areEqual: ";
39 if ( lhs == rhs )
return true;
40 Index nlhs = lhs.size();
41 Index nrhs = rhs.size();
42 Index nstr = nlhs > nrhs ? nlhs : nrhs;
43 cout << myname <<
"Unequal string vectors: " <<
endl;
44 cout << myname <<
" Vector lengths: " << nlhs <<
", " << nrhs <<
endl;
45 for (
Index istr=0; istr<nstr; ++istr ) {
46 string slhs = istr < nlhs ? lhs[istr] :
"";
47 string srhs = istr < nrhs ? rhs[istr] :
"";
48 cout << myname <<
" " << istr <<
": " << slhs <<
", " << srhs <<
endl;
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];
117 assert( sman.
logLevel() == logLevel );
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];
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];
168 cout << myname <<
" " << str <<
endl;
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;
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;
const StringVector & patternSplit(std::string spat)
int test_StringManipulator(bool copy, Index logLevel)
static std::string floatToString(float val, int prec, bool trunc, std::string sdot="", std::string smin="")
const std::vector< std::string > & split(std::string seps, bool fullSplit=false)
int replace(std::string substr, const T &xsub)
const std::string & str() const
bool checkEqual(string s1, string s2)
unsigned int toUnsignedInt(unsigned int valbad=0) const
void setLogLevel(Index logLevel)
bool areEqual(const StringVector &lhs, const StringVector &rhs)
Q_EXPORT QTSManip setw(int w)
int toInt(int valbad=0) const
int replaceFixedWidth(std::string substr, const T &xsub, Index width)
std::vector< string > StringVector
void line(double t, double *p, double &x, double &y, double &z)
std::vector< StringVector > StringVV
std::vector< std::string > StringVector
QTextStream & endl(QTextStream &s)
float toFloat(float valbad=0) const