forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xstrides.cpp
80 lines (73 loc) · 3.72 KB
/
test_xstrides.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "gtest/gtest.h"
#include "xtensor/xarray.hpp"
#include "test_common.hpp"
namespace xt
{
TEST(xstrides, unravel_from_strides)
{
{
SCOPED_TRACE("row_major strides");
row_major_result<> rm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::ptrdiff_t>(rm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_from_strides(offset, rm.strides(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
{
SCOPED_TRACE("column_major strides");
column_major_result<> cm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::ptrdiff_t>(cm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_from_strides(offset, cm.strides(), layout_type::column_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
{
SCOPED_TRACE("unit_major strides");
unit_shape_result<> um;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 0, 1 };
auto offset = element_offset<std::ptrdiff_t>(um.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_from_strides(offset, um.strides(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
}
TEST(xstrides, unravel_index)
{
{
SCOPED_TRACE("row_major strides");
row_major_result<> rm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::size_t>(rm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_index(offset, rm.shape(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
{
SCOPED_TRACE("column_major strides");
column_major_result<> cm;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 1, 1 };
auto offset = element_offset<std::size_t>(cm.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_index(offset, cm.shape(), layout_type::column_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
{
SCOPED_TRACE("unit_major strides");
unit_shape_result<> um;
using index_type = xt::dynamic_shape<std::ptrdiff_t>;
index_type index = { 2, 0, 1 };
auto offset = element_offset<std::size_t>(um.strides(), index.cbegin(), index.cend());
index_type unrav_index = unravel_index(offset, um.shape(), layout_type::row_major);
EXPECT_TRUE(std::equal(unrav_index.cbegin(), unrav_index.cend(), index.cbegin()));
}
}
}