passwdgen/tests/passwdgen_test.cpp

49 lines
1.3 KiB
C++
Raw Normal View History

2025-09-05 11:43:46 +00:00
// tests/passwdgen_test.cpp
#include "../passwdgen.h"
#include <iostream>
#include <regex>
#include <cassert>
void test_random_string_length() {
for (const int lengths[] = {0, 1, 10, 32, 100}; const auto length : lengths) {
2025-09-05 11:43:46 +00:00
std::string result = random_string(length, false);
assert(result.length() == length);
std::string result_with_punc = random_string(length, true);
assert(result_with_punc.length() == length);
}
std::cout << "Length test passed\n";
}
void test_alphanumeric_only() {
const std::regex alphanumeric_only("^[a-zA-Z0-9]*$");
2025-09-05 11:43:46 +00:00
for (int i = 0; i < 10; i++) {
std::string result = random_string(20, false);
assert(std::regex_match(result, alphanumeric_only));
}
std::cout << "Alphanumeric test passed\n";
}
void test_with_punctuation() {
bool found_special = false;
for (int i = 0; i < 100 && !found_special; i++) {
std::string result = random_string(100, true);
if (!std::regex_match(result, std::regex("^[a-zA-Z0-9]*$"))) {
found_special = true;
}
}
assert(found_special);
std::cout << "Punctuation test passed\n";
}
int main() {
test_random_string_length();
test_alphanumeric_only();
test_with_punctuation();
std::cout << "All tests passed!\n";
return 0;
}