forked from irods/irods
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ 7098] Add utility to generate random alphanumeric strings
Adds a function which randomly generates an alphanumeric character and concatenates a string which is returned to the user. Unit tests included.
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
unit_tests/cmake/test_config/irods_generate_random_alphanumeric_string.cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
set(IRODS_TEST_TARGET irods_generate_random_alphanumeric_string) | ||
|
||
set(IRODS_TEST_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp | ||
${CMAKE_CURRENT_SOURCE_DIR}/src/test_generate_random_alphanumeric_string.cpp) | ||
|
||
set(IRODS_TEST_INCLUDE_PATH ${IRODS_EXTERNALS_FULLPATH_BOOST}/include | ||
${IRODS_EXTERNALS_FULLPATH_FMT}/include) | ||
|
||
set(IRODS_TEST_LINK_LIBRARIES irods_common | ||
${IRODS_EXTERNALS_FULLPATH_FMT}/lib/libfmt.so) |
31 changes: 31 additions & 0 deletions
31
unit_tests/src/test_generate_random_alphanumeric_string.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <catch2/catch.hpp> | ||
|
||
#include "irods/irods_exception.hpp" | ||
#include "irods/irods_random.hpp" | ||
|
||
#include <algorithm> | ||
#include <cctype> | ||
#include <string> | ||
|
||
TEST_CASE("length of 0 results in an empty string") | ||
{ | ||
CHECK(irods::generate_random_alphanumeric_string(0).empty()); | ||
} | ||
|
||
TEST_CASE("length of 1 effectively calls generate_random_alphanumeric_character") | ||
{ | ||
constexpr std::size_t character_count = 1; | ||
const auto s = irods::generate_random_alphanumeric_string(character_count); | ||
REQUIRE(!s.empty()); | ||
CHECK(s.size() == character_count); | ||
CHECK(std::all_of(std::begin(s), std::end(s), [](const auto& c) { return std::isalnum(c); })); | ||
} | ||
|
||
TEST_CASE("only alphanumeric characters appear in string of statistically significant length") | ||
{ | ||
constexpr std::size_t character_count = 1'000'000; | ||
const auto s = irods::generate_random_alphanumeric_string(character_count); | ||
REQUIRE(!s.empty()); | ||
CHECK(s.size() == character_count); | ||
CHECK(std::all_of(std::begin(s), std::end(s), [](const auto& c) { return std::isalnum(c); })); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters