-
Notifications
You must be signed in to change notification settings - Fork 1
/
testapp.cpp
94 lines (87 loc) · 2.46 KB
/
testapp.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* File testApp.cpp
* Test the BurstTrie class */
#include "record.h"
#include "BurstTrie.h"
#include <fstream>
#include <unordered_map>
#include <string.h>
bool openInFile(const std::string &filename, std::ifstream &file){
file.open(filename.c_str(),std::ifstream::in);
if(!file.is_open()) {
printf("Could not open input file %s", filename.c_str());
return false;
}
return true;
}
void testBurstTrie(const std::string& dictFileName, const std::string &searchFileName){
BurstTrie bt;
std::ifstream dictFile;
if(openInFile(dictFileName,dictFile)){
int lineNo=0;
std::string line;
while(getline(dictFile,line)){
lineNo++;
//printf("Adding line no %d\n",lineNo);
bt.addRecord(Record(line));
}
printf("Added %d lines\n",lineNo);
dictFile.close();
if(openInFile(searchFileName,dictFile)){
int found=0;
int notFound=0;
while(getline(dictFile,line)){
if(bt.findRecord(Record(line)))
//printf("Found record %s\n",line.c_str());
found++;
else
notFound++;
//printf("Record %s not found\n",line.c_str());
}
printf("Found %d records not found %d records.\n",found,notFound);
dictFile.close();
}
}
}
void testHashMap(const std::string& dictFileName, const std::string &searchFileName){
std::unordered_map<std::string,std::string> map;
std::ifstream dictFile;
if(openInFile(dictFileName,dictFile)){
int lineNo=0;
std::string line;
while(getline(dictFile,line)){
lineNo++;
//printf("Adding line no %d\n",lineNo);
std::pair<std::string,std::string> record (line,line);
map.insert(record);
}
printf("Added %d lines\n",lineNo);
dictFile.close();
if(openInFile(searchFileName,dictFile)){
int found=0;
int notFound=0;
while(getline(dictFile,line)){
if(map.find(line)==map.end())
//printf("Found record %s\n",line.c_str());
notFound++;
else
found++;
//printf("Record %s not found\n",line.c_str());
}
printf("Found %d records not found %d records.\n",found,notFound);
dictFile.close();
}
}
}
int main(int argc, char* argv[]){
if(argc<2){
printf("Usage: testapp -[hb]\n\t-h: test using hashmap\n\t-b: test using burst trie\n");
return 1;
}
std::string dictFileName="eng_com.dic";
std::string srchFileName="toSearch.dic";
if(strcmp(argv[1],"-b")==0)
testBurstTrie(dictFileName,srchFileName);
if(strcmp(argv[1],"-h")==0)
testHashMap(dictFileName,srchFileName);
return 0;
}