This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestArrayGenerator.java
196 lines (190 loc) · 4.65 KB
/
TestArrayGenerator.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
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/*
* Unit Tests for ArrayGenerator
*/
public class TestArrayGenerator
{
//testing random array generation
private static String TestGenRandomArray()
{
String n = "";
//setup
int[] arr;
Integer[] arrAscending = new Integer[100000];
Integer[] arrDescending = new Integer[100000];
//experiment step
arr = ArrayGenerator.genRandomArray(100000);
//testing case
for(int i=0; i<arr.length; i++)
{
arrDescending[i] = arr[i];
arrAscending[i] = arr[i];
}
Arrays.sort(arrAscending);
Arrays.sort(arrDescending, Collections.reverseOrder());
//using the assumption that if the array is sorted in any way then it isn't random
//given a sufficiently large array size n=100000
if(arr.equals(arrAscending) || arr.equals(arrDescending))
{
n = "Random Array Generation";
}
return n;
}
//testing reverse sorted array generation
private static ArrayList<String> TestGenReverseSortedArray()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
ExpParams e = new ExpParams();
e.setArrSize(10000);
//experiment step 1
e.setSortDirection(Direction.ASCENDING);
int[] arr1 = ArrayGenerator.genReverseSortedArray(e);
//testing case 1
for(int i=0; i<arr1.length-1; i++)
{
if(arr1[i+1] > arr1[i])
{
n = "Reverse Sorted Array Generation (Ascending)";
arrOfProblems.add(n);
}
}
//experiment step 2
e.setSortDirection(Direction.DESCENDING);
int[] arr2 = ArrayGenerator.genReverseSortedArray(e);
//testing case 2
for(int i=0; i<arr2.length-1; i++)
{
if(arr2[i+1] < arr2[i])
{
n = "Reverse Sorted Array Generation (Descending)";
arrOfProblems.add(n);
}
}
return arrOfProblems;
}
//testing 5 distinct value array generation
private static String Test5DistinctValueArray()
{
String n = "";
//setup
int[] arr;
//experiment step
arr = ArrayGenerator.gen5DistinctValueArray(20000);
//testing case
for(int i=0; i<arr.length; i++)
{
if(arr[i]<0 || arr[i]>4)
{
n = "5 Distinct Value Array Generation";
}
}
return n;
}
//test genArray method
private static ArrayList<String> TestGenArray()
{
String n = "";
ArrayList<String> arrOfProblems = new ArrayList<String>();
//setup
ExpParams e1 = new ExpParams();
ExpParams e2 = new ExpParams();
ExpParams e3 = new ExpParams();
e1.setGenArrayType(GenArrayType.RANDOM);
e2.setGenArrayType(GenArrayType.REVERSESORTED);
e3.setGenArrayType(GenArrayType.FIVEDISTINCTVALUES);
e1.setArrSize(50000);
e2.setArrSize(50000);
e2.setSortDirection(Direction.ASCENDING);
e3.setArrSize(50000);
//experiment step 1
int[] arr1 = ArrayGenerator.genArray(e1);
Integer[] arr1Ascending = new Integer[50000];
Integer[] arr1Descending = new Integer[50000];
//testing case 1
for(int i=0; i<arr1.length; i++)
{
arr1Descending[i] = arr1[i];
arr1Ascending[i] = arr1[i];
}
Arrays.sort(arr1Ascending);
Arrays.sort(arr1Descending, Collections.reverseOrder());
if(arr1.equals(arr1Ascending) || arr1.equals(arr1Descending))
{
n = "Array Generator General Method (RANDOM)";
arrOfProblems.add(n);
}
//experiment step 2
int[] arr2 = ArrayGenerator.genArray(e2);
//testing case 2
for(int i=0; i<arr2.length-1; i++)
{
if(arr2[i+1] > arr2[i])
{
n = "Array Generator General Method (REVERSESORTED)";
arrOfProblems.add(n);
}
}
//experiment step 3
int[] arr3 = ArrayGenerator.genArray(e3);
//testing case 3
for(int i=0; i<arr3.length; i++)
{
if(arr3[i]<0 || arr3[i]>4)
{
n = "Array Generator General Method (5DISTINCTVALUES)";
arrOfProblems.add(n);
}
}
//returns true if none of the experiments done were false
return arrOfProblems;
}
/*
* Runs all of the unit tests in this class
*/
public static void TestArrGen()
{
ArrayList<String> whatTests = new ArrayList<String>();
String Testname = "Array Generator";
whatTests.add(TestGenRandomArray());
ArrayList<String> g = TestGenReverseSortedArray();
if(!g.isEmpty())
{
for(int i=0; i<g.size(); i++)
{
if(!g.get(i).equals(""))
{
whatTests.add(g.get(i));
}
}
}
whatTests.add(Test5DistinctValueArray());
ArrayList<String> g2 = TestGenArray();
if(!g2.isEmpty())
{
for(int i=0; i<g2.size(); i++)
{
if(!g2.get(i).equals(""))
{
whatTests.add(g2.get(i));
}
}
}
boolean isCorrect = testIfFalse(whatTests);
FileIOUnitTestMenu.displayUnitTestResults(isCorrect, Testname, whatTests);
}
private static boolean testIfFalse(ArrayList<String> whatTests)
{
for(int i=0; i<whatTests.size(); i++)
{
if(!whatTests.get(i).equals(""))
{
return false;
}
}
return true;
}
}