-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTestST.cpp
48 lines (42 loc) · 1.53 KB
/
TestST.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
#include "TestST.h"
#include <random>
namespace TestST {
void init(ST<std::string, int> &st, std::istream &is, std::ostream &os) {
std::string word;
for (int i = 0; is >> word; ++i) st.put(word, i);
os << "size = " << st.size() << std::endl;
}
// Print keys using keys().
void listAll(const ST<std::string, int> &st, std::ostream &os) {
for (const auto &s: st.keys()) {
os << s << " " << st.get(s).value_or(INVALID_VALUE) << std::endl;
}
}
// Remove some randomly selected keys.
void removeSome(ST<std::string, int> &st, std::ostream &os) {
std::default_random_engine e(std::random_device{}());
std::bernoulli_distribution b;
int i = 0;
for (const auto &s: st.keys()) {
if (b(e)) {
st.remove(s);
++i;
}
}
os << "After removing " << i << " randomly selected keys, size = " << st.size() << std::endl;
os << "--------------------------------" << std::endl;
listAll(st, os);
}
// Remove all the remaining keys.
void removeAll(ST<std::string, int> &st, std::ostream &os) {
for (const auto &s: st.keys()) {
st.remove(s);
}
os << "After removing the remaining keys, size = " << st.size() << std::endl;
}
void testKeys(const ST<std::string, int> &st, std::ostream &os) {
os << "Testing keys()" << std::endl;
os << "--------------------------------" << std::endl;
listAll(st, os);
}
}