-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
adding_credits.cc
130 lines (115 loc) · 3.68 KB
/
adding_credits.cc
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
#include <iterator>
#include <map>
#include <ostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include "test_framework/fmt_print.h"
#include "test_framework/generic_test.h"
#include "test_framework/test_failure.h"
using std::map;
using std::string;
using std::unordered_map;
using std::unordered_set;
class ClientsCreditsInfo {
public:
void Insert(const string& client_id, int c) {
Remove(client_id);
client_to_credit_.emplace(client_id, c - offset_);
credit_to_clients_[c - offset_].emplace(client_id);
}
bool Remove(const string& client_id) {
if (auto credit_iter = client_to_credit_.find(client_id);
credit_iter != end(client_to_credit_)) {
credit_to_clients_[credit_iter->second].erase(client_id);
if (empty(credit_to_clients_[credit_iter->second])) {
credit_to_clients_.erase(credit_iter->second);
}
client_to_credit_.erase(credit_iter);
return true;
}
return false;
}
int Lookup(const string& client_id) const {
auto credit_iter = client_to_credit_.find(client_id);
return credit_iter == cend(client_to_credit_)
? -1
: credit_iter->second + offset_;
}
void AddAll(int C) { offset_ += C; }
string Max() const {
auto iter = crbegin(credit_to_clients_);
return iter == crend(credit_to_clients_) || empty(iter->second)
? ""
: *cbegin(iter->second);
}
friend std::ostream& operator<<(std::ostream& os,
const ClientsCreditsInfo& info) {
PrintTo(os, info.credit_to_clients_);
return os;
}
private:
int offset_ = 0;
unordered_map<string, int> client_to_credit_;
map<int, unordered_set<string>> credit_to_clients_;
};
struct Operation {
std::string op;
std::string s_arg;
int i_arg;
};
std::ostream& operator<<(std::ostream& out, const Operation& op) {
return out << FmtStr("{}({}, {})", op.op, op.s_arg, op.i_arg);
}
namespace test_framework {
template <>
struct SerializationTrait<Operation>
: UserSerTrait<Operation, std::string, std::string, int> {};
} // namespace test_framework
void ClientsCreditsInfoTester(const std::vector<Operation>& ops) {
ClientsCreditsInfo cr;
int op_idx = 0;
for (auto& op : ops) {
if (op.op == "ClientsCreditsInfo") {
continue;
} else if (op.op == "remove") {
bool result = cr.Remove(op.s_arg);
if (result != op.i_arg) {
throw TestFailure()
.WithProperty(PropertyName::STATE, cr)
.WithProperty(PropertyName::COMMAND, op)
.WithMismatchInfo(op_idx, op.i_arg, result);
}
} else if (op.op == "max") {
auto result = cr.Max();
if (result != op.s_arg) {
throw TestFailure()
.WithProperty(PropertyName::STATE, cr)
.WithProperty(PropertyName::COMMAND, op)
.WithMismatchInfo(op_idx, op.i_arg, result);
}
} else if (op.op == "insert") {
cr.Insert(op.s_arg, op.i_arg);
} else if (op.op == "add_all") {
cr.AddAll(op.i_arg);
} else if (op.op == "lookup") {
auto result = cr.Lookup(op.s_arg);
if (result != op.i_arg) {
throw TestFailure()
.WithProperty(PropertyName::STATE, cr)
.WithProperty(PropertyName::COMMAND, op)
.WithMismatchInfo(op_idx, op.i_arg, result);
;
}
}
op_idx++;
}
}
// clang-format off
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"ops"};
return GenericTestMain(args, "adding_credits.cc", "adding_credits.tsv", &ClientsCreditsInfoTester,
DefaultComparator{}, param_names);
}
// clang-format on