Skip to content

Commit 6175892

Browse files
committed
Testing for kvselect and kvpartial_sort
1 parent 16fe62c commit 6175892

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

tests/test-keyvalue.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "rand_array.h"
77
#include "x86simdsort.h"
88
#include "x86simdsort-scalar.h"
9+
#include "test-qsort-common.h"
910
#include <gtest/gtest.h>
1011

1112
template <typename T>
@@ -106,6 +107,70 @@ TYPED_TEST_P(simdkvsort, test_kvsort)
106107
}
107108
}
108109

110+
TYPED_TEST_P(simdkvsort, test_kvselect)
111+
{
112+
using T1 = typename std::tuple_element<0, decltype(TypeParam())>::type;
113+
using T2 = typename std::tuple_element<1, decltype(TypeParam())>::type;
114+
for (auto type : this->arrtype) {
115+
bool hasnan = (type == "rand_with_nan") ? true : false;
116+
for (auto size : this->arrsize) {
117+
size_t k = rand() % size;
118+
119+
std::vector<T1> key = get_array<T1>(type, size);
120+
std::vector<T2> val = get_array<T2>(type, size);
121+
std::vector<T1> key_bckp = key;
122+
std::vector<T2> val_bckp = val;
123+
124+
xss::scalar::keyvalue_qsort(
125+
key_bckp.data(), val_bckp.data(), size, hasnan);
126+
127+
// Test select by using it as part of partial_sort
128+
x86simdsort::keyvalue_select(key.data(), val.data(), k, size, hasnan);
129+
IS_ARR_PARTITIONED<T1>(key, k, key_bckp[k], type);
130+
xss::scalar::keyvalue_qsort(key.data(), val.data(), k, hasnan);
131+
132+
133+
bool is_kv_equivalent = kv_equivalent<T1, T2>(key.data(), val.data(), key_bckp.data(), val_bckp.data(), k);
134+
ASSERT_EQ(is_kv_equivalent, true);
135+
136+
key.clear();
137+
val.clear();
138+
key_bckp.clear();
139+
val_bckp.clear();
140+
}
141+
}
142+
}
143+
144+
TYPED_TEST_P(simdkvsort, test_kvpartial_sort)
145+
{
146+
using T1 = typename std::tuple_element<0, decltype(TypeParam())>::type;
147+
using T2 = typename std::tuple_element<1, decltype(TypeParam())>::type;
148+
for (auto type : this->arrtype) {
149+
bool hasnan = (type == "rand_with_nan") ? true : false;
150+
for (auto size : this->arrsize) {
151+
size_t k = rand() % size;
152+
153+
std::vector<T1> key = get_array<T1>(type, size);
154+
std::vector<T2> val = get_array<T2>(type, size);
155+
std::vector<T1> key_bckp = key;
156+
std::vector<T2> val_bckp = val;
157+
x86simdsort::keyvalue_partial_sort(key.data(), val.data(), k, size, hasnan);
158+
xss::scalar::keyvalue_qsort(
159+
key_bckp.data(), val_bckp.data(), size, hasnan);
160+
161+
IS_ARR_PARTIALSORTED<T1>(key, k, key_bckp, type);
162+
163+
bool is_kv_equivalent = kv_equivalent<T1, T2>(key.data(), val.data(), key_bckp.data(), val_bckp.data(), k);
164+
ASSERT_EQ(is_kv_equivalent, true);
165+
166+
key.clear();
167+
val.clear();
168+
key_bckp.clear();
169+
val_bckp.clear();
170+
}
171+
}
172+
}
173+
109174
TYPED_TEST_P(simdkvsort, test_validator)
110175
{
111176
// Tests a few edge cases to verify the tests are working correctly and identifying it as functional
@@ -152,7 +217,7 @@ TYPED_TEST_P(simdkvsort, test_validator)
152217
ASSERT_EQ(is_kv_equivalent, true);
153218
}
154219

155-
REGISTER_TYPED_TEST_SUITE_P(simdkvsort, test_kvsort, test_validator);
220+
REGISTER_TYPED_TEST_SUITE_P(simdkvsort, test_kvsort, test_kvselect, test_kvpartial_sort, test_validator);
156221

157222
#define CREATE_TUPLES(type) \
158223
std::tuple<double, type>, std::tuple<uint64_t, type>, \

tests/test-qsort.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ TYPED_TEST_P(simdsort, test_partial_qsort_ascending)
181181
for (auto type : this->arrtype) {
182182
bool hasnan = (type == "rand_with_nan") ? true : false;
183183
for (auto size : this->arrsize) {
184-
// k should be at least 1
185-
size_t k = std::max((size_t)1, rand() % size);
184+
size_t k = rand() % size;
186185
std::vector<TypeParam> basearr = get_array<TypeParam>(type, size);
187186

188187
// Ascending order

utils/custom-compare.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef UTILS_CUSTOM_COMPARE
2+
#define UTILS_CUSTOM_COMPARE
3+
14
#include <limits>
25
#include <cmath>
36
#include "xss-custom-float.h"
@@ -42,3 +45,5 @@ struct compare_arg {
4245
}
4346
const T *arr;
4447
};
48+
49+
#endif // UTILS_CUSTOM_COMPARE

0 commit comments

Comments
 (0)