TestHelper.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-15
  6. * Changed: 2022-05-16
  7. *
  8. * */
  9. #ifndef LS_STD_IO_TEST_HELPER_HPP
  10. #define LS_STD_IO_TEST_HELPER_HPP
  11. #include <string>
  12. #include <vector>
  13. #include <stdexcept>
  14. #include <algorithm>
  15. #include <sstream>
  16. #include <climits>
  17. #include <unistd.h>
  18. #include <fstream>
  19. namespace ls_std_test
  20. {
  21. class TestHelper
  22. {
  23. public:
  24. TestHelper() = default;
  25. ~TestHelper() = default;
  26. static ::std::string getResourcesFolderLocation()
  27. {
  28. return TestHelper::getTestFolderLocation() + "resources" + ls_std_test::TestHelper::_getFilePathSeparator();
  29. }
  30. static ::std::string getTestFolderLocation()
  31. {
  32. ::std::string buildDirectory = ls_std_test::TestHelper::_getWorkingDirectory();
  33. buildDirectory = ls_std_test::TestHelper::_normalizePath(buildDirectory);
  34. return ls_std_test::TestHelper::_getParent(buildDirectory) + "test" + ls_std_test::TestHelper::_getFilePathSeparator();
  35. }
  36. static ::std::string normalize(const ::std::string &_path)
  37. {
  38. return ls_std_test::TestHelper::_normalizePath(_path);
  39. }
  40. static ::std::string readFile(const ::std::string &_absoluteFilePath)
  41. {
  42. char *data;
  43. ::std::ifstream inputStream{_absoluteFilePath, ::std::ifstream::binary};
  44. int length = (int) ls_std_test::TestHelper::_getFileSize(_absoluteFilePath);
  45. data = new ls::std::core::type::byte[length];
  46. inputStream.read(data, length);
  47. if (inputStream.fail())
  48. {
  49. throw ::std::runtime_error("invalid file operation!");
  50. }
  51. inputStream.close();
  52. ls::std::core::type::byte_field readData = ls::std::core::type::byte_field{data, (size_t) ls_std_test::TestHelper::_getFileSize(_absoluteFilePath)};
  53. delete[] data;
  54. return readData;
  55. }
  56. private:
  57. static bool _fileExists(const ::std::string &_path)
  58. {
  59. struct stat _stat{};
  60. return (stat(_path.c_str(), &_stat) == 0);
  61. }
  62. static char _getFilePathSeparator()
  63. {
  64. char separator;
  65. #ifdef _WIN32
  66. separator = '\\';
  67. #endif
  68. #if defined(unix) || defined(__APPLE__)
  69. separator = '/';
  70. #endif
  71. return separator;
  72. }
  73. static long _getFileSize(const ::std::string &_absoluteFilePath)
  74. {
  75. ::std::streampos fileSize{};
  76. if (ls_std_test::TestHelper::_fileExists(_absoluteFilePath))
  77. {
  78. ::std::ifstream fileHandler{_absoluteFilePath, ::std::ios::in};
  79. fileSize = fileHandler.tellg();
  80. fileHandler.seekg(0, ::std::ios::end);
  81. fileSize = fileHandler.tellg() - fileSize;
  82. fileHandler.close();
  83. }
  84. return (long) fileSize;
  85. }
  86. static std::string _getParent(const ::std::string &_path)
  87. {
  88. ::std::string parent{};
  89. ::std::vector<::std::string> subDirectoryNames = ls_std_test::TestHelper::_splitIntoSubDirectoryNames(_path);
  90. const char separator = ls_std_test::TestHelper::_getFilePathSeparator();
  91. subDirectoryNames.pop_back();
  92. for (auto const &subDirectoryName: subDirectoryNames)
  93. {
  94. parent += subDirectoryName + separator;
  95. }
  96. return parent;
  97. }
  98. static ::std::string _getWorkingDirectory()
  99. {
  100. ::std::string workingDirectory{};
  101. #if defined(unix) || defined(__APPLE__)
  102. workingDirectory = ls_std_test::TestHelper::_getWorkingDirectoryUnix();
  103. #endif
  104. #ifdef _WIN32
  105. workingDirectory = ls_std_test::TestHelper::_getWorkingDirectoryWindows();
  106. #endif
  107. return workingDirectory;
  108. }
  109. #if defined(unix) || defined(__APPLE__)
  110. static ::std::string _getWorkingDirectoryUnix()
  111. {
  112. ::std::string workingDirectory{};
  113. char buffer[PATH_MAX];
  114. if (getcwd(buffer, sizeof(buffer)) == nullptr)
  115. {
  116. throw ::std::runtime_error{"invalid file operation!"};
  117. }
  118. else
  119. {
  120. workingDirectory = ::std::string(buffer);
  121. }
  122. return workingDirectory;
  123. }
  124. #endif
  125. #ifdef _WIN32
  126. ::std::string _getWorkingDirectoryWindows()
  127. {
  128. ::std::string workingDirectory{};
  129. TCHAR buffer[MAX_PATH];
  130. if (!GetCurrentDirectory(MAX_PATH, buffer))
  131. {
  132. throw ::std::runtime_error{"invalid file operation!"};
  133. }
  134. else
  135. {
  136. workingDirectory = ::std::string(buffer);
  137. }
  138. return workingDirectory;
  139. }
  140. #endif
  141. static ::std::string _normalizePath(::std::string _path)
  142. {
  143. _path = ls_std_test::TestHelper::_replaceWrongSeparator(_path);
  144. _path = ls_std_test::TestHelper::_reduceSeparators(_path);
  145. return _path;
  146. }
  147. static ::std::string _reduceSeparators(const ::std::string &_path)
  148. {
  149. static const char separator = {ls_std_test::TestHelper::_getFilePathSeparator()};
  150. ::std::string normalizedPath{};
  151. int index{};
  152. while (index < _path.size())
  153. {
  154. if (_path[index] == separator)
  155. {
  156. normalizedPath += _path[index];
  157. do
  158. {
  159. index++;
  160. } while (_path[index] == separator);
  161. }
  162. else
  163. {
  164. normalizedPath += _path[index];
  165. index++;
  166. }
  167. }
  168. return normalizedPath;
  169. }
  170. static ::std::string _replaceWrongSeparator(::std::string _path)
  171. {
  172. static const char unixSeparator = '/';
  173. static const char windowsSeparator = '\\';
  174. #if defined(unix) || defined(__APPLE__)
  175. ::std::replace(_path.begin(), _path.end(), windowsSeparator, unixSeparator);
  176. #endif
  177. #ifdef _WIN32
  178. ::std::replace(_path.begin(), _path.end(), unixSeparator, windowsSeparator);
  179. #endif
  180. return _path;
  181. }
  182. static ::std::vector<::std::string> _splitIntoSubDirectoryNames(const ::std::string &_path)
  183. {
  184. ::std::vector<::std::string> subDirectoryNames{};
  185. ::std::stringstream _stream{_path};
  186. ::std::string subDirectoryName{};
  187. const char separator = ls_std_test::TestHelper::_getFilePathSeparator();
  188. while (::std::getline(_stream, subDirectoryName, separator))
  189. {
  190. subDirectoryNames.push_back(subDirectoryName);
  191. }
  192. return subDirectoryNames;
  193. }
  194. };
  195. }
  196. #endif