LCOV - code coverage report
Current view: top level - tests/unit/helpers - random_string.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 9 9 100.0 %
Date: 2024-04-30 13:21:35 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          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 <string>
      30             : #include <random>
      31             : #include <array>
      32             : #include <algorithm>
      33             : 
      34             : namespace helpers {
      35             : 
      36             : std::string
      37        5434 : random_string(std::size_t length) {
      38        5434 :     constexpr std::array<char, 63> charset{"0123456789"
      39             :                                            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      40             :                                            "abcdefghijklmnopqrstuvwxyz"};
      41             : 
      42        5436 :     thread_local static std::mt19937 rng{std::random_device{}()};
      43        5434 :     thread_local static std::uniform_int_distribution<std::string::size_type>
      44        5434 :             pick(0, std::size(charset) - 2);
      45             : 
      46        5434 :     std::string s(length, '\0');
      47             : 
      48        5434 :     std::generate_n(std::begin(s), length,
      49    14756062 :                     [&]() { return charset[pick(rng)]; });
      50             : 
      51        5434 :     return s;
      52             : }
      53             : 
      54             : } // namespace helpers

Generated by: LCOV version 1.16