ENH: fix uppercase version so defines are not upper as well
[cmake.git] / Source / kwsys / testSystemTools.cxx
blob65d42edc25801467963ce0bc1951e9660845957b
1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: testSystemTools.cxx,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #include "kwsysPrivate.h"
16 #if defined(_MSC_VER)
17 # pragma warning (disable:4786)
18 #endif
20 #include KWSYS_HEADER(SystemTools.hxx)
21 #include KWSYS_HEADER(ios/iostream)
23 // Work-around CMake dependency scanning limitation. This must
24 // duplicate the above list of headers.
25 #if 0
26 # include "SystemTools.hxx.in"
27 # include "kwsys_ios_iostream.h.in"
28 #endif
30 // Include with <> instead of "" to avoid getting any in-source copy
31 // left on disk.
32 #include <testSystemTools.h>
34 #include <string.h> /* strcmp */
36 //----------------------------------------------------------------------------
37 const char* toUnixPaths[][2] =
39 { "/usr/local/bin/passwd", "/usr/local/bin/passwd" },
40 { "/usr/lo cal/bin/pa sswd", "/usr/lo cal/bin/pa sswd" },
41 { "/usr/lo\\ cal/bin/pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
42 { "c:/usr/local/bin/passwd", "c:/usr/local/bin/passwd" },
43 { "c:/usr/lo cal/bin/pa sswd", "c:/usr/lo cal/bin/pa sswd" },
44 { "c:/usr/lo\\ cal/bin/pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
45 { "\\usr\\local\\bin\\passwd", "/usr/local/bin/passwd" },
46 { "\\usr\\lo cal\\bin\\pa sswd", "/usr/lo cal/bin/pa sswd" },
47 { "\\usr\\lo\\ cal\\bin\\pa\\ sswd", "/usr/lo\\ cal/bin/pa\\ sswd" },
48 { "c:\\usr\\local\\bin\\passwd", "c:/usr/local/bin/passwd" },
49 { "c:\\usr\\lo cal\\bin\\pa sswd", "c:/usr/lo cal/bin/pa sswd" },
50 { "c:\\usr\\lo\\ cal\\bin\\pa\\ sswd", "c:/usr/lo\\ cal/bin/pa\\ sswd" },
51 { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" },
52 { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" },
53 { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo\\ cal/bin/pa\\ sswd" },
54 {0, 0}
57 bool CheckConvertToUnixSlashes(kwsys_stl::string input,
58 kwsys_stl::string output)
60 kwsys_stl::string result = input;
61 kwsys::SystemTools::ConvertToUnixSlashes(result);
62 if ( result != output )
64 kwsys_ios::cerr
65 << "Problem with ConvertToUnixSlashes - input: " << input.c_str()
66 << " output: " << result.c_str() << " expected: " << output.c_str()
67 << kwsys_ios::endl;
68 return false;
70 return true;
73 //----------------------------------------------------------------------------
74 const char* checkEscapeChars[][4] =
76 { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
77 { " {} ", "{}", "#", " #{#} "},
78 {0, 0, 0, 0}
81 bool CheckEscapeChars(kwsys_stl::string input,
82 const char *chars_to_escape,
83 char escape_char,
84 kwsys_stl::string output)
86 kwsys_stl::string result = kwsys::SystemTools::EscapeChars(
87 input.c_str(), chars_to_escape, escape_char);
88 if (result != output)
90 kwsys_ios::cerr
91 << "Problem with CheckEscapeChars - input: " << input.c_str()
92 << " output: " << result.c_str() << " expected: " << output.c_str()
93 << kwsys_ios::endl;
94 return false;
96 return true;
99 //----------------------------------------------------------------------------
100 bool CheckFileOperations()
102 bool res = true;
104 if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_BIN_FILE) !=
105 kwsys::SystemTools::FileTypeBinary)
107 kwsys_ios::cerr
108 << "Problem with DetectFileType - failed to detect type of: "
109 << TEST_SYSTEMTOOLS_BIN_FILE << kwsys_ios::endl;
110 res = false;
113 if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_SRC_FILE) !=
114 kwsys::SystemTools::FileTypeText)
116 kwsys_ios::cerr
117 << "Problem with DetectFileType - failed to detect type of: "
118 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
119 res = false;
122 if (kwsys::SystemTools::FileLength(TEST_SYSTEMTOOLS_BIN_FILE) != 766)
124 kwsys_ios::cerr
125 << "Problem with FileLength - incorrect length for: "
126 << TEST_SYSTEMTOOLS_BIN_FILE << kwsys_ios::endl;
127 res = false;
130 return res;
133 //----------------------------------------------------------------------------
134 bool CheckStringOperations()
136 bool res = true;
138 kwsys_stl::string test = "mary had a little lamb.";
139 if (kwsys::SystemTools::CapitalizedWords(test) != "Mary Had A Little Lamb.")
141 kwsys_ios::cerr
142 << "Problem with CapitalizedWords "
143 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
144 res = false;
147 test = "Mary Had A Little Lamb.";
148 if (kwsys::SystemTools::UnCapitalizedWords(test) !=
149 "mary had a little lamb.")
151 kwsys_ios::cerr
152 << "Problem with UnCapitalizedWords "
153 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
154 res = false;
157 test = "MaryHadTheLittleLamb.";
158 if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test) !=
159 "Mary Had The Little Lamb.")
161 kwsys_ios::cerr
162 << "Problem with AddSpaceBetweenCapitalizedWords "
163 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
164 res = false;
167 char * cres =
168 kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
169 if (strcmp(cres,"Mary Had A Little Lamb."))
171 kwsys_ios::cerr
172 << "Problem with AppendStrings "
173 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
174 res = false;
176 delete [] cres;
178 cres =
179 kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
180 if (strcmp(cres,"Mary Had A Little Lamb."))
182 kwsys_ios::cerr
183 << "Problem with AppendStrings "
184 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
185 res = false;
187 delete [] cres;
189 if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
191 kwsys_ios::cerr
192 << "Problem with CountChar "
193 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
194 res = false;
197 cres =
198 kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
199 if (strcmp(cres,"Mry Hd A Lttl Lmb."))
201 kwsys_ios::cerr
202 << "Problem with RemoveChars "
203 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
204 res = false;
206 delete [] cres;
208 cres =
209 kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
210 if (strcmp(cres,"A"))
212 kwsys_ios::cerr
213 << "Problem with RemoveCharsButUpperHex "
214 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
215 res = false;
217 delete [] cres;
219 char *cres2 = new char [strlen("Mary Had A Little Lamb.")+1];
220 strcpy(cres2,"Mary Had A Little Lamb.");
221 kwsys::SystemTools::ReplaceChars(cres2,"aeiou",'X');
222 if (strcmp(cres2,"MXry HXd A LXttlX LXmb."))
224 kwsys_ios::cerr
225 << "Problem with ReplaceChars "
226 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
227 res = false;
229 delete [] cres2;
231 if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
232 "Mary "))
234 kwsys_ios::cerr
235 << "Problem with StringStartsWith "
236 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
237 res = false;
240 if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
241 " Lamb."))
243 kwsys_ios::cerr
244 << "Problem with StringEndsWith "
245 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
246 res = false;
249 cres = kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
250 if (strcmp(cres,"Mary Had A Little Lamb."))
252 kwsys_ios::cerr
253 << "Problem with DuplicateString "
254 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
255 res = false;
257 delete [] cres;
259 test = "Mary Had A Little Lamb.";
260 if (kwsys::SystemTools::CropString(test,13) !=
261 "Mary ...Lamb.")
263 kwsys_ios::cerr
264 << "Problem with CropString "
265 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
266 res = false;
269 kwsys_stl::vector<kwsys_stl::string> lines;
270 kwsys::SystemTools::Split("Mary Had A Little Lamb.",lines,' ');
271 if (lines[0] != "Mary" || lines[1] != "Had" ||
272 lines[2] != "A" || lines[3] != "Little" || lines[4] != "Lamb.")
274 kwsys_ios::cerr
275 << "Problem with Split "
276 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
277 res = false;
280 if (kwsys::SystemTools::ConvertToWindowsOutputPath
281 ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
282 "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
284 kwsys_ios::cerr
285 << "Problem with ConvertToWindowsOutputPath "
286 << kwsys_ios::endl;
287 res = false;
290 if (kwsys::SystemTools::ConvertToWindowsOutputPath
291 ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
292 "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
294 kwsys_ios::cerr
295 << "Problem with ConvertToWindowsOutputPath "
296 << kwsys_ios::endl;
297 res = false;
300 if (kwsys::SystemTools::ConvertToUnixOutputPath
301 ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
302 "/Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
304 kwsys_ios::cerr
305 << "Problem with ConvertToUnixOutputPath "
306 << kwsys_ios::endl;
307 res = false;
310 int targc;
311 char **targv;
312 kwsys::SystemTools::ConvertWindowsCommandLineToUnixArguments
313 ("\"Local Mojo\\Voodoo.asp\" -CastHex \"D:\\My Secret Mojo\\Voodoo.mp3\"", &targc, &targv);
314 if (targc != 4 || strcmp(targv[1],"Local Mojo\\Voodoo.asp") ||
315 strcmp(targv[2],"-CastHex") ||
316 strcmp(targv[3],"D:\\My Secret Mojo\\Voodoo.mp3"))
318 kwsys_ios::cerr
319 << "Problem with ConvertWindowsCommandLineToUnixArguments"
320 << TEST_SYSTEMTOOLS_SRC_FILE << kwsys_ios::endl;
321 res = false;
323 for (;targc >=0; --targc)
325 delete [] targv[targc];
327 delete [] targv;
329 return res;
332 //----------------------------------------------------------------------------
333 int testSystemTools(int, char*[])
335 bool res = true;
337 int cc;
338 for ( cc = 0; toUnixPaths[cc][0]; cc ++ )
340 res &= CheckConvertToUnixSlashes(toUnixPaths[cc][0], toUnixPaths[cc][1]);
343 // Special check for ~
344 kwsys_stl::string output;
345 if(kwsys::SystemTools::GetEnv("HOME", output))
347 output += "/foo bar/lala";
348 res &= CheckConvertToUnixSlashes("~/foo bar/lala", output);
351 for (cc = 0; checkEscapeChars[cc][0]; cc ++ )
353 res &= CheckEscapeChars(checkEscapeChars[cc][0], checkEscapeChars[cc][1],
354 *checkEscapeChars[cc][2], checkEscapeChars[cc][3]);
357 res &= CheckFileOperations();
359 res &= CheckStringOperations();
361 return res ? 0 : 1;