forked from MaxRobinson/Alt-AIResearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_TransitionTable.java
More file actions
409 lines (343 loc) · 13 KB
/
Test_TransitionTable.java
File metadata and controls
409 lines (343 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package randomPackage;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.ArrayList;
public class Test_TransitionTable {
/**
* testTransitionTable()
*
* Test the functionality of the constructor (TransitionTable())
*
* Cases Tested: - When a new table is created, check for a null table -
* Make sure that the size of the table is one (null row inserted properly)
*/
@Test
public void testTransitionTable() {
TransitionTable transitionTable = new TransitionTable();
assertNotNull("Make sure the Table is not Null", transitionTable);
boolean nullRowAtIndexZero = transitionTable.table.get(0) == null
&& transitionTable.table.size() == 1;
assertTrue("Make sure that there is one null row at index 0.",
nullRowAtIndexZero);
// Test non-default constructor
char[] alphabet = createAlphabet(5);
transitionTable = new TransitionTable(alphabet);
assertNotNull("Make sure the Table is not Null", transitionTable);
nullRowAtIndexZero = transitionTable.table.get(0) == null
&& transitionTable.table.size() == 1;
assertTrue("Make sure that there is one null row at index 0.",
nullRowAtIndexZero);
transitionTable.table.add(new StateID[alphabet.length]);
int maxIndex = transitionTable.table.size() - 1;
boolean alphabetLengthCorrect = isCorrectLength(
transitionTable.table.get(maxIndex), alphabet.length);
assertTrue(
"Make sure that the table's rows are the same length as the alphabet provided",
alphabetLengthCorrect);
}
/**
* testAddNullRow()
*
* Test the functionality of the addNullRow method
*
* Cases Tested: - Ensure the size of the table increases by one when a null
* row is added
*
*/
@Test
public void testAddNullRow() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
int sizeBeforeAdd = transitionTable.table.size();
transitionTable.addNullRow();
int sizeAfterAdd = transitionTable.table.size();
assertTrue(
"There is one more row in the table after a null row is added",
(sizeAfterAdd - sizeBeforeAdd) == 1);
}
/**
* testAddEmptyRow()
*
* Test the functionality of the addEmptyRow method
*
* Cases Tested: - Ensure the size of the table increases by one when an
* empty row is added - Ensure that all values in the new row are '-1' -
* Ensure that the length of the row is the same as the length of the
* alphabet
*
*/
@Test
public void testAddEmptyRow() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
transitionTable.addEmptyRow();
// This is what the row that was added *should* be
StateID[] correctRow = new StateID[transitionTable.alphabet.length];
for (int i = 0; i < correctRow.length; i++) {
correctRow[i] = new StateID(-1);
}
assertNotNull("Make sure that row 1 is not null.",
transitionTable.table.get(1));
assertTrue("Make sure that all values in the row are -1",
checkRowValues(transitionTable.table.get(1), correctRow));
assertTrue(
"Make sure that the row length = alphabet length",
isCorrectLength(transitionTable.table.get(1),
transitionTable.alphabet.length));
}
/**
* testAddRow()
*
* Test the functionality of adding a given row to the table
*
*/
@Test
public void testAddRow() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
int numRowsBeforeAdd = transitionTable.table.size();
StateID[] newRow = createRow(9000, transitionTable.alphabet.length);
transitionTable.addRow(newRow);
int numRowsAfterAdd = transitionTable.table.size();
boolean numRowsCorrect = numRowsAfterAdd - 1 == numRowsBeforeAdd;
int maxIndexOfTable = numRowsAfterAdd - 1;
assertTrue(
"Make sure the size of the table increases by one when a row is added",
numRowsCorrect);
assertTrue("Make sure that the row added is at the end of the table",
checkRowPosition(transitionTable, newRow, maxIndexOfTable));
assertTrue(
"Make sure that the values in the row are as expected",
checkRowValues(transitionTable.table.get(maxIndexOfTable),
newRow));
}
/**
* testIsTransitionTableFull()
*
* Test the functionality of the method that checks whether or not a table
* is full
*
*/
@Test
public void testIsTransitionTableFull() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
assertTrue(
"With no rows added the table should be full as there are no unknown transitions",
!transitionTable.containsUnknownTransitions());
transitionTable.addNullRow();
assertTrue("With a null row added, the table is still full",
!transitionTable.containsUnknownTransitions());
transitionTable
.addRow(createRow(9000, transitionTable.alphabet.length));
assertTrue(
"With a full row added with no unknown transitions, the table is still full",
!transitionTable.containsUnknownTransitions());
transitionTable.addEmptyRow();
assertTrue(
"With an empty row added to the table, the table is not full",
transitionTable.containsUnknownTransitions());
}
/**
* testUpdateSingleTransition()
*
* Test the functionality of the UpdateSingleTransition method
*
*/
@Test
public void testUpdateSingleTransition() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
StateID[] newRow = createRow(2, transitionTable.alphabet.length);
StateID[] newRow2 = createRow(3, transitionTable.alphabet.length);
transitionTable.addRow(newRow);
transitionTable.addRow(newRow2);
StateID beforeUpdate = transitionTable.table.get(1)[2];
Episode source = new Episode(' ', -1, 1);
Episode target = new Episode('c', -1, 9000);
transitionTable.updateSingleTransition(source, target);
StateID afterUpdate = transitionTable.table.get(1)[2];
assertTrue("Ensure that a value in the table is changed",
beforeUpdate.get() != afterUpdate.get());
assertTrue("The value was actually updated to the appropriate value",
afterUpdate.get() == 9000);
}
/**
* testAddPath()
*
* Test the functionality of the addPath method
*
*/
@Test
public void testAddPath() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
// Create an initial state
StateID[] starterRow = createRow(2, transitionTable.alphabet.length);
transitionTable.addRow(starterRow);
// create an array with 5 episodes
ArrayList<Episode> eps = new ArrayList<Episode>();
for (int i = 0; i < 5; ++i) {
eps.add(new Episode(alphabet[i], -1, i + 2));
}
// Use the addPath method with the list
transitionTable.addPath(eps);
// Verify they were added properly
for (int i = 1; i < 5; ++i) {
assertTrue("Testing for correct transition from episodes",
transitionTable.table.get(i + 1)[i].get() == i + 2);
}
}
/**
* testPrint()
*
* Test the functionality of the addPath method
*
*/
@Test
public void testPrint() {
char[] alphabet = createAlphabet(5);
TransitionTable transitionTable = new TransitionTable(alphabet);
// Create an initial state
StateID[] starterRow = createRow(2, transitionTable.alphabet.length);
transitionTable.addRow(starterRow);
// create an array with 5 episodes
ArrayList<Episode> eps = new ArrayList<Episode>();
for (int i = 0; i < 5; ++i) {
eps.add(new Episode(alphabet[i], -1, i + 2));
}
// Use the addPath method with the list
transitionTable.addPath(eps);
// Print it
transitionTable.print();
}// testPrint
// ---------------------------------HELPER FUNCTIONS-------------------------------------//
/**
* checkRowValues()
*
* Checks two rows for equivalence
*
* @param rowToValidate
* - The row that needs to be validated
* @param correctRow
* - The control row used for comparison
*
* @return boolean - True if the two rows are equal --OR-- False if the two
* rows are different
*
*/
private boolean checkRowValues(StateID[] rowToValidate, StateID[] correctRow) {
if (rowToValidate == null || correctRow == null)
return false;
if (rowToValidate.length != correctRow.length)
return false;
for (int i = 0; i < rowToValidate.length; ++i) {
if (rowToValidate[i].get() != correctRow[i].get()) {
return false;
}
}
return true;
}
/**
* isCorrectLength()
*
* Checks a row for correct length
*
* @param row
* - The row that needs to be validated
* @param length
* - The length the row is assumed to be
*
* @return boolean - True if the length of the row matches the given length
* --OR-- False if the length of the row differs from the given
* length
*
*/
private boolean isCorrectLength(StateID[] row, int length) {
return row.length == length;
}
/**
* checkRowPosition()
*
* Ensures that the row given is at the specified location. Given a table, a
* row index of that table and an array of StateID's, checks to make sure
* that the row at the given index is equivalent to the given row.
*
* @param table
* - The table in question
* @param row
* - The row of StateID's in question
* @param rowIndex
* - The suspected index of the row
*
* @return boolean - True if the row at the given index matches the given
* row --OR-- False if the row does not match the row at the given
* index
*/
public boolean checkRowPosition(TransitionTable table, StateID[] row,
int rowIndex) {
return checkRowValues(table.table.get(rowIndex), row);
}
/**
* doesTableContainValues()
*
* Given a TransitionTable, checks to see if the table contains any values
* or if all rows are null
*
*
* @param table
* - The transition table in question
*
* @return boolean - True if the table contains values --OR-- False if the
* table has only null entries
*/
public boolean doesTableContainValues(TransitionTable table) {
for (StateID[] row : table.table) {
if (row != null)
return true;
}
return false;
}
/**
* createRow()
*
* create a non empty array of StateID's, all values must not be equal to
* '-1'
*
* @param value
* - The Value to be placed in each slot of the array This value
* should most likely be 9000 in reference to the jackhawk9000
* which is of course available at walmart
*
* @param length
* - The length of the array to be created
*
* @return StateID[] - An array of StateID's with specified values
*/
public StateID[] createRow(int value, int length) {
StateID[] row = new StateID[length];
for (int i = 0; i < row.length; i++) {
row[i] = new StateID(value);
}
return row;
}
/**
* createAlphabet()
*
* Given a number of characters, create an array of that many characters
*
* @param numchars
* - Number of characters to be placed in the alphabet
* @return char[] - The alphabet created
*/
private char[] createAlphabet(int numChars) {
char[] wholeAlphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };
char[] returnAlphabet = new char[numChars];
for (int i = 0; i < numChars; ++i) {
returnAlphabet[i] = wholeAlphabet[i];
}
return returnAlphabet;
}
}