summaryrefslogtreecommitdiff
path: root/labs/lab6_toh/TowersTester.java
blob: c8aed2f3463f805ef962e0193b22e69f57ea47c0 (plain)
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
public class TowersTester
{
	public static void main(String[] args)
	{
		int numDiscs = 7;
		int point = 0;
		
		TowerOfHanoi t = new TowerOfHanoi(numDiscs);
		
		// Testing the method getNumberOfDiscs()
		
		System.out.println("Construct the Table of Hanoi puzzle with 7 disks using the following statement:");
		System.out.println("  TowerOfHanoi t = new TowerOfHanoi(7);");
		System.out.println("All test will be base on results of manipulating TowerOfHanoi t.\n");
		
		System.out.print("Testing the method getNumberOfDiscs(): ");
		
		if(t.getNumberOfDiscs() != numDiscs)
		{
			System.out.println("FAIL");
			System.out.println("The method getNumberOfDiscs() should return " + numDiscs + ".");
			System.out.println("But your method getNumberOfDiscs() returns " + t.getNumberOfDiscs() + ".\n");
		}
		else
		{
			point++;
			System.out.println("PASS");
		}
		System.out.println("Your current point is " + point + ".\n");
		
		// Testing initial puzzle
		
		System.out.println("Testing initial puzzle");

		int[][] tower0 = {{6,5,4,3,2,1,0},{},{}};
		
		int[] nd0 = {7,0,0};
		
		if(testPuzzle(t, nd0, tower0))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
		System.out.println("Move top disc from tower 0 to tower 1");
		if(testMTD(t.moveTopDisc(0, 1), true, 0, 1))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		System.out.println("Testing the current puzzle");
		int[][] tower1 = {{6,5,4,3,2,1},{0},{}};
		int[] nd1 = {6,1,0};
		if(testPuzzle(t, nd1, tower1))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");

		// Move
		
		System.out.println("Move top disc from tower 0 to tower 2");
		if(testMTD(t.moveTopDisc(0, 2), true, 0, 2))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		System.out.println("Testing the current puzzle");
		int[][] tower2 = {{6,5,4,3,2},{0},{1}};
		int[] nd2 = {5,1,1};
		if(testPuzzle(t, nd2, tower2))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
		// Move
		
		System.out.println("Move top disc from tower 1 to tower 2");
		if(testMTD(t.moveTopDisc(1, 2), true, 1, 2))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		System.out.println("Testing the current puzzle");
		int[][] tower3 = {{6,5,4,3,2},{},{1,0}};
		int[] nd3 = {5,0,2};
		if(testPuzzle(t, nd3, tower3))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
		// Move (false)
		
		System.out.println("Move top disc from tower 0 to tower 2 (MUST RETURN FALSE)");
		if(testMTD(t.moveTopDisc(0, 2), false, 0, 2))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		System.out.println("Testing the current puzzle (MUST REMAIN UNCHANGED FROM PREVIOUS TEST)");
		if(testPuzzle(t, nd3, tower3))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
	}

	private static boolean testMTD(boolean aResult, boolean eResult, int fromTower, int toTower)
	{
		System.out.print("Test return value of the method moveTopDisc(): ");
		if(aResult != eResult)
		{
			System.out.println("FAIL");
			System.out.println("t.moveTopDisc(" + fromTower + "," + toTower + ") should return " + eResult + ".");
			System.out.println("But your method moveTopDisc(" + fromTower + "," + toTower + ") return " + aResult + ".");
		}
		else
		{
			System.out.println("PASS");
		}
		
		return aResult == eResult;
	}
	
	private static boolean testPuzzle(TowerOfHanoi t, int[] numDiscs, int[][] tower)
	{
		boolean result = true;
		
		for(int i = 0; i < 3; i++)
		{
			System.out.print("Testing the method getNumberOfDiscs(" + i + "): ");
			
			if(t.getNumberOfDiscs(i) != numDiscs[i])
			{
				System.out.println("FAIL");
				System.out.println("The method getNumberOfDiscs(" + i + ") should return " + numDiscs[i] + ".");
				System.out.println("But your method getNumberOfDiscs(" + i + ") returns " + t.getNumberOfDiscs(i) + ".\n");
				result = false;
			}
			else
			{
				System.out.println("PASS");
			}

			// Testing the method getArrayOfDiscs(0)
			
			System.out.print("Testing the method getArrayOfDiscs(" + i + "): ");
			
			if(!arrayToString(tower[i]).equals(arrayToString(t.getArrayOfDiscs(i))))
			{
				System.out.println("FAIL");
				System.out.println("The method getArrayOfDiscs(" + i + ") should return the array " + arrayToString(tower[i]) + ".");
				System.out.println("But your method getArrayOfDiscs(" + i + ") returns the array " + arrayToString(t.getArrayOfDiscs(i)) + ".\n");
				result = false;
			}
			else
			{
				System.out.println("PASS");
			}
		}
		
		return result;
	}
	
	private static String arrayToString(int[] array)
	{
		String result = "[";
		
		if(array.length > 0)
		{
			for(int i = 0; i < array.length - 1; i++)
			{
				result = result + array[i] + ",";
			}
		
			result = result + array[array.length - 1];
		}
		
		return result + "]";
	}
}