StringUtils_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include <iostream>
40 #include <string>
41 #include <sstream>
42 #include <iterator>
43 #include "StringUtils.hpp"
44 #include "TestUtil.hpp"
45 
46 using namespace gnsstk::StringUtils;
47 using namespace std;
48 
49 namespace std
50 {
51  std::ostream& operator<<(std::ostream& s, const std::vector<std::string>& v)
52  {
53  s << "[ ";
54  std::copy(v.begin(), v.end(),
55  std::ostream_iterator<std::string>(s, ", "));
56  s << " ]";
57  return s;
58  }
59 }
60 
61 
63 const char hexDumpArray1[] =
64 { 0x00, 0x10, 0x30, 0x33, 0x30, 0x31, 0x30, 0x35,
65  0x3A, 0x65, 0x70, 0x68, 0x20, 0x3A, 0x30, 0x3A,
66  0x35, 0x32, 0x36, 0x34, 0x30, 0x2E, 0x33, 0x33,
67  0x33, 0x34, 0x30, 0x32, 0x37, 0x37, 0x37, 0x37,
68  0x37, 0x37, 0x37, 0x38, 0x41, 0x20, 0x52, 0x61,
69  0x6e, 0x64, 0x6F, 0x6D, 0x20, 0x4D, 0x65, 0x73,
70  0x73, 0x61, 0x67, 0x65, 0x32, 0x35, 0x39, 0x32,
71  0x35, 0x39, 0x32, 0x34, 0x20, 0x32, 0x20, 0x35,
72  0x32, 0x36, 0x34, 0x30, 0x2E, 0x33, 0x33, 0x38,
73  0x38, 0x33, 0x36, 0x33, 0x34, 0x32, 0x35, 0x39,
74  0x32, 0x35, 0x39, 0x33, 0x20, 0x7F, 0x20, 0x38,
75  0x35, 0x34, 0x31, 0x32, 0x20, 0x31, 0x20, 0x31,
76  0x20, 0x30, 0x20, 0x31, 0x20, 0x31, 0x20, 0x34,
77  0x20, 0x32, 0x20, 0x34, 0x20, 0x30, 0x20, 0x30,
78  0x20, 0x33, 0x20, 0x31, 0x31, 0x39, 0x39, 0x20,
79  0x32, 0x32, 0x63, 0x30, 0x61, 0x66, 0x30, 0x33,
80  0x20, 0x31, 0x37, 0x37, 0x30, 0x32, 0x39, 0x65,
81  0x34, 0x20, 0x61, 0x66, 0x34, 0x30, 0x30, 0x31,
82  0x66, 0x20, 0x65, 0x36, 0x31, 0x62, 0x35, 0x38,
83  0x61, 0x20, 0x35, 0x35, 0x66, 0x33, 0x37, 0x35,
84  0x30, 0x20, 0x34, 0x63, 0x63, 0x61, 0x38, 0x38,
85  0x37, 0x20, 0x31, 0x31, 0x35, 0x64, 0x37, 0x63,
86  0x66, 0x63, 0x20, 0x33, 0x61, 0x64, 0x32, 0x30,
87  0x34, 0x38, 0x61, 0x20, 0x63, 0x65, 0x61, 0x20,
88  0x33, 0x36, 0x32, 0x63, 0x34, 0x38, 0x20, 0x31,
89  0x31, 0x39, 0x39, 0x20, 0x32, 0x32, 0x63, 0x30,
90  0x61, 0x66, 0x30, 0x33, 0x20, 0x31, 0x37, 0x37,
91  0x30, 0x34, 0x61, 0x37, 0x34, 0x20, 0x33, 0x61,
92  0x63, 0x34, 0x36, 0x36, 0x63, 0x31, 0x20, 0x62,
93  0x64, 0x30, 0x63, 0x36, 0x64, 0x63, 0x20, 0x33,
94  0x39, 0x31, 0x62, 0x33, 0x37, 0x38, 0x33, 0x20,
95  0x33, 0x65, 0x30, 0x34, 0x30 };
96 
97 
100 const char hexDumpArray2[] =
101 { 0x61, 0x66, 0x30, 0x33, 0x30, 0x31, 0x30, 0x35,
102  0x3A, 0x65, 0x70, 0x68, 0x20, 0x3A, 0x30, 0x3A,
103  0x35, 0x32, 0x36, 0x34, 0x30, 0x2E, 0x33, 0x33,
104  0x33, 0x34, 0x30, 0x32, 0x37, 0x37, 0x37, 0x37,
105  0x37, 0x37, 0x37, 0x38, 0x41, 0x20, 0x52, 0x61,
106  0x6e, 0x64, 0x6F, 0x6D, 0x20, 0x4D, 0x65, 0x73,
107  0x73, 0x61, 0x67, 0x65, 0x32, 0x35, 0x39, 0x32,
108  0x35, 0x39, 0x32, 0x34, 0x20, 0x32, 0x20, 0x35,
109  0x32, 0x36, 0x34, 0x30, 0x2E, 0x33, 0x33, 0x38,
110  0x38, 0x33, 0x36, 0x33, 0x34, 0x32, 0x35, 0x39,
111  0x32, 0x35, 0x39, 0x33, 0x20, 0x32, 0x20, 0x38,
112  0x35, 0x34, 0x31, 0x32, 0x20, 0x31, 0x20, 0x31,
113  0x20, 0x30, 0x20, 0x31, 0x20, 0x31, 0x20, 0x34,
114  0x20, 0x32, 0x20, 0x34, 0x20, 0x30, 0x20, 0x30,
115  0x20, 0x33, 0x20, 0x31, 0x31, 0x39, 0x39, 0x20,
116  0x32, 0x32, 0x63, 0x30, 0x61, 0x66, 0x30, 0x33,
117  0x20, 0x31, 0x37, 0x37, 0x30, 0x32, 0x39, 0x65,
118  0x34, 0x20, 0x61, 0x66, 0x34, 0x30, 0x30, 0x31,
119  0x66, 0x20, 0x65, 0x36, 0x31, 0x62, 0x35, 0x38,
120  0x61, 0x20, 0x35, 0x35, 0x66, 0x33, 0x37, 0x35,
121  0x30, 0x20, 0x34, 0x63, 0x63, 0x61, 0x38, 0x38,
122  0x37, 0x20, 0x31, 0x31, 0x35, 0x64, 0x37, 0x63,
123  0x66, 0x63, 0x20, 0x33, 0x61, 0x64, 0x32, 0x30,
124  0x34, 0x38, 0x61, 0x20, 0x63, 0x65, 0x61, 0x20,
125  0x33, 0x36, 0x32, 0x63, 0x34, 0x38, 0x20, 0x31,
126  0x31, 0x39, 0x39, 0x20, 0x32, 0x32, 0x63, 0x30,
127  0x61, 0x66, 0x30, 0x33, 0x20, 0x31, 0x37, 0x37,
128  0x30, 0x34, 0x61, 0x37, 0x34, 0x20, 0x33, 0x61,
129  0x63, 0x34, 0x36, 0x36, 0x63, 0x31, 0x20, 0x62,
130  0x64, 0x30, 0x63, 0x36, 0x64, 0x63, 0x20, 0x33,
131  0x39, 0x31, 0x62, 0x33, 0x37, 0x38, 0x33, 0x20,
132  0x33, 0x65, 0x30, 0x34, 0x30 };
133 
134 
140 {
141 public:
144 
149  string getFileContents(const string& fileName)
150  {
151  static const int BUF_SIZ = 512;
152  ifstream s(fileName.c_str());
153  std::string rv;
154  GNSSTK_ASSERT(s);
155  char *buffer = new char[BUF_SIZ];
156  while (s)
157  {
158  s.read(buffer, BUF_SIZ);
159  rv += string(buffer, s.gcount());
160  }
161  delete[] buffer;
162  return rv;
163  }
164 
170  unsigned justificationTest()
171  {
172  TUDEF("StringUtils", "Justify");
173  string baseString= "1234567890"; // base string to manipulate
174  string resultString; // storage for the correct response
175  string manipulatedString; // storage for the manipulated base string
176  char fillerChar = '-';
177 
178  manipulatedString = baseString;
179  resultString = string(6, fillerChar) + baseString;
180  // Add six filler characters to the front of the string
181  rightJustify(manipulatedString, manipulatedString.length() + 6,
182  fillerChar);
183  TUCSM("rightJustify");
184  TUASSERTE(std::string, resultString, manipulatedString);
185 
186  manipulatedString = baseString;
187  resultString = baseString + string(6, fillerChar);
188  // Add six filler characters to the end of the string
189  leftJustify(manipulatedString, manipulatedString.length()+6, fillerChar);
190  TUCSM("leftJustify");
191  TUASSERTE(std::string, resultString, manipulatedString);
192 
193  manipulatedString = baseString;
194  resultString = string(3, fillerChar) + baseString +
195  string(3, fillerChar);
196  // Add three filler characters to both ends of the string
197  center(manipulatedString, manipulatedString.length()+6, fillerChar);
198  TUCSM("center");
199  TUASSERTE(std::string, resultString, manipulatedString);
200 
201  TURETURN();
202  }
203 
204 
205 
212  unsigned stripLeadingTest()
213  {
214  TUDEF("StringUtils", "stripLeading");
215  string baseString= "---1234567890---"; // base string to manipulate
216  string resultString; // storage for the correct response
217  string manipulatedString; // storage for the manipulated base string
218  char charToBeRemoved = '-';
219  string stringToBeRemoved = "--";
220 
221  manipulatedString = baseString;
222  resultString = baseString.substr(1,string::npos);
223  // Strip one of the character -'s from the beginning of the string
224  manipulatedString = stripLeading(manipulatedString, charToBeRemoved, 1);
225  TUASSERTE(std::string, resultString, manipulatedString);
226 
227  manipulatedString = baseString;
228  resultString = baseString.substr(2,string::npos);
229  // Strip one of the string --'s from the beginning of the string
230  manipulatedString = stripLeading(manipulatedString, stringToBeRemoved,
231  1);
232  TUASSERTE(std::string, resultString, manipulatedString);
233 
234  manipulatedString = baseString;
235  resultString = baseString.substr(3,string::npos);
236  // Strip all of the character -'s from the beginning of the string
237  manipulatedString = stripLeading(manipulatedString, charToBeRemoved);
238  TUASSERTE(std::string, resultString, manipulatedString);
239 
240  manipulatedString = baseString;
241  resultString = baseString.substr(2,string::npos);
242  // Strip all of the string --'s from the beginning of the string
243  manipulatedString = stripLeading(manipulatedString, stringToBeRemoved);
244  TUASSERTE(std::string, resultString, manipulatedString);
245 
246  TURETURN();
247  }
248 
249 
256  unsigned stripTrailingTest()
257  {
258  TUDEF("StringUtils", "stripTrailing");
259  string baseString= "---1234567890---"; // base string to manipulate
260  string resultString; // storage for the correct response
261  string manipulatedString; // storage for the manipulated base string
262  char charToBeRemoved = '-';
263  string stringToBeRemoved = "--";
264 
265  manipulatedString = baseString;
266  resultString = baseString.substr(0, baseString.length()-1);
267  // Strip one of the character -'s from the end of the string
268  manipulatedString = stripTrailing(manipulatedString, charToBeRemoved, 1);
269  TUASSERTE(std::string, resultString, manipulatedString);
270 
271  manipulatedString = baseString;
272  resultString = baseString.substr(0, baseString.length()-2);
273  // Strip one of the string --'s from the end of the string
274  manipulatedString = stripTrailing(manipulatedString, stringToBeRemoved,
275  1);
276  TUASSERTE(std::string, resultString, manipulatedString);
277 
278  manipulatedString = baseString;
279  resultString = baseString.substr(0, baseString.length()-3);
280  // Strip all of the character -'s from the end of the string
281  manipulatedString = stripTrailing(manipulatedString, charToBeRemoved);
282  TUASSERTE(std::string, resultString, manipulatedString);
283 
284  manipulatedString = baseString;
285  resultString = baseString.substr(0, baseString.length()-2);
286  // Strip all of the string --'s from the end of the string
287  manipulatedString = stripTrailing(manipulatedString, stringToBeRemoved);
288  TUASSERTE(std::string, resultString, manipulatedString);
289 
290  TURETURN();
291  }
292 
293 
300  unsigned stripTest()
301  {
302  TUDEF("StringUtils", "strip");
303  string baseString= "---1234567890---"; // base string to manipulate
304  string resultString; // storage for the correct response
305  string manipulatedString; // storage for the manipulated base string
306  char charToBeRemoved = '-';
307  string stringToBeRemoved = "--";
308 
309  manipulatedString = baseString;
310  resultString = baseString.substr(1,baseString.length()-2);
311  // Strip one of the character -'s from both ends of the string
312  manipulatedString = strip(manipulatedString, charToBeRemoved, 1);
313  TUASSERTE(std::string, resultString, manipulatedString);
314 
315  manipulatedString = baseString;
316  resultString = baseString.substr(2,baseString.length()-4);
317  // Strip one of the string --'s from both ends of the string
318  manipulatedString = strip(manipulatedString, stringToBeRemoved, 1);
319  TUASSERTE(std::string, resultString, manipulatedString);
320 
321  manipulatedString = baseString;
322  resultString = baseString.substr(3, baseString.length()-6);
323  // Strip all of the character -'s from both ends of the string
324  manipulatedString = strip(manipulatedString, charToBeRemoved);
325  TUASSERTE(std::string, resultString, manipulatedString);
326 
327  manipulatedString = baseString;
328  resultString = baseString.substr(2, baseString.length()-4);
329  // Strip all of the string --'s from both ends of the string
330  manipulatedString = strip(manipulatedString, stringToBeRemoved);
331  TUASSERTE(std::string, resultString, manipulatedString);
332 
333  TURETURN();
334  }
335 
336 
342  unsigned translateTest()
343  {
344  TUDEF("StringUtils", "strip");
345  // Original String to replace characters on
346  string baseString = "Hello World! I am the Translation test String!";
347  // storage for manipulated base string
348  string manipulatedString;
349  // Characters to replace
350  string translateFrom = "aeoWTS!";
351  // Characters that will replace the ones in translateFrom
352  string translateTo1 = "@30M7Q1";
353  // Correctly translated base string with translateTo1
354  string expResult1 = "H3ll0 M0rld1 I @m th3 7r@nsl@ti0n t3st Qtring1";
355  // Characters that will replace the ones in translateFrom
356  string translateTo2 = "@30M7";
357  // Correctly translated base string with translateTo2
358  string expResult2 = "H3ll0 M0rld I @m th3 7r@nsl@ti0n t3st tring ";
359  // Characters that will replace the ones in translateFrom
360  string translateTo3 = "aeoWTS!";
361  // Correctly translated base string with translateTo3
362  string expResult3 = "Hello World! I am the Translation test String!";
363 
364  manipulatedString = translate(baseString, translateFrom, translateTo1);
365  TUASSERTE(std::string, expResult1, manipulatedString);
366 
367  manipulatedString = translate(baseString, translateFrom, translateTo2);
368  TUASSERTE(std::string, expResult2, manipulatedString);
369 
370  manipulatedString = translate(baseString, translateFrom, translateTo3);
371  TUASSERTE(std::string, expResult3, manipulatedString);
372 
373  TURETURN();
374  }
375 
376 
383  {
384  TUDEF("StringUtils", "asInt");
385  // String to convert to a number
386  string baseString = "12345.67890";
387  // Storage for converted string and the expected result value
388  int resultingInt, comparisonInt = 12345;
389  // Storage for converted string and the expected result value
390  double resultingDouble, comparisonDouble = 12345.67890;
391  // Storage for converted string and the expected result value
392  float resultingFloat, comparisonFloat = 12345.67890f;
393  // Storage for converted string and the expected result value
394  long double resultingLongDouble, comparisonLongDouble = 12345.67890L;
395 
396  resultingInt = asInt(baseString);
397  TUASSERTE(int, comparisonInt, resultingInt);
398 
399  TUCSM("asDouble");
400  resultingDouble = asDouble(baseString);
401  TUASSERTE(double, comparisonDouble, resultingDouble);
402 
403  TUCSM("asFloat");
404  resultingFloat = asFloat(baseString);
405  TUASSERTE(float, comparisonFloat, resultingFloat);
406 
407  TUCSM("asLongDouble");
408  resultingLongDouble = asLongDouble(baseString);
409  TUASSERTE(long double, comparisonLongDouble, resultingLongDouble);
410 
411  TURETURN();
412  }
413 
414 
425  {
426  TUDEF("StringUtils", "asString");
427  string convertedString; // Storage for converted number
428  int sampleInt = 1234567890; // Example integer to test
429  double sampleDouble = 12345.67890; // Example double to test
430  float sampleFloat = 12345.7f; // Example float to test
431  // Example long double to test
432  long double sampleLongDouble = 12345.67890L;
433 
434  convertedString = asString(sampleInt);
435  TUASSERTE(int, sampleInt, asInt(convertedString));
436 
437  convertedString = asString(sampleDouble);
438  TUASSERTE(double, sampleDouble, asDouble(convertedString));
439 
440  convertedString = asString(sampleFloat);
441  TUASSERTE(float, sampleFloat, asFloat(convertedString));
442 
443  convertedString = asString(sampleLongDouble);
444  TUASSERTE(long double, sampleLongDouble, asLongDouble(convertedString));
445 
446  TURETURN();
447  }
448 
449 
455  unsigned hexConversionTest()
456  {
457  TUDEF("StringUtils", "x2d");
458  // Example string with hex values in it
459  string initialHexString = "0x100 foo";
460  // Resulting string when the hex -> decimal conversion was made
461  string correctConvertedHexString = "256 foo";
462  // Example decimal string
463  string initialDecimalString ="100 bar";
464  // Resulting string when the decimal -> hex conversion was made
465  string correctConvertedDecimalString = "64 bar";
466  // Storage for manipulated strings
467  string manipulatedResult;
468 
469  manipulatedResult = x2d(initialHexString);
470  TUASSERTE(std::string, correctConvertedHexString, initialHexString);
471 
472  TUCSM("d2x");
473  manipulatedResult = d2x(initialDecimalString);
474  TUASSERTE(std::string, correctConvertedDecimalString,
475  initialDecimalString);
476 
477  TURETURN();
478  }
479 
480 
489  unsigned stringReplaceTest()
490  {
491  TUDEF("StringUtils", "replaceAll");
492  string baseString = "010101000111010";
493  string replaced1s = "0-0-0-000---0-0";
494  string replaced1withABC = "0ABC0ABC0ABC000ABCABCABC0ABC0";
495  string replaced1with123 = "01230123012300012312312301230";
496  string manipulatedString;
497 
498  manipulatedString = baseString;
499  manipulatedString = replaceAll(manipulatedString, string("1"),
500  string("-"));
501  TUASSERTE(std::string, replaced1s, manipulatedString);
502 
503  manipulatedString = baseString;
504  manipulatedString = replaceAll(manipulatedString, string("1"),
505  string("ABC"));
506  TUASSERTE(std::string, replaced1withABC, manipulatedString);
507 
508  manipulatedString = baseString;
509  manipulatedString = replaceAll(manipulatedString, string("1"),
510  string("123"));
511  TUASSERTE(std::string, replaced1with123, manipulatedString);
512 
513  TURETURN();
514  }
515 
516 
522  unsigned wordTest()
523  {
524  TUDEF("StringUtils", "firstWord");
525  // Original string to manipulate
526  string originalString = " The Quick Brown Fox Jumps Over"
527  " The Lazy Dog. ";
528  // Expected result for use of firstWord with the original string
529  string firstWordInString = "The";
530  // Number of words in the original string
531  int numberOfWords = 9;
532  int resultInt;
533  // Expected result for the fifth word in the original string
534  string fifthWordInString = "Jumps";
535  // Expected result for removal of the 4th and 5th words
536  string removedFourthFifthWords = " The Quick Brown Over The"
537  " Lazy Dog. ";
538  // Expected result for the removal of the 1st word in
539  // removedFourthFifthWords
540  string removedFirstWord = "Quick Brown Over The Lazy "
541  "Dog. ";
542  // Expected result of the removal of the sixth word in
543  // removedFirstWord
544  string removedSixthWord = "Quick Brown Over The Lazy";
545  // Expected result of the reporting of the words from the
546  // fourth to the end of removedSixthWord
547  string allWordsFromFourthOn = "The Lazy";
548  // String to store resulting data.
549  string resultString;
550 
551  resultString = firstWord("");
552  TUASSERTE(std::string, "", resultString);
553 
554  resultString = firstWord(originalString);
555  TUASSERTE(std::string, firstWordInString, resultString);
556 
557  TUCSM("numWords");
558  resultInt = numWords("");
559  TUASSERTE(int, 0, resultInt);
560 
561  resultInt = numWords(originalString);
562  TUASSERTE(int, numberOfWords, resultInt);
563 
564  TUCSM("word");
565  resultString = word("",2);
566  TUASSERTE(std::string, "", resultString);
567 
568  resultString = word(originalString,4);
569  TUASSERTE(std::string, fifthWordInString, resultString);
570 
571  resultString = word(originalString,10);
572  TUASSERTE(std::string, "", resultString);
573 
574  std::string empty;
575 
576  TUCSM("removeWords");
577  // This changes the string passed to the method
578  resultString = removeWords(empty, 1, 1);
579  TUASSERTE(std::string, "", resultString);
580 
581  resultString = removeWords(originalString, 3, 2);
582  TUASSERTE(std::string, removedFourthFifthWords, originalString);
583  TUASSERTE(std::string, removedFourthFifthWords, resultString);
584 
585  TUCSM("stripFirstWord");
586  // This changes the string passed to the method
587  resultString = stripFirstWord(empty);
588  TUASSERTE(std::string, "", resultString);
589 
590  resultString = stripFirstWord(originalString);
591  TUASSERTE(std::string, removedFirstWord, originalString);
592  TUASSERTE(std::string, firstWordInString, resultString);
593 
594  TUCSM("removeWords");
595  // This changes the string passed to the method
596  resultString = removeWords(empty, 2);
597  TUASSERTE(std::string, "", resultString);
598  TUASSERTE(std::string, "", removeWords(empty, 0));
599  TUASSERTE(std::string, "", removeWords(empty, 1));
600  TUASSERTE(std::string, "", removeWords(empty, 2));
601 
602  std::string oneWord("foo");
603  TUASSERTE(std::string, "foo", removeWords(oneWord, 2));
604  TUASSERTE(std::string, "foo", removeWords(oneWord, 1));
605  TUASSERTE(std::string, "", removeWords(oneWord, 0));
606 
607  resultString = removeWords(originalString, 5);
608  TUASSERTE(std::string, removedSixthWord, originalString);
609  TUASSERTE(std::string, removedSixthWord, resultString);
610 
611  std::string removeAllOfMe(originalString);
612  resultString = removeWords(removeAllOfMe, 0);
613  TUASSERTE(std::string, "", removeAllOfMe);
614  TUASSERTE(std::string, "", resultString);
615 
616  TUCSM("words");
617  TUASSERTE(std::string, "", words("", 0));
618  TUASSERTE(std::string, "", words("", 1));
619  resultString = words("", 2);
620  TUASSERTE(std::string, std::string(), resultString);
621 
622  resultString = words(originalString, 3);
623  TUASSERTE(std::string, allWordsFromFourthOn, resultString);
624 
625  resultString = words(originalString, 10);
626  TUASSERTE(std::string, std::string(), resultString);
627 
628  TUASSERTE(std::string, "foo", words("foo", 0));
629  TUASSERTE(std::string, "", words("foo", 1));
630  TUASSERTE(std::string, "", words("foo", 2));
631 
632  TURETURN();
633  }
634 
635 
643  {
644  TUDEF("StringUtils", "isLike");
645  // String to be searched
646  string originalString = " The Quick Brown Fox Jumps Over"
647  " The Lazy Dog. ";
648  // Storage for isLike result
649  int resultInt;
650  // A query which will be found
651  string testedRegularExpression1 = "Br.*Over.*";
652  // A query which will not be found
653  string testedRegularExpression2 = "Br.Over.*";
654  // Storage for matches method
655  string resultString;
656  // Expected string to be found
657  string correctResult = "Brown Fox Jumps Over The Lazy"
658  " Dog. ";
659 
660  resultInt = isLike(originalString, testedRegularExpression1);
661  TUASSERTE(int, 1, resultInt);
662 
663  resultInt = isLike(originalString, testedRegularExpression2);
664  TUASSERTE(int, 0, resultInt);
665 
666  TUCSM("matches");
667  resultString = matches(originalString, testedRegularExpression1);
668  TUASSERTE(std::string, correctResult, resultString);
669 
670  TURETURN();
671  }
672 
673 
679  unsigned prettyPrintTest()
680  {
681  TUDEF("StringUtils", "prettyPrint");
682  string originalString("This is a very long sentence that will get cut"
683  " up into lines with a maximum length of 40"
684  " characters, each line indented by a few"
685  " spaces.");
686  string prettyPrintThis = originalString;
687  string prettyPrinted = " This is a very long sentence that\n"
688  " will get cut up into lines with a\n"
689  " maximum length of 40 characters,\n"
690  " each line indented by a few spaces.\n";
691 
692  prettyPrint(prettyPrintThis, "\n", " ", " ", 40);
693  TUASSERTE(std::string, prettyPrinted, prettyPrintThis);
694 
695  prettyPrintThis = "ThisXtestsXprintingXwithXaXwordXdelimiterXotherXthan"
696  "XspaceXasXwellXasXlineXdelimetersXotherXthanXnewline";
697  prettyPrinted = "mehhhhhhhhXThisXtestsXprintingXwithXaZZZZcruddycrudcrud"
698  "XwordXdelimiterXotherZZZZcruddycrudcrudXthanXspaceXasXwellXasZZZZ"
699  "cruddycrudcrudXlineXdelimetersXotherZZZZcruddycrudcrudXthanXnewline"
700  "ZZZZ";
701  prettyPrint(prettyPrintThis, "ZZZZ", "cruddycrudcrud", "mehhhhhhhh", 37,
702  'X');
703  TUASSERTE(std::string, prettyPrinted, prettyPrintThis);
704 
705  // This tests handling of newlines which is useful for
706  // command line option documentation in particular.
707  originalString = "Bit-field: inject errors"
708  "\nbit 0 - corrupt lengths"
709  "\nbit 1 - corrupt addressing"
710  "\nbit 2 - corrupt checksum"
711  "\nbit 3 - corrupt `format'";
712  prettyPrintThis = originalString;
713  prettyPrinted = " -j, --error-inj=NUM Bit-field: inject errors\n"
714  " bit 0 - corrupt lengths\n"
715  " bit 1 - corrupt addressing\n"
716  " bit 2 - corrupt checksum\n"
717  " bit 3 - corrupt `format'\n";
718  prettyPrint(prettyPrintThis, "\n", " ",
719  " -j, --error-inj=NUM ", 80);
720  TUASSERTE(std::string, prettyPrinted, prettyPrintThis);
721 
722  TURETURN();
723  }
724 
725 
733  {
734  TUDEF("StringUtils", "splitWithQuotes");
735  // no quotes
736  std::string originalString("String with no quotes");
737  std::vector<std::string> expectedResult;
738  expectedResult.push_back("String");
739  expectedResult.push_back("with");
740  expectedResult.push_back("no");
741  expectedResult.push_back("quotes");
742  std::vector<std::string> observedResult =
743  splitWithQuotes(originalString);
744  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
745 
746  // empty string
747  originalString = "";
748  expectedResult.clear();
749  observedResult = splitWithQuotes(originalString);
750  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
751 
752  // only whitespace
753  originalString = " ";
754  expectedResult.clear();
755  expectedResult.push_back("");
756  expectedResult.push_back("");
757  observedResult = splitWithQuotes(originalString,' ',false,false);
758  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
759 
760  // double quotes
761  originalString = std::string("String with \"double quoted values\"");
762  expectedResult.clear();
763  expectedResult.push_back("String");
764  expectedResult.push_back("with");
765  expectedResult.push_back("double quoted values");
766  observedResult = splitWithQuotes(originalString);
767  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
768 
769  // single quotes
770  originalString= std::string("String with \'single quoted values\'");
771  expectedResult.clear();
772  expectedResult.push_back("String");
773  expectedResult.push_back("with");
774  expectedResult.push_back("single quoted values");
775  observedResult = splitWithQuotes(originalString);
776  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
777 
778  // leading quote
779  originalString= std::string("\"First words\" quoted");
780  expectedResult.clear();
781  expectedResult.push_back("First words");
782  expectedResult.push_back("quoted");
783  observedResult = splitWithQuotes(originalString);
784  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
785 
786  // back to back quotes
787  originalString= std::string("\"Back to\" \"back quotes\"");
788  expectedResult.clear();
789  expectedResult.push_back("Back to");
790  expectedResult.push_back("back quotes");
791  observedResult = splitWithQuotes(originalString);
792  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
793 
794  // empty, non-whitespace fields
795  originalString =
796  std::string("Zan.35(KB04-KB01), +90 7 7.73619,,,,-1.52405,no,,,");
797  expectedResult.clear();
798  expectedResult.push_back("Zan.35(KB04-KB01)");
799  expectedResult.push_back("+90 7 7.73619");
800  expectedResult.push_back("");
801  expectedResult.push_back("");
802  expectedResult.push_back("");
803  expectedResult.push_back("-1.52405");
804  expectedResult.push_back("no");
805  expectedResult.push_back("");
806  expectedResult.push_back("");
807  expectedResult.push_back("");
808  observedResult = splitWithQuotes(originalString,',',true,false);
809  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
810 
811  TURETURN();
812  }
813 
820  {
821  TUDEF("StringUtils", "splitWithDoubleQuotes");
822  // no quotes
823  std::string originalString("String with no quotes");
824  std::vector<std::string> expectedResult;
825  expectedResult.push_back("String");
826  expectedResult.push_back("with");
827  expectedResult.push_back("no");
828  expectedResult.push_back("quotes");
829  std::vector<std::string> observedResult =
830  splitWithDoubleQuotes(originalString);
831  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
832 
833  // empty string
834  originalString = "";
835  expectedResult.clear();
836  observedResult = splitWithDoubleQuotes(originalString);
837  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
838 
839  // only whitespace
840  originalString = " ";
841  expectedResult.clear();
842  expectedResult.push_back("");
843  expectedResult.push_back("");
844  observedResult = splitWithDoubleQuotes(originalString,' ',false,false);
845  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
846 
847  // double quotes
848  originalString = std::string("String with \"double quoted values\"");
849  expectedResult.clear();
850  expectedResult.push_back("String");
851  expectedResult.push_back("with");
852  expectedResult.push_back("double quoted values");
853  observedResult = splitWithDoubleQuotes(originalString);
854  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
855 
856  // single quotes
857  originalString= std::string("String with \'single quoted values\'");
858  expectedResult.clear();
859  expectedResult.push_back("String");
860  expectedResult.push_back("with");
861  expectedResult.push_back("\'single");
862  expectedResult.push_back("quoted");
863  expectedResult.push_back("values\'");
864  observedResult = splitWithDoubleQuotes(originalString);
865  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
866 
867  // leading quote
868  originalString= std::string("\"First words\" quoted");
869  expectedResult.clear();
870  expectedResult.push_back("First words");
871  expectedResult.push_back("quoted");
872  observedResult = splitWithDoubleQuotes(originalString);
873  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
874 
875  // back to back quotes
876  originalString= std::string("\"Back to\" \"back quotes\"");
877  expectedResult.clear();
878  expectedResult.push_back("Back to");
879  expectedResult.push_back("back quotes");
880  observedResult = splitWithDoubleQuotes(originalString);
881  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
882 
883  // empty, non-whitespace fields
884  originalString =
885  std::string("Zan.35(KB04-KB01), +90 7 7.73619,,,,-1.52405,no,,,");
886  expectedResult.clear();
887  expectedResult.push_back("Zan.35(KB04-KB01)");
888  expectedResult.push_back("+90 7 7.73619");
889  expectedResult.push_back("");
890  expectedResult.push_back("");
891  expectedResult.push_back("");
892  expectedResult.push_back("-1.52405");
893  expectedResult.push_back("no");
894  expectedResult.push_back("");
895  expectedResult.push_back("");
896  expectedResult.push_back("");
897  observedResult = splitWithDoubleQuotes(originalString,',',true,false);
898  TUASSERTE(std::vector<std::string>, expectedResult, observedResult);
899 
900 
901  TURETURN();
902  }
903 
904 
916  unsigned hexToAsciiTest()
917  {
918  TUDEF("StringUtils", "hexDumpData");
919  stringstream hexDumpStream;
920  string correctHexDump;
921  stringstream configHexDumpStream;
922  string correctConfigHexDump;
923  string correctPrintableMessage;
924  stringstream printableMessageStream;
925 
926  // Correct format for hexDumpData
927  string refPath = gnsstk::getPathData() +
928  gnsstk::getFileSep() +
929  "StringUtils" +
930  gnsstk::getFileSep() +
931  "hexToAsciiTest_";
932  correctHexDump = getFileContents(refPath + "hexDump.exp");
933 
934  // Correct format for the configured hexDumpData.
935  correctConfigHexDump = getFileContents(refPath + "configHexDump.exp");
936 
937  // Build the hexDumpString and output it to stringstream
938  string hexDumpString(hexDumpArray1, sizeof(hexDumpArray1));
939  hexDumpData(hexDumpStream, hexDumpString);
940  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
941 
942  // configure the hexDumpData, dump it to output stringstream
943  HexDumpDataConfig hdcfg(true, true, true, 8, 2, 2, 1, 0, 0, 16, true,
944  '\'', 1);
945  hexDumpData(configHexDumpStream, hexDumpString, 6, hdcfg);
946  TUCSM("HexDumpDataConfig");
947  TUASSERTE(std::string, correctConfigHexDump, configHexDumpStream.str());
948 
949  // Correct format for the printable
950  correctPrintableMessage = getFileContents(refPath +
951  "printableMessage.exp");
952  printableMessageStream << printable(hexDumpString) ;
953  TUCSM("printable");
954  TUASSERTE(std::string, correctPrintableMessage,
955  printableMessageStream.str());
956 
957  TURETURN();
958  }
959 
960 
967  unsigned hexDumpDataTest()
968  {
969  TUDEF("StringUtils", "hexDumpData");
970  stringstream hexDumpStream;
971  string correctHexDump;
972  stringstream configHexDumpStream;
973  string correctConfigHexDump;
974  string correctPrintableMessage;
975  stringstream printableMessageStream;
976 
977  // Correct format for hexDumpData
978  string refPath = gnsstk::getPathData() +
979  gnsstk::getFileSep() +
980  "StringUtils" +
981  gnsstk::getFileSep() +
982  "hexDumpDataTest_";
983  correctHexDump = getFileContents(refPath + "hexDump.exp");
984 
985  // Correct format for the configured hexDumpData
986  correctConfigHexDump = getFileContents(refPath + "configHexDump.exp");
987 
988  // Build the hexDumpString and output it to stringstream
989  string hexDumpString(hexDumpArray2, sizeof(hexDumpArray2));
990  hexDumpData(hexDumpStream, hexDumpString);
991  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
992 
993  // configure the hexDumpData, dump it to output stringstream
994  HexDumpDataConfig hdcfg(true, true, true, 8, 2, 2, 1, 0, 0, 16, true,
995  '\'', 1);
996  hexDumpData(configHexDumpStream, hexDumpString, 6, hdcfg);
997  TUCSM("HexDumpDataConfig");
998  TUASSERTE(std::string, correctConfigHexDump, configHexDumpStream.str());
999 
1000  // Correct format for the printable
1001  correctPrintableMessage = getFileContents(refPath +
1002  "printableMessage.exp");
1003  printableMessageStream << printable(hexDumpString) ;
1004  TUCSM("printable");
1005  TUASSERTE(std::string, correctPrintableMessage,
1006  printableMessageStream.str());
1007 
1008  TURETURN();
1009  }
1010 
1011 
1017  {
1018  TUDEF("StringUtils", "hexDumpData");
1019  stringstream hexDumpStream;
1020  string correctHexDump;
1021 
1022  // Correct format for hexDumpData
1023  string refPath = gnsstk::getPathData() +
1024  gnsstk::getFileSep() +
1025  "StringUtils" +
1026  gnsstk::getFileSep() +
1027  "hexDumpDataStreamFlagTest_";
1028 
1029  correctHexDump = getFileContents(refPath + "hexDump.exp");
1030  // set some stream flags that could mess up the output of hexDumpData
1031  hexDumpStream << std::left << setw(99) << setfill('Z') << boolalpha
1032  << showbase << oct << scientific << showpos << uppercase;
1033  // Build the hexDumpString and output it to stringstream
1034  string hexDumpString(hexDumpArray2, sizeof(hexDumpArray2));
1035  hexDumpData(hexDumpStream, hexDumpString);
1036  hexDumpStream << "foo" << " " << true << " " << false << " " << 12 << " "
1037  << -1.23e12 << " " << hex << 0xdeadbeef << dec << endl;
1038  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1039 
1040  TURETURN();
1041  }
1042 
1043 
1048  {
1049  TUDEF("StringUtils", "hexDumpData");
1050  stringstream hexDumpStream;
1051  string correctHexDump;
1052  string hexDumpString;
1053 
1054  // Correct format for hexDumpData
1055  string refPath = gnsstk::getPathData() +
1056  gnsstk::getFileSep() +
1057  "StringUtils" +
1058  gnsstk::getFileSep() +
1059  "hexDumpDataConfigTest_";
1060 
1061  // no index
1062  HexDumpDataConfig cfg1(false, true, false, 4, 1, 1, 1, 8, 2, 16, true,
1063  0, 4);
1064  correctHexDump = getFileContents(refPath + "hexDump_1.exp");
1065  // Build the hexDumpString and output it to stringstream
1066  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1067  hexDumpData(hexDumpStream, hexDumpString, 0, cfg1);
1068  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1069  //cout << "cfg1:" << endl << hexDumpStream.str() << endl;
1070  hexDumpStream.str("");
1071 
1072  // no index, no ASCII
1073  HexDumpDataConfig cfg2(false, true, false, 4, 1, 1, 1, 8, 2, 16, false,
1074  0, 4);
1075  correctHexDump = getFileContents(refPath + "hexDump_2.exp");
1076  // Build the hexDumpString and output it to stringstream
1077  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1078  hexDumpData(hexDumpStream, hexDumpString, 0, cfg2);
1079  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1080  //cout << "cfg2:" << endl << hexDumpStream.str() << endl;
1081  hexDumpStream.str("");
1082 
1083  // no index, no ASCII, no second-level grouping
1084  HexDumpDataConfig cfg3(false, true, false, 4, 1, 1, 1, 0, 2, 16, false,
1085  0, 4);
1086  correctHexDump = getFileContents(refPath + "hexDump_3.exp");
1087  // Build the hexDumpString and output it to stringstream
1088  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1089  hexDumpData(hexDumpStream, hexDumpString, 0, cfg3);
1090  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1091  //cout << "cfg3:" << endl << hexDumpStream.str() << endl;
1092  hexDumpStream.str("");
1093 
1094  // no index, no ASCII, no second-level grouping, 8 bytes per line
1095  HexDumpDataConfig cfg4(false, true, false, 4, 1, 1, 1, 0, 2, 8, false,
1096  0, 4);
1097  correctHexDump = getFileContents(refPath + "hexDump_4.exp");
1098  // Build the hexDumpString and output it to stringstream
1099  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1100  hexDumpData(hexDumpStream, hexDumpString, 0, cfg4);
1101  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1102  //cout << "cfg4:" << endl << hexDumpStream.str() << endl;
1103  hexDumpStream.str("");
1104 
1105  // above + data base
1106  HexDumpDataConfig cfg5(false, true, false, 4, 1, 1, 1, 0, 2, 8, false,
1107  0, 4, true);
1108  correctHexDump = getFileContents(refPath + "hexDump_5.exp");
1109  // Build the hexDumpString and output it to stringstream
1110  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1111  hexDumpData(hexDumpStream, hexDumpString, 0, cfg5);
1112  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1113  //cout << "cfg5:" << endl << hexDumpStream.str() << endl;
1114  hexDumpStream.str("");
1115 
1116  // testing text separators
1117  HexDumpDataConfig cfg6(true, true, false, 6, ":MEH", 1, "y", 8, "zz",
1118  16, true, 't', "FOO", true, true, "", "");
1119  correctHexDump = getFileContents(refPath + "hexDump_6.exp");
1120  // Build the hexDumpString and output it to stringstream
1121  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1122  hexDumpData(hexDumpStream, hexDumpString, 0, cfg6);
1123  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1124  //cout << "cfg6:" << endl << hexDumpStream.str() << endl;
1125  hexDumpStream.str("");
1126 
1127  // and this is where I was going with the separator changes:
1128  // a hexDumpData output that could be relatively easily copy
1129  // pasted into C/C++ code.
1130  HexDumpDataConfig cfg7(false, false, false, 4, "", 1, ", ", 0, "zz",
1131  8, false, 't', "FOO", true, true, ",", "");
1132  correctHexDump = getFileContents(refPath + "hexDump_7.exp");
1133  // Build the hexDumpString and output it to stringstream
1134  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1135  hexDumpData(hexDumpStream, hexDumpString, 0, cfg7);
1136  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1137  //cout << "cfg7:" << endl << hexDumpStream.str() << endl;
1138  hexDumpStream.str("");
1139 
1140  // testing text separators and index bases w/ capitalization
1141  HexDumpDataConfig cfg8(true, true, true, 6, ":MEH", 1, "y", 8, "zz",
1142  16, true, 't', "FOO", true, true, "meep",
1143  "shiftthissuckertotherightsome");
1144  correctHexDump = getFileContents(refPath + "hexDump_8.exp");
1145  // Build the hexDumpString and output it to stringstream
1146  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1147  hexDumpData(hexDumpStream, hexDumpString, 0, cfg8);
1148  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1149  //cout << "cfg8:" << endl << hexDumpStream.str() << endl;
1150  hexDumpStream.str("");
1151 
1152  // testing text alignment with two-byte words
1153  HexDumpDataConfig cfg9(true, true, true, 6, ".", 2, "!", 0, "",
1154  16, true, '`', "_", true, true, "meep",
1155  "shiftthissuckertotherightsome");
1156  correctHexDump = getFileContents(refPath + "hexDump_9.exp");
1157  // Build the hexDumpString and output it to stringstream
1158  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1159  hexDumpData(hexDumpStream, hexDumpString, 0, cfg9);
1160  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1161  //cout << "cfg9:" << endl << hexDumpStream.str() << endl;
1162  hexDumpStream.str("");
1163 
1164  // testing where the final line is the same number of bytes
1165  // as every other line.
1166  HexDumpDataConfig cfg10(true, true, true, 6, ".", 2, "!", 0, "",
1167  16, true, '`', "_", true, true, "meep",
1168  "shiftthissuckertotherightsome");
1169  correctHexDump = getFileContents(refPath + "hexDump_10.exp");
1170  // Build the hexDumpString and output it to stringstream
1171  unsigned truesize = sizeof(hexDumpArray2);
1172  hexDumpString = string(hexDumpArray2, truesize - (truesize % 16));
1173  hexDumpData(hexDumpStream, hexDumpString, 0, cfg10);
1174  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1175  //cout << "cfg10:" << endl << hexDumpStream.str() << endl;
1176  hexDumpStream.str("");
1177 
1178  // testing fill
1179  HexDumpDataConfig cfg11(false, false, false, 0, "", 1, ", ", 0, "", 8,
1180  false, (char)0, "", true, false, ",", "");
1181  correctHexDump = getFileContents(refPath + "hexDump_11.exp");
1182  // Build the hexDumpString and output it to stringstream
1183  unsigned char allChars[256];
1184  for (unsigned i = 0; i < 256; i++)
1185  allChars[i] = i;
1186  hexDumpString = string((char*)allChars, 256);
1187  hexDumpData(hexDumpStream, hexDumpString, 0, cfg11);
1188  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1189  //cout << "cfg11:" << endl << hexDumpStream.str() << endl;
1190  hexDumpStream.str("");
1191 
1192  // After updating hexDumpData to support a pre- and
1193  // post-ascii pair of strings rather than a single character
1194  // on either side. (e.g. for C-style comments)
1195  HexDumpDataConfig cfg12(false, false, false, 0, "", 1, ", ", 0, "",
1196  8, true, " /* ", " */", true, false, ",", "",
1197  " ");
1198  correctHexDump = getFileContents(refPath + "hexDump_12.exp");
1199  // Build the hexDumpString and output it to stringstream
1200  hexDumpString = string(hexDumpArray2, sizeof(hexDumpArray2));
1201  hexDumpData(hexDumpString, hexDumpStream, cfg12);
1202  TUASSERTE(std::string, correctHexDump, hexDumpStream.str());
1203  //cout << "cfg12:" << endl << hexDumpStream.str() << endl;
1204  hexDumpStream.str("");
1205 
1206  TURETURN();
1207  }
1208 
1209  unsigned floatFormatTest()
1210  {
1211  TUDEF("StringUtils", "floatFormat");
1212 
1213  double p1 = 1.2345; // positive test value
1214  double n1 = -1.2345; // negative test value
1215  TUASSERTE(string, "0.1234e+01", floatFormat(p1,FFLead::Zero,5,2));
1216  TUASSERTE(string, ".12345e+01", floatFormat(p1,FFLead::Decimal,5,2));
1217  TUASSERTE(string, "1.2345e+00", floatFormat(p1,FFLead::NonZero,5,2));
1218  TUASSERTE(string, "0.1234d+01", floatFormat(p1,FFLead::Zero,5,2,0,'d'));
1219  TUASSERTE(string, "0.1234D+01", floatFormat(p1,FFLead::Zero,5,2,0,'D'));
1220  TUASSERTE(string, "0.1234x+01", floatFormat(p1,FFLead::Zero,5,2,0,'x'));
1221  TUASSERTE(string, "0.1234E+01", floatFormat(p1,FFLead::Zero,5,2,0,'E',
1222  FFSign::NegOnly));
1223  TUASSERTE(string, " 0.1234E+01", floatFormat(p1,FFLead::Zero,5,2,0,'E',
1224  FFSign::NegSpace));
1225  TUASSERTE(string, "+0.1234E+01", floatFormat(p1,FFLead::Zero,5,2,0,'E',
1226  FFSign::NegPos));
1227  TUASSERTE(string, "+0.1234E+0001", floatFormat(p1,FFLead::Zero,5,4,0,'E',
1228  FFSign::NegPos));
1229 
1230  TUASSERTE(string, ".1234e+01", floatFormat(p1,FFLead::Decimal,4,2));
1231 
1232  TUASSERTE(string, "-0.1234e+01", floatFormat(n1,FFLead::Zero,5,2));
1233  TUASSERTE(string, "-.12345e+01", floatFormat(n1,FFLead::Decimal,5,2,0));
1234  TUASSERTE(string, "-1.2345e+00", floatFormat(n1,FFLead::NonZero,5,2,0));
1235  TUASSERTE(string, "-0.1234d+01", floatFormat(n1,FFLead::Zero,5,2,0,'d'));
1236  TUASSERTE(string, "-0.1234D+01", floatFormat(n1,FFLead::Zero,5,2,0,'D'));
1237  TUASSERTE(string, "-0.1234x+01", floatFormat(n1,FFLead::Zero,5,2,0,'x'));
1238  TUASSERTE(string, "-0.1234E+01", floatFormat(n1,FFLead::Zero,5,2,0,'E',
1239  FFSign::NegOnly));
1240  TUASSERTE(string, "-0.1234E+01", floatFormat(n1,FFLead::Zero,5,2,0,'E',
1241  FFSign::NegSpace));
1242  TUASSERTE(string, "-0.1234E+01", floatFormat(n1,FFLead::Zero,5,2,0,'E',
1243  FFSign::NegPos));
1244  TUASSERTE(string, "-0.1234E+0001", floatFormat(n1,FFLead::Zero,5,4,0,'E',
1245  FFSign::NegPos));
1246 
1247  TUASSERTE(string, "0.000000000000000e+000",
1248  floatFormat(0, FFLead::Zero, 16, 3));
1249  TUASSERTE(string, "-0.1234E+0001 ",
1250  floatFormat(n1,FFLead::Zero,5,4,19,'E',FFSign::NegPos));
1251  TUASSERTE(string, "-0.1234E+0001 ",
1252  floatFormat(n1,FFLead::Zero,5,4,19,'E',FFSign::NegPos,
1253  FFAlign::Left));
1254  TUASSERTE(string, " -0.1234E+0001",
1255  floatFormat(n1,FFLead::Zero,5,4,19,'E',FFSign::NegPos,
1256  FFAlign::Right));
1257 
1258  TURETURN();
1259  }
1260 };
1261 
1262 int main() // Main function to initialize and run all tests above
1263 {
1264  unsigned errorTotal = 0;
1265  StringUtils_T testClass;
1266 
1267  errorTotal += testClass.justificationTest();
1268  errorTotal += testClass.stripLeadingTest();
1269  errorTotal += testClass.stripTrailingTest();
1270  errorTotal += testClass.stripTest();
1271  errorTotal += testClass.stringToNumberTest();
1272  errorTotal += testClass.numberToStringTest();
1273  errorTotal += testClass.hexConversionTest();
1274  errorTotal += testClass.stringReplaceTest();
1275  errorTotal += testClass.wordTest();
1276  errorTotal += testClass.regularExpressionsTest();
1277  errorTotal += testClass.prettyPrintTest();
1278  errorTotal += testClass.splitWithQuotesTest();
1279  errorTotal += testClass.splitWithDoubleQuotesTest();
1280  errorTotal += testClass.hexDumpDataTest();
1281  errorTotal += testClass.hexDumpDataStreamFlagTest();
1282  errorTotal += testClass.hexDumpDataConfigTest();
1283  errorTotal += testClass.hexToAsciiTest();
1284  errorTotal += testClass.floatFormatTest();
1285 
1286  std::cout << "Total Failures for " << __FILE__ << ": " << errorTotal
1287  << std::endl;
1288 
1289  return errorTotal;
1290 }
gnsstk::StringUtils::replaceAll
std::string & replaceAll(std::string &s, const std::string &oldString, const std::string &newString)
Definition: StringUtils.hpp:1849
TUCSM
#define TUCSM(METHOD)
Definition: TestUtil.hpp:59
gnsstk::StringUtils::asInt
long asInt(const std::string &s)
Definition: StringUtils.hpp:713
gnsstk::StringUtils::HexDumpDataConfig
Class for configuring the appearance of hexDumpData() output.
Definition: HexDumpDataConfig.hpp:58
gnsstk::StringUtils::FFAlign::Left
@ Left
Formatted output will be left-aligned.
gnsstk::StringUtils::word
std::string word(const std::string &s, const std::string::size_type wordNum=0, const char delimiter=' ')
Definition: StringUtils.hpp:1112
StringUtils.hpp
gnsstk::StringUtils::center
std::string & center(std::string &s, const std::string::size_type length, const char pad=' ')
Definition: StringUtils.hpp:1607
StringUtils_T::splitWithQuotesTest
unsigned splitWithQuotesTest()
Definition: StringUtils_T.cpp:732
StringUtils_T::hexDumpDataTest
unsigned hexDumpDataTest()
Definition: StringUtils_T.cpp:967
gnsstk::StringUtils::removeWords
std::string & removeWords(std::string &s, const std::string::size_type first=0, const std::string::size_type wordsToReplace=std::string::npos, const char delimiter=' ')
Definition: StringUtils.hpp:2429
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
gnsstk::StringUtils::asFloat
float asFloat(const std::string &s)
Definition: StringUtils.hpp:1636
gnsstk::StringUtils::asLongDouble
long double asLongDouble(const std::string &s)
Definition: StringUtils.hpp:1652
gnsstk::StringUtils::printable
std::string printable(const std::string &aStr)
Definition: StringUtils.hpp:2731
gnsstk::StringUtils::asString
std::string asString(IonexStoreStrategy e)
Convert a IonexStoreStrategy to a whitespace-free string name.
Definition: IonexStoreStrategy.cpp:46
hexDumpArray1
const char hexDumpArray1[]
Array of hex values to output for hexToAsciiTest.
Definition: StringUtils_T.cpp:63
StringUtils_T::stripLeadingTest
unsigned stripLeadingTest()
Definition: StringUtils_T.cpp:212
StringUtils_T::justificationTest
unsigned justificationTest()
Definition: StringUtils_T.cpp:170
gnsstk::StringUtils::FFSign::NegPos
@ NegPos
Prefix output with a minus sign (neg) or plus sign (pos)
gnsstk::StringUtils::stripLeading
std::string & stripLeading(std::string &s, const std::string &aString, std::string::size_type num=std::string::npos)
Definition: StringUtils.hpp:1426
gnsstk::StringUtils::FFAlign::Right
@ Right
Formatted output will be right-aligned.
gnsstk::StringUtils::words
std::string words(const std::string &s, const std::string::size_type firstWord=0, const std::string::size_type numWords=std::string::npos, const char delimiter=' ')
Definition: StringUtils.hpp:2199
StringUtils_T::hexDumpDataConfigTest
unsigned hexDumpDataConfigTest()
Definition: StringUtils_T.cpp:1047
StringUtils_T::numberToStringTest
unsigned numberToStringTest()
Definition: StringUtils_T.cpp:424
StringUtils_T::floatFormatTest
unsigned floatFormatTest()
Definition: StringUtils_T.cpp:1209
gnsstk::StringUtils::floatFormat
std::string floatFormat(double d, FFLead lead, unsigned mantissa, unsigned exponent, unsigned width, char expChar, FFSign sign, FFAlign align)
Definition: StringUtils.cpp:210
gnsstk::StringUtils::stripTrailing
std::string & stripTrailing(std::string &s, const std::string &aString, std::string::size_type num=std::string::npos)
Definition: StringUtils.hpp:1453
StringUtils_T::stripTrailingTest
unsigned stripTrailingTest()
Definition: StringUtils_T.cpp:256
gnsstk::StringUtils::numWords
int numWords(const std::string &s, const char delimiter=' ')
Definition: StringUtils.hpp:2171
main
int main()
Definition: StringUtils_T.cpp:1262
gnsstk::StringUtils::FFSign::NegOnly
@ NegOnly
Prefix output with a minus sign (neg) or nothing (pos)
TestUtil.hpp
gnsstk::StringUtils::FFLead::Zero
@ Zero
Start with zero, e.g. 0.12345.
StringUtils_T::stripTest
unsigned stripTest()
Definition: StringUtils_T.cpp:300
gnsstk::StringUtils::FFSign::NegSpace
@ NegSpace
Prefix output with a minus sign (neg) or space (pos)
gnsstk::StringUtils::splitWithDoubleQuotes
std::vector< std::string > splitWithDoubleQuotes(const std::string &aStr, const char delimiter=' ', bool trimWhitespace=true, bool ignoreEmpty=true)
Definition: StringUtils.hpp:2376
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
gnsstk::StringUtils::d2x
std::string & d2x(std::string &s)
Definition: StringUtils.hpp:1708
gnsstk::StringUtils::hexDumpData
void hexDumpData(const std::string &data, std::ostream &s, const HexDumpDataConfig &cfg)
Definition: StringUtils.cpp:62
StringUtils_T::StringUtils_T
StringUtils_T()
Definition: StringUtils_T.cpp:142
gnsstk::StringUtils::FFLead::Decimal
@ Decimal
Start with decimal, e.g. .12345.
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
StringUtils_T::wordTest
unsigned wordTest()
Definition: StringUtils_T.cpp:522
gnsstk::StringUtils::FFLead::NonZero
@ NonZero
Start with the first non-zero digit, e.g. 1.2345.
gnsstk::StringUtils::stripFirstWord
std::string stripFirstWord(std::string &s, const char delimiter=' ')
Definition: StringUtils.hpp:2253
StringUtils_T::regularExpressionsTest
unsigned regularExpressionsTest()
Definition: StringUtils_T.cpp:642
gnsstk::StringUtils::asDouble
double asDouble(const std::string &s)
Definition: StringUtils.hpp:705
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
gnsstk::StringUtils::x2d
std::string & x2d(std::string &s)
Definition: StringUtils.hpp:1797
hexDumpArray2
const char hexDumpArray2[]
Definition: StringUtils_T.cpp:100
StringUtils_T::getFileContents
string getFileContents(const string &fileName)
Definition: StringUtils_T.cpp:149
StringUtils_T::hexDumpDataStreamFlagTest
unsigned hexDumpDataStreamFlagTest()
Definition: StringUtils_T.cpp:1016
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::StringUtils::isLike
bool isLike(const std::string &s, const std::string &aPattern, const char zeroOrMore=' *', const char oneOrMore='+', const char anyChar='.')
Definition: StringUtils.hpp:948
GNSSTK_ASSERT
#define GNSSTK_ASSERT(CONDITION)
Provide an "ASSERT" type macro.
Definition: Exception.hpp:373
gnsstk::StringUtils::rightJustify
std::string & rightJustify(std::string &s, const std::string::size_type length, const char pad=' ')
Definition: StringUtils.hpp:1557
StringUtils_T::hexToAsciiTest
unsigned hexToAsciiTest()
Definition: StringUtils_T.cpp:916
StringUtils_T::~StringUtils_T
~StringUtils_T()
Definition: StringUtils_T.cpp:143
StringUtils_T
Definition: StringUtils_T.cpp:139
std
Definition: Angle.hpp:142
gnsstk::StringUtils::firstWord
std::string firstWord(const std::string &s, const char delimiter=' ')
Definition: StringUtils.hpp:2138
StringUtils_T::hexConversionTest
unsigned hexConversionTest()
Definition: StringUtils_T.cpp:455
gnsstk::StringUtils::strip
std::string & strip(std::string &s, const std::string &aString, std::string::size_type num=std::string::npos)
Definition: StringUtils.hpp:1482
gnsstk::StringUtils::leftJustify
std::string & leftJustify(std::string &s, const std::string::size_type length, const char pad=' ')
Definition: StringUtils.hpp:1582
StringUtils_T::splitWithDoubleQuotesTest
unsigned splitWithDoubleQuotesTest()
Definition: StringUtils_T.cpp:819
StringUtils_T::stringReplaceTest
unsigned stringReplaceTest()
Definition: StringUtils_T.cpp:489
gnsstk::StringUtils::matches
std::string matches(const std::string &s, const std::string &aPattern, const char zeroOrMore=' *', const char oneOrMore='+', const char anyChar='.')
Definition: StringUtils.hpp:1934
StringUtils_T::translateTest
unsigned translateTest()
Definition: StringUtils_T.cpp:342
StringUtils_T::stringToNumberTest
unsigned stringToNumberTest()
Definition: StringUtils_T.cpp:382
gnsstk::StringUtils::prettyPrint
std::string & prettyPrint(std::string &aStr, const std::string &lineDelim="\n", const std::string &indent="", const std::string &firstIndent=" ", const std::string::size_type len=80, const char wordDelim=' ')
Definition: StringUtils.hpp:2774
gnsstk::StringUtils::splitWithQuotes
std::vector< std::string > splitWithQuotes(const std::string &aStr, const char delimiter=' ', bool trimWhitespace=true, bool ignoreEmpty=true)
Definition: StringUtils.hpp:2322
StringUtils_T::prettyPrintTest
unsigned prettyPrintTest()
Definition: StringUtils_T.cpp:679
gnsstk::StringUtils::translate
std::string translate(const std::string &aString, const std::string &inputChars, const std::string &outputChars, const char pad=' ')
Definition: StringUtils.hpp:1491


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:41