-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetTest.java
280 lines (228 loc) · 10.5 KB
/
DatasetTest.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
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
package recommender.sol;
import recommender.src.IAttributeDataset;
import tester.Tester;
import java.util.LinkedList;
/**
* class to test the methods in Dataset
*/
public class DatasetTest {
/**
* empty constructor for datasetTest
*/
public DatasetTest() {
}
/**
* @return a dataset of vegetables that will be used in following tests
*/
public Dataset<Vegetable> setupData1() {
Object[] lst1 = new Object[]{"spinach", "green", true, true, false};
Object[] lst2 = new Object[]{"kale", "green", true, true, true};
Object[] lst3 = new Object[]{"peas", "green", false, true, true};
Object[] lst4 = new Object[]{"carrot", "orange", false, false, false};
Object[] lst5 = new Object[]{"lettuce", "green", true, false, true};
LinkedList<Vegetable> data = new LinkedList<Vegetable>();
data.addLast(new Vegetable(lst1));
data.addLast(new Vegetable(lst2));
data.addLast(new Vegetable(lst3));
data.addLast(new Vegetable(lst4));
data.addLast(new Vegetable(lst5));
LinkedList<String> atts = new LinkedList<String>();
atts.addLast("name");
atts.addLast("color");
atts.addLast("lowCarb");
atts.addLast("highFiber");
atts.addLast("likeToEat");
return new Dataset<Vegetable>(atts, data);
}
/**
* @return a dataset of vegetables that will be used in following tests
*/
public Dataset<Vegetable> setupData2() {
Object[] lst1 = new Object[]{"spinach", "green", true, true, false};
Object[] lst2 = new Object[]{"kale", "green", true, true, true};
Object[] lst3 = new Object[]{"peas", "green", false, true, true};
//Object[] lst4 = new Object[]{"carrot", "orange", false, false, false};
Object[] lst5 = new Object[]{"lettuce", "green", true, false, true};
LinkedList<Vegetable> data = new LinkedList<Vegetable>();
data.addLast(new Vegetable(lst1));
data.addLast(new Vegetable(lst2));
data.addLast(new Vegetable(lst3));
//data.addLast(new Vegetable(lst4));
data.addLast(new Vegetable(lst5));
LinkedList<String> atts = new LinkedList<String>();
atts.addLast("name");
atts.addLast("color");
atts.addLast("lowCarb");
atts.addLast("highFiber");
atts.addLast("likeToEat");
return new Dataset<Vegetable>(atts, data);
}
/**
* @return a dataset of candidates that will be used in following tests
*/
public Dataset<Candidate> setupData3() {
LinkedList<Candidate> data = new LinkedList<Candidate>();
data.addLast(new Candidate("Female", false, "<1", "2", "5+", "2.0-2.9", "local", true));
data.addLast(new Candidate("Female", true, "1-2", "3+", "5+", "2.0-2.9", "nonlocal", false));
data.addLast(new Candidate("Male", false, "<1", "2", "5+", "2.0-2.9", "local", true));
data.addLast(new Candidate("Female", false, "3+", "2", "5+", "2.0-2.9", "local", true));
data.addLast(new Candidate("Male", false, "<1", "2", "5+", "2.0-2.9", "local", false));
LinkedList<String> atts = new LinkedList<String>();
atts.addLast("Gender");
atts.addLast("Leadership Experience");
atts.addLast("Last Position Duration");
atts.addLast("Number of Work Experiences");
atts.addLast("Programming Languages");
atts.addLast("GPA");
atts.addLast("Location");
atts.addLast("Hired");
return new Dataset<Candidate>(atts, data);
}
/**
* tests the method allSameValue
*
* @param t
*/
public void testAllSameValue(Tester t) {
Dataset<Vegetable> data1 = this.setupData1();
Dataset<Vegetable> data2 = this.setupData2(); // no orange
Dataset<Candidate> cand = this.setupData3();
t.checkExpect(!(data1.allSameValue("color")));
t.checkExpect(data2.allSameValue("color")); // all green
t.checkExpect(!(cand.allSameValue("gender")));
Dataset<Candidate> cand2 = new Dataset<Candidate>((LinkedList<String>) cand.attributes.clone(), (LinkedList<Candidate>) cand.rows.clone()); // copy of cand
for (Candidate datum : cand.rows) {
if (!datum.gender.equals("Female")) {
cand2.rows.remove(datum); // remove all non female candidates
}
}
t.checkExpect(cand2.allSameValue("gender")); // now gender should be all female
}
/**
* tests the method Filter
*
* @param t
*/
public void testFilter(Tester t) {
Dataset<Vegetable> data1 = this.setupData1();
Dataset<Vegetable> data2 = this.setupData2(); // no orange
Object[] lst4 = new Object[]{"carrot", "orange", false, false, false};
LinkedList<Vegetable> vegLst = new LinkedList<Vegetable>();
vegLst.addLast(new Vegetable(lst4));
Dataset<Vegetable> data3 = new Dataset<Vegetable>(data1.attributes, vegLst); //dataset with just carrot
Dataset<Vegetable> data4 = new Dataset<Vegetable>(data1.attributes, new LinkedList<Vegetable>()); //empty dataset
Dataset<Candidate> cand = this.setupData3();
t.checkExpect(data1.filter("color", "green"), data2);
t.checkExpect(data1.filter("color", "orange"), data3);
t.checkExpect(data1.filter("color", "yellow"),
new Dataset<Vegetable>(data1.attributes, new LinkedList<Vegetable>()));
Dataset<Candidate> cand2 = new Dataset<Candidate>((LinkedList<String>) cand.attributes.clone(), (LinkedList<Candidate>) cand.rows.clone()); // copy of cand
for (Candidate datum : cand.rows) {
if (!datum.gender.equals("Female")) {
cand2.rows.remove(datum); // remove all non female candidates
}
}
t.checkExpect(cand.filter("gender", "Female"), cand2); //filtering should remove all non female
}
/**
* tests the method extractValues
*
* @param t
*/
public void testExtractValues(Tester t) {
Dataset<Vegetable> data1 = this.setupData1();
Dataset<Vegetable> data2 = this.setupData2();
LinkedList<Object> lst = new LinkedList<Object>(); // for color
lst.addLast("green");
lst.addLast("orange");
LinkedList<Object> lst1 = new LinkedList<Object>(); // for name
lst1.addLast("spinach");
lst1.addLast("kale");
lst1.addLast("peas");
lst1.addLast("carrot");
lst1.addLast("lettuce");
LinkedList<Object> lst3 = new LinkedList<Object>(); // for lowCarb
lst3.addLast(true);
lst3.addLast(false);
LinkedList<Object> lst4 = new LinkedList<Object>(); // for highFiber
lst4.addLast(true);
lst4.addLast(false);
LinkedList<Object> lst5 = new LinkedList<Object>(); // for likeToEat
lst5.addLast(false);
lst5.addLast(true);
Dataset<Candidate> cand = this.setupData3();
LinkedList<Object> lst6 = new LinkedList<Object>(); // for gender
lst6.addLast("Female");
lst6.addLast("Male");
t.checkExpect(data1.extractValues("color"), lst);
t.checkExpect(data1.extractValues("name"), lst1);
t.checkExpect(data1.extractValues("lowCarb"), lst3);
t.checkExpect(data1.extractValues("highFiber"), lst4);
t.checkExpect(data1.extractValues("likeToEat"), lst5);
t.checkExpect(cand.extractValues("gender"), lst6);
}
/**
* tests method partition
*
* @param t
*/
public void testPartition(Tester t) {
Dataset<Vegetable> data1 = this.setupData1();
Dataset<Vegetable> data2 = this.setupData2(); // no orange
Object[] lst4 = new Object[]{"carrot", "orange", false, false, false};
LinkedList<Vegetable> vegLst = new LinkedList<Vegetable>();
vegLst.addLast(new Vegetable(lst4));
Dataset<Vegetable> data3 = new Dataset<Vegetable>(data1.attributes, vegLst); // just carrot
Dataset<Vegetable> data4 = new Dataset<Vegetable>(data1.attributes, new LinkedList<Vegetable>()); // empty dataset
Dataset<Candidate> cand = this.setupData3();
LinkedList<IAttributeDataset<Vegetable>> output = data1.partition("color");
t.checkExpect(output.get(0), data2);
t.checkExpect(output.get(1), data3);
t.checkExpect(output.size() == 2);
t.checkExpect(data4.partition("color").isEmpty());
LinkedList<IAttributeDataset<Candidate>> dataLst = new LinkedList<>();
dataLst.addLast(cand.filter("gender", "Female"));
dataLst.addLast(cand.filter("gender", "Male"));
t.checkExpect(cand.partition("gender"), dataLst); // partition gives a list of filtered datasets
}
/**
* tests method getSharedValue
*
* @param t
*/
public void testGetSharedValue(Tester t) {
Dataset<Vegetable> data1 = this.setupData1();
Dataset<Vegetable> data2 = this.setupData2(); // no orange
Object[] lst4 = new Object[]{"carrot", "orange", false, false, false};
LinkedList<Vegetable> vegLst = new LinkedList<Vegetable>();
vegLst.addLast(new Vegetable(lst4));
Dataset<Vegetable> data3 = new Dataset<Vegetable>(data1.attributes, vegLst); // just carrot
Dataset<Vegetable> data4 = new Dataset<Vegetable>(data1.attributes, new LinkedList<Vegetable>()); // empty dataset
Dataset<Candidate> cand = this.setupData3();
t.checkExpect(data2.getSharedValue("color"), "green");
t.checkExpect(data3.getSharedValue("highFiber"), false);
t.checkExpect(cand.filter("gender", "Female").getSharedValue("gender"),
"Female"); // after filtering, the shared value should be what we filtered with
}
/**
* tests method mostCommonValue
*
* @param t
*/
public void testMostCommonValue(Tester t) {
Dataset<Vegetable> data1 = this.setupData1();
Dataset<Candidate> cand = this.setupData3();
t.checkExpect(data1.mostCommonValue("name"), "spinach");
t.checkExpect(data1.mostCommonValue("color"), "green");
t.checkExpect(data1.mostCommonValue("lowCarb"), true);
t.checkExpect(cand.mostCommonValue("hired"), true);
}
/**
* runs tests
*
* @param args
*/
public static void main(String[] args) {
Tester.run(new DatasetTest());
}
}