Hashing tests (#5026)

* add deck hashing tests

* format

* fix header

* fix cmakelists

* fix test

* add 5 second timeout to test

let the optimising begin

* expand tests

* remove debug message

* manually format

* I installed cmake format from the aur

* use decklist library

* format
This commit is contained in:
ebbit1q
2025-12-23 17:48:10 +01:00
committed by GitHub
parent 421d6b334a
commit 521046fb09
5 changed files with 124 additions and 8 deletions

View File

@@ -1,16 +1,20 @@
enable_testing()
add_test(NAME dummy_test COMMAND dummy_test)
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_performance_test COMMAND deck_hash_performance_test)
set_tests_properties(deck_hash_performance_test PROPERTIES TIMEOUT 5)
# Find GTest
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_performance_test deck_hash_performance_test.cpp)
find_package(GTest)
@@ -41,6 +45,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_performance_test gtest)
endif()
include_directories(${GTEST_INCLUDE_DIRS})
@@ -50,6 +55,10 @@ target_link_libraries(test_age_formatting Threads::Threads ${GTEST_BOTH_LIBRARIE
target_link_libraries(
password_hash_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
)
target_link_libraries(
deck_hash_performance_test libcockatrice_deck_list libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES}
${TEST_QT_MODULES}
)
add_subdirectory(carddatabase)
add_subdirectory(loading_from_clipboard)

View File

@@ -0,0 +1,81 @@
#include "gtest/gtest.h"
#include <QDebug>
#include <libcockatrice/deck_list/deck_list.h>
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

@@ -3,22 +3,32 @@
#include <QTextStream>
#include <libcockatrice/deck_list/tree/deck_list_card_node.h>
void testEmpty(const QString &clipboard)
DeckList getDeckList(const QString &clipboard)
{
QString cp(clipboard);
DeckList deckList;
QString cp(clipboard);
QTextStream stream(&cp); // text stream requires local copy
deckList.loadFromStream_Plain(stream, false);
return deckList;
}
void testEmpty(const QString &clipboard)
{
DeckList deckList = getDeckList(clipboard);
ASSERT_TRUE(deckList.getCardList().isEmpty());
}
void testHash(const QString &clipboard, const std::string &hash)
{
DeckList deckList = getDeckList(clipboard);
ASSERT_EQ(deckList.getDeckHash().toStdString(), hash);
}
void testDeck(const QString &clipboard, const Result &result)
{
QString cp(clipboard);
DeckList deckList;
QTextStream stream(&cp); // text stream requires local copy
deckList.loadFromStream_Plain(stream, false);
DeckList deckList = getDeckList(clipboard);
ASSERT_EQ(result.name, deckList.getName().toStdString());
ASSERT_EQ(result.comments, deckList.getComments().toStdString());

View File

@@ -21,7 +21,7 @@ struct Result
};
void testEmpty(const QString &clipboard);
void testHash(const QString &clipboard, const std::string &hash);
void testDeck(const QString &clipboard, const Result &result);
#endif // CLIPBOARD_TESTING_H

View File

@@ -203,6 +203,22 @@ TEST(LoadingFromClipboardTest, emptyMainBoard)
testEmpty(clipboard);
}
TEST(LoadingFromClipboardTest, emptyHash)
{
QString clipboard("");
testHash(clipboard, "r8sq7riu");
}
TEST(LoadingFromClipboardTest, deckHash)
{
QString clipboard("1 Mountain\n"
"2 Island\n"
"SB: 3 Forest\n");
testHash(clipboard, "5cac19qm");
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);