-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.cpp
More file actions
143 lines (134 loc) · 4.71 KB
/
check.cpp
File metadata and controls
143 lines (134 loc) · 4.71 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
#include "hash_map.h"
#include <iostream>
#include <chrono>
#include <random>
#include <string>
#include <vector>
#include <algorithm>
std::mt19937 rnd( 3);
int get(int l, int r) {
return rnd() % (r - l + 1) + l;
}
const int MIN_KEY = -100, MAX_KEY = 100, MIN_VALUE = -100, MAX_VALUE = 100, cnt = 5, n = 50;
const std::vector<std::string> operation = {"insert", "[]change", "[]", "erase", "find",
"=", "clear"};
void output(std::vector<size_t> &ind, std::vector<std::string>& tp, std::vector<std::vector<int>>& lst) {
std::cout << "WA\n";
std::cout << n << " " << cnt << "\n";
for (int i = 0; i < n; i++) {
std::cout << "index: " << ind[i] << "\n";
std::cout << tp[i] << " ";
for (auto j : lst[i]) {
std::cout << j << " ";
}
std::cout << "\n";
}
exit(0);
}
bool cmp_elements(std::unordered_map<int, int>& a, HashMap<int, int>& b) {
std::vector<std::pair<int, int>> x, y;
for (auto [key, val] : a) {
x.push_back({key, val});
}
for (auto [key, val] : b) {
y.push_back({key, val});
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
return x == y;
}
bool cmp_size(std::unordered_map<int, int>& a, HashMap<int, int>& b) {
return a.size() == b.size();
}
bool cmp_pair(std::unordered_map<int, int>& a, HashMap<int, int>& b, int key) {
auto it = a.find(key);
auto it2 = b.find(key);
if (it == a.end() && it2 == b.end()) {
return true;
}
if (it == a.end() || it2 == b.end()) {
return false;
}
return it -> second == it2 -> second;
}
int main() {
for (int test = 0; test < 100000; test++) {
//std::cout << n << "\n";
//std::cout << cnt << "\n";
std::vector<size_t> ind;
std::vector<std::string> tp;
std::vector<std::vector<int>> lst;
std::vector<std::unordered_map<int, int>> mp(cnt);
std::vector<HashMap<int, int>> hash_map(cnt);
for (int iter = 0; iter < n; iter++) {
size_t index = get(0, cnt - 1);
//std::cout << "index: " << index << "\n";
ind.push_back(index);
std::string request = operation[get(0, operation.size() - 1)];
//std::cout << request << "\n";
tp.push_back(request);
std::unordered_map<int, int>& a = mp[index];
HashMap<int, int>&b = hash_map[index];
if (request.front() == 'i') {
int key = get(MIN_KEY, MAX_KEY), val = get(MIN_VALUE, MAX_VALUE);
lst.push_back({key, val});
a.insert({key, val});
b.insert({key, val});
if (!cmp_pair(a, b, key)) {
output(ind, tp, lst);
}
} else if (request[0] == '[' && request[2] == 'c') {
int key = get(MIN_KEY, MAX_KEY), val = get(MIN_VALUE, MAX_VALUE);
lst.push_back({key, val});
a[key] = val;
b[key] = val;
if (!cmp_pair(a, b, key)) {
output(ind, tp, lst);
}
} else if (request[0] == '[') {
int key = get(MIN_KEY, MAX_KEY);
lst.push_back({key});
a[key];
b[key];
if (!cmp_pair(a, b, key)) {
output(ind, tp, lst);
}
} else if (request[0] == 'e') {
int key = get(MIN_KEY, MAX_KEY);
lst.push_back({key});
a.erase(key);
b.erase(key);
if (!cmp_pair(a, b, key)) {
output(ind, tp, lst);
}
} else if (request[0] == 'f'){
int key = get(MIN_KEY, MAX_KEY);
lst.push_back({key});
auto it = a.find(key);
auto it2 = b.find(key);
if (!cmp_pair(a, b, key)) {
output(ind, tp, lst);
}
} else if (request[0] == '='){
int id = get(0, cnt - 1);
lst.push_back({id});
a = mp[id];
b = hash_map[id];
} else if (request[0] == 'c') {
lst.push_back({});
a.clear();
b.clear();
}
if (!cmp_size(a, b)) {
output(ind, tp, lst);
}
if (!cmp_elements(a, b)) {
output(ind, tp, lst);
}
}
if (test % 1000 == 0) {
std::cout << test << " " << "OK\n";
}
}
return 0;
}