aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/parallelsort.cpp
blob: 8a9f547bcb8db71995c6835112b86094c15dae70 (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zenutil/parallelsort.h>

#include <zencore/testing.h>

namespace zen {

#if ZEN_WITH_TESTS

void
parallelsort_forcelink()
{
}

TEST_SUITE_BEGIN("util.parallelsort");

TEST_CASE("empty range")
{
	WorkerThreadPool   Pool(2);
	eastl::vector<int> Vec;
	ParallelSort(Pool, Vec.begin(), Vec.end(), [](int A, int B) { return A < B; });
	CHECK(Vec.empty());
}

TEST_CASE("single element")
{
	WorkerThreadPool   Pool(2);
	eastl::vector<int> Vec = {42};
	ParallelSort(Pool, Vec.begin(), Vec.end(), [](int A, int B) { return A < B; });
	CHECK(Vec.size() == 1);
	CHECK(Vec[0] == 42);
}

TEST_CASE("small array below threshold")
{
	WorkerThreadPool   Pool(2);
	eastl::vector<int> Vec = {5, 3, 8, 1, 9, 2, 7, 4, 6, 0};
	ParallelSort(Pool, Vec.begin(), Vec.end(), [](int A, int B) { return A < B; });
	for (size_t I = 0; I < Vec.size(); ++I)
	{
		CHECK(Vec[I] == int(I));
	}
}

TEST_CASE("large array triggers parallel path")
{
	WorkerThreadPool Pool(4);

	// 200K elements — well above the 64K threshold.
	constexpr size_t		N = 200'000;
	eastl::vector<uint32_t> Vec(N);

	// Fill with descending values.
	for (size_t I = 0; I < N; ++I)
	{
		Vec[I] = uint32_t(N - 1 - I);
	}

	ParallelSort(Pool, Vec.begin(), Vec.end(), [](uint32_t A, uint32_t B) { return A < B; });

	for (size_t I = 0; I < N; ++I)
	{
		CHECK_MESSAGE(Vec[I] == uint32_t(I), "index=", I, " got=", Vec[I]);
	}
}

TEST_CASE("already sorted")
{
	WorkerThreadPool Pool(4);

	constexpr size_t		N = 200'000;
	eastl::vector<uint32_t> Vec(N);
	for (size_t I = 0; I < N; ++I)
	{
		Vec[I] = uint32_t(I);
	}

	ParallelSort(Pool, Vec.begin(), Vec.end(), [](uint32_t A, uint32_t B) { return A < B; });

	for (size_t I = 0; I < N; ++I)
	{
		CHECK_MESSAGE(Vec[I] == uint32_t(I), "index=", I, " got=", Vec[I]);
	}
}

TEST_CASE("reverse sorted")
{
	WorkerThreadPool Pool(4);

	constexpr size_t		N = 200'000;
	eastl::vector<uint32_t> Vec(N);
	for (size_t I = 0; I < N; ++I)
	{
		Vec[I] = uint32_t(N - 1 - I);
	}

	ParallelSort(Pool, Vec.begin(), Vec.end(), [](uint32_t A, uint32_t B) { return A < B; });

	for (size_t I = 0; I < N; ++I)
	{
		CHECK_MESSAGE(Vec[I] == uint32_t(I), "index=", I, " got=", Vec[I]);
	}
}

TEST_CASE("duplicate keys")
{
	WorkerThreadPool Pool(4);

	constexpr size_t		N = 200'000;
	eastl::vector<uint32_t> Vec(N);
	for (size_t I = 0; I < N; ++I)
	{
		Vec[I] = uint32_t(I % 100);	 // only 100 distinct values
	}

	ParallelSort(Pool, Vec.begin(), Vec.end(), [](uint32_t A, uint32_t B) { return A < B; });

	for (size_t I = 1; I < N; ++I)
	{
		CHECK_MESSAGE(Vec[I - 1] <= Vec[I], "not sorted at index=", I);
	}
}

TEST_CASE("custom comparator descending")
{
	WorkerThreadPool Pool(4);

	constexpr size_t		N = 200'000;
	eastl::vector<uint32_t> Vec(N);
	for (size_t I = 0; I < N; ++I)
	{
		Vec[I] = uint32_t(I);
	}

	ParallelSort(Pool, Vec.begin(), Vec.end(), [](uint32_t A, uint32_t B) { return A > B; });

	for (size_t I = 0; I < N; ++I)
	{
		CHECK_MESSAGE(Vec[I] == uint32_t(N - 1 - I), "index=", I, " got=", Vec[I]);
	}
}

TEST_SUITE_END();

#endif

}  // namespace zen