ctkScopedCurrentDirTest1.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) [Organization Name]
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDir>
  16. #include <QTemporaryFile>
  17. // CTK includes
  18. #include "ctkScopedCurrentDir.h"
  19. // STD includes
  20. #include <cstdlib>
  21. #include <iostream>
  22. //-----------------------------------------------------------------------------
  23. int ctkScopedCurrentDirTest1(int argc, char * argv [])
  24. {
  25. Q_UNUSED(argc);
  26. Q_UNUSED(argv);
  27. QString savedCurrentPath = QDir::currentPath();
  28. QTemporaryFile foo(QDir::tempPath() + "/ctkScopedCurrentDirTest1-XXXXXX.txt");
  29. foo.setAutoRemove(false);
  30. bool opened = foo.open();
  31. if(!opened)
  32. {
  33. std::cerr << "Line " << __LINE__ << " - Failed to create temporary file !" << std::endl;
  34. return EXIT_FAILURE;
  35. }
  36. {
  37. ctkScopedCurrentDir scopedCurrentDir(QDir::tempPath());
  38. QString currentPath = scopedCurrentDir.currentPath();
  39. QString expectedCurrentPath = QDir::tempPath();
  40. if (currentPath != expectedCurrentPath)
  41. {
  42. std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
  43. << "\tcurrentPath:" << qPrintable(currentPath) << "\n"
  44. << "\texpectedCurrentPath:" << qPrintable(expectedCurrentPath) << std::endl;
  45. return EXIT_FAILURE;
  46. }
  47. if (scopedCurrentDir.savedCurrentPath() != savedCurrentPath)
  48. {
  49. std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
  50. << "\tsavedCurrentPath:" << qPrintable(scopedCurrentDir.savedCurrentPath()) << "\n"
  51. << "\texpectedSavedCurrentPath:" << qPrintable(savedCurrentPath) << std::endl;
  52. return EXIT_FAILURE;
  53. }
  54. if (!QFile::exists(foo.fileName()))
  55. {
  56. std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
  57. << "\tfile [" << qPrintable(foo.fileName()) <<"] doesn't exist "
  58. << "in directory [" << qPrintable(QDir::currentPath()) << "]!" << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. }
  62. QString currentPath = QDir::currentPath();
  63. if (savedCurrentPath != currentPath)
  64. {
  65. std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
  66. << "\tsavedCurrentPath [" << qPrintable(savedCurrentPath) << "]\n"
  67. << "\tcurrentPath [" << qPrintable(currentPath) << "]" << std::endl;
  68. return EXIT_FAILURE;
  69. }
  70. return EXIT_SUCCESS;
  71. }