diff options
Diffstat (limited to 'src/tests/StringUtiltest.cc')
-rw-r--r-- | src/tests/StringUtiltest.cc | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/tests/StringUtiltest.cc b/src/tests/StringUtiltest.cc new file mode 100644 index 0000000..09b73c7 --- /dev/null +++ b/src/tests/StringUtiltest.cc | |||
@@ -0,0 +1,143 @@ | |||
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 | #ifndef _GNU_SOURCE | ||
24 | #define _GNU_SOURCE | ||
25 | #endif //_GNU_SOURCE | ||
26 | |||
27 | #include <vector> | ||
28 | #include <iostream> | ||
29 | #include <memory> | ||
30 | |||
31 | #ifdef UDS | ||
32 | #include <uds/init.hh> | ||
33 | #include <uds/uds.hh> | ||
34 | // configure UDS | ||
35 | uds::uds_flags_t uds::flags = uds::leak_check|uds::log_allocs; | ||
36 | #endif | ||
37 | |||
38 | using namespace std; | ||
39 | |||
40 | void testStringtok() { | ||
41 | vector<string> ls; | ||
42 | StringUtil::stringtok(ls, " arg1 arg2 \targ3\n arg4 arg5\t\t\t\targ6\n\n \n\n \t\t\narg7"); | ||
43 | cerr<<"Size: "<<ls.size()<<". Should be: 7."<<endl; | ||
44 | for (vector<string>::const_iterator i = ls.begin(); | ||
45 | i != ls.end(); ++i) { | ||
46 | cerr << ':' << (*i) << ":\n"; | ||
47 | } | ||
48 | } | ||
49 | |||
50 | void testExpandFilename() { | ||
51 | auto_ptr<char> filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4")); | ||
52 | cerr<<"test "; | ||
53 | string test = string(getenv("HOME"))+"/filename/~filename2/file3~/file4"; | ||
54 | if (strcmp(test.c_str(), filename.get())==0) | ||
55 | cerr<<"ok."; | ||
56 | else | ||
57 | cerr<<"faild"; | ||
58 | cerr<<endl; | ||
59 | } | ||
60 | |||
61 | void testStrcasestr() { | ||
62 | cerr<<"test1 "; | ||
63 | if (StringUtil::strcasestr("Test", "TEST") == strcasestr("Test", "TEST")) | ||
64 | cerr<<"ok."<<endl; | ||
65 | else | ||
66 | cerr<<"faild."<<endl; | ||
67 | |||
68 | cerr<<"test2 "; | ||
69 | if (StringUtil::strcasestr("Test", "ESTabc") == strcasestr("Test", "ESTabc")) | ||
70 | cerr<<"ok."<<endl; | ||
71 | else | ||
72 | cerr<<"faild."<<endl; | ||
73 | |||
74 | cerr<<"test3 "; | ||
75 | if (StringUtil::strcasestr("TeSt", "abcTEStabc") == strcasestr("TeSt", "abcTEStabc")) | ||
76 | cerr<<"ok."<<endl; | ||
77 | else | ||
78 | cerr<<"faild."<<endl; | ||
79 | |||
80 | cerr<<"test4 "; | ||
81 | if (StringUtil::strcasestr("TEST", "_TEST;_") == strcasestr("TEST", "_TEST;_")) | ||
82 | cerr<<"ok."<<endl; | ||
83 | else | ||
84 | cerr<<"faild."<<endl; | ||
85 | |||
86 | } | ||
87 | |||
88 | void showError(int line, int pos, string& instr) { | ||
89 | |||
90 | cerr<<"Error on line: "<<line<<endl; | ||
91 | cerr<<instr<<endl; | ||
92 | for (int c=0; c<pos; c++) { | ||
93 | if (instr[c]=='\t') | ||
94 | cerr<<'\t'; | ||
95 | else | ||
96 | cerr<<" "; | ||
97 | } | ||
98 | cerr<<"^ here"<<endl; | ||
99 | |||
100 | } | ||
101 | |||
102 | void testGetStringBetween() { | ||
103 | string out; | ||
104 | vector<string> stringlist; | ||
105 | stringlist.push_back(" \t\t\t \t[(in \\)\t haha )] \t\t "); | ||
106 | stringlist.push_back("(in\\)) {_ _ my_ _}"); | ||
107 | stringlist.push_back("(in) {_ _ my_ _}"); | ||
108 | stringlist.push_back("(in){_ _ my_ _}"); | ||
109 | stringlist.push_back("\t \t \t ( in ) {haha}"); | ||
110 | stringlist.push_back("\t \t \t (( in \\) ) {haha}"); | ||
111 | stringlist.push_back("\t \t \t (( in \\) ){hihi}"); | ||
112 | stringlist.push_back("\t \t \t (( in \\) )|{hihi}"); | ||
113 | for (unsigned int i=0; i<stringlist.size(); i++) { | ||
114 | int pos = StringUtil::getStringBetween(out, stringlist[i].c_str(), '(', ')'); | ||
115 | int total_pos = 0; | ||
116 | if (pos<0) { | ||
117 | showError(i+1, -pos, stringlist[i]); | ||
118 | continue; | ||
119 | } | ||
120 | cerr<<"string="<<stringlist[i]<<endl; | ||
121 | cerr<<"pos="<<pos<<" ::"<<out; | ||
122 | total_pos += pos; | ||
123 | pos = StringUtil::getStringBetween(out, stringlist[i].c_str()+total_pos, '{', '}'); | ||
124 | if (pos<=0) { | ||
125 | pos=-pos; | ||
126 | showError(i+1, total_pos+pos, stringlist[i]); | ||
127 | continue; | ||
128 | } | ||
129 | cerr<<"::"<<out<<"::"<<endl; | ||
130 | total_pos += pos; | ||
131 | } | ||
132 | } | ||
133 | int main() { | ||
134 | #ifdef UDS | ||
135 | uds::Init uds_init; | ||
136 | #endif | ||
137 | cerr<<"Testing stringtok."<<endl; | ||
138 | testStringtok(); | ||
139 | cerr<<"Testing expandFilename."<<endl; | ||
140 | testExpandFilename(); | ||
141 | cerr<<"Testing strcasestr."<<endl; | ||
142 | testStrcasestr(); | ||
143 | } | ||