-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAssignment3_tester
356 lines (336 loc) · 16 KB
/
Assignment3_tester
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
//version 1.0.6
import java.math.BigInteger;
import java.util.*;
public class Test {
public static void main(String[] args) {
//runAllTests(); //Runs all the tests
//testPartA(); //Runs all tests for part A
//testPartB(); //Runs all tests for part B
//testQ1E1(); //Runs tests for 1.1
//testQ1E2(); //Runs tests for 1.2
//testQ1E3(); //Runs tests for 1.3
//testQ1E4(); //Runs tests for 1.4, needs parameter n to run n times
//testQ2E1(); //Runs tests for 2.1
//testQ2E2(); //Runs tests for 2.2
//testQ2E3(); //Runs tests for 2.3
//testQ2E4(); //Runs tests for 2.4
//testQ2E5(); //Runs tests for 2.5
//testQ2E6(); //Runs tests for 2.6
//testQ3E2(); //Runs tests for 3.2
//testQ3E3(); //Runs tests for 3.3
//testQ3E5(); //Runs tests for 3.5
//testQ3E6(); //Runs tests for 3.6
}
private static void runAllTests() {
testPartA();
testPartB();
}
private static void testPartA() {
System.out.println("Testing 1.1: ");
testQ1E1();
System.out.println();
System.out.println("Testing 1.2: ");
testQ1E2();
System.out.println();
System.out.println("Testing 1.3: ");
testQ1E3();
System.out.println();
int n = 2 + (int) (Math.random() * 5);
System.out.println("Testing 1.4 " + n + " times: ");
testQ1E4(n);
System.out.println();
System.out.println("Testing 2.1: ");
testQ2E1();
System.out.println();
System.out.println("Testing 2.2: ");
testQ2E2();
System.out.println();
System.out.println("Testing 2.3: ");
testQ2E3();
System.out.println();
}
private static void testPartB() {
System.out.println("Testing 2.4: ");
testQ2E4();
System.out.println();
System.out.println("Testing 2.5: ");
testQ2E5();
System.out.println();
System.out.println("Testing 2.6: ");
testQ2E6();
System.out.println();
System.out.println("Testing 3.2: ");
testQ3E2();
System.out.println();
System.out.println("Testing 3.3: ");
testQ3E3();
System.out.println();
System.out.println("Testing 3.5: ");
testQ3E5();
System.out.println();
System.out.println("Testing 3.6: ");
testQ3E6();
System.out.println();
}
private static void testQ1E1() {
boolean passed = true;
BigInteger output;
String[] inputs = {"-10", "0", "7", "99999"};
String[] expectedOutputs = {"0", "0", "21", "4999850001"};
for (int i = 0; i < inputs.length; i++) {
output = Part1_BigInteger.sumSmaller(new BigInteger(inputs[i]));
if (!output.equals(new BigInteger(expectedOutputs[i]))) {
passed = false;
System.out.println("Failed on input: " + inputs[i]);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ1E2() {
int n = (int) (Math.random() * 11);
System.out.println("Expected " + n + " random integers");
Part1_BigInteger.printRandoms(n);
}
private static void testQ1E3() {
boolean passed = true;
boolean output;
String[] inputs = {"0","1", "2", "274876858367", "4398042316799", "3576702497", "5915587277"};
boolean[] expectedOutputs = {false, false, true, true, true, false, true};
for (int i = 0; i < inputs.length; i++) {
output = Part1_BigInteger.isPrime(new BigInteger(inputs[i]));
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i]);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ1E4(int timesToTest) {
boolean passed = true;
BigInteger bi, twoToThePowerOfN;
for (int i = 0; i < timesToTest; i++) {
int n = 2 + (int) (Math.random() * 14);
twoToThePowerOfN = twoToThePowerOfN(n);
bi = Part1_BigInteger.randomPrime(n);
if (!Part1_BigInteger.isPrime(bi)) {
passed = false;
System.out.println("Failed: The returned number " + bi + " isn't prime according to your function Part1_BigInteger.isPrime");
}
if (bi.compareTo(twoToThePowerOfN) != -1) {
passed = false;
System.out.println("Failed: The returned number " + bi + " isn't smaller than 2^" + n + "=" + twoToThePowerOfN);
}
if (bi.compareTo(BigInteger.ONE) != 1) {
passed = false;
System.out.println("Failed: The returned number " + bi + " isn't bigger than 1");
}
}
if (passed)
System.out.println("Passed all tests");
}
private static BigInteger twoToThePowerOfN(int n) {
byte[] bits;
byte activeBit = 1;
int count = 1 + (n / 8);
for (int i = 0; i < n % 8; i++) {
activeBit *= 2;
}
if (activeBit < 0) {
count += 1;
bits = new byte[count];
bits[1] = activeBit;
} else {
bits = new byte[count];
bits[0] = activeBit;
}
return new BigInteger(bits);
}
private static void testQ2E1() {
boolean passed = true;
boolean output;
int[][] inputs = {{2, 3}, {8, 9}, {2, 5, 10}, {2, 5, 10}, {2, 10, 20, 100}};
int[] inputs2 = {2, 59, 13, 16, 15};
boolean[] expectedOutputs = {true, true, true, true, false};
for (int i = 0; i < inputs.length; i++) {
output = Part1_Change.change(inputs[i], inputs2[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]) + " , " + inputs2[i]);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ2E2() {
boolean passed = true;
boolean output;
int[][] inputs = {{2, 3}, {2, 3}, {3, 7, 8, 9, 12}, {3, 7, 8, 9, 12, 13, 17, 19, 21, 26, 38}, {7, 8, 9, 12, 13, 17, 19, 21, 26, 38}, {2, 3}};
int[] inputs2 = {3, 2, 165, 165, 165, 2};
int[] inputs3 = {1, 1, 15, 54, 23, 1};
boolean[] expectedOutputs = {true, true, true, false, true, true};
for (int i = 0; i < inputs.length; i++) {
output = Part1_Change.changeLimited(inputs[i], inputs2[i], inputs3[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]) + " , " + inputs2[i] + " , " + inputs3[i]);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ2E3() {
int[][] inputs = {{2, 3}, {2, 3}, {3, 7, 8, 9, 12}, {3, 7, 8, 9, 12, 13, 17, 19, 21, 26, 38}, {7, 8, 9, 12, 13, 17, 19, 21, 26, 38}, {2, 3}};
int[] inputs2 = {3, 2, 165, 165, 165, 2};
int[] inputs3 = {1, 1, 15, 54, 23, 1};
boolean[] expectedOutputs = {true, true, true, false, true, true};
for (int i = 0; i < inputs.length; i++) {
if (expectedOutputs[i]) {
System.out.println("A solution should be printed:");
Part1_Change.printChangeLimited(inputs[i], inputs2[i], inputs3[i]);
System.out.println("Check if the sum of the printed sequence =" + inputs2[i] + ", that there is no ',' at the end, no spaces AND that it is sorted");
} else {
System.out.println("No solution should be printed:");
Part1_Change.printChangeLimited(inputs[i], inputs2[i], inputs3[i]);
System.out.println("If a sequence has been printed your code is defective");
}
System.out.println();
}
}
private static void testQ2E4() {
boolean passed = true;
int output;
int[][] inputs = {{1, 2, 3}, {5, 10, 20, 50, 100}, {5, 10, 50}};
int[] inputs2 = {4, 100, 65};
int[] inputs3 = {2, 5, 2};
int[] expectedOutputs = {2, 3, 0};
for (int i = 0; i < inputs.length; i++) {
output = Part2_Change.countChangeLimited(inputs[i], inputs2[i], inputs3[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]) + " , " + inputs2[i] + " , " + inputs3[i]);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ2E5() {
int[][] inputs = {{1, 2, 3}, {5, 10, 20, 50, 100}, {5, 10, 50}};
int[] inputs2 = {4, 100, 65};
int[] inputs3 = {2, 5, 2};
int[] expectedOutputs = {2, 3, 0}; //number of lines we expect as output
for (int i = 0; i < inputs.length; i++) {
System.out.println("We expect a print of " + expectedOutputs[i] + " lines:");
Part2_Change.printAllChangeLimited(inputs[i], inputs2[i], inputs3[i]);
System.out.println("Check if " + expectedOutputs[i] + " unique sequences have been printed,");
System.out.println("if the sum of each printed sequences =" + inputs2[i] + " , that there is no ',' at the end, no spaces AND that it is sorted");
System.out.println();
}
}
private static void testQ2E6() {
boolean passed = true;
int output;
int[] inputs = {1, 2, 3, 13, 20, 35, 50};
int[] expectedOutputs = {3, 7, 14, 1228, 8689, 178636, 1655256};
for (int i = 0; i < inputs.length; i++) {
output = Part2_Change.changeInCuba(inputs[i]);
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + inputs[i]);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ3E2() {
boolean passed = true;
Bit input;
int output;
boolean[] inputs = {false, true};
int[] expectedOutputs = {0, 1};
for (int i = 0; i < inputs.length; i++) {
input = new Bit(inputs[i]); //input
output = input.toInt();
if (output != expectedOutputs[i]) {
passed = false;
System.out.println("Failed on input: " + input);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ3E3() {
boolean passed = true;
Bit input;
String output;
boolean[] inputs = {false, true};
String[] expectedOutputs = {"0", "1"};
for (int i = 0; i < inputs.length; i++) {
input = new Bit(inputs[i]); //input
output = input.toString();
if (!output.equals(expectedOutputs[i])) {
passed = false;
System.out.println("Failed on input: " + input);
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + output);
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ3E5() {
boolean passed = true;
NumberAsBits input;
Bit[][] inputs = {{},
{new Bit(false)},
{new Bit(true)},
{new Bit(true), new Bit(true)},
{new Bit(false), new Bit(true), new Bit(true)},
{new Bit(true), new Bit(false), new Bit(false), new Bit(false)},
{new Bit(true), new Bit(true), new Bit(false), new Bit(false)},
{new Bit(true), new Bit(true), new Bit(false), new Bit(true)},
{new Bit(true), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false)},
{new Bit(true), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false),new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false), new Bit(false)}};
String[] expectedOutputs = {"", "0", "1", "3", "3", "8", "12", "13", "4294967296","36893488147419103232"};
for (int i = 0; i < expectedOutputs.length; i++) {
input = new NumberAsBits(inputs[i]); //input
if (!input.toString().equals(expectedOutputs[i])) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]));
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + input.toString());
}
}
if (passed)
System.out.println("Passed all tests");
}
private static void testQ3E6() {
boolean passed = true;
NumberAsBits input;
Bit[][] inputs = {{},
{new Bit(false)},
{new Bit(true)},
{new Bit(true), new Bit(true)},
{new Bit(true), new Bit(false), new Bit(false), new Bit(false)},
{new Bit(true), new Bit(true), new Bit(false), new Bit(false)},
{new Bit(true), new Bit(true), new Bit(false), new Bit(true)},
{new Bit(false), new Bit(true), new Bit(false), new Bit(true)}};
String[] expectedOutputs = {"", "0", "1", "11", "1000", "1100", "1101", "101"};
for (int i = 0; i < expectedOutputs.length; i++) {
input = new NumberAsBits(inputs[i]); //input
if (!input.base2().equals(expectedOutputs[i])) {
passed = false;
System.out.println("Failed on input: " + Arrays.toString(inputs[i]));
System.out.println("Expected output: " + expectedOutputs[i] + " But output was: " + input.base2());
}
}
if (passed)
System.out.println("Passed all tests");
}
}