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"
17 # pragma warning (disable:4786)
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.
26 # include "SystemTools.hxx.in"
27 # include "kwsys_ios_iostream.h.in"
30 // Include with <> instead of "" to avoid getting any in-source copy
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" },
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
)
65 << "Problem with ConvertToUnixSlashes - input: " << input
.c_str()
66 << " output: " << result
.c_str() << " expected: " << output
.c_str()
73 //----------------------------------------------------------------------------
74 const char* checkEscapeChars
[][4] =
76 { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2"},
77 { " {} ", "{}", "#", " #{#} "},
81 bool CheckEscapeChars(kwsys_stl::string input
,
82 const char *chars_to_escape
,
84 kwsys_stl::string output
)
86 kwsys_stl::string result
= kwsys::SystemTools::EscapeChars(
87 input
.c_str(), chars_to_escape
, escape_char
);
91 << "Problem with CheckEscapeChars - input: " << input
.c_str()
92 << " output: " << result
.c_str() << " expected: " << output
.c_str()
99 //----------------------------------------------------------------------------
100 bool CheckFileOperations()
104 if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_BIN_FILE
) !=
105 kwsys::SystemTools::FileTypeBinary
)
108 << "Problem with DetectFileType - failed to detect type of: "
109 << TEST_SYSTEMTOOLS_BIN_FILE
<< kwsys_ios::endl
;
113 if (kwsys::SystemTools::DetectFileType(TEST_SYSTEMTOOLS_SRC_FILE
) !=
114 kwsys::SystemTools::FileTypeText
)
117 << "Problem with DetectFileType - failed to detect type of: "
118 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
122 if (kwsys::SystemTools::FileLength(TEST_SYSTEMTOOLS_BIN_FILE
) != 766)
125 << "Problem with FileLength - incorrect length for: "
126 << TEST_SYSTEMTOOLS_BIN_FILE
<< kwsys_ios::endl
;
133 //----------------------------------------------------------------------------
134 bool CheckStringOperations()
138 kwsys_stl::string test
= "mary had a little lamb.";
139 if (kwsys::SystemTools::CapitalizedWords(test
) != "Mary Had A Little Lamb.")
142 << "Problem with CapitalizedWords "
143 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
147 test
= "Mary Had A Little Lamb.";
148 if (kwsys::SystemTools::UnCapitalizedWords(test
) !=
149 "mary had a little lamb.")
152 << "Problem with UnCapitalizedWords "
153 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
157 test
= "MaryHadTheLittleLamb.";
158 if (kwsys::SystemTools::AddSpaceBetweenCapitalizedWords(test
) !=
159 "Mary Had The Little Lamb.")
162 << "Problem with AddSpaceBetweenCapitalizedWords "
163 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
168 kwsys::SystemTools::AppendStrings("Mary Had A"," Little Lamb.");
169 if (strcmp(cres
,"Mary Had A Little Lamb."))
172 << "Problem with AppendStrings "
173 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
179 kwsys::SystemTools::AppendStrings("Mary Had"," A ","Little Lamb.");
180 if (strcmp(cres
,"Mary Had A Little Lamb."))
183 << "Problem with AppendStrings "
184 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
189 if (kwsys::SystemTools::CountChar("Mary Had A Little Lamb.",'a') != 3)
192 << "Problem with CountChar "
193 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
198 kwsys::SystemTools::RemoveChars("Mary Had A Little Lamb.","aeiou");
199 if (strcmp(cres
,"Mry Hd A Lttl Lmb."))
202 << "Problem with RemoveChars "
203 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
209 kwsys::SystemTools::RemoveCharsButUpperHex("Mary Had A Little Lamb.");
210 if (strcmp(cres
,"A"))
213 << "Problem with RemoveCharsButUpperHex "
214 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
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."))
225 << "Problem with ReplaceChars "
226 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
231 if (!kwsys::SystemTools::StringStartsWith("Mary Had A Little Lamb.",
235 << "Problem with StringStartsWith "
236 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
240 if (!kwsys::SystemTools::StringEndsWith("Mary Had A Little Lamb.",
244 << "Problem with StringEndsWith "
245 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
249 cres
= kwsys::SystemTools::DuplicateString("Mary Had A Little Lamb.");
250 if (strcmp(cres
,"Mary Had A Little Lamb."))
253 << "Problem with DuplicateString "
254 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
259 test
= "Mary Had A Little Lamb.";
260 if (kwsys::SystemTools::CropString(test
,13) !=
264 << "Problem with CropString "
265 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
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.")
275 << "Problem with Split "
276 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
280 if (kwsys::SystemTools::ConvertToWindowsOutputPath
281 ("L://Local Mojo/Hex Power Pack/Iffy Voodoo") !=
282 "\"L:\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
285 << "Problem with ConvertToWindowsOutputPath "
290 if (kwsys::SystemTools::ConvertToWindowsOutputPath
291 ("//grayson/Local Mojo/Hex Power Pack/Iffy Voodoo") !=
292 "\"\\\\grayson\\Local Mojo\\Hex Power Pack\\Iffy Voodoo\"")
295 << "Problem with ConvertToWindowsOutputPath "
300 if (kwsys::SystemTools::ConvertToUnixOutputPath
301 ("//Local Mojo/Hex Power Pack/Iffy Voodoo") !=
302 "/Local\\ Mojo/Hex\\ Power\\ Pack/Iffy\\ Voodoo")
305 << "Problem with ConvertToUnixOutputPath "
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"))
319 << "Problem with ConvertWindowsCommandLineToUnixArguments"
320 << TEST_SYSTEMTOOLS_SRC_FILE
<< kwsys_ios::endl
;
323 for (;targc
>=0; --targc
)
325 delete [] targv
[targc
];
332 //----------------------------------------------------------------------------
333 int testSystemTools(int, char*[])
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();