OSDN Git Service

Initial commit.
[gamerandomizer/gamerandomizer.git] / GameRandomizer / srctest / jp / sourceforge / gamerandomizerlib / GameTest.java
1 //
2 // Copyright (c) 2013  Motoyuki Kasahara
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 package jp.sourceforge.gamerandomizerlib;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Random;
22 import junit.framework.*;
23 import junit.textui.TestRunner;
24 import nedragtna.random.MTRandom;
25
26 public final class GameTest extends TestCase {
27         public GameTest(String name) {
28                 super(name);
29         }
30
31         //
32         // Test Game().
33         //
34         public void testCostructor() {
35                 try {
36                         String id = "test-game-id";
37                         String title = "test-game-title";
38                         int selectionSize = 10;
39
40                         Game g = new Game(id, title, selectionSize);
41
42                         assertEquals(g.getId(), id);
43                         assertEquals(g.getTitle(), title);
44                         assertEquals(g.getSelectionSize(), selectionSize);
45                 } catch (Exception e) {
46                         fail(e.getMessage());
47                 }
48         }
49
50         //
51         // Test Game(game).
52         //
53         public void testCopyCostructor() {
54                 try {
55                         String id = "test-game-id";
56                         String title = "test-game-title";
57                         int selectionSize = 10;
58
59                         Game g1 = new Game(id, title, selectionSize);
60                         Game g2 = new Game(g1);
61
62                         assertEquals(g2.getId(), id);
63                         assertEquals(g2.getTitle(), title);
64                         assertEquals(g2.getSelectionSize(), selectionSize);
65                 } catch (Exception e) {
66                         fail(e.getMessage());
67                 }
68         }
69
70         //
71         // Test getId(), getTitle() and getSelectionSize().
72         //
73         public void testBasicGetters() {
74                 try {
75                         String id = "test-game-id";
76                         String title = "test-game-title";
77                         int selectionSize = 10;
78
79                         int gamesSize = 10;
80                         Game[] games = new Game[gamesSize];
81                         for (int i = 0; i < gamesSize; i++)
82                                 games[i] = new Game(id + i, title + i, selectionSize + i);
83
84                         for (int i = 0; i < gamesSize; i++) {
85                                 assertEquals(games[i].getId(), id + i);
86                                 assertEquals(games[i].getTitle(), title + i);
87                                 assertEquals(games[i].getSelectionSize(), selectionSize + i);
88                         }
89                 } catch (Exception e) {
90                         fail(e.getMessage());
91                 }
92         }
93
94         //
95         // Test clear().
96         //
97         public void testClear() {
98                 try {
99                         Game g = new Game("test-game-id", "", 10);
100                         Expansion x1 = new Expansion("test-expansion-id1", "", 1000);
101                         Expansion x2 = new Expansion("test-expansion-id2", "", 1010);
102                         g.addExpansion(x1);
103                         g.addExpansion(x2);
104                         g.addCard(new Card("test-card-id1", "", "", x1));
105                         g.addCard(new Card("test-card-id1", "", "", x2));  // duplicate
106                         g.addCard(new Card("test-card-id2", "", "", x2));
107                         g.addCard(new Card("test-card-id3", "", "", x2));
108                         assertTrue(g.getExpansions().size() > 0);
109                         assertTrue(g.getCards().size() > 0);
110
111                         g.clear();
112                         
113                         assertTrue(g.getExpansions().size() == 0);
114                         assertTrue(g.getCards().size() == 0);
115
116                         //
117                         // Check if duplicate cards are also removed by clear().
118                         //
119                         g.addCard(new Card("test-card-id1", "", "", x1));
120                         g.removeCard(new Card("test-card-id1", "", "", x1));
121
122                         assertTrue(g.getCards().size() == 0);
123                 } catch (Exception e) {
124                         fail(e.getMessage());
125                 }
126         }
127
128         //
129         // Compare 'List<Expansion>' and 'Expansion ...expansions'.
130         //
131         public boolean compareExpansions(List<Expansion> list,
132                 Expansion ...expansions) {
133                 int i = 0;
134                 for (Expansion x : expansions) {
135                         if (list.size() >= i || !x.equals(list.get(i)))
136                                 return false;
137                         i++;
138                 }
139                 return true;
140         }
141                 
142         //
143         // Compare 'List<Card>' and 'Card ...cards'.
144         //
145         public boolean compareCards(List<Card> list, Card ...cards) {
146                 int i = 0;
147                 for (Card c : cards) {
148                         if (list.size() >= i || !c.equals(list.get(i)))
149                                 return false;
150                         i++;
151                 }
152                 return true;
153         }
154                 
155         //
156         // Test addExpansion().
157         //
158         public void testAddExpansion() {
159                 try {
160                         Game g = new Game("test-game-id", "", 10);
161
162                         Expansion x1 = new Expansion("test-expansion-id1", "", 1010);
163                         assertTrue(g.addExpansion(x1));
164                         compareExpansions(g.getExpansions(), x1);
165
166                         Expansion x2 = new Expansion("test-expansion-id2", "", 1020);
167                         assertTrue(g.addExpansion(x2));
168                         compareExpansions(g.getExpansions(), x1, x2);
169
170                         Expansion x3 = new Expansion("test-expansion-id3", "", 1000);
171                         assertTrue(g.addExpansion(x3));
172                         compareExpansions(g.getExpansions(), x3, x1, x2);
173
174                         Expansion x4 = new Expansion("test-expansion-id3", "?", 1030);
175                         assertFalse(g.addExpansion(x4));
176                         compareExpansions(g.getExpansions(), x3, x1, x2);
177                 } catch (Exception e) {
178                         fail(e.getMessage());
179                 }
180         }
181
182         //
183         // Test addExpansions().
184         //
185         public void testAddExpansions() {
186                 try {
187                         Game g = new Game("test-game-id", "", 10);
188
189                         List<Expansion> xl = new ArrayList<Expansion>();
190                         Expansion x1 = new Expansion("test-expansion-id1", "", 1010);
191                         Expansion x2 = new Expansion("test-expansion-id2", "", 1020);
192                         Expansion x3 = new Expansion("test-expansion-id3", "", 1000);
193
194                         assertTrue(g.addExpansion(x1));
195                         compareExpansions(g.getExpansions(), x1);
196
197                         assertTrue(g.addExpansion(x2));
198                         compareExpansions(g.getExpansions(), x1, x2);
199
200                         assertTrue(g.addExpansion(x3));
201                         compareExpansions(g.getExpansions(), x3, x1, x2);
202
203                         Expansion x4 = new Expansion("test-expansion-id3", "?", 1030);
204                         assertFalse(g.addExpansion(x4));
205                         compareExpansions(g.getExpansions(), x3, x1, x2);
206                 } catch (Exception e) {
207                         fail(e.getMessage());
208                 }
209         }
210
211         //
212         // Test toString().
213         //
214         public void testToString() {
215                 try {
216                         String title = "test-game-title";
217                         Game g = new Game("test-game-id", title, 10);
218                         assertEquals(g.toString(), title);
219                 } catch (Exception e) {
220                         fail(e.getMessage());
221                 }
222         }
223
224         public static void main(String[] args) {
225                 junit.textui.TestRunner.run(GameTest.class);
226         }
227 }