Skip to content

Commit 40c41f5

Browse files
committed
Refactor test suite and add tests for partial sort
1 parent bf7f733 commit 40c41f5

File tree

6 files changed

+124
-18
lines changed

6 files changed

+124
-18
lines changed

tests/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ endif
1111

1212
if cpp.has_argument('-march=icelake-client')
1313
libtests += static_library('tests_qsort',
14-
files('test_qsort.cpp', ),
14+
files('test_qsort.cpp', 'test_qsort_partial.cpp', 'test_qsort_partialrange.cpp', ),
1515
dependencies: gtest_dep,
1616
include_directories : [src, utils],
1717
cpp_args : ['-O3', '-march=icelake-client'],

tests/test-qsort-common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef AVX512_TEST_COMMON
2+
#define AVX512_TEST_COMMON
3+
4+
#include "avx512-16bit-qsort.hpp"
5+
#include "avx512-32bit-qsort.hpp"
6+
#include "avx512-64bit-qsort.hpp"
7+
#include "cpuinfo.h"
8+
#include "rand_array.h"
9+
#include <gtest/gtest.h>
10+
#include <vector>
11+
12+
#endif

tests/test_qsort.cpp renamed to tests/test_qsort.hpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33
* * SPDX-License-Identifier: BSD-3-Clause
44
* *******************************************/
55

6-
#include "avx512-16bit-qsort.hpp"
7-
#include "avx512-32bit-qsort.hpp"
8-
#include "avx512-64bit-qsort.hpp"
9-
#include "cpuinfo.h"
10-
#include "rand_array.h"
11-
#include <gtest/gtest.h>
12-
#include <vector>
6+
#include "test-qsort-common.h"
137

148
template <typename T>
159
class avx512_sort : public ::testing::Test {
@@ -46,13 +40,3 @@ TYPED_TEST_P(avx512_sort, test_arrsizes)
4640
}
4741

4842
REGISTER_TYPED_TEST_SUITE_P(avx512_sort, test_arrsizes);
49-
50-
using Types = testing::Types<uint16_t,
51-
int16_t,
52-
float,
53-
double,
54-
uint32_t,
55-
int32_t,
56-
uint64_t,
57-
int64_t>;
58-
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefix, avx512_sort, Types);

tests/test_qsort_partial.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "test-qsort-common.h"
2+
3+
template <typename T>
4+
class avx512_sort_partial : public ::testing::Test {
5+
};
6+
TYPED_TEST_SUITE_P(avx512_sort_partial);
7+
8+
TYPED_TEST_P(avx512_sort_partial, test_arrsizes)
9+
{
10+
if (cpu_has_avx512bw()) {
11+
if ((sizeof(TypeParam) == 2) && (!cpu_has_avx512_vbmi2())) {
12+
GTEST_SKIP() << "Skipping this test, it requires avx512_vbmi2";
13+
}
14+
std::vector<int64_t> arrsizes;
15+
for (int64_t ii = 0; ii < 1024; ++ii) {
16+
arrsizes.push_back(ii);
17+
}
18+
std::vector<TypeParam> arr;
19+
std::vector<TypeParam> sortedarr;
20+
std::vector<TypeParam> psortedarr;
21+
for (size_t ii = 0; ii < arrsizes.size(); ++ii) {
22+
/* Random array */
23+
arr = get_uniform_rand_array<TypeParam>(arrsizes[ii]);
24+
sortedarr = arr;
25+
/* Sort with std::sort for comparison */
26+
std::sort(sortedarr.begin(), sortedarr.end());
27+
for (size_t k = 0; k < arr.size(); ++k) {
28+
psortedarr = arr;
29+
avx512_qsort_partial<TypeParam>(k+1, psortedarr.data(), psortedarr.size());
30+
ASSERT_EQ(sortedarr[k], psortedarr[k]);
31+
psortedarr.clear();
32+
}
33+
arr.clear();
34+
sortedarr.clear();
35+
}
36+
}
37+
else {
38+
GTEST_SKIP() << "Skipping this test, it requires avx512bw";
39+
}
40+
}
41+
42+
REGISTER_TYPED_TEST_SUITE_P(avx512_sort_partial, test_arrsizes);

tests/test_qsort_partialrange.hpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "test-qsort-common.h"
2+
3+
template <typename T>
4+
class avx512_sort_partialrange : public ::testing::Test {
5+
};
6+
TYPED_TEST_SUITE_P(avx512_sort_partialrange);
7+
8+
TYPED_TEST_P(avx512_sort_partialrange, test_arrsizes)
9+
{
10+
int64_t arrsize = 1024;
11+
int64_t nranges = 500;
12+
13+
if (cpu_has_avx512bw()) {
14+
if ((sizeof(TypeParam) == 2) && (!cpu_has_avx512_vbmi2())) {
15+
GTEST_SKIP() << "Skipping this test, it requires avx512_vbmi2";
16+
}
17+
std::vector<TypeParam> arr;
18+
std::vector<TypeParam> sortedarr;
19+
std::vector<TypeParam> prsortedarr;
20+
/* Random array */
21+
arr = get_uniform_rand_array<TypeParam>(arrsize);
22+
sortedarr = arr;
23+
/* Sort with std::sort for comparison */
24+
std::sort(sortedarr.begin(), sortedarr.end());
25+
26+
int64_t lb, ub;
27+
std::vector<int64_t> inds;
28+
for (size_t ii = 0; ii < nranges; ++ii) {
29+
prsortedarr = arr;
30+
31+
/* Pick a random start and end for the range */
32+
inds = get_uniform_rand_array<int64_t>(2, arrsize, 1);
33+
std::sort(inds.begin(), inds.end());
34+
lb = inds[0], ub = inds[1];
35+
36+
/* Sort the range and verify all the required elements match the presorted set */
37+
avx512_qsort_partialrange<TypeParam>(lb, ub, prsortedarr.data(), prsortedarr.size());
38+
for (size_t k = (lb-1); k < ub; ++k) {
39+
ASSERT_EQ(sortedarr[k], prsortedarr[k]);
40+
}
41+
42+
prsortedarr.clear();
43+
}
44+
45+
arr.clear();
46+
sortedarr.clear();
47+
}
48+
else {
49+
GTEST_SKIP() << "Skipping this test, it requires avx512bw";
50+
}
51+
}
52+
53+
REGISTER_TYPED_TEST_SUITE_P(avx512_sort_partialrange, test_arrsizes);

tests/test_sort.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "test_qsort.hpp"
2+
#include "test_qsort_partial.hpp"
3+
#include "test_qsort_partialrange.hpp"
4+
5+
using Types = testing::Types<uint16_t,
6+
int16_t,
7+
float,
8+
double,
9+
uint32_t,
10+
int32_t,
11+
uint64_t,
12+
int64_t>;
13+
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefix, avx512_sort, Types);
14+
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefix, avx512_sort_partial, Types);
15+
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefix, avx512_sort_partialrange, Types);

0 commit comments

Comments
 (0)