diff options
Diffstat (limited to 'Source/kwsys/testSystemTools.cxx')
-rw-r--r-- | Source/kwsys/testSystemTools.cxx | 93 |
1 files changed, 70 insertions, 23 deletions
diff --git a/Source/kwsys/testSystemTools.cxx b/Source/kwsys/testSystemTools.cxx index ffa6a297d..1f3a15b59 100644 --- a/Source/kwsys/testSystemTools.cxx +++ b/Source/kwsys/testSystemTools.cxx @@ -52,7 +52,7 @@ static const char* toUnixPaths[][2] = { { "\\\\usr\\local\\bin\\passwd", "//usr/local/bin/passwd" }, { "\\\\usr\\lo cal\\bin\\pa sswd", "//usr/lo cal/bin/pa sswd" }, { "\\\\usr\\lo\\ cal\\bin\\pa\\ sswd", "//usr/lo/ cal/bin/pa/ sswd" }, - { KWSYS_NULLPTR, KWSYS_NULLPTR } + { nullptr, nullptr } }; static bool CheckConvertToUnixSlashes(std::string const& input, @@ -71,7 +71,7 @@ static bool CheckConvertToUnixSlashes(std::string const& input, static const char* checkEscapeChars[][4] = { { "1 foo 2 bar 2", "12", "\\", "\\1 foo \\2 bar \\2" }, { " {} ", "{}", "#", " #{#} " }, - { KWSYS_NULLPTR, KWSYS_NULLPTR, KWSYS_NULLPTR, KWSYS_NULLPTR } + { nullptr, nullptr, nullptr, nullptr } }; static bool CheckEscapeChars(std::string const& input, @@ -160,7 +160,7 @@ static bool CheckFileOperations() res = false; } // calling with 0 pointer should return false - if (kwsys::SystemTools::MakeDirectory(KWSYS_NULLPTR)) { + if (kwsys::SystemTools::MakeDirectory(nullptr)) { std::cerr << "Problem with MakeDirectory(0)" << std::endl; res = false; } @@ -218,11 +218,11 @@ static bool CheckFileOperations() } // calling with 0 pointer should return false - if (kwsys::SystemTools::FileExists(KWSYS_NULLPTR)) { + if (kwsys::SystemTools::FileExists(nullptr)) { std::cerr << "Problem with FileExists(0)" << std::endl; res = false; } - if (kwsys::SystemTools::FileExists(KWSYS_NULLPTR, true)) { + if (kwsys::SystemTools::FileExists(nullptr, true)) { std::cerr << "Problem with FileExists(0) as file" << std::endl; res = false; } @@ -999,30 +999,45 @@ static bool writeFile(const char* fileName, const char* data) return true; } +static std::string readFile(const char* fileName) +{ + kwsys::ifstream in(fileName, std::ios::binary); + std::stringstream sstr; + sstr << in.rdbuf(); + std::string data = sstr.str(); + if (!in) { + std::cerr << "Failed to read file: " << fileName << std::endl; + return std::string(); + } + return data; +} + +struct +{ + const char* a; + const char* b; + bool differ; +} diff_test_cases[] = { { "one", "one", false }, + { "one", "two", true }, + { "", "", false }, + { "\n", "\r\n", false }, + { "one\n", "one\n", false }, + { "one\r\n", "one\n", false }, + { "one\n", "one", false }, + { "one\ntwo", "one\ntwo", false }, + { "one\ntwo", "one\r\ntwo", false } }; + static bool CheckTextFilesDiffer() { - struct - { - const char* a; - const char* b; - bool differ; - } test_cases[] = { { "one", "one", false }, - { "one", "two", true }, - { "", "", false }, - { "\n", "\r\n", false }, - { "one\n", "one\n", false }, - { "one\r\n", "one\n", false }, - { "one\n", "one", false }, - { "one\ntwo", "one\ntwo", false }, - { "one\ntwo", "one\r\ntwo", false } }; - const int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]); + const int num_test_cases = + sizeof(diff_test_cases) / sizeof(diff_test_cases[0]); for (int i = 0; i < num_test_cases; ++i) { - if (!writeFile("file_a", test_cases[i].a) || - !writeFile("file_b", test_cases[i].b)) { + if (!writeFile("file_a", diff_test_cases[i].a) || + !writeFile("file_b", diff_test_cases[i].b)) { return false; } if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") != - test_cases[i].differ) { + diff_test_cases[i].differ) { std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1 << "." << std::endl; return false; @@ -1032,6 +1047,36 @@ static bool CheckTextFilesDiffer() return true; } +static bool CheckCopyFileIfDifferent() +{ + bool ret = true; + const int num_test_cases = + sizeof(diff_test_cases) / sizeof(diff_test_cases[0]); + for (int i = 0; i < num_test_cases; ++i) { + if (!writeFile("file_a", diff_test_cases[i].a) || + !writeFile("file_b", diff_test_cases[i].b)) { + return false; + } + const char* cptarget = + i < 4 ? TEST_SYSTEMTOOLS_BINARY_DIR "/file_b" : "file_b"; + if (!kwsys::SystemTools::CopyFileIfDifferent("file_a", cptarget)) { + std::cerr << "CopyFileIfDifferent() returned false for test case " + << i + 1 << "." << std::endl; + ret = false; + continue; + } + std::string bdata = readFile("file_b"); + if (diff_test_cases[i].a != bdata) { + std::cerr << "Incorrect CopyFileIfDifferent file contents in test case " + << i + 1 << "." << std::endl; + ret = false; + continue; + } + } + + return ret; +} + int testSystemTools(int, char* []) { bool res = true; @@ -1077,5 +1122,7 @@ int testSystemTools(int, char* []) res &= CheckTextFilesDiffer(); + res &= CheckCopyFileIfDifferent(); + return res ? 0 : 1; } |