-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
71 lines (58 loc) · 1.71 KB
/
test.cpp
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
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#include "karprabin.h"
#include "ahocorasick.h"
string text;
vector<string> random_patterns;
vector<pair<ll,ll>> positions;
const ll PATTERN_LENGTH_MAX = 500;
const ll PATTERN_AMOUNT = 2000;
void load_text() {
ifstream in("test");
stringstream buffer;
buffer << in.rdbuf();
text = buffer.str();
}
void load_random_patterns() {
srand(time(NULL));
for (int i = 0; i < PATTERN_AMOUNT; i++) {
ll b = rand() % (text.length() - PATTERN_LENGTH_MAX - 1);
random_patterns.push_back(text.substr(b, 200+(PATTERN_LENGTH_MAX)));
}
}
void run_brute() {
for (string& sub : random_patterns) {
ll pos = text.find(sub, 0);
pair<ll,ll> match = {pos, sub.length()};
while(pos != string::npos) {
positions.push_back({pos,sub.length()});
pos = text.find(sub,pos+1);
}
}
}
void test_AC_with_english() {
preprocess_ac(random_patterns);
vector<pair<ll,ll>> output = findPattern(text);
sort(positions.begin(), positions.end());
sort(output.begin(), output.end());
if (output != positions) cout << "AC FAIL" << endl;
cout << "Aho Corasick found " << output.size() << " matches" << endl;
}
void test_KR_with_english() {
preprocess_KR(random_patterns, text);
vector<pair<ll,ll>> output = findPatterns(text, random_patterns);
sort(positions.begin(), positions.end());
sort(output.begin(), output.end());
if (output != positions) cout << "KR FAIL" << endl;
cout << "Karp-Rabin found " << output.size() << " matches" << endl;
}
int main() {
load_text();
load_random_patterns();
cout << "Text and patterns loaded." << endl;
run_brute();
cout << "Brute force found " << positions.size() << " matches" << endl;
test_AC_with_english();
test_KR_with_english();
}