-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTester.java
222 lines (191 loc) · 7.11 KB
/
Tester.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
package finalproject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/* This tester tests the
* # of vertices added during crawling,
* # of edges added during crawling,
* if all vertices are correctly marked visited after crawling
* if assignPageRanks in the SearchEngine class produces plausible output
* if the search results are in correct order based on what I know they should be */
public class Tester {
public static void main(String[] args) {
SearchEngine searchEngine;
try {
searchEngine = new SearchEngine("test.xml");
searchEngine.crawlAndIndex("www.cs.mcgill.ca");
} catch (Exception e) {
e.printStackTrace();
}
testNumVertices(0);
testNumEdges(1);
testVisited(2);
testAssignRanks(3);
testGetResults(4);
}
private static void testNumVertices(int testIdx)
{
System.out.println("[" + testIdx + "]: Test the number of vertices added during crawling.");
try {
SearchEngine searchEngine = new SearchEngine("test.xml");
searchEngine.crawlAndIndex("www.cs.mcgill.ca");
if (searchEngine.internet.vertexList.size() == 6) {
System.out.println("Success. The number of vertices added during crawling was correct.\n");
} else {
System.out.println("Error: Number of vertices added during crawling is incorrect.\n");
}
}
catch (Exception e) {
System.out.println("There was a problem.");
e.printStackTrace();
}
}
private static void testNumEdges(int testIdx)
{
System.out.println("[" + testIdx + "]: Test the number of edges added during crawling.");
try {
SearchEngine searchEngine = new SearchEngine("test.xml");
searchEngine.crawlAndIndex("www.cs.mcgill.ca");
ArrayList<String> v = searchEngine.internet.getVertices();
int numEdges = 0;
for(String url: v){
int outDeg = searchEngine.internet.getOutDegree(url);
numEdges+=outDeg;
}
if(numEdges == 21)
System.out.println("Success. Correct Number of edges created.\n");
else {
System.out.println("Error: Number of edges added during crawling is incorrect.\n");
}
}
catch (Exception e) {
System.out.println("There was a problem.");
e.printStackTrace();
}
}
private static void testVisited(int testIdx)
{
System.out.print("[" + testIdx + "]: Test if vertices are marked visited during crawling.\n");
try {
SearchEngine searchEngine = new SearchEngine("test.xml");
searchEngine.crawlAndIndex("www.cs.mcgill.ca");
Iterator it = searchEngine.internet.vertexList.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if (!searchEngine.internet.getVisited((String) pair.getKey())) {
System.out.println("Error: Vertices are not marked visited during crawling.\n");
break;
}
}
System.out.println("Success. Vertices marked visited during crawling.\n");
}
catch (Exception e) {
System.out.println("There was a problem.");
e.printStackTrace();
}
}
private static void testAssignRanks(int testIdx)
{
System.out.println("[" + testIdx + "]: Test whether AssignPageRanks produces plausible output. Passing this test does not gurantee your output is correct.");
String comment = "";
try {
boolean result = true;
SearchEngine searchEngine = new SearchEngine("test.xml");
searchEngine.crawlAndIndex("www.cs.mcgill.ca");
ArrayList<String> vertices = searchEngine.internet.getVertices();
ArrayList<Double> rankAfterOneIteration = searchEngine.computeRanks(vertices);
// set initial values for the ranks
for (String v : vertices)
searchEngine.internet.setPageRank(v, 1.0);
for (int i = 0; i < vertices.size(); i++) {
if (Double.isInfinite(rankAfterOneIteration.get(i))) {
System.out.println(" Page rank for vertex " + vertices.get(i) + " is infinite. Check for divide by zero errors." + "\n");
result = false;
break;
}
}
for (int i = 0; i < vertices.size(); i++) {
if (Double.isNaN(rankAfterOneIteration.get(i))) {
System.out.println(" Page rank for vertex " + vertices.get(i) + " is NaN. Check for divide by zero errors." + "\n");
result = false;
break;
}
}
searchEngine.assignPageRanks(1000);
for (String v : vertices) {
if (searchEngine.internet.getPageRank(v) <= 0) {
System.out.println(" Page rank for vertex " + v + " is negetive. Page rank should be positive at each iteration." + "\n");
result = false;
break;
}
}
for (String v: vertices) {
System.out.printf("%.3f ", searchEngine.internet.getPageRank(v));
System.out.println(v);
}
}
catch (Exception e) {
System.out.println("There was a problem.");
e.printStackTrace();
}
System.out.println("Success. There were no impossible results. Do they look like good numbers to you?\n");
}
private static void testGetResults(int testIdx)
{
System.out.println("[" + testIdx + "]: Test whether the search results are correct.");
try {
boolean succeeded = true;
SearchEngine searchEngine = new SearchEngine("test.xml");
searchEngine.crawlAndIndex("www.cs.mcgill.ca");
searchEngine.assignPageRanks(0.01);
//try a word in the graph
ArrayList<String> results = searchEngine.getResults("and");
if(results != null && (results.get(0).equals("www.ea.com")==true || results.get(0).equals("www.unity.com")==true)){
System.out.println("Success. The #1 ranked result was correct.");
} else {
System.out.println("Failiure. The #1 ranked result was incorrect.");
succeeded = false;
}
if(results.get(1).equals("www.unity.com")==true || results.get(1).equals("www.ea.com")==true) {
System.out.println("Success. The #2 ranked result was correct.");
} else {
System.out.println("Failiure. The #2 ranked result was incorrect.");
succeeded = false;
}
if(results.get(2).equals("www.cs.mcgill.ca")==true) {
System.out.println("Success. The #3 ranked result was correct.");
} else {
System.out.println("Failiure. The #3 ranked result was incorrect.");
succeeded = false;
}
if(results.get(3).equals("www.ubisoft.com")==true) {
System.out.println("Success. The #4 ranked result was correct.");
} else {
System.out.println("Failiure. The #4 ranked result was incorrect.");
succeeded = false;
}
if(results.get(4).equals("www.eidos.com")==true) {
System.out.println("Success. The #5 ranked result was correct.");
} else {
System.out.println("Failiure. The #5 ranked result was incorrect.");
succeeded = false;
}
if(results.get(5).equals("www.unreal.com")==true) {
System.out.println("Success. The #6 ranked result was correct.");
} else {
System.out.println("Failiure. The #6 ranked result was incorrect.");
succeeded = false;
}
if(!succeeded){
System.out.println("OVERALL FAILIRUE. At least one result was ranked incorrectly.\n");
} else {
System.out.println("GREAT SUCCESS!");
}
}
catch (Exception e) {
System.out.println("There was a problem.");
e.printStackTrace();
}
}
}