summaryrefslogtreecommitdiff
path: root/test/other/sos.cpp
blob: f3a610949c97bd35f3f4c3446573216a6d4ca9d0 (plain)
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
#include "../util.h"

vector<ll> sos(const vector<ll>& in) {
	#include <other/sos.cpp>
	return res;
}

vector<ll> naive(const vector<ll>& in) {
	vector<ll> res(sz(in));
	for (ll i = 0; i < sz(in); i++) {
		for (ll j = 0; j <= i; j++) {
			if ((i | j) == i) {
				res[i] += in[j];
			}
		}
	}
	return res;
}

void stress_test() {
	ll tests = 0;
	for (ll i = 0; i < 1000; i++) {
		int n = Random::integer<int>(1, 100);
		auto in = Random::integers<ll>(n, -1000, 1000);
		auto got = sos(in);
		auto expected = naive(in);
		if (got != expected) cerr << "error: " << i << FAIL;
		tests += n;
	}
	cerr << "tested random queries: " << tests << endl;
}

constexpr int N = 10'000'000;
void performance_test() {
	timer t;
	auto in = Random::integers<ll>(N, -1000, 1000);
	t.start();
	auto res = sos(in);
	t.stop();
	hash_t hash = 0;
	for (ll x : res) hash += x;
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test();
	performance_test();
}