tatami_test
Utilities for testing tatami libraries
Loading...
Searching...
No Matches
test_access.hpp
Go to the documentation of this file.
1#ifndef TATAMI_TEST_TEST_ACCESS_HPP
2#define TATAMI_TEST_TEST_ACCESS_HPP
3
4#include <gtest/gtest.h>
5
6#include <vector>
7#include <limits>
8#include <random>
9#include <cmath>
10#include <memory>
11#include <cstdint>
12#include <type_traits>
13#include <cassert>
14
15#include "tatami/utils/make_Extractor.hpp"
18#include "tatami/utils/copy.hpp"
19
21#include "create_seed.hpp"
22#include "utils.hpp"
23
29namespace tatami_test {
30
38enum class TestAccessOrder : char { FORWARD, REVERSE, RANDOM };
39
47 bool use_oracle = false;
48
52 bool use_row = true;
60 TestAccessOrder order = TestAccessOrder::FORWARD;
61
65 int jump = 1;
66
70 bool check_sparse = true;
71
76 std::optional<SeedType> seed;
77};
78
88typedef std::tuple<bool, TestAccessOrder, int> TestAccessOptionsCombination;
89
98 TestAccessOptions output;
99 output.use_oracle = std::get<0>(x);
100 output.order = std::get<1>(x);
101 output.jump = std::get<2>(x);
102 return output;
103}
104
110 return ::testing::Combine(
111 ::testing::Values(true, false), /* whether to use an oracle. */
112 ::testing::Values(TestAccessOrder::FORWARD, TestAccessOrder::REVERSE, TestAccessOrder::RANDOM), /* access order. */
113 ::testing::Values(1, 3) /* jump between rows/columns. */
114 );
115}
116
120// Back-compatibility only.
121typedef std::tuple<bool, bool, TestAccessOrder, int> StandardTestAccessOptions;
122
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);
129 return output;
130}
131
132inline auto standard_test_access_options_combinations() {
133 return ::testing::Combine(
134 ::testing::Values(true, false), /* whether to access the rows. */
135 ::testing::Values(true, false), /* whether to use an oracle. */
136 ::testing::Values(TestAccessOrder::FORWARD, TestAccessOrder::REVERSE, TestAccessOrder::RANDOM), /* access order. */
137 ::testing::Values(1, 3) /* jump between rows/columns. */
138 );
139}
147inline SeedType combine_hash(SeedType old_seed, const TestAccessOptions& options) {
148 return combine_hash(old_seed, options.use_oracle, (int)options.order, options.jump);
149}
150
151namespace internal {
152
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 << ")";
161 } else {
162 ASSERT_EQ(expected_val, observed_val) << "different values at position " << i << " (" << context << ")";
163 }
164 }
165}
166
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));
170}
171
172template<typename Index_>
173std::vector<Index_> simulate_test_access_sequence(const Index_ NR, const Index_ NC, bool use_row, const TestAccessOptions& options) {
174 RngEngine rng(create_seed(NR, NC, use_row, options));
175
176 Index_ start = rng() % options.jump;
177 std::vector<Index_> sequence;
178 const auto limit = (use_row ? NR : NC);
179 if (start < limit) {
180 while (1) {
181 sequence.push_back(start);
182 const Index_ remainder = limit - start;
183 // Make sure this comparison involves two unsigned integers to avoid GCC warnings.
184 if (sanisizer::is_less_than_or_equal(remainder, options.jump)) {
185 break;
186 }
187 start += options.jump;
188 }
189 }
190
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);
195 }
196
197 return sequence;
198}
199
200template<bool use_oracle_, typename Index_>
201tatami::MaybeOracle<use_oracle_, Index_> create_oracle(const std::vector<Index_>& sequence, const TestAccessOptions& options) {
202 if constexpr(use_oracle_) {
203 std::shared_ptr<tatami::Oracle<Index_> > oracle;
204 if (options.jump == 1 && options.order == TestAccessOrder::FORWARD) {
205 oracle.reset(new tatami::ConsecutiveOracle<Index_>(0, sanisizer::Cast(sequence.size())));
206 } else {
207 oracle.reset(new tatami::FixedViewOracle<Index_>(sequence.data(), sanisizer::Cast(sequence.size())));
208 }
209 return oracle;
210 } else {
211 return false;
212 }
213}
214
215template<bool use_oracle_, typename Value_, typename Index_, class SparseExpand_, typename ...Args_>
216void test_access_base(
217 const tatami::Matrix<Value_, Index_>& matrix,
218 const tatami::Matrix<Value_, Index_>& reference,
219 bool use_row,
220 const TestAccessOptions& options,
221 const Index_ extent,
222 const SparseExpand_ sparse_expand,
223 Args_... args
224) {
225 const auto NR = matrix.nrow();
226 ASSERT_EQ(NR, reference.nrow());
227 const auto NC = matrix.ncol();
228 ASSERT_EQ(NC, reference.ncol());
229
230 auto refwork = reference.dense(use_row, args..., tatami::Options());
231
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());
235
236 tatami::Options opt;
237 auto swork = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
238 opt.sparse_extract_index = false;
239 auto swork_v = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
240 opt.sparse_extract_value = false;
241 auto swork_n = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
242 opt.sparse_extract_index = true;
243 auto swork_i = tatami::make_Extractor<true, use_oracle_>(matrix, use_row, oracle, args..., opt);
244
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;
250
251 for (const auto i : sequence) {
252 {
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);
256 tatami::copy_n(ref_ptr, extent, ref_buf);
257 }
258
259 // Dense retrieval.
260 {
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);
266 } else {
267 return pwork->fetch(i, mat_buf);
268 }
269 }();
270 tatami::copy_n(mat_ptr, extent, mat_buf);
271 compare_vectors(ref_dense_buffer, mat_dense_buffer, "dense retrieval");
272 }
273
274 // Sparse retrieval with both values and indices.
275 {
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();
280
281 const auto observed = [&]() {
282 if constexpr(use_oracle_) {
283 return swork->fetch(vbuf, ibuf);
284 } else {
285 return swork->fetch(i, vbuf, ibuf);
286 }
287 }();
288 compare_vectors(ref_dense_buffer, sparse_expand(observed), "sparse retrieval");
289
290 if (!has_sparse && sanisizer::is_less_than(observed.number, extent)) {
291 has_sparse = true;
292 }
293
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;
298 break;
299 }
300 }
301 ASSERT_TRUE(is_increasing);
302
303 mat_vstore1.clear();
304 mat_vstore1.insert(mat_vstore1.end(), observed.value, observed.value + observed.number);
305 mat_istore1.clear();
306 mat_istore1.insert(mat_istore1.end(), observed.index, observed.index + observed.number);
307 }
308
309 // Sparse retrieval with indices only.
310 {
311 std::fill(mat_ibuffer.begin(), mat_ibuffer.end(), 0);
312 const auto ibuf = mat_ibuffer.data();
313
314 auto observed_i = [&]() {
315 if constexpr(use_oracle_) {
316 return swork_i->fetch(NULL, ibuf);
317 } else {
318 return swork_i->fetch(i, NULL, ibuf);
319 }
320 }();
321
322 ASSERT_TRUE(observed_i.value == NULL);
323 mat_istore2.clear();
324 mat_istore2.insert(mat_istore2.end(), observed_i.index, observed_i.index + observed_i.number);
325 ASSERT_EQ(mat_istore1, mat_istore2);
326 }
327
328 // Sparse retrieval with values only.
329 {
330 std::fill(mat_vbuffer.begin(), mat_vbuffer.end(), 0);
331 const auto vbuf = mat_vbuffer.data();
332
333 auto observed_v = [&]() {
334 if constexpr(use_oracle_) {
335 return swork_v->fetch(vbuf, NULL);
336 } else {
337 return swork_v->fetch(i, vbuf, NULL);
338 }
339 }();
340
341 ASSERT_TRUE(observed_v.index == NULL);
342 mat_vstore2.clear();
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");
345 }
346
347 // Sparse retrieval with neither indices or values.
348 {
349 auto observed_n = [&]() {
350 if constexpr(use_oracle_) {
351 return swork_n->fetch(NULL, NULL);
352 } else {
353 return swork_n->fetch(i, NULL, NULL);
354 }
355 }();
356
357 ASSERT_TRUE(observed_n.value == NULL);
358 ASSERT_TRUE(observed_n.index == NULL);
359 ASSERT_EQ(mat_vstore1.size(), observed_n.number);
360 }
361 }
362
363 if (options.check_sparse && matrix.is_sparse()) {
364 EXPECT_TRUE(has_sparse);
365 }
366}
367
368template<typename Value_, typename Index_, typename ...Args_>
369void test_access(
370 const tatami::Matrix<Value_, Index_>& matrix,
371 const tatami::Matrix<Value_, Index_>& reference,
372 const bool use_row,
373 const TestAccessOptions& options,
374 Args_... args
375) {
376 if (options.use_oracle) {
377 test_access_base<true>(matrix, reference, use_row, options, std::forward<Args_>(args)...);
378 } else {
379 test_access_base<false>(matrix, reference, use_row, options, std::forward<Args_>(args)...);
380 }
381}
382
383}
402template<typename Value_, typename Index_>
404 const tatami::Matrix<Value_, Index_>& matrix,
405 const tatami::Matrix<Value_, Index_>& reference,
406 const bool use_row,
407 const TestAccessOptions& options
408) {
409 const Index_ nsecondary = (use_row ? reference.ncol() : reference.nrow());
410 auto expected = sanisizer::create<std::vector<Value_> >(nsecondary);
411 internal::test_access(
412 matrix,
413 reference,
414 use_row,
415 options,
416 nsecondary,
417 [&](const tatami::SparseRange<Value_, Index_>& svec) -> const std::vector<Value_>& {
418 std::fill(expected.begin(), expected.end(), 0);
419 for (I<decltype(svec.number)> i = 0; i < svec.number; ++i) {
420 expected[svec.index[i]] = svec.value[i];
421 }
422 return expected;
423 }
424 );
425}
426
444template<typename Value_, typename Index_>
446 const tatami::Matrix<Value_, Index_>& matrix,
447 const tatami::Matrix<Value_, Index_>& reference,
448 const bool use_row,
449 const Index_ start,
450 const Index_ length,
451 const TestAccessOptions& options
452) {
453 assert(check_block(matrix, use_row, start, length));
454 auto expected = sanisizer::create<std::vector<Value_> >(length);
455 internal::test_access(
456 matrix,
457 reference,
458 use_row,
459 options,
460 length,
461 [&](const tatami::SparseRange<Value_, Index_>& svec) -> const std::vector<Value_>& {
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];
465 }
466 return expected;
467 },
468 start,
469 length
470 );
471}
472
489template<typename Value_, typename Index_>
491 const tatami::Matrix<Value_, Index_>& matrix,
492 const tatami::Matrix<Value_, Index_>& reference,
493 const bool use_row,
494 const tatami::VectorPtr<Index_>& indices_ptr,
495 const TestAccessOptions& options
496) {
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);
502 {
503 const auto& indices = *indices_ptr;
504 for (Index_ i = 0; i < num_indices; ++i) {
505 reposition[indices[i]] = i;
506 }
507 }
508
509 auto expected = sanisizer::create<std::vector<Value_> >(num_indices);
510 internal::test_access(
511 matrix,
512 reference,
513 use_row,
514 options,
515 num_indices,
516 [&](const tatami::SparseRange<Value_, Index_>& svec) -> const std::vector<Value_>& {
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];
520 }
521 return expected;
522 },
523 indices_ptr
524 );
525}
526
530// For back-compatibility.
531template<typename Value_, typename Index_>
532void test_full_access(
533 const tatami::Matrix<Value_, Index_>& matrix,
534 const tatami::Matrix<Value_, Index_>& reference,
535 const TestAccessOptions& options
536) {
537 test_access(matrix, reference, options.use_row, options);
538}
539
540template<typename Value_, typename Index_>
541void test_block_access(
542 const tatami::Matrix<Value_, Index_>& matrix,
543 const tatami::Matrix<Value_, Index_>& reference,
544 const double relative_start,
545 const double relative_length,
546 const TestAccessOptions& options
547) {
548 auto block = define_non_target_block(matrix, options.use_row, relative_start, relative_length);
549 test_access(matrix, reference, options.use_row, block.first, block.second, options);
550}
551
552template<typename Value_, typename Index_>
553void test_indexed_access(
554 const tatami::Matrix<Value_, Index_>& matrix,
555 const tatami::Matrix<Value_, Index_>& reference,
556 const double relative_start,
557 const double probability,
558 const TestAccessOptions& options
559) {
560 const Index_ nsecondary = (options.use_row ? reference.ncol() : reference.nrow());
561 auto indices_ptr = create_indexed_subset(
562 nsecondary,
563 relative_start,
564 probability,
565 static_cast<SeedType>(
566 create_seed(matrix.nrow(), matrix.ncol(), options.use_row, options)
567 + static_cast<SeedType>(999 * probability)
568 + static_cast<SeedType>(85 * relative_start)
569 )
570 );
571 test_access(matrix, reference, options.use_row, indices_ptr, options);
572}
573
574template<typename Value_, typename Index_>
575void test_simple_column_access(const tatami::Matrix<Value_, Index_>& matrix, const tatami::Matrix<Value_, Index_>& reference) {
576 TestAccessOptions options;
577 options.use_row = false;
578 test_full_access(matrix, reference, options);
579}
580
581template<typename Value_, typename Index_>
582void test_simple_row_access(const tatami::Matrix<Value_, Index_>& matrix, const tatami::Matrix<Value_, Index_>& reference) {
583 TestAccessOptions options;
584 options.use_row = false;
585 test_full_access(matrix, reference, options);
586}
591}
592
593#endif
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
create a seed.
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
const Value_ * value
const Index_ * index
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
Miscellaneous utilities.