-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartitionTest.java
151 lines (123 loc) · 4.62 KB
/
PartitionTest.java
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
package quick;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.junit.Test;
public class PartitionTest {
@Test(expected = AssertionError.class)
public void testAssertionEnabled() {
assert false;
}
@Test
public void testFewNums() {
final int size = 10;
int start = 0;
int end = size - 1;
for (int i = 0; i < 3; i++) {
int[] arr = TestTools.generateIntArray(size, 0, 10000);
int pivotIndex = Quick.partition(arr, start, end);
testArrayPivoted(arr, pivotIndex, start, end);
}
}
@Test
public void testLotsOfNums() {
final int size = 1000;
int start = 0;
int end = size - 1;
for (int i = 0; i < 2; i++) {
int[] arr = TestTools.generateIntArray(size, 0, 10000);
int pivotIndex = Quick.partition(arr, start, end);
testArrayPivoted(arr, pivotIndex, start, end);
}
}
@Test
public void testSpecificSizes() {
final int[] sizes = new int[] { 2, 2 * 2, 2 * 2 * 2, 2 * 2 * 2 * 2 * 2 * 2,
2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2, 3 * 3 * 3 };
// Test case of size 1 doesn't work
for (int size : sizes) {
int start = 0;
int end = size - 1;
int[] arr = TestTools.generateIntArray(size, 0, 10000);
int pivotIndex = Quick.partition(arr, start, end);
testArrayPivoted(arr, pivotIndex, start, end);
}
}
@Test
public void testLargeValueSpace() {
final int size = 50;
int start = 0;
int end = size - 1;
for (int i = 0; i < 2; i++) {
int[] arr = TestTools.generateIntArray(size, 0);
int pivotIndex = Quick.partition(arr, start, end);
testArrayPivoted(arr, pivotIndex, start, end);
}
}
@Test
public void testSmallValueSpace() {
final int size = 10;
int start = 0;
int end = size - 1;
for (int i = 0; i < 3; i++) {
int[] arr = TestTools.generateIntArray(size, 1, 20);
int pivotIndex = Quick.partition(arr, start, end);
testArrayPivoted(arr, pivotIndex, start, end);
}
}
@Test
public void testDutchRepetition() {
final int size = 1000;
int start = 0;
int end = size - 1;
for (int i = 0; i < 3; i++) {
int[] arr = TestTools.generateIntArray(size, 1, 3);
int result[] = Quick.partitionDutch(arr, start, end);
int low = result[0], high = result[1];
testArrayPivoted(arr, low, high, start, end);
}
}
@Test
public void testDutchTwoVals() {
final int size = 2;
int start = 0;
int end = size - 1;
for (int i = 0; i < 3; i++) {
int[] arr = TestTools.generateIntArray(size, 1);
int result[] = Quick.partitionDutch(arr, start, end);
int low = result[0], high = result[1];
testArrayPivoted(arr, low, high, start, end);
}
for (int i = 0; i < 3; i++) {
int[] arr = TestTools.generateIntArray(size, -5, 3);
int result[] = Quick.partitionDutch(arr, start, end);
int low = result[0], high = result[1];
testArrayPivoted(arr, low, high, start, end);
}
}
private void testArrayPivoted(int[] arr, int pivotIndex, int start, int end) {
int pivot = arr[pivotIndex];
for (int i = start; i < pivotIndex; i++) {
assertTrue("Vals on the left of pivot should be < pivot", arr[i] <= pivot);
}
for (int i = pivotIndex; i <= end; i++) {
assertTrue("Vals on the right of pivot should be > pivot", arr[i] >= pivot);
}
// System.out.println(Arrays.toString(arr));
// System.out.println("Pivot: " + pivot);
}
private void testArrayPivoted(int[] arr, int low, int high, int start, int end) {
int pivot = arr[low];
for (int i = start; i < low; i++) {
assertTrue("Vals on the left of low bound should be < pivot", arr[i] < pivot);
}
for (int i = low; i <= high; i++) {
assertTrue("Vals in between low and high bound should == pivot", arr[i] == pivot);
}
for (int i = high + 1; i <= end; i++) {
assertTrue("Vals on the right of high bound should be > pivot", arr[i] > pivot);
}
System.out.println(Arrays.toString(arr));
System.out.format("Low: %d, high: %d, pivot: %d%n", low, high, pivot);
System.out.format("Start: %d, end: %d%n", start, end);
}
}