From b6c5c5ea4cd8be8aed5075c069de0011816c163d Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Mon, 29 Apr 2024 19:43:44 +0200 Subject: [PATCH] add deck hashing tests --- tests/CMakeLists.txt | 6 +++ tests/deck_hash_test.cpp | 44 +++++++++++++++++++ .../clipboard_testing.cpp | 23 +++++++--- .../clipboard_testing.h | 2 +- .../loading_from_clipboard_test.cpp | 16 +++++++ 5 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 tests/deck_hash_test.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6a5eacf54..3a4ab65fc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,7 @@ 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) # Find GTest @@ -11,6 +12,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/decklist.cpp deck_hash_test.cpp) find_package(GTest) @@ -41,6 +43,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) endif() include_directories(${GTEST_INCLUDE_DIRS}) @@ -50,6 +53,9 @@ 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_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES} +) add_subdirectory(carddatabase) add_subdirectory(loading_from_clipboard) diff --git a/tests/deck_hash_test.cpp b/tests/deck_hash_test.cpp new file mode 100644 index 000000000..b5d52829e --- /dev/null +++ b/tests/deck_hash_test.cpp @@ -0,0 +1,44 @@ +#include "../../common/decklist.h" +#include + +#include "gtest/gtest.h" + +QString bigDeck; + +TEST(DeckHashTest, RepeatTest) +{ + DeckList decklist(R"()"); + for (int i = 0; i < 1e6; ++i){ // repeat hashing a million times + decklist.updateDeckHash(); + } + auto hash = decklist.getDeckHash().toStdString(); + ASSERT_EQ(hash, "5cac19qm") << "The hash matches"; +} + +TEST(DeckHashTest, NumberTest) +{ + DeckList decklist(R"()"); + 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"()"; + bigDeck = deckString.join(""); + + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tests/loading_from_clipboard/clipboard_testing.cpp b/tests/loading_from_clipboard/clipboard_testing.cpp index 2342bc088..fbc2237eb 100644 --- a/tests/loading_from_clipboard/clipboard_testing.cpp +++ b/tests/loading_from_clipboard/clipboard_testing.cpp @@ -3,22 +3,33 @@ #include #include -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); + std::cout << deckList.writeToString_Native().toStdString() << std::endl; + + 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()); diff --git a/tests/loading_from_clipboard/clipboard_testing.h b/tests/loading_from_clipboard/clipboard_testing.h index 5e9cff915..41d8ea794 100644 --- a/tests/loading_from_clipboard/clipboard_testing.h +++ b/tests/loading_from_clipboard/clipboard_testing.h @@ -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 diff --git a/tests/loading_from_clipboard/loading_from_clipboard_test.cpp b/tests/loading_from_clipboard/loading_from_clipboard_test.cpp index 6f9762be9..fcfbb22db 100644 --- a/tests/loading_from_clipboard/loading_from_clipboard_test.cpp +++ b/tests/loading_from_clipboard/loading_from_clipboard_test.cpp @@ -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);