-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
148 lines (116 loc) · 3.96 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
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
#include "Map.hpp"
#include <iostream>
#include <string>
#include <stdexcept>
#include <utility>
#include <random>
#include <chrono>
#include <iterator>
#include <cassert>
void stress(int stress_size) {
auto seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine gen(seed);
std::uniform_int_distribution<unsigned int> dist(0, 10000);
cs440::Map<int, int> m;
for(int i = 0; i < stress_size; ++i) {
auto g = dist(gen);
m.insert({g, g});
}
int num_erases = gen() % m.size();
for(int i = 0; i < num_erases; ++i) {
//select a random element
int choice = gen() % m.size();
auto iter = std::begin(m);
for (int j = 0; j < choice; ++j) {
++iter;
}
m.erase(iter);
}
}
// unrealistic examples
void access_by_key() {
cs440::Map<int, long> m;
m.insert({10, 10});
m.insert({3, 3});
m.insert({20, 20});
m.insert({15, 15});
m.at(10);
bool thrown = false;
try {
m.at(10000);
} catch (std::out_of_range) {
thrown = true;
}
assert(thrown); // the .at should have thrown an exception
// const auto& m_ref = m;
// m_ref.at(10); // const .at
auto iter = m.find(3);
assert((*iter).second == 3);
auto iter2 = m.find(100000); // not in map, should give iterator to end()
assert(iter2 == std::end(m));
m[30] = 30; // should default construct
m.erase(10);
assert(m.find(10) == std::end(m)); // 10 shouldn't be in the map anymore
}
void count_words() {
cs440::Map<std::string, int> words_count;
// just a big list of words
auto words = {"this", "is", "a", "string", "with", "words", "some",
"words", "in", "the", "string", "repeat", "the", "more", "they",
"repeat", "the", "more", "they", "should", "count", "they", "more",
"they", "count", "the", "more", "they", "will", "have", "their",
"count", "increased"};
for (auto& word : words) {
// this works because int can be default constructed, and the
// default of int (by doing int{} ) is 0.
words_count[word] += 1; // add 1 to the count
}
// print the frequency of each word
std::cout << "word frequency:\n";
for (auto& count : words_count) { // uses .begin() and .end()
std::cout << count.first << ": " << count.second << '\n';
}
// words_count.print();
std::cout << "word frequency reversed order:\n";
for (auto riter = words_count.rbegin();
riter != words_count.rend();
++riter) {
std::cout << (*riter).first << ": " << (*riter).second << '\n';
}
}
// creates a mapping from the values in the range [low, high) to their cubes
cs440::Map<int, int> cubes(int low, int high) {
cs440::Map<int, int> cb;
for (int i = low; i < high; ++i) {
cb.insert({i, i*i*i});
}
return cb;
}
int main () {
count_words();
auto cube = cubes(-5, 10); // move construct returned value
std::cout << cube.at(-2) << '\n'; // -8
std::cout << cube.at(5) << '\n'; // 125
const int n = 30;
try {
std::cout << cube.at(n) << '\n'; // 30 is not in the Map
} catch (std::out_of_range) {
std::cout << n << " not in cubes range\n";
}
// // constructors and assignment examples:
// // initializer_list example
cs440::Map<int, double> int_double_map {{1, 1.0}, {3, 5.67}, {13, 6.9}};
// // must support copy construction
cs440::Map<int, double> copy_example{int_double_map};
cs440::Map<int, double> assign_example;
// // must support copy assignment
assign_example = copy_example;
access_by_key();
stress(10000);
//adding map comparison operator test.
cs440::Map<int, double> custom_map{{1, 2.0}, {3, 6.0}, {15, 6.9}};
assert(int_double_map == copy_example);
assert(int_double_map < custom_map);
assert(int_double_map != custom_map);
return 0;
}