1 /*
2  * main.cxx
3  *
4  * PWLib application source file for StringTest
5  *
6  * Main program entry point.
7  *
8  * Copyright (c) 2006 Indranet Technologies Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Derek J Smithies
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 20385 $
27  * $Author: rjongbloed $
28  * $Date: 2008-06-04 05:40:38 -0500 (Wed, 04 Jun 2008) $
29  */
30 #include  <ptlib.h>
31 #include <ptlib/pprocess.h>
32 
33 #include <string>
34 
35 #include "main.h"
36 #include "version.h"
37 
38 
39 PCREATE_PROCESS(StringTest);
40 
41 
42 
43 
StringTest()44 StringTest::StringTest()
45   : PProcess("Derek Smithies code factory", "StringTest", MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
46 {
47 }
48 
49 
Main()50 void StringTest::Main()
51 {
52   PArgList & args = GetArguments();
53 
54   args.Parse(
55              "h-help."
56 	     "v-version."
57 
58 	     "a-assign."
59 	     "c-copy."
60 	     "e-everything."
61 	     "j-join."
62 	     "l-length."
63 
64 	     "i-iterations:"
65 	     "s-standard."
66 #if PTRACING
67              "o-output:"
68              "t-trace."
69 #endif
70 	     );
71 
72 #if PTRACING
73   PTrace::Initialise(args.GetOptionCount('t'),
74                      args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
75          PTrace::Blocks | PTrace::Timestamp | PTrace::Thread | PTrace::FileAndLine);
76 #endif
77 
78   if (args.HasOption('h')) {
79     PError << "Available options are: " << endl
80 	   << endl
81 	   << "-a or --assign         : construct a  strng with an assigned value\n"
82 	   << "-c or --copy           : copy one string to another\n"
83 	   << "-e or --everything     : everything together, in such a way to avoid compiler optimisations\n"
84 	   << "-j or --join           : test the joining of two strings \n"
85 	   << "-l or --length         : calculate the length of a string\n"
86 	   << "-i or --interations    : the number of (x1E6) iterations to repeat the test\n"
87 	   << "-h or --help           : print this help message.\n"
88 	   << "-v or --version        : report program version\n"
89 	   << "-s or --standard       : test std::string, and not the pwlib string\n"
90 #if PTRACING
91 	   << "-o or --output file    : file name for output of log messages\n"
92 	   << "-t or --trace          : degree of verbosity in error log (more times for more detail)\n"
93 #endif
94 	   << endl
95 	   << " e.g. stringtest -i 100 -e  " << endl << endl;
96     return;
97   }
98 
99   if (args.HasOption('v')) {
100         cout << "Product Name: " << GetName() << endl
101          << "Manufacturer: " << GetManufacturer() << endl
102          << "Version     : " << GetVersion(TRUE) << endl
103          << "System      : " << GetOSName() << '-'
104          << GetOSHardware() << ' '
105          << GetOSVersion() << endl;
106     return;
107   }
108 
109   PINDEX iterations = 100;
110   if (args.HasOption('i'))
111     iterations = args.GetOptionString('i').AsInteger(10);
112   iterations = PMAX(1, PMIN(iterations, 100000));
113   PError << "Will run the test for 1 million x " << iterations << " loops" << endl;
114 
115   PBoolean testPwlib = ! args.HasOption('s');
116 
117   if (args.HasOption('s'))
118     cerr << "examine std::string" << endl;
119   else
120     cerr << "examine pwlib string" << endl;
121 
122   TestToDo test = None;
123   if (args.HasOption('a')) {
124     cerr << "Test Assigning" << endl;
125     test = Assign;
126   }
127 
128   if (args.HasOption('c')) {
129     cerr << "Test copying the middle of one string to another" << endl;
130     test = Copy;
131   }
132 
133   if (args.HasOption('e')) {
134     cerr << "Every possible combination the author could think of" << endl;
135     test = Everything;
136   }
137 
138   if (args.HasOption('j')) {
139     cerr << "test joining two strings together " << endl;
140     test = Join;
141   }
142 
143   if (args.HasOption('l')) {
144     cerr << "test determining the length of a string " << endl;
145     test = Length;
146   }
147 
148 
149   startTime = PTime();
150 
151   for (PINDEX i = 0; i < iterations; i++)
152     switch(test) {
153 
154     case Assign:;
155       if (testPwlib)
156 	TestPwlibAssign();
157       else
158 	TestStandardAssign();
159       break;
160 
161     case Copy :;
162       if (testPwlib)
163 	TestPwlibCopy();
164       else
165 	TestStandardCopy();
166       break;
167 
168     case Everything: ;
169       if (testPwlib)
170 	TestPwlibEverything();
171       else
172 	TestStandardEverything();
173       break;
174 
175     case Join: ;
176       if (testPwlib)
177 	TestPwlibJoin();
178       else
179 	TestStandardJoin();
180       break;
181 
182     case Length: ;
183       if (testPwlib)
184 	TestPwlibLength();
185       else
186 	TestStandardLength();
187       break;
188 
189     case None: ;
190       if (testPwlib)
191 	TestPwlibNone();
192       else
193 	TestStandardNone();
194     }
195 
196   PTime endTime;
197   cerr << "Elapsed time is " << (endTime - startTime) << endl;
198 }
199 
TestPwlibAssign()200 void StringTest::TestPwlibAssign()
201 {
202   for (PINDEX i = 0; i < 1000000; i++) {
203     PString src("abcdefg");
204     PString dst;
205     dst = src;
206   }
207 }
208 
TestStandardAssign()209 void StringTest::TestStandardAssign()
210 {
211   for (PINDEX i = 0; i < 1000000; i++) {
212     std::string src("abcdefg");
213     std::string dst;
214     dst = src;
215   }
216 }
217 
218 
TestPwlibCopy()219 void StringTest::TestPwlibCopy()
220 {
221   PString src("abcdefghijkl");
222   PString dsta;
223   PString dstb;
224   for (PINDEX i = 0; i < 1000000; i++) {
225     switch(i % 3) {
226     case 0:
227       dsta = src.Mid(3, 6);
228       break;
229     case 1:
230       dstb = dsta.Mid(1, 1);
231       break;
232     case 2:
233       dsta = src + dsta;
234       break;
235     }
236   }
237 }
238 
TestStandardCopy()239 void StringTest::TestStandardCopy()
240 {
241   std::string src("abcdefghijkl");
242   std::string dsta;
243   std::string dstb;
244   for (PINDEX i = 0; i < 1000000; i++) {
245     switch(i % 3) {
246     case 0:
247       dsta = src.substr(3,6);
248       break;
249     case 1:
250       dstb = dsta.substr(1,1);
251       break;
252     case 2:
253       dsta = src + dsta;
254       break;
255     }
256   }
257 }
258 
259 
TestPwlibEverything()260 void StringTest::TestPwlibEverything()
261 {
262   PString answer;
263   PString src("abcdefghijkl");
264   PString dst;
265   for (PINDEX i = 0; i < 1000000; i++) {
266     dst = src + src + src + src + src;
267     src = dst.Mid(1, 5);
268 
269     dst = src + PString(i);
270 
271     dst = dst + src.Mid(1, 2) + PString(src.GetLength());
272 
273     // src = dst;
274     answer =  dst.Mid(dst.GetLength() - 1, 1);
275   }
276   cout << answer << endl;
277 }
278 
279 
TestStandardEverything()280 void StringTest::TestStandardEverything()
281 {
282   stringstream temp;
283   std::string answer;
284   std::string src("abcdefghijkl");
285   std::string dst;
286   for (PINDEX i = 0; i < 1000000; i++) {
287     dst = src + src + src + src + src;
288     src = dst.substr(1, 5);
289 
290     temp << i;
291     dst = src + temp.str();
292     temp.str("");
293 
294     temp << src.length();
295     dst = dst + src.substr(1,2) + temp.str();
296     temp.str("");
297 
298 
299     answer = dst.substr(dst.length() - 1, 1);
300   }
301   cout << answer << endl;
302 }
303 
304 
305 
TestPwlibJoin()306 void StringTest::TestPwlibJoin()
307 {
308   for (PINDEX i = 0; i < 1000000; i++) {
309     PString src("abcdefg");
310     PString dst("123");
311     dst = dst + src;;
312   }
313 }
314 
TestStandardJoin()315 void StringTest::TestStandardJoin()
316 {
317   for (PINDEX i = 0; i < 1000000; i++) {
318     std::string src("abcdefg");
319     std::string dst("123");
320     dst = dst + src;
321   }
322 }
323 
TestPwlibLength()324 void StringTest::TestPwlibLength()
325 {
326   for (PINDEX i = 0; i < 1000000; i++) {
327     PString src("abcdefg");
328     int i = src.GetLength();
329     i++;
330   }
331 }
332 
TestStandardLength()333 void StringTest::TestStandardLength()
334 {
335   for (PINDEX i = 0; i < 1000000; i++) {
336     std::string src("abcdefg");
337     int i = src.length();
338     i++;
339   }
340 }
341 
342 
TestPwlibNone()343 void StringTest::TestPwlibNone()
344 {
345   for (PINDEX i = 0; i < 1000000; i++) {
346     PString src;
347   }
348 }
349 
TestStandardNone()350 void StringTest::TestStandardNone()
351 {
352   for (PINDEX i = 0; i < 1000000; i++) {
353     std::string src;
354   }
355 }
356 
357 
358 // End of File ///////////////////////////////////////////////////////////////
359