Line data Source code
1 : #pragma once 2 : #ifndef CATCH_TESTS_HPP 3 : #define CATCH_TESTS_HPP 4 : 5 : /* 6 : Zipios -- a small C++ library that provides easy access to .zip files. 7 : 8 : Copyright (C) 2000-2007 Thomas Sondergaard 9 : Copyright (c) 2015-2022 Made to Order Software Corp. All Rights Reserved 10 : 11 : This library is free software; you can redistribute it and/or 12 : modify it under the terms of the GNU Lesser General Public 13 : License as published by the Free Software Foundation; either 14 : version 2.1 of the License, or (at your option) any later version. 15 : 16 : This library is distributed in the hope that it will be useful, 17 : but WITHOUT ANY WARRANTY; without even the implied warranty of 18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 : Lesser General Public License for more details. 20 : 21 : You should have received a copy of the GNU Lesser General Public 22 : License along with this library; if not, write to the Free Software 23 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 : */ 25 : 26 : /** \file 27 : * \brief Common header for all our catch tests. 28 : * 29 : * Zipios comes with a unit test suite. This header defines things 30 : * that all the tests access, such as the catch.hpp header file. 31 : */ 32 : 33 : #include <zipios/zipios-config.hpp> 34 : 35 : #include <catch2/snapcatch2.hpp> 36 : 37 : #include <memory> 38 : #include <sstream> 39 : 40 : #include <limits.h> 41 : 42 : 43 : #if defined(__sun) || defined(__sun__) || defined(__SunOS) || defined(__CYGWIN__) 44 : namespace std 45 : { 46 : 47 : // somehow they have g++ 4.8.2 but to_string() is missing 48 : template<typename T> 49 : std::string to_string(T v) 50 : { 51 : std::ostringstream ss; 52 : ss << v; 53 : return ss.str(); 54 : } 55 : 56 : } 57 : #endif 58 : 59 : 60 : namespace zipios_test 61 : { 62 : 63 : 64 : 65 : /** \brief Create a random number representing a size_t object. 66 : * 67 : * This function is used to accommodate 32 and 64 bit tests. It creates 68 : * a random number in a size_t variable. The result is expected to fill 69 : * all the bits in a random manner. 70 : * 71 : * \todo 72 : * Look into whether we want to define all the bits. Right now, it is 73 : * likely that bit 31 and 63 never get set. 74 : * 75 : * \return A random size_t variable. 76 : */ 77 49 : inline size_t rand_size_t() 78 : { 79 49 : return static_cast<size_t>(rand()) 80 : #if !defined(_ILP32) && ((UINT_MAX) != 0xFFFFFFFFU) 81 : | (static_cast<size_t>(rand()) << 32) 82 : #endif 83 : ; 84 : } 85 : 86 : 87 : class auto_unlink_t 88 : { 89 : public: 90 : auto_unlink_t(std::string const & filename, bool delete_on_creation); 91 : ~auto_unlink_t(); 92 : 93 : void unlink(); 94 : 95 : private: 96 : std::string const m_filename; 97 : }; 98 : 99 : 100 : // original found in snapdev 101 : class safe_chdir 102 : { 103 : public: 104 : safe_chdir(std::string const & path); 105 : safe_chdir(safe_chdir const & rhs) = delete; 106 : ~safe_chdir(); 107 : 108 : safe_chdir & operator = (safe_chdir const & rhs) = delete; 109 : 110 : private: 111 : std::unique_ptr<char> m_original_path = std::unique_ptr<char>(); 112 : }; 113 : 114 : 115 : class file_t 116 : { 117 : public: 118 : typedef std::shared_ptr<file_t> pointer_t; 119 : typedef std::vector<pointer_t> vector_t; 120 : typedef std::vector<std::string> filenames_t; 121 : 122 : enum class type_t 123 : { 124 : UNKNOWN, 125 : REGULAR, 126 : DIRECTORY 127 : }; 128 : 129 : file_t(type_t t, int children_count, std::string const & new_filename = ""); 130 : ~file_t(); 131 : type_t type() const; 132 : std::string const & filename() const; 133 : vector_t const & children() const; 134 : size_t size(); 135 : type_t find(std::string const & name); 136 : filenames_t get_all_filenames() const; 137 : 138 : private: 139 : void get_filenames(filenames_t & names, std::string const & parent) const; 140 : 141 : std::string m_filename; 142 : vector_t m_children; 143 : type_t m_type; 144 : }; 145 : 146 : 147 : } // zipios_test namespace 148 : 149 : // Local Variables: 150 : // mode: cpp 151 : // indent-tabs-mode: nil 152 : // c-basic-offset: 4 153 : // tab-width: 4 154 : // End: 155 : 156 : // vim: ts=4 sw=4 et 157 : #endif