From ade9f4e7c5be80c4410f0f9e4272060180b8b1c7 Mon Sep 17 00:00:00 2001 From: FrederickPi1969 <pixinyudeyouxiang@gmail.com> Date: Thu, 4 Feb 2021 21:51:12 -0600 Subject: [PATCH] Add test for card manager --- .idea/uiDesigner.xml | 124 ++++++++++++++++++ .../sp21-cs242-assignment1/cardDealTest.class | Bin 0 -> 3160 bytes src/Test/cardDealTest.java | 110 ++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 .idea/uiDesigner.xml create mode 100644 out/test/sp21-cs242-assignment1/cardDealTest.class create mode 100644 src/Test/cardDealTest.java diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..b93ac08 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Palette2"> + <group name="Swing"> + <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> + </item> + <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> + <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> + <initial-values> + <property name="text" value="Button" /> + </initial-values> + </item> + <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="RadioButton" /> + </initial-values> + </item> + <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="CheckBox" /> + </initial-values> + </item> + <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="Label" /> + </initial-values> + </item> + <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> + <preferred-size width="-1" height="20" /> + </default-constraints> + </item> + <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> + </item> + </group> + </component> +</project> \ No newline at end of file diff --git a/out/test/sp21-cs242-assignment1/cardDealTest.class b/out/test/sp21-cs242-assignment1/cardDealTest.class new file mode 100644 index 0000000000000000000000000000000000000000..33cd2ce1b1236cb0a8e86a00b7ae092483bc6674 GIT binary patch literal 3160 zcmb_dU2_xX6@Ff6<(1_PmJR-l>k?wXHZtZbq{Sd?Yy(Oqn}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eBPD3=v~gUZG$3;t#$B!DqzT literal 0 HcmV?d00001 diff --git a/src/Test/cardDealTest.java b/src/Test/cardDealTest.java new file mode 100644 index 0000000..130070a --- /dev/null +++ b/src/Test/cardDealTest.java @@ -0,0 +1,110 @@ +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; + + +/** + * These testcases test behavior of many functions, includes + * 1. All functions in CardManager + * 2. Interaction between Game and CardManager + * 3. Player.{drawCards, playOneRound} + */ +class cardDealTest { + + @Test + /** + * Test card manager can initialize 108 cards correctly + */ + void testInitialization() throws Exception { + CardManager cardManager = new CardManager(); + assert(cardManager.getCardPile().size() == 108); + for (int i = 1; i <= 108; i++) { + assert (cardManager.getCardPile().contains(i)); + } + } + + @Test + /** + * Given n players, test behavior of initial deal, as well as behavior of draw pile + * dependencies: Player.drawCards, CardManager.* + */ + void testInitialDeal() throws Exception { + + Game game1 = new Game(1); + for (Player p : game1.players) { + assert(p.getCards().size() == 7); + } + assert(game1.gameCardManager.numCardLeft() == 101); // 108 - 7 + + Game game2 = new Game(5); + for (Player p : game2.players) { + assert(p.getCards().size() == 7); + } + assert(game2.gameCardManager.numCardLeft() == 73); // 108 - 35 + + Game game3 = new Game(10); + for (Player p : game3.players) { + assert(p.getCards().size() == 7); + } + assert(game3.gameCardManager.numCardLeft() == 38); // 108 - 70 + } + + + @Test + /** + * Test the behavior of Player.drawCards + * under the condition where draw pile is not enough to draw, should draw from discard pile instead + * case 1: completely draw from discard pile + */ + void testDrawFromDiscardWhenDrawNotEnough_Case1() throws Exception { + Game game = new Game(1); + Player player = game.players.get(0); + ArrayList<Integer> cards = player.getCards(); // direct reference to player's card + player.drawCards(game.gameCardManager.numCardLeft()); // get all cards in draw pile + assert(game.gameCardManager.numCardLeft() == 0); + + // move 30 cards from players' to discard + for (int i = 0; i < 30; i++) { + int cardID = cards.get(0); + cards.remove(0); + game.gameCardManager.insertOneCardToDiscardPile(cardID); + } + assert(game.gameCardManager.numLeftDiscardPile() == 30); + + // then drawCards + player.drawCards(4); + assert(game.gameCardManager.numLeftDiscardPile() == 26); + } + + @Test + /** + * Test the behavior of Player.drawCards + * under the condition where draw pile is not enough to draw, should draw from discard pile instead + * case 2: partially from draw pile, partially from discard pile + */ + void testDrawFromDiscardWhenDrawNotEnough_Case2() throws Exception{ + Game game = new Game(1); + Player player = game.players.get(0); + ArrayList<Integer> cards = player.getCards(); // direct reference to player's card + player.drawCards(game.gameCardManager.numCardLeft() - 2); // get 99 cards from draw pile + assert(game.gameCardManager.numCardLeft() == 2); + + // move 30 cards from players' to discard + for (int i = 0; i < 30; i++) { + int cardID = cards.get(0); + cards.remove(0); + game.gameCardManager.insertOneCardToDiscardPile(cardID); + } + assert(game.gameCardManager.numLeftDiscardPile() == 30); + + // 2 from discard, 2 from draw + player.drawCards(4); + assert(game.gameCardManager.numCardLeft() == 0); + assert(game.gameCardManager.numLeftDiscardPile() == 28); + } + + + +} \ No newline at end of file -- GitLab