-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyArrayTest.java
More file actions
102 lines (83 loc) · 2.89 KB
/
Copy pathMyArrayTest.java
File metadata and controls
102 lines (83 loc) · 2.89 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
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class MyArrayTest {
@Test
public void testConstructor() {
int[] expectedArray_1 = {0, 0, 0, 0, 0};
MyArray myArray_1 = new MyArray(5);
assertEquals("Failed: Created instance by size",
Arrays.toString(expectedArray_1),
Arrays.toString(myArray_1.getElements()));
int[] expectedArray_2 = {1, 2, 3, 4, 5};
MyArray myArray_2 = new MyArray(expectedArray_2);
assertEquals("Failed: Created instance by an array",
Arrays.toString(expectedArray_2),
Arrays.toString(myArray_2.getElements()));
MyArray myArray_3 = new MyArray(0, 10, 3);
int[] expectedArray_3 = {0, 3, 6, 9};
assertEquals("Failed: Created instance by start, stop, and step",
Arrays.toString(expectedArray_3),
Arrays.toString(myArray_3.getElements()));
MyArray myArray_4 = new MyArray(0, 10, 0);
int[] expectedArray_4 = null;
assertEquals("Failed: Created instance by start, stop, and step with step = 0",
Arrays.toString(expectedArray_4),
Arrays.toString(myArray_4.getElements()));
MyArray myArray_5 = new MyArray(10, 10, 2);
int[] expectedArray_5 = {10};
assertEquals("Failed: Created instance by start, stop, and step with start = stop",
Arrays.toString(expectedArray_5),
Arrays.toString(myArray_5.getElements()));
MyArray myArray_6 = new MyArray(10, 0, 3);
int[] expectedArray_6 = null;
assertEquals("Failed: Created instance by start, stop, and step with start > stop",
Arrays.toString(expectedArray_6),
Arrays.toString(myArray_6.getElements()));
}
@Test
public void testRotation() {
int[] originalArray = {1, 2, 3, 4, 5};
int[] expectedLeftRotationArray = {3, 4, 5, 1, 2};
int[] expectedRightRotationArray = {5, 1, 2, 3, 4};
MyArray myArray = new MyArray(originalArray);
myArray.rotation(-2);
assertEquals("Failed: left rotation by 2",
Arrays.toString(expectedLeftRotationArray),
Arrays.toString(myArray.getElements()));
myArray.rotation(2);
assertEquals("Failed: right rotation by 2",
Arrays.toString(originalArray),
Arrays.toString(myArray.getElements()));
myArray.rotation(1);
assertEquals("Failed: right rotation by 1",
Arrays.toString(expectedRightRotationArray),
Arrays.toString(myArray.getElements()));
}
/*
* Finding the GCD of 2 numbers
* @param a: the first number
* @param b: the second number
* @return : the GCD of 2 above numbers
*/
public static int gcd(int a, int b) {
if (0 == b) {
return a;
}
else {
return gcd(b, a % b);
}
}
@Test
public void testGcd () {
int expectedResult = 2;
assertEquals("Failed: Wrong gcd for 2 and 4",
expectedResult, gcd(2, 4));
expectedResult = 5;
assertEquals("Failed: Wrong gcd for 5 and 0",
expectedResult, gcd(5, 0));
expectedResult = 7;
assertEquals("Failed: Wrong gcd for 0 and 7",
expectedResult, gcd(0, 7));
}
}