forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_equal_entries.cc
67 lines (58 loc) · 1.71 KB
/
group_equal_entries.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
#include <iterator>
#include <set>
#include <string>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
using std::string;
using std::vector;
struct Person {
int age;
string name;
};
void GroupByAge(vector<Person>* people) {
// TODO - you fill in here.
return;
}
namespace test_framework {
template <>
struct SerializationTrait<Person> : UserSerTrait<Person, int, string> {};
} // namespace test_framework
void GroupByAgeWrapper(TimedExecutor& executor, vector<Person>& people) {
if (people.empty()) {
return;
}
std::multiset<Person, std::function<bool(Person, Person)>> values(
begin(people), end(people), [](const Person& a, const Person& b) {
return a.age == b.age ? a.name < b.name : a.age < b.age;
});
executor.Run([&] { GroupByAge(&people); });
if (people.empty()) {
throw TestFailure("Empty result");
}
std::set<int> ages;
int last_age = people[0].age;
for (auto& x : people) {
if (ages.count(x.age)) {
throw TestFailure("Entries are not grouped by age");
}
if (x.age != last_age) {
ages.insert(last_age);
last_age = x.age;
}
auto it = values.find(x);
if (it == end(values)) {
throw TestFailure("Entry set changed");
}
values.erase(it);
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "people"};
return GenericTestMain(args, "group_equal_entries.cc",
"group_equal_entries.tsv", &GroupByAgeWrapper,
DefaultComparator{}, param_names);
}