1#ifndef TATAMI_TEST_TEST_ACCESS_HPP
2#define TATAMI_TEST_TEST_ACCESS_HPP
4#include <gtest/gtest.h>
15#include "tatami/utils/make_Extractor.hpp"
76 std::optional<SeedType>
seed;
100 output.
order = std::get<1>(x);
101 output.
jump = std::get<2>(x);
110 return ::testing::Combine(
111 ::testing::Values(
true,
false),
112 ::testing::Values(TestAccessOrder::FORWARD, TestAccessOrder::REVERSE, TestAccessOrder::RANDOM),
113 ::testing::Values(1, 3)
121typedef std::tuple<bool, bool, TestAccessOrder, int> StandardTestAccessOptions;
123inline TestAccessOptions convert_test_access_options(
const StandardTestAccessOptions& x) {
124 TestAccessOptions output;
125 output.use_row = std::get<0>(x);
126 output.use_oracle = std::get<1>(x);
127 output.order = std::get<2>(x);
128 output.jump = std::get<3>(x);
132inline auto standard_test_access_options_combinations() {
133 return ::testing::Combine(
134 ::testing::Values(
true,
false),
135 ::testing::Values(
true,
false),
136 ::testing::Values(TestAccessOrder::FORWARD, TestAccessOrder::REVERSE, TestAccessOrder::RANDOM),
137 ::testing::Values(1, 3)
147inline SeedType combine_hash(
SeedType old_seed,
const TestAccessOptions& options) {
148 return combine_hash(old_seed, options.use_oracle, (
int)options.order, options.jump);
153template<
typename Value_>
154void compare_vectors(
const std::vector<Value_>& expected,
const std::vector<Value_>& observed,
const std::string& context) {
155 const auto n_expected = expected.size();
156 ASSERT_EQ(n_expected, observed.size()) <<
"mismatch in vector length (" << context <<
")";
157 for (I<
decltype(n_expected)> i = 0; i < n_expected; ++i) {
158 auto expected_val = expected[i], observed_val = observed[i];
159 if (std::isnan(expected_val)) {
160 ASSERT_EQ(std::isnan(expected_val), std::isnan(observed_val)) <<
"mismatching NaNs at position " << i <<
" (" << context <<
")";
162 ASSERT_EQ(expected_val, observed_val) <<
"different values at position " << i <<
" (" << context <<
")";
167template<
typename Value_>
168void compare_vectors(
const std::vector<Value_>& expected,
const std::vector<Value_>& observed,
const char* context) {
169 compare_vectors(expected, observed, std::string(context));
172template<
typename Index_>
173std::vector<Index_> simulate_test_access_sequence(
const Index_ NR,
const Index_ NC,
bool use_row,
const TestAccessOptions& options) {
176 Index_ start = rng() % options.jump;
177 std::vector<Index_> sequence;
178 const auto limit = (use_row ? NR : NC);
181 sequence.push_back(start);
182 const Index_ remainder = limit - start;
184 if (sanisizer::is_less_than_or_equal(remainder, options.jump)) {
187 start += options.jump;
191 if (options.order == TestAccessOrder::REVERSE) {
192 std::reverse(sequence.begin(), sequence.end());
193 }
else if (options.order == TestAccessOrder::RANDOM) {
194 std::shuffle(sequence.begin(), sequence.end(), rng);
200template<
bool use_oracle_,
typename Index_>
202 if constexpr(use_oracle_) {
203 std::shared_ptr<tatami::Oracle<Index_> > oracle;
204 if (options.jump == 1 && options.order == TestAccessOrder::FORWARD) {
215template<
bool use_oracle_,
typename Value_,
typename Index_,
class SparseExpand_,
typename ...Args_>
216void test_access_base(
220 const TestAccessOptions& options,
222 const SparseExpand_ sparse_expand,
225 const auto NR = matrix.
nrow();
226 ASSERT_EQ(NR, reference.
nrow());
227 const auto NC = matrix.
ncol();
228 ASSERT_EQ(NC, reference.
ncol());
232 auto sequence = simulate_test_access_sequence(NR, NC, use_row, options);
233 auto oracle = create_oracle<use_oracle_>(sequence, options);
234 auto pwork = tatami::make_Extractor<false, use_oracle_>(matrix, use_row, oracle, args...,
tatami::Options());
237 auto swork = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
239 auto swork_v = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
241 auto swork_n = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
243 auto swork_i = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
245 sanisizer::as_size_type<std::vector<Value_> >(extent);
246 std::vector<Value_> mat_dense_buffer(extent), ref_dense_buffer(extent), mat_vbuffer(extent), mat_vstore1(extent), mat_vstore2(extent);
247 sanisizer::as_size_type<std::vector<Index_> >(extent);
248 std::vector<Index_> mat_ibuffer(extent), mat_istore1(extent), mat_istore2(extent);
249 bool has_sparse =
false;
251 for (
const auto i : sequence) {
253 std::fill(ref_dense_buffer.begin(), ref_dense_buffer.end(), 0);
254 const auto ref_buf = ref_dense_buffer.data();
255 const auto ref_ptr = refwork->fetch(i, ref_buf);
261 std::fill(mat_dense_buffer.begin(), mat_dense_buffer.end(), 0);
262 const auto mat_buf = mat_dense_buffer.data();
263 const auto mat_ptr = [&]() {
264 if constexpr(use_oracle_) {
265 return pwork->fetch(mat_buf);
267 return pwork->fetch(i, mat_buf);
271 compare_vectors(ref_dense_buffer, mat_dense_buffer,
"dense retrieval");
276 std::fill(mat_vbuffer.begin(), mat_vbuffer.end(), 0);
277 std::fill(mat_ibuffer.begin(), mat_ibuffer.end(), 0);
278 const auto vbuf = mat_vbuffer.data();
279 const auto ibuf = mat_ibuffer.data();
281 const auto observed = [&]() {
282 if constexpr(use_oracle_) {
283 return swork->fetch(vbuf, ibuf);
285 return swork->fetch(i, vbuf, ibuf);
288 compare_vectors(ref_dense_buffer, sparse_expand(observed),
"sparse retrieval");
290 if (!has_sparse && sanisizer::is_less_than(observed.number, extent)) {
294 bool is_increasing =
true;
295 for (I<
decltype(observed.number)> i = 1; i < observed.number; ++i) {
296 if (observed.index[i] <= observed.index[i-1]) {
297 is_increasing =
false;
301 ASSERT_TRUE(is_increasing);
304 mat_vstore1.insert(mat_vstore1.end(), observed.value, observed.value + observed.number);
306 mat_istore1.insert(mat_istore1.end(), observed.index, observed.index + observed.number);
311 std::fill(mat_ibuffer.begin(), mat_ibuffer.end(), 0);
312 const auto ibuf = mat_ibuffer.data();
314 auto observed_i = [&]() {
315 if constexpr(use_oracle_) {
316 return swork_i->fetch(NULL, ibuf);
318 return swork_i->fetch(i, NULL, ibuf);
322 ASSERT_TRUE(observed_i.value == NULL);
324 mat_istore2.insert(mat_istore2.end(), observed_i.index, observed_i.index + observed_i.number);
325 ASSERT_EQ(mat_istore1, mat_istore2);
330 std::fill(mat_vbuffer.begin(), mat_vbuffer.end(), 0);
331 const auto vbuf = mat_vbuffer.data();
333 auto observed_v = [&]() {
334 if constexpr(use_oracle_) {
335 return swork_v->fetch(vbuf, NULL);
337 return swork_v->fetch(i, vbuf, NULL);
341 ASSERT_TRUE(observed_v.index == NULL);
343 mat_vstore2.insert(mat_vstore2.end(), observed_v.value, observed_v.value + observed_v.number);
344 compare_vectors(mat_vstore1, mat_vstore2,
"sparse retrieval with values only");
349 auto observed_n = [&]() {
350 if constexpr(use_oracle_) {
351 return swork_n->fetch(NULL, NULL);
353 return swork_n->fetch(i, NULL, NULL);
357 ASSERT_TRUE(observed_n.value == NULL);
358 ASSERT_TRUE(observed_n.index == NULL);
359 ASSERT_EQ(mat_vstore1.size(), observed_n.number);
363 if (options.check_sparse && matrix.
is_sparse()) {
364 EXPECT_TRUE(has_sparse);
368template<
typename Value_,
typename Index_,
typename ...Args_>
373 const TestAccessOptions& options,
376 if (options.use_oracle) {
377 test_access_base<true>(matrix, reference, use_row, options, std::forward<Args_>(args)...);
379 test_access_base<false>(matrix, reference, use_row, options, std::forward<Args_>(args)...);
402template<
typename Value_,
typename Index_>
409 const Index_ nsecondary = (use_row ? reference.
ncol() : reference.
nrow());
410 auto expected = sanisizer::create<std::vector<Value_> >(nsecondary);
411 internal::test_access(
418 std::fill(expected.begin(), expected.end(), 0);
419 for (I<
decltype(svec.
number)> i = 0; i < svec.
number; ++i) {
444template<
typename Value_,
typename Index_>
453 assert(check_block(matrix, use_row, start, length));
454 auto expected = sanisizer::create<std::vector<Value_> >(length);
455 internal::test_access(
462 std::fill(expected.begin(), expected.end(), 0);
463 for (I<
decltype(svec.
number)> i = 0; i < svec.
number; ++i) {
464 expected[svec.
index[i] - start] = svec.
value[i];
489template<
typename Value_,
typename Index_>
497 assert(check_indices(matrix, use_row, indices_ptr));
498 const Index_ num_indices = indices_ptr->size();
499 constexpr std::size_t placeholder = -1;
500 const Index_ nsecondary = (use_row ? reference.
ncol() : reference.
nrow());
501 auto reposition = sanisizer::create<std::vector<std::size_t> >(nsecondary, placeholder);
503 const auto& indices = *indices_ptr;
504 for (Index_ i = 0; i < num_indices; ++i) {
505 reposition[indices[i]] = i;
509 auto expected = sanisizer::create<std::vector<Value_> >(num_indices);
510 internal::test_access(
517 std::fill(expected.begin(), expected.end(), 0);
518 for (I<
decltype(svec.
number)> i = 0; i < svec.
number; ++i) {
519 expected[reposition[svec.
index[i]]] = svec.
value[i];
531template<
typename Value_,
typename Index_>
532void test_full_access(
535 const TestAccessOptions& options
537 test_access(matrix, reference, options.use_row, options);
540template<
typename Value_,
typename Index_>
541void test_block_access(
544 const double relative_start,
545 const double relative_length,
546 const TestAccessOptions& options
549 test_access(matrix, reference, options.use_row, block.first, block.second, options);
552template<
typename Value_,
typename Index_>
553void test_indexed_access(
556 const double relative_start,
557 const double probability,
558 const TestAccessOptions& options
560 const Index_ nsecondary = (options.use_row ? reference.
ncol() : reference.
nrow());
561 auto indices_ptr = create_indexed_subset(
567 +
static_cast<SeedType>(999 * probability)
568 +
static_cast<SeedType>(85 * relative_start)
571 test_access(matrix, reference, options.use_row, indices_ptr, options);
574template<
typename Value_,
typename Index_>
576 TestAccessOptions options;
577 options.use_row =
false;
578 test_full_access(matrix, reference, options);
581template<
typename Value_,
typename Index_>
583 TestAccessOptions options;
584 options.use_row =
false;
585 test_full_access(matrix, reference, options);
virtual std::unique_ptr< MyopicDenseExtractor< Value_, Index_ > > dense(bool row, const Options &opt) const=0
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
virtual bool is_sparse() const=0
Define a part of the non-target dimension.
Utilities for testing tatami libraries.
Definition create_seed.hpp:17
std::tuple< bool, TestAccessOrder, int > TestAccessOptionsCombination
Definition test_access.hpp:88
SeedType create_seed(Args_ &&... args)
Definition create_seed.hpp:93
TestAccessOptions convert_to_test_access_options(const TestAccessOptionsCombination &x)
Definition test_access.hpp:97
std::mt19937_64 RngEngine
Definition utils.hpp:26
RngEngine::result_type SeedType
Definition utils.hpp:31
auto combine_test_access_options()
Definition test_access.hpp:109
TestAccessOrder
Definition test_access.hpp:38
std::pair< Index_, Index_ > define_non_target_block(const Index_ extent, const double relative_start, const double relative_length)
Definition define_non_target_part.hpp:41
void test_access(const tatami::Matrix< Value_, Index_ > &matrix, const tatami::Matrix< Value_, Index_ > &reference, const bool use_row, const TestAccessOptions &options)
Definition test_access.hpp:403
std::shared_ptr< const std::vector< Index_ > > VectorPtr
typename std::conditional< oracle_, std::shared_ptr< const Oracle< Index_ > >, bool >::type MaybeOracle
Value_ * copy_n(const Value_ *const input, const Size_ n, Value_ *const output)
bool sparse_extract_index
bool sparse_extract_value
Options for test_access().
Definition test_access.hpp:43
std::optional< SeedType > seed
Definition test_access.hpp:76
bool use_oracle
Definition test_access.hpp:47
int jump
Definition test_access.hpp:65
bool check_sparse
Definition test_access.hpp:70
TestAccessOrder order
Definition test_access.hpp:60