Skip to content

Commit d9f32f1

Browse files
mrknpitrou
authored andcommitted
Add a new test of coo sparse tensor creation from non-contiguous tensor
1 parent 0c1573c commit d9f32f1

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

cpp/src/arrow/sparse_tensor-test.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,58 @@ TEST(TestSparseCOOTensor, CreationFromTensor) {
186186
ASSERT_EQ(3, sidx->Value({11, 2}));
187187
}
188188

189+
TEST(TestSparseCOOTensor, CreationFromNonContiguousTensor) {
190+
std::vector<int64_t> shape = {2, 3, 4};
191+
std::vector<int64_t> values = {1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0,
192+
5, 0, 0, 0, 6, 0, 0, 0, 0, 0, 11, 0, 0, 0, 12, 0,
193+
13, 0, 0, 0, 14, 0, 0, 0, 0, 0, 15, 0, 0, 0, 16, 0};
194+
std::vector<int64_t> strides = {192, 64, 16};
195+
std::shared_ptr<Buffer> buffer = Buffer::Wrap(values);
196+
Tensor tensor(int64(), buffer, shape, strides);
197+
SparseTensorImpl<SparseCOOIndex> st(tensor);
198+
199+
ASSERT_EQ(12, st.non_zero_length());
200+
ASSERT_TRUE(st.is_mutable());
201+
202+
const int64_t* ptr = reinterpret_cast<const int64_t*>(st.raw_data());
203+
for (int i = 0; i < 6; ++i) {
204+
ASSERT_EQ(i + 1, ptr[i]);
205+
}
206+
for (int i = 0; i < 6; ++i) {
207+
ASSERT_EQ(i + 11, ptr[i + 6]);
208+
}
209+
210+
const auto& si = internal::checked_cast<const SparseCOOIndex&>(*st.sparse_index());
211+
std::shared_ptr<SparseCOOIndex::CoordsTensor> sidx = si.indices();
212+
ASSERT_EQ(std::vector<int64_t>({12, 3}), sidx->shape());
213+
ASSERT_TRUE(sidx->is_column_major());
214+
215+
// (0, 0, 0) -> 1
216+
ASSERT_EQ(0, sidx->Value({0, 0}));
217+
ASSERT_EQ(0, sidx->Value({0, 1}));
218+
ASSERT_EQ(0, sidx->Value({0, 2}));
219+
220+
// (0, 0, 2) -> 2
221+
ASSERT_EQ(0, sidx->Value({1, 0}));
222+
ASSERT_EQ(0, sidx->Value({1, 1}));
223+
ASSERT_EQ(2, sidx->Value({1, 2}));
224+
225+
// (0, 1, 1) -> 3
226+
ASSERT_EQ(0, sidx->Value({2, 0}));
227+
ASSERT_EQ(1, sidx->Value({2, 1}));
228+
ASSERT_EQ(1, sidx->Value({2, 2}));
229+
230+
// (1, 2, 1) -> 15
231+
ASSERT_EQ(1, sidx->Value({10, 0}));
232+
ASSERT_EQ(2, sidx->Value({10, 1}));
233+
ASSERT_EQ(1, sidx->Value({10, 2}));
234+
235+
// (1, 2, 3) -> 16
236+
ASSERT_EQ(1, sidx->Value({11, 0}));
237+
ASSERT_EQ(2, sidx->Value({11, 1}));
238+
ASSERT_EQ(3, sidx->Value({11, 2}));
239+
}
240+
189241
TEST(TestSparseCSRMatrix, CreationFromNumericTensor2D) {
190242
std::vector<int64_t> shape = {6, 4};
191243
std::vector<int64_t> values = {1, 0, 2, 0, 0, 3, 0, 4, 5, 0, 6, 0,

0 commit comments

Comments
 (0)