-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathLayout.cpp
More file actions
156 lines (143 loc) · 6.87 KB
/
Copy pathLayout.cpp
File metadata and controls
156 lines (143 loc) · 6.87 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// -*- c++ -*-
/*
* Copyright (c) 2010-2018, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#include <numeric>
#include "catch2/catch.hpp"
#define NDARRAY_ASSERT_AUDIT_ENABLED true
#include "ndarray/detail/Layout.hpp"
using namespace ndarray;
namespace {
template <Size N>
void check_get_dim(
detail::Layout<N> const & layout,
std::array<Size, N> const & shape,
std::array<Offset, N> const & strides,
std::integral_constant<Size, N>
) {
// empty specialization to break check_get_dim recursion (below)
}
template <Size P, Size N>
void check_get_dim(
detail::Layout<N> const & layout,
std::array<Size, N> const & shape,
std::array<Offset, N> const & strides,
std::integral_constant<Size, P>
) {
REQUIRE(detail::get_dim<P>(layout).size() == shape[P]);
REQUIRE(detail::get_dim<P>(layout).stride() == strides[P]);
check_get_dim(layout, shape, strides, std::integral_constant<Size, P+1>());
}
template <Size N>
void check_layout(
detail::Layout<N> const & layout,
std::array<Size, N> const & shape,
std::array<Offset, N> const & strides
) {
REQUIRE(layout == *detail::Layout<N>::make(shape, strides));
REQUIRE(layout.shape() == shape);
REQUIRE(layout.strides() == strides);
REQUIRE(layout.full_size() == std::accumulate(shape.begin(), shape.end(),
static_cast<Size>(1), std::multiplies<Size>()));
Size i = 0;
layout.for_each_dim(
[&i, &shape, &strides](auto const & d) {
REQUIRE(d.size() == shape[i]);
REQUIRE(d.stride() == strides[i] );
++i;
return true;
}
);
Size j = N - 1;
layout.for_each_dim_r(
[&j, &shape, &strides](auto const & d) {
REQUIRE(d.size() == shape[j]);
REQUIRE(d.stride() == strides[j]);
--j;
return true;
}
);
check_get_dim(layout, shape, strides, std::integral_constant<Size, 0>());
}
} // <anonymous>
TEST_CASE("detail::Layout: automatic row-major contiguous strides", "[detail][Layout]") {
Error::ScopedHandler errors(&Error::throw_handler<std::logic_error>);
Size const element_size = 2;
std::array<Size, 3> shape = {3, 4, 5};
std::array<Offset, 3> strides = {40, 10, 2};
auto layout = detail::Layout<3>::make(shape, element_size, MemoryOrder::ROW_MAJOR);
check_layout(*layout, shape, strides);
REQUIRE_NOTHROW(layout->check_contiguousness<3>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<2>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<1>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<0>(element_size));
REQUIRE_THROWS_AS(layout->check_contiguousness<-1>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<-2>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<-3>(element_size), std::logic_error);
}
TEST_CASE("detail::Layout: explicit partially row-major strides", "[detail][Layout]") {
Error::ScopedHandler errors(&Error::throw_handler<std::logic_error>);
Size const element_size = 2;
std::array<Size, 3> shape = {3, 4, 5};
std::array<Offset, 3> strides = {80, 10, 2};
auto layout = detail::Layout<3>::make(shape, strides);
check_layout(*layout, shape, strides);
REQUIRE_THROWS_AS(layout->check_contiguousness<3>(element_size), std::logic_error);
REQUIRE_NOTHROW(layout->check_contiguousness<2>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<1>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<0>(element_size));
REQUIRE_THROWS_AS(layout->check_contiguousness<-1>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<-2>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<-3>(element_size), std::logic_error);
}
TEST_CASE("detail::Layout: automatic column-major contiguous strides", "[detail][Layout]") {
Error::ScopedHandler errors(&Error::throw_handler<std::logic_error>);
Size const element_size = 2;
std::array<Size, 3> shape = {3, 4, 5};
std::array<Offset, 3> strides = {2, 6, 24};
auto layout = detail::Layout<3>::make(shape, element_size, MemoryOrder::COL_MAJOR);
check_layout(*layout, shape, strides);
REQUIRE_THROWS_AS(layout->check_contiguousness<3>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<2>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<1>(element_size), std::logic_error);
REQUIRE_NOTHROW(layout->check_contiguousness<0>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<-1>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<-2>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<-3>(element_size));
}
TEST_CASE("detail::Layout: explicit partially column-major strides", "[detail][Layout]") {
Error::ScopedHandler errors(&Error::throw_handler<std::logic_error>);
Size const element_size = 2;
std::array<Size, 3> shape = {3, 4, 5};
std::array<Offset, 3> strides = {2, 6, 48};
auto layout = detail::Layout<3>::make(shape, strides);
check_layout(*layout, shape, strides);
REQUIRE_THROWS_AS(layout->check_contiguousness<3>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<2>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<1>(element_size), std::logic_error);
REQUIRE_NOTHROW(layout->check_contiguousness<0>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<-1>(element_size));
REQUIRE_NOTHROW(layout->check_contiguousness<-2>(element_size));
REQUIRE_THROWS_AS(layout->check_contiguousness<-3>(element_size), std::logic_error);
}
TEST_CASE("detail::Layout: explicit noncontiguous strides", "[detail][Layout]") {
Error::ScopedHandler errors(&Error::throw_handler<std::logic_error>);
Size const element_size = 2;
std::array<Size, 3> shape = {3, 4, 5};
std::array<Offset, 3> strides = {4, 60, 12};
auto layout = detail::Layout<3>::make(shape, strides);
check_layout(*layout, shape, strides);
REQUIRE_THROWS_AS(layout->check_contiguousness<3>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<2>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<1>(element_size), std::logic_error);
REQUIRE_NOTHROW(layout->check_contiguousness<0>(element_size));
REQUIRE_THROWS_AS(layout->check_contiguousness<-1>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<-2>(element_size), std::logic_error);
REQUIRE_THROWS_AS(layout->check_contiguousness<-3>(element_size), std::logic_error);
}