-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
340 lines (334 loc) · 9.55 KB
/
example.cpp
File metadata and controls
340 lines (334 loc) · 9.55 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "insertion_ordered_map.h"
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
int ile = 0;
string message;
bool rzucaj = false;
bool wypisuj = true;
class XD {};
class Key {
public:
int v=0;
Key() {}
explicit Key(int w): v(w) {}
Key(const Key &k) {
v = k.v;
if (ile > 0) {
if (wypisuj) cout << message << "\n";
ile--;
}
if (!wypisuj && ile==0) throw XD{};
if (rzucaj) throw XD{};
}
Key(Key &&k) noexcept{
v = std::move(k.v);
}
Key& operator=(Key k) noexcept {
v=std::move(k.v);
return *this;
}
};
bool operator==(Key const &lhs, Key const &rhs) {
return lhs.v == rhs.v;
}
bool operator!=(Key const &lhs, Key const &rhs) {
return lhs.v != rhs.v;
}
struct Hash {
size_t operator()(const Key &k) const {
return std::hash<int>()(k.v);
}
};
void check(const insertion_ordered_map<Key,int,Hash> &m,
const vector<int>&order, const vector<int>&values) {
auto o=order.begin();
auto v=values.begin();
for (auto &i:m) {
assert(i.first == Key(*o));
assert(i.second == *v);
o++;
v++;
}
}
void check2(const insertion_ordered_map<Key,Key,Hash> &m,
const vector<int>&order, const vector<int>&values) {
auto o=order.begin();
auto v=values.begin();
for (auto &i:m) {
assert(i.first == Key(*o));
assert(i.second == Key(*v));
o++;
v++;
}
}
void check3(const insertion_ordered_map<Key,Key,Hash> &m,
vector<typename insertion_ordered_map<Key,Key,Hash>::iterator> &order) {
size_t xd=0;
for (auto it=m.begin(); it!=m.end(); ++it, ++xd) {
assert(it == order[xd]);
assert(it->first == order[xd]->first);
assert(it->second == order[xd]->second);
}
}
int main() {
cout << "Jeżeli program przejdzie do końca i wypisze komunikat \"Accepted\""
<< " to znaczy, że przeszedł test. Przy odpalaniu z valgrindem wyrzuć fragment "
<< "z tworzeniem dużej tablicy (valgrind nie umie obsłużyć bad_alloc rzuconego "
<< "przez new[])." << std::endl << std::endl;
try {
insertion_ordered_map<int, int> *t = new insertion_ordered_map<int, int>[1000000000];
delete[] t;
cout << "czemu to nie wyjebało pamięci xD\n";
} catch (bad_alloc &e) {
cout << "bad_alloc (ok)\n";
}
insertion_ordered_map<Key, int, Hash> m1;
m1.insert(Key{2},3);
ile = 1;
message = "gunwo";
insertion_ordered_map<Key, int, Hash> m2(m1); // nic nie powinno sie wypisać
try {
auto &ref = m1.at(Key(1));
cout << "miałeś wyjątek rzucić\n";
} catch (lookup_error &e) {
cout << "ok wyjątek\n";
}
insertion_ordered_map<Key, int, Hash> m3 = m1; // dalej nic nie powinno się wypisać
message = "kopiuję się (ok)";
rzucaj = true;
try {
auto &ref = m1[Key(2)];
cout << "XDDDDDDDD?\n";
ref = 1;
assert(m3[Key(2)] == 3);
} catch (XD &e) {
cout <<"ok\n";
} catch (...) {
cout << "????????\n";
throw;
}
rzucaj = false;
auto &ref = m1[Key(2)];
m1.insert(Key(3), -1); // m1 powinien być czysty
rzucaj = true;
try {
m2 = m1;
insertion_ordered_map<Key, int, Hash> mm(m1);
cout << "ok (nie kopiuje się)\n";
} catch (XD &e) {
cout << "Nie powinno się skopiować\n";
throw;
}
rzucaj = false;
auto &r = m1[Key(2)];
m1.erase(Key(2)); // dalej powinien być czysty – zmiana słownika unieważnia referencje do obiektów
rzucaj = true;
try {
insertion_ordered_map<Key, int, Hash> ddd(m1);
cout << "Ok\n";
} catch (XD &e) {
cout << "źle\n";
throw;
}
insertion_ordered_map<Key, int, Hash> c = std::move(m1);
size_t s = m1.size(); // Peczar mówił że jak jest segfault to jest źle
assert(m1.begin() == m1.begin() && m1.end() == m1.end());
rzucaj = false;
m1.clear();
for (int i = 5; i > 0; i--) m1.insert(Key(i),i+2);
int dd=5;
for (insertion_ordered_map<Key, int, Hash>::iterator it = m1.begin(); it != m1.end(); ++it, dd--) {
if (dd == 5) assert(it == m1.begin());
assert(it->first == Key(dd) && it->second == dd+2);
assert((*it).first == Key(dd) && (*it).second == dd+2);
insertion_ordered_map<Key, int, Hash>::iterator j(it);
assert(it == j);
assert(j->first == Key(dd) && j->second == dd+2);
}
assert(m1.begin() != m1.end());
m1.merge(m1);
assert(m1.size() == 5);
check(m1, {5, 4, 3, 2, 1}, {7, 6, 5, 4, 3});
m2.clear();
for (int i=1; i<=3; i++) m2[Key(i)]=i+2;
check(m2, {1,2,3}, {3,4,5});
m1.merge(m2);
check(m1, {5, 4, 1, 2, 3}, {7, 6, 3,4,5});
for (int i=1; i<=3; i++) m2[Key(i)] = i;
m1.merge(m2);
check(m1, {5, 4, 1, 2, 3}, {7, 6, 3, 4, 5});
m1.clear();
vector<int> v1;
for (int i=1; i<10000000; i++) {
v1.push_back(i);
m1.insert(Key(i),i);
}
check(m1, v1, v1);
m1.merge(m1);//*/
check(m1, v1, v1); //może się wyjebać przez rehash
// -------------
insertion_ordered_map<Key, Key, Hash> fajna;
v1.clear();
vector<typename insertion_ordered_map<Key,Key,Hash>::iterator> order;
for (int i=1; i<10; i++) {
fajna.insert(Key(i), Key(i));
v1.push_back(i);
}
for (auto it=fajna.begin(); it!=fajna.end(); ++it) order.push_back(it);
insertion_ordered_map<Key, Key, Hash> fajna2(fajna);
for (int i = 0; i < 50; i++) {
try {
wypisuj=true;
ile=0;
Key k(i), v(i-2);
wypisuj=false;
ile=i;
fajna.insert(k,v);
fajna = fajna2;
break;
} catch (XD &e) {
wypisuj=true; ile=0;
check2(fajna, v1, v1);
check2(fajna2, v1, v1);
check3(fajna, order);
}
}
for (int i = 0; i < 50; i++) {
try {
wypisuj=true;
ile=0;
Key k(i), v(i-2);
wypisuj=false;
ile=i;
fajna.erase(k);
fajna = fajna2;
break;
} catch (XD &e) {
wypisuj=true; ile=0;
check2(fajna, v1, v1);
check2(fajna2, v1, v1);
check3(fajna, order);
} catch (lookup_error &e) {
} catch (exception &e) {
cout << "chyba coś nie tak" << endl;
throw;
} catch (...) {
cout << "X D\n";
throw;
}
}
for (int i = 0; i < 50; i++) {
try {
wypisuj=true;
ile=0;
Key k(i), v(i-2);
wypisuj=false;
ile=i;
auto &ref = fajna.at(k);
ile=0;
wypisuj=true;
ref.v++;
assert(fajna.at(k) != fajna2.at(k));
fajna = fajna2;
break;
} catch (XD &e) {
wypisuj=true; ile=0;
check2(fajna, v1, v1);
check2(fajna2, v1, v1);
check3(fajna, order);
} catch (lookup_error &e) {
} catch (exception &e) {
cout << "chyba coś nie tak" << endl;
throw;
} catch (...) {
cout << "X D\n";
throw;
}
}
for (int i = 0; i < 50; i++) {
try {
wypisuj=true;
ile=0;
Key k(i);
wypisuj=false;
ile=i;
const auto &ref = fajna.at(k);
fajna = fajna2;
break;
} catch (XD &e) {
} catch (lookup_error &e) {
} catch (exception &e) {
cout << "chyba coś nie tak" << endl;
throw;
} catch (...) {
cout << "X D\n";
throw;
}
}
for (int i = 0; i < 50; i++) {
try {
wypisuj=true;
ile=0;
Key k(i), v(i-2);
wypisuj=false;
ile=i;
auto &ref =fajna[k];
ile=0;
wypisuj=true;
ref.v++;
assert(fajna.at(k) != fajna2.at(k));
fajna = fajna2;
break;
} catch (XD &e) {
wypisuj=true; ile=0;
check2(fajna, v1, v1);
check2(fajna2, v1, v1);
check3(fajna, order);
} catch (lookup_error &e) {
fajna = fajna2;
break;
} catch (exception &e) {
cout << "chyba coś nie tak" << endl;
throw;
} catch (...) {
cout << "X D\n";
throw;
}
}
srand(2137);
for (int i = 0; i < 50; i++) {
try {
wypisuj=true;
ile=0;
insertion_ordered_map<Key,Key,Hash> map2;
for (int i=0; i<20; ++i) map2.insert(Key(rand()%20),Key(rand()%20));
wypisuj=false;
ile=i;
fajna.merge(map2);
fajna = fajna2;
break;
} catch (XD &e) {
wypisuj=true; ile=0;
check2(fajna, v1, v1);
check2(fajna2, v1, v1);
check3(fajna, order);
} catch (lookup_error &e) {
fajna = fajna2;
break;
} catch (exception &e) {
cout << "chyba coś nie tak" << endl;
throw;
} catch (...) {
cout << "X D\n";
throw;
}
}
// sprawdzam czy jest konstruktor bezparametrowy i kopiujący
insertion_ordered_map<int,int> it, it2(it);
assert(it.empty());
assert(it2.empty());
cout << "Accepted\n";
}