-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTests.java
More file actions
160 lines (142 loc) · 4.27 KB
/
ProjectTests.java
File metadata and controls
160 lines (142 loc) · 4.27 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
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
package test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.*;
import app.*;
public class ProjectTests {
IntegerBucketSorter sorter;
/**
* Fixture initialization (common initialization for all tests).
**/
@Before
public void setUp() {
sorter = new IntegerBucketSorter();
}
@Test
public void getPlaceValueTest1() {
int place = 1;
int num = 49;
int correct = 9;
int res = sorter.getPlaceValue(place, num);
assertEquals("Test the getPlaceValue method on place = 1, num = 49.", correct, res);
}
@Test
public void getPlaceValueTest2() {
int place = 2;
int num = 49;
int correct = 4;
int res = sorter.getPlaceValue(place, num);
assertEquals("Test the getPlaceValue method on place = 2, num = 49.", correct, res);
}
@Test
public void getPlaceValueTest3() {
int place = 3;
int num = 49;
int correct = 0;
int res = sorter.getPlaceValue(place, num);
assertEquals("Test the getPlaceValue method on place = 3, num = 49.", correct, res);
}
@Test
public void findIntLengthTest1() {
boolean exThrown = false;
int correct = 5;
int res = 0;
try {
res = sorter.findIntLength(10000);
} catch (Exception ex) {
exThrown = true;
}
assertEquals("Test the findIntLength method threw an Exception on valid input.", false, exThrown);
assertEquals("Test the findIntLength method on an int == MAX_DIGIT_LIMIT.", correct, res);
}
@Test
public void findIntLengthTest2() {
boolean exThrown = false;
try {
sorter.findIntLength(100000);
} catch (Exception ex) {
exThrown = true;
}
assertEquals("Test the findIntLength method on an int > MAX_DIGIT_LIMIT.", true, exThrown);
}
@Test
public void findIntLengthTest() throws Exception {
int testNum = 0, corrLen = 0;
boolean correct = true;
for (int i = 0; i < 5; i++) {
testNum = (int) Math.pow(10, i);
corrLen = sorter.findIntLength(testNum);
if (corrLen != i + 1) {
correct = false;
break;
}
}
assertEquals("Test the findIntLength method.", true, correct);
}
@Test
public void findMaxIntLengthTest1() {
int[] testArr = {};
int ans = -1;
try {
ans = sorter.findMaxIntLength(testArr);
} catch (Exception e) {
}
assertEquals("Test the findMaxIntLength method on an empty array.", 0, ans);
}
@Test
public void findMaxIntLengthTest2() {
int[] testArr = { 15, 20, 500, 90, 3, 37000 };
int ans = -1;
try {
ans = sorter.findMaxIntLength(testArr);
} catch (Exception e) {
}
assertEquals("Test the findMaxIntLength method on an array.", 5, ans);
}
@Test
public void findMaxIntLengthTest3() {
int[] testArr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int ans = -1;
try {
ans = sorter.findMaxIntLength(testArr);
} catch (Exception e) {
}
assertEquals("Test the findMaxIntLength method on an array.", 1, ans);
}
@Test
public void findMaxIntLengthTest4() {
int[] testArr = { 1000000 };
boolean exThrown = false;
try {
sorter.findMaxIntLength(testArr);
} catch (Exception ex) {
exThrown = true;
}
assertEquals("Test the findMaxWordLength method on an int > MAX_DIGIT_LIMIT.", true, exThrown);
}
@Test
public void resetBucketValuesTest1() {
boolean notNeg1 = false;
try {
Field f = sorter.getClass().getDeclaredField("buckets"); // NoSuchFieldException}
f.setAccessible(true);
int[] data = {100, 10, 23};
sorter.sort(data);
sorter.resetBucketValues();
int[][] buckets = (int[][]) f.get(sorter);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < buckets[i].length; j++) {
if (buckets[i][j] != -1) {
notNeg1 = true;
break;
}
}
}
} catch (Exception e) {
assertEquals("No buckets field found in class", true, false);
return;
}
assertEquals("Test the resetBucketValues", notNeg1, false);
}
}