summaryrefslogtreecommitdiff
path: root/test/geometry/sortAround.cpp
blob: 895a6d66350c350a1ca26ee0ed22391758511a47 (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
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
#include "../util.h"
constexpr ll EPS = 0;
#define double ll
#define polar polar<ll>
#include <geometry/formulas.cpp>
#undef polar
#undef double
#include <geometry/sortAround.cpp>

//expected order:
//1 8 7
//2 . 6
//3 4 5
void test_tiny() {
	vector<pt> expected = {
		{-1, 1},
		{-1, 0},
		{-1,-1},
		{ 0,-1},
		{ 1,-1},
		{ 1, 0},
		{ 1, 1},
		{ 0, 1},
	};
	auto got = expected;
	for (int i = 0; i < 100'000; i++) {
		ranges::shuffle(got, Random::rng);
		sortAround(0, got);
		if (got != expected) cerr << "error" << FAIL;
	}
	cerr << "tested tiny" << endl;
}

void stress_test(ll range) {
	ll queries = 0;
	for (int tries = 0; tries < 100'000; tries++) {
		int n = Random::integer<int>(2, 100);
		auto ps = Random::points<ll>(n, -range, range);

		auto contains = [&](pt p){
			for (pt pp : ps) if (pp == p) return true;
			return false;
		};

		pt c;
		do {
			c = Random::point<ll>(-range, range);
		} while (contains(c));

		sortAround(c, ps);

		auto isLeft = [&](pt p){return real(p - c) < 0 || (real(p - c) == 0 && imag(p - c) < 0);};
		auto isCCW = [&](pt a, pt b){return cross(c, a, b) > 0;};
		if (!ranges::is_partitioned(ps, isLeft)) cerr << "error 1" << FAIL;
		auto mid = ranges::partition_point(ps, isLeft);
		if (!is_sorted(ps.begin(), mid, isCCW)) cerr << "error 2" << FAIL;
		if (!is_sorted(mid, ps.end(), isCCW)) cerr << "error 3" << FAIL;
		queries += n;
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 2'000'000;
void performance_test() {
	timer t;
	auto ps = Random::points<ll>(N, -1'000'000'000, 1'000'000'000);

	t.start();
	sortAround(0, ps);
	t.stop();

	hash_t hash = 0;
	for (pt p : ps) hash += real(p) * imag(p);
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	test_tiny();
	stress_test(100);
	stress_test(1'000'000'000);
	if (!sanitize) performance_test();
}