-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainSearch.java
More file actions
134 lines (108 loc) · 4.68 KB
/
Copy pathMainSearch.java
File metadata and controls
134 lines (108 loc) · 4.68 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
package test;
import test.file_search.*;
import java.io.FileWriter;
import java.io.PrintWriter;
public class MainSearch {
public static void testLRU() {
CacheReplacementPolicy lru = new LRU();
lru.add("a");
lru.add("b");
lru.add("c");
lru.add("a");
if (!lru.remove().equals("b"))
System.out.println("wrong implementation for LRU (-10)");
}
public static void testLFU() {
CacheReplacementPolicy lfu = new LFU();
lfu.add("a");
lfu.add("b");
lfu.add("b");
lfu.add("c");
lfu.add("a");
if (!lfu.remove().equals("c"))
System.out.println("wrong implementation for LFU (-10)");
}
public static void testCacheManager() {
CacheManager exists = new CacheManager(3, new LRU());
boolean b = exists.query("a");
b |= exists.query("b");
b |= exists.query("c");
if (b)
System.out.println("wrong result for CacheManager first queries (-5)");
exists.add("a");
exists.add("b");
exists.add("c");
b = exists.query("a");
b &= exists.query("b");
b &= exists.query("c");
if (!b)
System.out.println("wrong result for CacheManager second queries (-5)");
boolean bf = exists.query("d"); // false, LRU is "a"
exists.add("d");
boolean bt = exists.query("d"); // true
bf |= exists.query("a"); // false
exists.add("a");
bt &= exists.query("a"); // true, LRU is "b"
if (bf || !bt)
System.out.println("wrong result for CacheManager last queries (-10)");
}
public static void testBloomFilter() {
BloomFilter bf = new BloomFilter(256, "MD5", "SHA1");
String[] words = "the quick brown fox jumps over the lazy dog".split(" ");
for (String w : words)
bf.add(w);
if (!bf.toString().equals("0010010000000000000000000000000000000000000100000000001000000000000000000000010000000001000000000000000100000010100000000010000000000000000000000000000000110000100000000000000000000000000010000000001000000000000000000000000000000000000000000000000000001")) {
System.out.println("problem in the bit vector of the bloom filter (-10)");
//print in how many bits the bit vector is wrong
int count = 0;
for (int i = 0; i < bf.toString().length(); i++)
if (bf.toString().charAt(i) != "0010010000000000000000000000000000000000000100000000001000000000000000000000010000000001000000000000000100000010100000000010000000000000000000000000000000110000100000000000000000000000000000010000000001000000000000000000000000000000000000000000000000000001".charAt(i))
count++;
System.out.println("wrong bits: " + count);
}
boolean found = true;
for (String w : words)
found &= bf.contains(w);
if (!found)
System.out.println("problem finding words that should exist in the bloom filter (-15)");
found = false;
for (String w : words)
found |= bf.contains(w + "!");
if (found)
System.out.println("problem finding words that should not exist in the bloom filter (-15)");
}
public static void testIOSearch() throws Exception {
String words1 = "the quick brown fox \n jumps over the lazy dog";
String words2 = "A Bloom filter is a space efficient probabilistic data structure, \n conceived by Burton Howard Bloom in 1970";
PrintWriter out = new PrintWriter(new FileWriter("text1.txt"));
out.println(words1);
out.close();
out = new PrintWriter(new FileWriter("text2.txt"));
out.println(words2);
out.close();
if (!IOSearcher.search("is", "text1.txt", "text2.txt"))
System.out.println("oyur IOsearch did not found a word (-5)");
if (IOSearcher.search("cat", "text1.txt", "text2.txt"))
System.out.println("your IOsearch found a word that does not exist (-5)");
}
public static void testDictionary() {
Dictionary d = new Dictionary("text1.txt", "text2.txt");
if (!d.query("is"))
System.out.println("problem with dictionarry in query (-5)");
if (!d.challenge("lazy"))
System.out.println("problem with dictionarry in query (-5)");
}
public static void main(String[] args) {
testLRU();
testLFU();
testCacheManager();
testBloomFilter();
try {
testIOSearch();
} catch (Exception e) {
System.out.println("you got some exception (-10)");
}
testDictionary();
System.out.println("done");
}
}