You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uoFilesTest.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*********************************************************************
  2. * Software License Agreement (AGPL-3 License)
  3. *
  4. * OpenViBE SDK Test Software
  5. * Based on OpenViBE V1.1.0, Copyright (C) Inria, 2006-2015
  6. * Copyright (C) Inria, 2015-2017,V1.0
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program.
  19. * If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <string>
  22. #include <fs/Files.h>
  23. #include "ovtAssert.h"
  24. #include <iostream>
  25. int uoFilesTest(int /*argc*/, char* argv[])
  26. {
  27. std::string outputDirectory = argv[1];
  28. OVT_ASSERT(FS::Files::directoryExists(outputDirectory.c_str()), "Failure to find test data directory");
  29. std::string testDir = outputDirectory + "uoFilesTest";
  30. std::string testFile = testDir + "/uoFilesTest.txt";
  31. // test path creation
  32. FS::Files::createParentPath(testFile.c_str());
  33. OVT_ASSERT(FS::Files::directoryExists(testDir.c_str()), "Failure to create directory");
  34. OVT_ASSERT(!FS::Files::fileExists(testFile.c_str()), "Failure to create parent path only");
  35. // test path retrieval methods
  36. char parentPathFromFile[1024];
  37. FS::Files::getParentPath(testFile.c_str(), parentPathFromFile);
  38. OVT_ASSERT_STREQ(std::string(parentPathFromFile), testDir, "Failure to retrieve parent path from full path");
  39. char parentPathFromDir[1024];
  40. FS::Files::getParentPath(testDir.c_str(), parentPathFromDir);
  41. std::cout << parentPathFromDir << " " << outputDirectory << std::endl;
  42. OVT_ASSERT_STREQ((std::string(parentPathFromDir) + "/"), outputDirectory, "Failure to retrieve parent path from path with no slash");
  43. testDir += "/"; // adding a slash should now give testDir as parent
  44. FS::Files::getParentPath(testDir.c_str(), parentPathFromDir);
  45. OVT_ASSERT_STREQ((std::string(parentPathFromDir) + "/"), testDir, "Failure to retrieve parent path from path with slash");
  46. char filename[256];
  47. FS::Files::getFilename(testFile.c_str(), filename);
  48. OVT_ASSERT_STREQ(std::string(filename), std::string("uoFilesTest.txt"), "Failure to retrieve filename from full path");
  49. FS::Files::getFilenameWithoutExtension(testFile.c_str(), filename);
  50. OVT_ASSERT_STREQ(std::string(filename), std::string("uoFilesTest"), "Failure to retrieve filename with no extension from full path");
  51. // test file creation and opening
  52. std::ofstream ostream;
  53. FS::Files::openOFStream(ostream, testFile.c_str());
  54. OVT_ASSERT(FS::Files::fileExists(testFile.c_str()), "Failure to create file");
  55. OVT_ASSERT(ostream.is_open(), "Failure to open file");
  56. ostream.close();
  57. std::string testFileInMissingnDir = testDir + "/newDir/uoFilesTest.txt";
  58. FS::Files::openOFStream(ostream, testFileInMissingnDir.c_str());
  59. OVT_ASSERT(!FS::Files::fileExists(testFileInMissingnDir.c_str()), "Failure to check for non-existing file");
  60. std::ifstream istream;
  61. FS::Files::openIFStream(istream, testFile.c_str());
  62. OVT_ASSERT(istream.is_open(), "Failure to open file in an input stream");
  63. istream.close();
  64. std::fstream fstream;
  65. FS::Files::openFStream(fstream, testFile.c_str(), std::ios_base::out);
  66. OVT_ASSERT(fstream.is_open(), "Failure to open file in a generic stream");
  67. fstream.close();
  68. auto file = FS::Files::open(testFile.c_str(), "r");
  69. OVT_ASSERT(file != nullptr, "Failure to open file in a FILE object");
  70. file = FS::Files::open(testFileInMissingnDir.c_str(), "r");
  71. OVT_ASSERT(file == nullptr, "Failure to return NULL FILE object for non-existing file");
  72. testDir = outputDirectory + "uoFilesTest2/long spaced/path";
  73. FS::Files::createPath(testDir.c_str());
  74. OVT_ASSERT(FS::Files::directoryExists(testDir.c_str()), "Failure to create directory with path containing spaces");
  75. // test equality
  76. OVT_ASSERT(!FS::Files::equals(testFile.c_str(), testFileInMissingnDir.c_str()), "Failure to compare different files");
  77. OVT_ASSERT(FS::Files::equals(testFile.c_str(), testFile.c_str()), "Failure to compare same files");
  78. // test folder copy
  79. std::string testFile2 = outputDirectory + "uoFilesTest" + "/uoFilesTestChild/uoFilesTest.txt";
  80. std::string testTargetDir = outputDirectory + "uoFilesTestCopy";
  81. std::string testTargetFile1 = testTargetDir + "/uoFilesTest.txt";
  82. std::string testTargetFile2 = testTargetDir + "/uoFilesTestChild/uoFilesTest.txt";
  83. // create a subfolder with file
  84. FS::Files::createParentPath(testFile2.c_str());
  85. std::ofstream ostream2;
  86. FS::Files::openOFStream(ostream2, testFile2.c_str());
  87. OVT_ASSERT(FS::Files::fileExists(testFile2.c_str()), "Failure to create file in subfolder");
  88. OVT_ASSERT(ostream2.is_open(), "Failure to open file");
  89. ostream2.close();
  90. // copy folder
  91. testDir = outputDirectory + "uoFilesTest";
  92. FS::Files::copyDirectory(testDir.c_str(), testTargetDir.c_str());
  93. OVT_ASSERT(FS::Files::directoryExists(testTargetDir.c_str()), "Failure in copying folder");
  94. OVT_ASSERT(FS::Files::fileExists(testTargetFile1.c_str()), "Failure in copying child files of the folder");
  95. OVT_ASSERT(FS::Files::fileExists(testTargetFile2.c_str()), "Failure in copying subfolder");
  96. // test folder copy on existing folder
  97. OVT_ASSERT(!FS::Files::copyDirectory(testDir.c_str(), testTargetDir.c_str()), "Failure: Copy should not have been done if folder exits");
  98. return EXIT_SUCCESS;
  99. }