expand tests

This commit is contained in:
ebbit1q 2025-10-03 18:37:54 +02:00
parent c59fdfd74f
commit cfaa159209
3 changed files with 87 additions and 53 deletions

View file

@ -4,8 +4,8 @@ add_test(NAME expression_test COMMAND expression_test)
add_test(NAME test_age_formatting COMMAND test_age_formatting)
add_test(NAME password_hash_test COMMAND password_hash_test)
add_test(NAME deck_hash_test COMMAND deck_hash_test)
set_tests_properties(deck_hash_test PROPERTIES TIMEOUT 5)
add_test(NAME deck_hash_performance_test COMMAND deck_hash_performance_test)
set_tests_properties(deck_hash_performance_test PROPERTIES TIMEOUT 5)
# Find GTest
@ -13,7 +13,7 @@ add_executable(dummy_test dummy_test.cpp)
add_executable(expression_test expression_test.cpp)
add_executable(test_age_formatting test_age_formatting.cpp)
add_executable(password_hash_test password_hash_test.cpp)
add_executable(deck_hash_test ../common/deck_list.cpp deck_hash_test.cpp)
add_executable(deck_hash_performance_test ../common/deck_list.cpp deck_hash_performance_test.cpp)
find_package(GTest)
@ -44,7 +44,7 @@ if(NOT GTEST_FOUND)
add_dependencies(expression_test gtest)
add_dependencies(test_age_formatting gtest)
add_dependencies(password_hash_test gtest)
add_dependencies(deck_hash_test gtest)
add_dependencies(deck_hash_performance_test gtest)
endif()
include_directories(${GTEST_INCLUDE_DIRS})
@ -55,7 +55,7 @@ target_link_libraries(
password_hash_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
)
target_link_libraries(
deck_hash_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
deck_hash_performance_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
)
add_subdirectory(carddatabase)

View file

@ -0,0 +1,82 @@
#include "../common/deck_list.h"
#include "gtest/gtest.h"
#include <QDebug>
static constexpr int amount = 1e5;
QString repeatDeck;
QString numberDeck;
QString uniquesDeck;
QString uniquesXorDeck;
QString duplicatesDeck;
TEST(DeckHashTest, RepeatTest)
{
DeckList decklist(repeatDeck);
for (int i = 0; i < amount; ++i) {
decklist.getDeckHash();
decklist.refreshDeckHash();
}
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "5cac19qm") << "The hash does not match!";
}
TEST(DeckHashTest, NumberTest)
{
DeckList decklist(numberDeck);
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "e0m38p19") << "The hash does not match!";
}
TEST(DeckHashTest, UniquesTest)
{
DeckList decklist(uniquesDeck);
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "88prk025") << "The hash does not match!";
}
TEST(DeckHashTest, UniquesTestXor)
{
DeckList decklist(uniquesXorDeck);
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "hkn6q4pf") << "The hash does not match!";
}
TEST(DeckHashTest, DuplicatesTest)
{
DeckList decklist(duplicatesDeck);
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "ekt6tg1h") << "The hash does not match!";
}
int main(int argc, char **argv)
{
const QString deckStart =
R"(<?xml version="1.0"?><cockatrice_deck version="1"><deckname></deckname><comments></comments><zone name="main">)";
const QString deckEnd = R"(</zone></cockatrice_deck>)";
repeatDeck =
deckStart +
R"(<card number="1" name="Mountain"/><card number="2" name="Island"/></zone><zone name="side"><card number="3" name="Forest"/>)" +
deckEnd;
numberDeck = deckStart + QString(R"(<card number="%1" name="Island"/>)").arg(amount) + deckEnd;
QStringList deckString{deckStart};
QStringList deckStringXor = deckString;
int len = QString::number(amount).length();
for (int i = 0; i < amount; ++i) {
// creates already sorted list
deckString << R"(<card number="1" name="card )" << QString::number(i).rightJustified(len, '0') << R"("/>)";
// xor in order to mess with sorting
deckStringXor << R"(<card number="1" name="card )" << QString::number(i ^ amount) << R"("/>)";
}
deckString << deckEnd;
deckStringXor << deckEnd;
uniquesDeck = deckString.join("");
uniquesXorDeck = deckStringXor.join("");
duplicatesDeck = deckStart + QString(R"(<card number="1" name="card"/>)").repeated(amount) + deckEnd;
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -1,48 +0,0 @@
#include "../common/deck_list.h"
#include "gtest/gtest.h"
#include <QDebug>
QString bigDeck;
TEST(DeckHashTest, RepeatTest)
{
DeckList decklist(
R"(<?xml version="1.0"?><cockatrice_deck version="1"><deckname></deckname><comments></comments><zone name="main"><card number="1" name="Mountain"/><card number="2" name="Island"/></zone><zone name="side"><card number="3" name="Forest"/></zone></cockatrice_deck>)");
for (int i = 0; i < 1e6; ++i) { // repeat hashing a million times
decklist.getDeckHash();
decklist.refreshDeckHash();
}
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "5cac19qm") << "The hash matches";
}
TEST(DeckHashTest, NumberTest)
{
DeckList decklist(
R"(<?xml version="1.0"?><cockatrice_deck version="1"><deckname></deckname><comments></comments><zone name="main"><card number="1000000" name="Island"/></zone></cockatrice_deck>)");
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "2k8cdi4s") << "The hash matches";
}
TEST(DeckHashTest, UniquesTest)
{
DeckList decklist(bigDeck);
auto hash = decklist.getDeckHash().toStdString();
ASSERT_EQ(hash, "s544mlan") << "The hash matches";
}
int main(int argc, char **argv)
{
QStringList deckString;
deckString
<< R"(<?xml version="1.0"?><cockatrice_deck version="1"><deckname></deckname><comments></comments><zone name="main"><card number="1" name="card 0)";
for (int i = 1; i < 1e6; ++i) { // add a million unique cards
deckString << R"("/><card number="1" name="card )" + QString::number(i);
}
deckString << R"("/></zone></cockatrice_deck>)";
bigDeck = deckString.join("");
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}