-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
even_odd_array.cc
52 lines (44 loc) · 1.24 KB
/
even_odd_array.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
#include <algorithm>
#include <iterator>
#include <set>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
using std::swap;
using std::vector;
void EvenOdd(vector<int>* A_ptr) {
vector<int>& A = *A_ptr;
int next_even = 0, next_odd = size(A) - 1;
while (next_even < next_odd) {
if (A[next_even] % 2 == 0) {
++next_even;
} else {
swap(A[next_even], A[next_odd--]);
}
}
}
void EvenOddWrapper(TimedExecutor& executor, vector<int> A) {
std::multiset<int> before(begin(A), end(A));
executor.Run([&] { EvenOdd(&A); });
bool in_odd = false;
for (int a : A) {
if (a % 2 == 0) {
if (in_odd) {
throw TestFailure("Even elements appear in odd part");
}
} else {
in_odd = true;
}
}
std::multiset<int> after(begin(A), end(A));
if (before != after) {
throw TestFailure("Elements mismatch");
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "A"};
return GenericTestMain(args, "even_odd_array.cc", "even_odd_array.tsv",
&EvenOddWrapper, DefaultComparator{}, param_names);
}