ordle/test/test.cc
2024-12-26 19:34:21 +01:00

133 lines
3.4 KiB
C++

#include "ordle.h"
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
void test_read_candidates() {
std::istringstream input("apple\nbanana\ncherry\nimbus\n");
auto words = read_candidates(input);
auto w = std::vector<std::string>{"apple", "imbus"};
assert(words == w);
std::cout << "test_read_candidates passed.\n";
}
void test_contains_any_of() {
assert(contains_any_of("apple", "aeiou"));
assert(!contains_any_of("brrr", "aeiou"));
std::cout << "test_contains_any_of passed.\n";
}
void test_contains_at() {
assert(contains_at("apple", 'a', 0));
assert(!contains_at("apple", 'p', 0));
assert(!contains_at("apple", 'p', 100));
std::cout << "test_contains_at passed.\n";
}
void test_contains_but_not_at() {
assert(contains_but_not_at("apple", 'p', 0));
assert(!contains_but_not_at("apple", 'a', 0));
assert(!contains_but_not_at("abc", 'k', 1000));
std::cout << "test_contains_but_not_at passed.\n";
}
void test_wrong_fn() {
wrong_fn wrong("aeiou");
assert(wrong("apple")); // Contains a,e
assert(!wrong("brrr")); // Contains none
std::cout << "test_wrong_fn passed.\n";
}
void test_correct_fn() {
IndexMap green = {{0, 'a'}, {4, 'e'}};
correct_fn correct(green);
assert(correct("apple"));
assert(correct("ample"));
assert(!correct("jedi"));
std::cout << "test_correct_fn passed.\n";
}
void test_misplaced_fn() {
IndexMap yellow = {{1, 'p'}, {2, 'p'}};
misplaced_fn misplaced(yellow);
assert(!misplaced("apple"));
assert(misplaced("puppy"));
assert(!misplaced("joink"));
std::cout << "test_misplaced_fn passed.\n";
}
void test_do_filter() {
std::vector<std::string> candidates = {"apple", "ample", "angle"};
std::string wrong = "iou";
IndexMap green = {{0, 'a'}, {1, 'p'}};
IndexMap yellow = {{3, 'e'}};
do_filter(candidates, wrong, green, yellow);
std::for_each(candidates.begin(), candidates.end(),
[&](auto &word) { std::cout << word << std::endl; });
assert(candidates == std::vector<std::string>{"apple"});
std::cout << "test_do_filter passed.\n";
}
void test_build_list() {
{
std::string input = "a0 b1 c2";
IndexMap result = build_list(input);
assert(result.size() == 3);
assert(result[0] == 'a');
assert(result[1] == 'b');
assert(result[2] == 'c');
std::cout << "Test case 1 passed.\n";
}
{ // Empty input
std::string input = "";
IndexMap result = build_list(input);
assert(result.empty());
std::cout << "Test case 2 passed.\n";
}
{ // Single input
std::string input = "z9";
IndexMap result = build_list(input);
assert(result.size() == 1);
assert(result[9] == 'z');
std::cout << "Test case 3 passed.\n";
}
{ // Irregular spacing
std::string input = "x3 y4 z5";
IndexMap result = build_list(input);
assert(result.size() == 3);
assert(result[3] == 'x');
assert(result[4] == 'y');
assert(result[5] == 'z');
std::cout << "Test case 4 passed.\n";
}
}
int main() {
test_read_candidates();
test_contains_any_of();
test_contains_at();
test_contains_but_not_at();
test_wrong_fn();
test_correct_fn();
test_misplaced_fn();
test_do_filter();
test_build_list();
std::cout << "All tests passed!\n";
return 0;
}