Line data Source code
1 : /* 2 : Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain 3 : Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany 4 : 5 : This software was partially supported by the 6 : EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu). 7 : 8 : This software was partially supported by the 9 : ADA-FS project under the SPPEXA project funded by the DFG. 10 : 11 : This file is part of GekkoFS. 12 : 13 : GekkoFS is free software: you can redistribute it and/or modify 14 : it under the terms of the GNU General Public License as published by 15 : the Free Software Foundation, either version 3 of the License, or 16 : (at your option) any later version. 17 : 18 : GekkoFS is distributed in the hope that it will be useful, 19 : but WITHOUT ANY WARRANTY; without even the implied warranty of 20 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 : GNU General Public License for more details. 22 : 23 : You should have received a copy of the GNU General Public License 24 : along with GekkoFS. If not, see <https://www.gnu.org/licenses/>. 25 : 26 : SPDX-License-Identifier: GPL-3.0-or-later 27 : */ 28 : 29 : #include <catch2/catch.hpp> 30 : #include "helpers/helpers.hpp" 31 : 32 5432 : SCENARIO("random strings can be generated", "[test_helpers][random_string]") { 33 : 34 21728 : GIVEN("A desired length ") { 35 : 36 16297 : WHEN("Length is zero ") { 37 : 38 2 : const auto s = helpers::random_string(0); 39 : 40 4 : THEN(" The generated string is empty ") { 41 1 : REQUIRE(s.empty()); 42 2 : REQUIRE(s.length() == 0); 43 : } 44 : } 45 : 46 21727 : WHEN("Length is a positive integer") { 47 : 48 5433 : const std::size_t n = GENERATE(range(1, 5432)); 49 10862 : const auto s = helpers::random_string(n); 50 : 51 21724 : THEN(" The generated string is empty ") { 52 10862 : REQUIRE(s.length() == n); 53 : } 54 : } 55 : } 56 5432 : } 57 : 58 2 : SCENARIO(" temporary directories can be created ", 59 : "[test_helpers][temporary_directory]") { 60 : 61 8 : GIVEN(" a temporary directory ") { 62 : 63 4 : auto tmpdir = std::make_unique<helpers::temporary_directory>(); 64 : 65 7 : WHEN(" dirname is called ") { 66 4 : THEN(" a corresponding directory has been created ") { 67 2 : REQUIRE(fs::exists(tmpdir->dirname())); 68 : } 69 : } 70 : 71 7 : WHEN(" the temporary_directory has been destroyed ") { 72 : 73 2 : const auto dirname = tmpdir->dirname(); 74 1 : tmpdir.reset(); 75 : 76 1 : REQUIRE(!fs::exists(dirname)); 77 : } 78 : } 79 2 : } 80 : 81 6 : SCENARIO(" temporary files can be created ", "[test_helpers][temporary_file]") { 82 : 83 : 84 24 : GIVEN(" a filename ") { 85 12 : const std::string filename{"foobar"}; 86 : 87 21 : AND_GIVEN(" a temporary file ") { 88 : 89 6 : auto tmpfile = std::make_unique<helpers::temporary_file>(filename); 90 : 91 10 : WHEN(" a temporary_file is created ") { 92 : 93 4 : THEN(" a corresponding temporary file is created ") { 94 3 : REQUIRE(tmpfile->filename() == filename); 95 2 : REQUIRE(fs::exists(tmpfile->filename())); 96 : } 97 : } 98 : 99 10 : WHEN(" a temporary_file is destroyed ") { 100 : 101 1 : tmpfile.reset(); 102 : 103 4 : THEN(" the file is removed ") { 104 : 105 2 : REQUIRE(!fs::exists(filename)); 106 : } 107 : } 108 : 109 10 : WHEN(" text data is written to the file ") { 110 : 111 2 : const std::string text{"sometext"}; 112 1 : tmpfile->write(text); 113 : 114 4 : THEN(" its contents match the provided text ") { 115 : 116 2 : std::string contents; 117 1 : helpers::load_string_file(tmpfile->filename(), contents); 118 : 119 2 : REQUIRE(contents == text); 120 : } 121 : } 122 : } 123 : 124 21 : AND_GIVEN(" some text for data ") { 125 : 126 6 : const std::string text{"sometext"}; 127 : 128 12 : AND_GIVEN(" a temporary file ") { 129 : 130 3 : auto tmpfile = std::make_unique<helpers::temporary_file>( 131 6 : filename, text); 132 : 133 10 : WHEN(" a temporary_file is created ") { 134 : 135 4 : THEN(" a corresponding temporary file is created ") { 136 3 : REQUIRE(tmpfile->filename() == filename); 137 2 : REQUIRE(fs::exists(tmpfile->filename())); 138 : 139 4 : AND_THEN(" its contents match the provided text ") { 140 : 141 2 : std::string contents; 142 1 : helpers::load_string_file(tmpfile->filename(), 143 : contents); 144 : 145 2 : REQUIRE(contents == text); 146 : } 147 : } 148 : } 149 : 150 10 : WHEN(" a temporary_file is destroyed ") { 151 : 152 1 : tmpfile.reset(); 153 : 154 4 : THEN(" the file is removed ") { 155 1 : tmpfile.reset(); 156 : 157 2 : REQUIRE(!fs::exists(filename)); 158 : } 159 : } 160 : 161 10 : WHEN(" text data is written to the file ") { 162 : 163 2 : const std::string more_text{"moretext"}; 164 1 : tmpfile->write(more_text); 165 : 166 4 : THEN(" its contents match the provided text ") { 167 : 168 2 : std::string contents; 169 1 : helpers::load_string_file(tmpfile->filename(), contents); 170 : 171 2 : REQUIRE(contents == text + more_text); 172 : } 173 : } 174 : } 175 : } 176 : } 177 6 : }