51 lines
1.3 KiB
C++
51 lines
1.3 KiB
C++
|
// tests/passwdgen_test.cpp
|
||
|
#include "../passwdgen.h"
|
||
|
#include <iostream>
|
||
|
#include <regex>
|
||
|
#include <cassert>
|
||
|
|
||
|
void test_random_string_length() {
|
||
|
const int lengths[] = {0, 1, 10, 32, 100};
|
||
|
|
||
|
for (auto length : lengths) {
|
||
|
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() {
|
||
|
std::regex alphanumeric_only("^[a-zA-Z0-9]*$");
|
||
|
|
||
|
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;
|
||
|
}
|