Chapter 12 Exercise Set 0: Chapter ReviewΒΆ
Create files named
Cards.h
andCards.cpp
to hold theCard
object developed in this chapter. Create atest_cards.cpp
with the following contents:#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include <doctest.h> #include <iostream> #include <string> #include "Card.h" using namespace std; TEST_CASE("Test can create Cards") { Card c1; CHECK(c1.suit == 0); CHECK(c1.rank == 0); Card c2(3, 4); CHECK(c2.suit == 3); CHECK(c2.rank == 4); } TEST_CASE("Test can render Cards") { Card c3(3, 12); CHECK(c3.to_string() == "Queen of Hearts"); Card c4(2, 10); CHECK(c4.to_string() == "10 of Diamonds"); Card c5; CHECK(c5.to_string() == "Joker"); }
Write enough scaffolding code to let the tests compile (i.e. with minimal function bodies to take the correct arguments and return some value of the expected type), but not pass the tests. Then add function bodies to make the tests pass.
Now add tests for the
==
,>
and<=
operators forCards
.Use TDD to add member functions for
<
,>=
and!=
. In other words, write tests first, then write the code to make them pass.TEST_CASE("Test comparison of Cards") { Card c1(2, 9); Card c2(1, 9); Card c3(1, 10); Card c4(1, 10); CHECK(c2 < c1 == true); CHECK(c3 <= c2 == false); CHECK(c1 >= c3 == true); CHECK(c3 == c4 == true); CHECK(c3 != c4 == false); }
In the version of
Card
introduced in this chapter, aces are less than deuces (2s). Add the following tests and then change this so that aces are ranked higher than Kings, as they are in most card games:TEST_CASE("Test comparisons with Aces") { Card c1(4, 1); Card c2(4, 2); Card c3(4, 13); CHECK((c1 > c2) == true); CHECK((c1 > c3) == true); }
Encapsulate the deck-building code from the Vectors of cards section in a function named
build_deck
that takes no arguments and that returns a fully-populated vector ofCard
s.