forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
156 lines (125 loc) · 3.98 KB
/
main.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
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
#include "swift/Basic/PrefixMap.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/SmallString.h"
#include <map>
#include <random>
#include <string>
const unsigned RandomSpread = 26000;
using namespace swift;
namespace {
struct Tester {
std::map<std::string, int> StdMap;
PrefixMap<char, int> PreMap;
Tester() {
assert(PreMap.empty());
}
static ArrayRef<char> asArray(StringRef str) {
return ArrayRef<char>(str.begin(), str.end());
}
static StringRef asString(ArrayRef<char> str) {
return StringRef(str.begin(), str.size());
}
void insert(StringRef key, int value) {
auto stdmapResult = StdMap.insert({key, value});
auto premapResult = PreMap.insert(asArray(key), value);
assert(stdmapResult.second == premapResult.second);
if (premapResult.second) {
assert(value == *premapResult.first);
}
}
void find(StringRef key) {
auto stdmapResult = StdMap.lower_bound(key);
while (stdmapResult == StdMap.end() ||
!key.startswith(stdmapResult->first)) {
if (stdmapResult == StdMap.begin()) {
stdmapResult = StdMap.end();
break;
}
--stdmapResult;
}
bool hasStdmapEntry = (stdmapResult != StdMap.end());
auto premapResult = PreMap.findPrefix(asArray(key));
assert(hasStdmapEntry == bool(premapResult.first));
if (!hasStdmapEntry) return;
assert(key.startswith(stdmapResult->first));
auto stdmapValue = stdmapResult->second;
assert(stdmapValue == *premapResult.first);
assert(premapResult.second == key.begin() + stdmapResult->first.size());
}
void clear() {
StdMap.clear();
PreMap.clear();
assert(PreMap.empty());
assert(PreMap.size() == 0);
}
void validate() {
assert(StdMap.empty() == PreMap.empty());
assert(StdMap.size() == PreMap.size());
auto si = StdMap.begin(), se = StdMap.end();
auto pi = PreMap.begin(), pe = PreMap.end();
while (true) {
if (si == se) {
assert(pi == pe);
return;
}
assert(pi != pe);
assert(si->second == (*pi).getValue());
llvm::SmallString<128> buffer;
assert(StringRef(si->first) == asString((*pi).getKey(buffer)));
++si;
++pi;
}
}
void dump() {
llvm::outs() << "StdMap:\n";
for (auto i = StdMap.begin(), e = StdMap.end(); i != e; ++i) {
llvm::outs() << " \"" << i->first << "\": " << i->second << "\n";
}
llvm::outs() << "PreMap:\n";
for (auto i = PreMap.begin(), e = PreMap.end(); i != e; ++i) {
llvm::SmallVector<char, 128> buffer;
(*i).getKey(buffer);
llvm::outs() << " \"" << buffer << "\": " << (*i).getValue() << "\n";
}
PreMap.dump();
}
};
}
int main(int argc, char **argv) {
std::random_device randomDevice; // used for seeding
std::default_random_engine generator(randomDevice());
std::uniform_int_distribution<unsigned> distribution(0,RandomSpread);
Tester tester;
if (argc < 0) tester.dump(); // force this to be used
auto next = [&] { return distribution(generator); };
llvm::SmallString<128> key;
while (true) {
auto operation = next();
// Add elements to key.
if (operation <= .15 * RandomSpread) {
unsigned n = next() % 1 ? 5 : 7;
for (unsigned i = 0; i != n; ++i)
key.push_back('a' + (next() % 26));
// Remove elements from key.
} else if (operation <= .3 * RandomSpread) {
unsigned n = next() % 1 ? 5 : 7;
for (unsigned i = 0; i != n; ++i)
if (!key.empty()) key.pop_back();
// Insert.
} else if (operation <= .7 * RandomSpread) {
unsigned value = next();
llvm::outs() << " tester.insert(\"" << key << "\", " << value << ");\n";
tester.insert(key, value);
// Find.
} else if (operation <= .98 * RandomSpread) {
llvm::outs() << " tester.find(\"" << key << "\");\n";
tester.find(key);
// Clear.
} else {
llvm::outs() << " tester.clear();\n";
tester.clear();
}
llvm::outs() << " tester.validate();\n";
tester.validate();
}
}