summaryrefslogtreecommitdiff
path: root/labs/lab01_pair/PairTester.java
blob: a1d3d50420e1cd5b13559c5c8ed361ab0944636a (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
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
import java.util.Random;

public class PairTester
{
	public static void main(String[] args)
	{
		int point = 0;
		Random rand = new Random();
		
		// Test a pair of string and integer
		
		System.out.print("Testing Pair<String,Integer>: ");

		int l1 = rand.nextInt(10) + 10;
		String s1 = randomString(l1);
		PairInterface<String,Integer> p1 = new Pair<String,Integer>(s1,l1);

		if(checkFromToString("(" + s1 + "," + l1 + ")", p1.toString()))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
		// Test a pair of integer and string
		
		System.out.print("Testing Pair<Integer,String>: ");
		int l2 = rand.nextInt(10) + 10;
		String s2 = randomString(l2);
		PairInterface<Integer,String> p2 = new Pair<Integer,String>(l2,s2);
		if(checkFromToString("(" + l2 + "," + s2 + ")", p2.toString()))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
	
		// Test a pair of String and pair
		
		System.out.print("Testing Pair<String,Pair<Integer,String>>: ");
		int l3_1 = rand.nextInt(10) + 10;
		int l3_2 = rand.nextInt(10) + 10;
		String s3_1 = randomString(l3_1);
		String s3_2 = randomString(l3_2);
		PairInterface<Integer,String> p3_1 = new Pair<Integer,String>(l3_1 + l3_2, s3_1);
		PairInterface<String, PairInterface<Integer,String>> p3_2 = new Pair<String, PairInterface<Integer,String>>(s3_2, p3_1);
		if(checkFromToString("(" + s3_2 + ",(" + (l3_1 + l3_2) + "," + s3_1 + "))", p3_2.toString()))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");

		// Test a pair of pair and Integer
		
		System.out.print("Testing Pair<String,Pair<Integer,String>>: ");
		int l4_1 = rand.nextInt(10) + 10;
		int l4_2 = rand.nextInt(10) + 10;
		String s4 = randomString(l4_1 + l4_2);
		PairInterface<String,Integer> p4_1 = new Pair<String,Integer>(s4,l4_1);
		Pair<Integer, PairInterface<String,Integer>> p4_2 = new Pair<Integer, PairInterface<String,Integer>>(l4_2, p4_1);
		if(checkFromToString("(" + l4_2 + ",(" + s4 + "," + l4_1 + "))", p4_2.toString()))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");

		// Test 5-tuple
		
		System.out.print("Testing 5-tuple (e.g., (1,(2,(3,(4,5))))): ");
		int[] l5 = new int[5];
		for(int i = 0; i < 5; i++)
		{
			l5[i] = rand.nextInt(10) + 10;
		}
		String s5 = "(" + l5[0] + ",(" + l5[1] + ",(" + l5[2] + ",(" + l5[3] + "," + l5[4] + "))))";
		PairInterface<Integer,Integer> p5_1 = new Pair<Integer,Integer>(l5[3],l5[4]);
		PairInterface<Integer,PairInterface<Integer,Integer>> p5_2 = new Pair<Integer,PairInterface<Integer,Integer>>(l5[2],p5_1);
		PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>> p5_3 =
				new Pair<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>>(l5[1],p5_2);
		PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>>> p5_4 =
				new Pair<Integer,PairInterface<Integer,PairInterface<Integer,PairInterface<Integer,Integer>>>>(l5[0],p5_3);
		if(checkFromToString(s5, p5_4.toString()))
		{
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");

		if(point != 5)
		{
			System.out.println("This test class will use variables generated in");
			System.out.println("previous tests. Your class Pair fails one or more");
			System.out.println("test(s). This test class will terminated. Fix");
			System.out.println("your class Pair first and run the test class again.");
			System.out.println("Your current point is " + point + ".\n");
			return;
		}
		
		// Testing the method equals()
		
		System.out.println("Testing the method equals()");
		
		PairInterface<String,Integer> p6 = new Pair<String,Integer>(s1,l1);

		System.out.print("Test whether " + p1 + " is equals to " + p6 + ": ");
		if(!p1.equals(p6))
		{
			System.out.println("FAIL");
			System.out.println("The pair " + p1 + " and the pair " + p6 + " are equal.");
			System.out.println("But your method equals() returned false.");
			System.out.println("Fix your method equals() first and run the test class again.");
			System.out.println("Your current point is " + point + ".\n");
			return;
		}
		else
		{
			System.out.println("PASS");
		}
		
		PairInterface<String,Integer> p6_2 = new Pair<String,Integer>(s1.substring(1,s1.length()), l1);
		System.out.print("Test whether " + p1 + " is equals to " + p6_2 + ": ");
		if(p1.equals(p6_2))
		{
			System.out.println("FAIL");
			System.out.println("The pair " + p1 + " and the pair " + p6_2 + " are not equal.");
			System.out.println("But your method equals() returned true.");
			System.out.println("Fix your method equals() first and run the test class again.");
			System.out.println("Your current point is " + point + ".\n");
			return;
		}
		else
		{
			System.out.println("PASS");
		}
		
		PairInterface<String,Integer> p6_3 = new Pair<String,Integer>(s1, l1 + 1);
		System.out.print("Test whether " + p1 + " is equals to " + p6_3 + ": ");
		if(p1.equals(p6_3))
		{
			System.out.println("FAIL");
			System.out.println("The pair " + p1 + " and the pair " + p6_3 + " are not equal.");
			System.out.println("But your method equals() returned true.");
			System.out.println("Fix your method equals() first and run the test class again.");
			System.out.println("Your current point is " + point + ".\n");
			return;
		}
		else
		{
			System.out.println("PASS");
		}
		
		point++;
		System.out.println("Your current point is " + point + ".\n");
		
		

		System.out.print("Testing the method equals() (x,(y,z)) = (x,(y,z)): ");
		PairInterface<Integer,String> p7_1 = new Pair<Integer,String>(l3_1 + l3_2, s3_1);
		Pair<String, PairInterface<Integer,String>> p7_2 = new Pair<String, PairInterface<Integer,String>>(s3_2, p7_1);
		if(!p3_2.equals(p7_2))
		{
			System.out.println("FAIL");
			System.out.println("The pair " + p3_2 + " and the pair " + p7_2 + " are equal.");
			System.out.println("But your method equals() returned false.");
			System.out.println("Fix your method equals() first and run the test class again.");
			System.out.println("Your current point is " + point + ".\n");
			return;
		}
		else
		{
			System.out.println("PASS");
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");

		// Testing the method fst()

		System.out.print("Testing the method fst(): ");
		if(!s1.equals(p1.fst()))
		{
			System.out.println("FAIL");
			System.out.println("First element of the pair " + p1 + " is " + s1 + ".");
			System.out.println("But the method fst() of your class returned " + p1.fst() + ".\n");
		}
		else
		{
			System.out.println("PASS");
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
		// Testing the method snd()
		
		System.out.print("Testing the method snd(): ");
		if(!s2.equals(p2.snd()))
		{
			System.out.println("FAIL");
			System.out.println("Second element of the pair " + p2 + " is " + s2 + ".");
			System.out.println("But the method snd() of your class returned " + p2.fst() + ".\n");
		}
		else
		{
			System.out.println("PASS");
			point++;
		}
		System.out.println("Your current point is " + point + ".\n");
		
		// Test setFst() and setSnd()
		
		System.out.print("Testing the methods setFst() and setSnd(): ");
		PairInterface<String,Integer> p10_1 = new Pair<String,Integer>("Hello",123);
		PairInterface<String,Integer> p10_2 = new Pair<String,Integer>("How are you?",54321);
		
		p10_1.setFst("How are you?");
		p10_1.setSnd(54321);
		
		if(!p10_1.equals(p10_2))
		{
			System.out.println("FAIL");
			System.out.println("The pair p10_1 was constructed using the following statement:");
			System.out.println("   PairInterface<String,Integer> p10_1 = new Pair<String,Integer>(\"Hello\",123);");
			System.out.println("Then the folloiwng statements are executed:");
			System.out.println("   p10_1.setFst(\"How are you?\");");
			System.out.println("   p10_1.setSnd(54321);");
			System.out.println("After above statements, the pair p10_1 should be " + p10_2 + ".");
			System.out.println("But the method toString() of our pair p10_1 is returns " + p10_1 + ".\n");
		}
		else
		{
			System.out.println("PASS");
			point++;
		}
		System.out.println("Your final point is " + point + ".\n");

		if(point == 10)
		{
			System.out.println("Contratulation! Your class Pair works perfectly (I guess).");
			System.out.println("You can run ParabolaFrame to see how Pair can be used in a program.");
		}
		else
		{
			System.out.println("There is one or more errors in your class.");
			System.out.println("Fix your bugs to get more points.");
		}
	}
	
	public static boolean checkFromToString(String s1, String s2)
	{
		if(s1.equals(s2))
		{
			System.out.println("PASS");
			return true;
		}
		else
		{
			System.out.println("FAILED");
			System.out.println("The method toString() should to return: " + s1);
			System.out.println("But your method toString() returns    : " + s2);
			return false;
		}
	}
	
	public static String randomString(int length)
	{
		String result = "";
		
		for(int i = 0; i < length; i++)
		{
			result = result + randomChar();
		}
		
		return result;
	}
	
	public static char randomChar()
	{
		Random rand = new Random();
		int modifier = rand.nextInt(26);
		
		if(rand.nextBoolean())
		{
			return (char) ('a' + modifier); 
		}
		else
		{
			return (char) ('A' + modifier);
		}
	}
}