BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
testUtil.cxx
Go to the documentation of this file.
1/// @file testUtil.cxx
2#include "facilities/Util.h"
3#include <iostream>
4#include <string>
5/** Miniscule program to test a couple modes of Util::expandEnvVar
6 * Caller should have an environment variable SRC with some
7 * sensible definition.
8 */
9int main( int, char** ) {
10 std::string name = std::string( "${GLAST_EXT}/xerces" );
11 std::string nameAgain = std::string( "${GLAST_EXT}/xerces" );
12 std::string oDelim = std::string( "${" );
13 std::string cDelim = std::string( "}" );
14 int ntrans;
15
16 std::cout << "Test expandEnvVar: " << std::endl;
17 try
18 {
19 ntrans = facilities::Util::expandEnvVar( &name, oDelim, cDelim );
20 std::cout << "Translated name (right delimiters) is " << name << std::endl;
21 ntrans = facilities::Util::expandEnvVar( &nameAgain );
22 std::cout << "Translated name (wrong delimiters) is " << nameAgain << std::endl;
23 } catch ( facilities::Untranslatable err )
24 { std::cout << "Failed to completely translate " << name << std::endl; }
25
26 // Process running this must have environment variable SRC
27 std::string multi = std::string( "$(FACILITIESROOT)/$(SRC)" );
28
29 try
30 {
31 ntrans = facilities::Util::expandEnvVar( &multi );
32
33 std::cout << "Translated name is " << multi << std::endl;
34 std::cout << ntrans << " variables were translated." << std::endl;
35 } catch ( facilities::Untranslatable err )
36 { std::cout << "Failed to completely translate " << multi << std::endl; }
37
38 std::cout << std::endl << "Test itoa " << std::endl;
39 // Test the new itoa routine
40 std::string intStr;
41 facilities::Util::itoa( 12, intStr );
42 std::cout << "My String is " << intStr << std::endl;
43
44 // basename & stringTokenize
45 std::string unixname( "/a/path/myUnixFile.txt" );
46 std::string wname( "C:\\a\\longer\\path\\myWindowsFile.txt" );
47
48 std::vector<std::string> tokens;
49 unsigned int i;
50
51 std::cout << std::endl << "Test stringTokenize and basename" << std::endl;
52 facilities::Util::stringTokenize( unixname, "/", tokens );
53
54 std::cout << "Processing string " << unixname << std::endl;
55 for ( i = 0; i < tokens.size(); i++ )
56 { std::cout << "Token " << i << " is: " << tokens[i] << std::endl; }
57
58 std::cout << "basename is " << facilities::Util::basename( unixname ) << std::endl;
59
60 facilities::Util::stringTokenize( wname, "\\", tokens );
61
62 std::cout << "Processing string " << wname << std::endl;
63 for ( i = 0; i < tokens.size(); i++ )
64 { std::cout << "Token " << i << " is: " << tokens[i] << std::endl; }
65
66 std::cout << "basename is " << facilities::Util::basename( wname ) << std::endl;
67
68 // test keyValueTokenize to map routine
69 std::cout << std::endl << "Test keyValueTokenize " << std::endl;
70 std::string input1( "apple=green,lemon=yellow,blueberry=blue" );
71 std::cout << "Input string: '" << input1 << "'" << std::endl;
72 std::map<std::string, std::string> maptokens;
73 facilities::Util::keyValueTokenize( input1, ",", maptokens, "=", false );
74 std::map<std::string, std::string>::const_iterator tokens_itr = maptokens.begin();
75 while ( tokens_itr != maptokens.end() )
76 {
77 std::cout << "Token key " << ( *tokens_itr ).first
78 << " and value: " << ( *tokens_itr ).second << std::endl;
79 tokens_itr++;
80 }
81
82 std::cout << "appending to previous map:" << std::endl;
83 std::string input2( "apple2/green2,lemon2/yellow2,blueberry2/blue2" );
84 std::cout << "New string is '" << input2 << "'" << std::endl;
85 facilities::Util::keyValueTokenize( input2, ",", maptokens, "/", false );
86 tokens_itr = maptokens.begin();
87 while ( tokens_itr != maptokens.end() )
88 {
89 std::cout << "Token key " << ( *tokens_itr ).first
90 << " and value: " << ( *tokens_itr ).second << std::endl;
91 tokens_itr++;
92 }
93
94 std::cout << "clearing the map first:" << std::endl;
95 facilities::Util::keyValueTokenize( input2, ",", maptokens, "/", true );
96 tokens_itr = maptokens.begin();
97 while ( tokens_itr != maptokens.end() )
98 {
99 std::cout << "Token key " << ( *tokens_itr ).first
100 << " and value: " << ( *tokens_itr ).second << std::endl;
101 tokens_itr++;
102 }
103
104 std::cout << "Use a multi-character pairDelimiter argument " << std::endl;
105 std::string input3( "apple2:=green2 lemon2:=yellow2 blueberry2:=blue2" );
106 std::cout << "input is: '" << input3 << "'" << std::endl;
107 facilities::Util::keyValueTokenize( input3, " ", maptokens, ":=" );
108 tokens_itr = maptokens.begin();
109 while ( tokens_itr != maptokens.end() )
110 {
111 std::cout << "Token key " << ( *tokens_itr ).first
112 << " and value: " << ( *tokens_itr ).second << std::endl;
113 tokens_itr++;
114 }
115
116 // Test stringToDouble routine
117 std::cout << std::endl << "Test stringToDouble " << std::endl;
118 std::string okDouble( "3.14159" );
119 std::string badDouble( "3.garbage56" );
120
121 double result = -1;
122
123 try
124 {
125 result = facilities::Util::stringToDouble( okDouble );
126 std::cout << "Converted (string) " << okDouble << " to (double) " << result << std::endl;
127 } catch ( facilities::WrongType ex )
128 { std::cout << "Failed with exception " << ex.getMsg() << std::endl; }
129
130 try
131 {
132 result = facilities::Util::stringToDouble( badDouble );
133 std::cout << "Converted (string) " << badDouble << " to (double) " << result << std::endl;
134 } catch ( facilities::WrongType ex )
135 { std::cout << "Failed with exception " << ex.getMsg() << std::endl; }
136
137 // Test stringToInt routine
138 std::cout << std::endl << "Test stringToInt " << std::endl;
139
140 std::string okInt( "33550" );
141 std::string badInt1( "3garbage56" );
142 std::string badInt2( "garbage356" );
143
144 int intResult = -1;
145
146 try
147 {
148 intResult = facilities::Util::stringToInt( okInt );
149 std::cout << "Converted (string) " << okInt << " to (int) " << intResult << std::endl;
150 } catch ( facilities::WrongType ex )
151 { std::cout << "Failed with exception " << ex.getMsg() << std::endl; }
152
153 try
154 {
155 intResult = facilities::Util::stringToInt( badInt1 );
156 std::cout << "Converted (string) " << badInt1 << " to (int) " << intResult << std::endl;
157 } catch ( facilities::WrongType ex )
158 { std::cout << "Failed with exception " << ex.getMsg() << std::endl; }
159
160 try
161 {
162 intResult = facilities::Util::stringToInt( badInt2 );
163 std::cout << "Converted (string) " << badInt2 << " to (int) " << intResult << std::endl;
164 } catch ( facilities::WrongType ex )
165 { std::cout << "Failed with exception " << ex.getMsg() << std::endl; }
166
167 // Try out trimTrailing method
168 std::cout << std::endl << "Test trimTrailing " << std::endl;
169 std::string string1( "ends with 2 blanks " );
170 std::string string2( "ends with newline\n" );
171 std::string string3( "no trailing whitespace" );
172
173 unsigned nTrimmed = facilities::Util::trimTrailing( &string1 );
174 std::cout << "Trimmed " << nTrimmed << " from string1; has new value : " << string1
175 << "*EOS*" << std::endl;
176
177 nTrimmed = facilities::Util::trimTrailing( &string2 );
178 std::cout << "Trimmed " << nTrimmed << " from string2; has new value : " << string2 << "*EOS"
179 << std::endl;
180
181 nTrimmed = facilities::Util::trimTrailing( &string3 );
182 std::cout << "Trimmed " << nTrimmed << " from string3; has new value : " << string3 << "*EOS"
183 << std::endl;
184
185 return 0;
186}
static double stringToDouble(const std::string &InStr)
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
static std::string basename(const std::string &path)
static int stringToInt(const std::string &InStr)
static void keyValueTokenize(std::string input, const std::string &delimiters, std::map< std::string, std::string > &tokenMap, const std::string &pairDelimiter=std::string("="), bool clear=true)
static const char * itoa(int val, std::string &outStr)
static unsigned trimTrailing(std::string *toTrim)
Exception class used when converting from string to numeric type.
int main()
Definition phokhara.cc:42