1#ifndef TATAMI_TEST_TEST_UNSORTED_ACCESS_HPP
2#define TATAMI_TEST_TEST_UNSORTED_ACCESS_HPP
4#include <gtest/gtest.h>
30template<
bool use_oracle_,
typename Value_,
typename Index_,
typename ...Args_>
31void test_unsorted_access_base(
const tatami::Matrix<Value_, Index_>& matrix,
const TestAccessOptions& options,
const Index_ extent, Args_... args) {
32 const auto NR = matrix.
nrow();
33 const auto NC = matrix.
ncol();
35 auto sequence = simulate_test_access_sequence(NR, NC, options);
36 auto oracle = create_oracle<use_oracle_>(sequence, options);
49 sanisizer::as_size_type<std::vector<Value_> >(extent);
50 std::vector<Value_> mat_vbuffer(extent), mat_vstore1(extent), mat_vstore2(extent);
51 sanisizer::as_size_type<std::vector<Index_> >(extent);
52 std::vector<Index_> mat_ibuffer(extent), mat_istore1(extent), mat_istore2(extent);
54 std::vector<std::pair<Index_, Value_> > collected;
55 std::vector<Value_> sorted_v;
56 std::vector<Index_> sorted_i;
58 for (
const auto i : sequence) {
61 std::fill(mat_ibuffer.begin(), mat_ibuffer.end(), 0);
62 std::fill(mat_vbuffer.begin(), mat_vbuffer.end(), 0);
63 const auto vbuf = mat_vbuffer.data();
64 const auto ibuf = mat_ibuffer.data();
66 auto observed = [&]() {
67 if constexpr(use_oracle_) {
68 return swork->fetch(vbuf, ibuf);
70 return swork->fetch(Fix(i), vbuf, ibuf);
75 mat_vstore1.insert(mat_vstore1.end(), observed.value, observed.value + observed.number);
77 mat_istore1.insert(mat_istore1.end(), observed.index, observed.index + observed.number);
82 std::fill(mat_ibuffer.begin(), mat_ibuffer.end(), 0);
83 std::fill(mat_vbuffer.begin(), mat_vbuffer.end(), 0);
84 const auto vbuf = mat_vbuffer.data();
85 const auto ibuf = mat_ibuffer.data();
87 auto observed_uns = [&]() {
88 if constexpr(use_oracle_) {
89 return swork_uns->fetch(vbuf, ibuf);
91 return swork_uns->fetch(Fix(i), vbuf, ibuf);
97 for (I<
decltype(observed_uns.number)> i = 0; i < observed_uns.number; ++i) {
98 collected.emplace_back(observed_uns.index[i], observed_uns.value[i]);
100 std::sort(collected.begin(), collected.end());
103 for (
const auto& p : collected) {
104 sorted_i.push_back(p.first);
105 sorted_v.push_back(p.second);
107 ASSERT_EQ(mat_istore1, sorted_i);
108 compare_vectors(mat_vstore1, sorted_v,
"unsorted sparse");
111 mat_vstore1.insert(mat_vstore1.end(), observed_uns.value, observed_uns.value + observed_uns.number);
113 mat_istore1.insert(mat_istore1.end(), observed_uns.index, observed_uns.index + observed_uns.number);
118 std::fill(mat_ibuffer.begin(), mat_ibuffer.end(), 0);
119 const auto ibuf = mat_ibuffer.data();
121 auto observed_i = [&]() {
122 if constexpr(use_oracle_) {
123 return swork_uns_i->fetch(NULL, ibuf);
125 return swork_uns_i->fetch(Fix(i), NULL, ibuf);
129 ASSERT_TRUE(observed_i.value == NULL);
131 mat_istore2.insert(mat_istore2.end(), observed_i.index, observed_i.index + observed_i.number);
132 ASSERT_EQ(mat_istore1, mat_istore2);
137 std::fill(mat_vbuffer.begin(), mat_vbuffer.end(), 0);
138 const auto vbuf = mat_vbuffer.data();
140 auto observed_v = [&]() {
141 if constexpr(use_oracle_) {
142 return swork_uns_v->fetch(vbuf, NULL);
144 return swork_uns_v->fetch(Fix(i), vbuf, NULL);
148 ASSERT_TRUE(observed_v.index == NULL);
150 mat_vstore2.insert(mat_vstore2.end(), observed_v.value, observed_v.value + observed_v.number);
151 compare_vectors(mat_vstore1, mat_vstore2,
"unsorted sparse, values only");
155 auto observed_n = [&]() {
156 if constexpr(use_oracle_) {
157 return swork_uns_n->fetch(NULL, NULL);
159 return swork_uns_n->fetch(Fix(i), NULL, NULL);
163 ASSERT_TRUE(observed_n.value == NULL);
164 ASSERT_TRUE(observed_n.index == NULL);
165 ASSERT_EQ(mat_vstore1.size(), observed_n.number);
170template<
bool use_oracle_,
typename Value_,
typename Index_>
172 const Index_ nsecondary = (options.use_row ? matrix.
ncol() : matrix.
nrow());
173 internal::test_unsorted_access_base<use_oracle_>(matrix, options, nsecondary);
176template<
bool use_oracle_,
typename Value_,
typename Index_>
178 const Index_ nsecondary = (options.use_row ? matrix.
ncol() : matrix.
nrow());
179 const Index_ start = nsecondary * relative_start;
180 const Index_ length = nsecondary * relative_length;
181 internal::test_unsorted_access_base<use_oracle_>(matrix, options, nsecondary, start, length);
184template<
bool use_oracle_,
typename Value_,
typename Index_>
186 const Index_ nsecondary = (options.use_row ? matrix.
ncol() : matrix.
nrow());
192 create_seed(matrix.
nrow(), matrix.
ncol(), options)
193 +
static_cast<SeedType>(1001 * probability)
194 +
static_cast<SeedType>(13 * relative_start)
197 const Index_ num_indices = index_ptr->size();
198 internal::test_unsorted_access_base<use_oracle_>(matrix, options, num_indices, std::move(index_ptr));
218template<
typename Value_,
typename Index_>
221 internal::test_unsorted_full_access<true>(matrix, options);
223 internal::test_unsorted_full_access<false>(matrix, options);
245template<
typename Value_,
typename Index_>
248 internal::test_unsorted_block_access<true>(matrix, relative_start, relative_length, options);
250 internal::test_unsorted_block_access<false>(matrix, relative_start, relative_length, options);
272template<
typename Value_,
typename Index_>
275 internal::test_unsorted_indexed_access<true>(matrix, relative_start, probability, options);
277 internal::test_unsorted_indexed_access<false>(matrix, relative_start, probability, options);
virtual Index_ ncol() const=0
virtual Index_ nrow() const=0
Create an indexed subset of dimension elements.
Utilities for testing tatami libraries.
Definition create_indexed_subset.hpp:16
void test_unsorted_full_access(const tatami::Matrix< Value_, Index_ > &matrix, const TestAccessOptions &options)
Definition test_unsorted_access.hpp:219
tatami::VectorPtr< Index_ > create_indexed_subset(const Index_ extent, const double relative_start, const double probability, const SeedType seed)
Definition create_indexed_subset.hpp:36
void test_unsorted_block_access(const tatami::Matrix< Value_, Index_ > &matrix, double relative_start, double relative_length, const TestAccessOptions &options)
Definition test_unsorted_access.hpp:246
RngEngine::result_type SeedType
Definition utils.hpp:39
void test_unsorted_indexed_access(const tatami::Matrix< Value_, Index_ > &matrix, double relative_start, double probability, const TestAccessOptions &options)
Definition test_unsorted_access.hpp:273
auto new_extractor(const Matrix< Value_, Index_ > &matrix, const bool row, MaybeOracle< oracle_, Index_ > oracle, Args_ &&... args)
bool sparse_extract_index
bool sparse_ordered_index
bool sparse_extract_value
Options for test_full_access() and friends.
Definition test_access.hpp:40
bool use_oracle
Definition test_access.hpp:44
Test access patterns on a tatami::Matrix.