diff options
Diffstat (limited to 'src/tests/StringUtiltest.cc')
-rw-r--r-- | src/tests/StringUtiltest.cc | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/tests/StringUtiltest.cc b/src/tests/StringUtiltest.cc new file mode 100644 index 0000000..95dd5f0 --- /dev/null +++ b/src/tests/StringUtiltest.cc | |||
@@ -0,0 +1,140 @@ | |||
1 | // StringUtiltest.cc | ||
2 | // Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "../StringUtil.hh" | ||
23 | |||
24 | #include <vector> | ||
25 | #include <iostream> | ||
26 | #include <memory> | ||
27 | |||
28 | #ifdef UDS | ||
29 | #include <uds/init.hh> | ||
30 | #include <uds/uds.hh> | ||
31 | // configure UDS | ||
32 | uds::uds_flags_t uds::flags = uds::leak_check|uds::log_allocs; | ||
33 | #endif | ||
34 | |||
35 | using namespace std; | ||
36 | |||
37 | void testStringtok() { | ||
38 | vector<string> ls; | ||
39 | StringUtil::stringtok(ls, " arg1 arg2 \targ3\n arg4 arg5\t\t\t\targ6\n\n \n\n \t\t\narg7"); | ||
40 | cerr<<"Size: "<<ls.size()<<". Should be: 7."<<endl; | ||
41 | for (vector<string>::const_iterator i = ls.begin(); | ||
42 | i != ls.end(); ++i) { | ||
43 | cerr << ':' << (*i) << ":\n"; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | void testExpandFilename() { | ||
48 | auto_ptr<char> filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4")); | ||
49 | cerr<<"test "; | ||
50 | string test = string(getenv("HOME"))+"/filename/~filename2/file3~/file4"; | ||
51 | if (strcmp(test.c_str(), filename.get())==0) | ||
52 | cerr<<"ok."; | ||
53 | else | ||
54 | cerr<<"faild"; | ||
55 | cerr<<endl; | ||
56 | } | ||
57 | |||
58 | void testStrcasestr() { | ||
59 | cerr<<"test1 "; | ||
60 | if (StringUtil::strcasestr("Test", "TEST") == strcasestr("Test", "TEST")) | ||
61 | cerr<<"ok."<<endl; | ||
62 | else | ||
63 | cerr<<"faild."<<endl; | ||
64 | |||
65 | cerr<<"test2 "; | ||
66 | if (StringUtil::strcasestr("Test", "ESTabc") == strcasestr("Test", "ESTabc")) | ||
67 | cerr<<"ok."<<endl; | ||
68 | else | ||
69 | cerr<<"faild."<<endl; | ||
70 | |||
71 | cerr<<"test3 "; | ||
72 | if (StringUtil::strcasestr("TeSt", "abcTEStabc") == strcasestr("TeSt", "abcTEStabc")) | ||
73 | cerr<<"ok."<<endl; | ||
74 | else | ||
75 | cerr<<"faild."<<endl; | ||
76 | |||
77 | cerr<<"test4 "; | ||
78 | if (StringUtil::strcasestr("TEST", "_TEST;_") == strcasestr("TEST", "_TEST;_")) | ||
79 | cerr<<"ok."<<endl; | ||
80 | else | ||
81 | cerr<<"faild."<<endl; | ||
82 | |||
83 | } | ||
84 | |||
85 | void showError(int line, int pos, string& instr) { | ||
86 | |||
87 | cerr<<"Error on line: "<<line<<endl; | ||
88 | cerr<<instr<<endl; | ||
89 | for (int c=0; c<pos; c++) { | ||
90 | if (instr[c]=='\t') | ||
91 | cerr<<'\t'; | ||
92 | else | ||
93 | cerr<<" "; | ||
94 | } | ||
95 | cerr<<"^ here"<<endl; | ||
96 | |||
97 | } | ||
98 | |||
99 | void testGetStringBetween() { | ||
100 | string out; | ||
101 | vector<string> stringlist; | ||
102 | stringlist.push_back(" \t\t\t \t[(in \\)\t haha )] \t\t "); | ||
103 | stringlist.push_back("(in\\)) {_ _ my_ _}"); | ||
104 | stringlist.push_back("(in) {_ _ my_ _}"); | ||
105 | stringlist.push_back("(in){_ _ my_ _}"); | ||
106 | stringlist.push_back("\t \t \t ( in ) {haha}"); | ||
107 | stringlist.push_back("\t \t \t (( in \\) ) {haha}"); | ||
108 | stringlist.push_back("\t \t \t (( in \\) ){hihi}"); | ||
109 | stringlist.push_back("\t \t \t (( in \\) )|{hihi}"); | ||
110 | for (unsigned int i=0; i<stringlist.size(); i++) { | ||
111 | int pos = StringUtil::getStringBetween(out, stringlist[i].c_str(), '(', ')'); | ||
112 | int total_pos = 0; | ||
113 | if (pos<0) { | ||
114 | showError(i+1, -pos, stringlist[i]); | ||
115 | continue; | ||
116 | } | ||
117 | cerr<<"string="<<stringlist[i]<<endl; | ||
118 | cerr<<"pos="<<pos<<" ::"<<out; | ||
119 | total_pos += pos; | ||
120 | pos = StringUtil::getStringBetween(out, stringlist[i].c_str()+total_pos, '{', '}'); | ||
121 | if (pos<=0) { | ||
122 | pos=-pos; | ||
123 | showError(i+1, total_pos+pos, stringlist[i]); | ||
124 | continue; | ||
125 | } | ||
126 | cerr<<"::"<<out<<"::"<<endl; | ||
127 | total_pos += pos; | ||
128 | } | ||
129 | } | ||
130 | int main() { | ||
131 | #ifdef UDS | ||
132 | uds::Init uds_init; | ||
133 | #endif | ||
134 | cerr<<"Testing stringtok."<<endl; | ||
135 | testStringtok(); | ||
136 | cerr<<"Testing expandFilename."<<endl; | ||
137 | testExpandFilename(); | ||
138 | cerr<<"Testing strcasestr."<<endl; | ||
139 | testStrcasestr(); | ||
140 | } | ||