Skip to content
Snippets Groups Projects
Commit 99fc7b84 authored by Flavia Taras's avatar Flavia Taras
Browse files

serialization tests for shoe, other small changes

parent b3a07c61
No related branches found
No related tags found
No related merge requests found
...@@ -32,10 +32,6 @@ void shoe::shuffle() { ...@@ -32,10 +32,6 @@ void shoe::shuffle() {
std::shuffle(_cards.begin(), _cards.end(), std::mt19937(std::random_device()())); std::shuffle(_cards.begin(), _cards.end(), std::mt19937(std::random_device()()));
} }
bool shoe::is_empty() const noexcept {
return _cards.empty();
}
void shoe::setup_round(std::string &err) { void shoe::setup_round(std::string &err) {
// remove all cards // remove all cards
for (int i = 0; i < _cards.size(); i++) { for (int i = 0; i < _cards.size(); i++) {
......
...@@ -32,13 +32,15 @@ public: ...@@ -32,13 +32,15 @@ public:
// pile functions // pile functions
void shuffle(); void shuffle();
bool is_empty() const noexcept;
void setup_round(std::string &err); void setup_round(std::string &err);
card* draw_card(hand* h, std::string& err); card* draw_card(hand* h, std::string& err);
// serialization // serialization
virtual void write_into_json(rapidjson::Value& json, rapidjson::Document::AllocatorType& allocator) const override; virtual void write_into_json(rapidjson::Value& json, rapidjson::Document::AllocatorType& allocator) const override;
static shoe* from_json(const rapidjson::Value& json); static shoe* from_json(const rapidjson::Value& json);
//friend function used for testing
friend std::vector<card*> get_cards(const shoe& shoe);
}; };
......
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
// Created by Flavia Taras on 20.05.22. // Created by Flavia Taras on 20.05.22.
// //
//TODO try to get extra cards when you already have over 21 points
//TODO: adding the same card more than the number of decks in a shoe should not work
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "../source/general/game_state/hand.hpp" #include "../source/general/game_state/hand.hpp"
#include "../source/general/exceptions/BlackjackException.hpp" #include "../source/general/exceptions/BlackjackException.hpp"
......
...@@ -2,3 +2,53 @@ ...@@ -2,3 +2,53 @@
// Created by Flavia Taras on 24.05.22. // Created by Flavia Taras on 24.05.22.
// //
#include "gtest/gtest.h"
#include "../source/general/game_state/shoe.hpp"
#include "../source/general/exceptions/BlackjackException.hpp"
#include "../source/general/serialization/json_utils.h"
std::vector<card*> get_cards(const shoe& shoe) {
return shoe._cards;
}
class ShoeTest : public ::testing::Test {
protected:
virtual void SetUp() {
cards.resize(224);
for (int i = 0; i < 4; ++i) { //number of decks in a shoe
for (int j = 1; j < 14; ++j) { //card values
for (int k = 0; k < 4; ++k) { //card suits
cards.push_back(new card(j, k));
}
}
}
}
/* Any object and subroutine declared here can be accessed in the tests */
shoe shoe_;
std::vector<card*> cards;
std::string err;
};
// Serialization and subsequent deserialization must yield the same object
TEST_F(ShoeTest, SerializationEquality) {
rapidjson::Document* json_send = shoe_.to_json();
std::string message = json_utils::to_string(json_send);
delete json_send;
rapidjson::Document json_received = rapidjson::Document(rapidjson::kObjectType);
json_received.Parse(message.c_str());
shoe* shoe_received = shoe::from_json(json_received);
EXPECT_EQ(get_cards(shoe_), get_cards(*shoe_received));
delete shoe_received;
}
// Deserializing an invalid string must throw a BlackjackException
TEST_F(ShoeTest, SerializationException) {
rapidjson::Document json = rapidjson::Document(rapidjson::kObjectType);
json.Parse("not json");
EXPECT_THROW(shoe::from_json(json), BlackjackException);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment