-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.cc
221 lines (209 loc) · 10.4 KB
/
types.cc
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/test - Show what types conform to what type predicates.
// (c) Daniel Llorens - 2016-2017
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#include <chrono>
#include <numeric>
#include <typeinfo>
#include <iostream>
#include "ra/test.hh"
#include "mpdebug.hh"
#include <stdfloat>
using std::cout, std::endl, std::flush, ra::TestRecorder;
using ra::mp::int_list, ra::mp::nil;
template <class A>
void
test_predicates(char const * type, TestRecorder & tr,
bool ra, bool slice, bool iterator, bool scalar, bool fov)
{
cout << std::string(90-size(std::string(type)), ' ') << type << " : "
<< (ra::is_ra<A> ? "ra " : "")
<< (ra::SliceConcept<A> ? "slice " : "")
<< (ra::IteratorConcept<A> ? "iterator " : "")
<< (ra::is_scalar<A> ? "scalar " : "")
<< (ra::is_fov<A> ? "fov " : "")
<< (ra::is_builtin_array<A> ? "builtinarray " : "")
<< (std::ranges::range<A> ? "range " : "")
<< (std::is_const_v<A> ? "const " : "")
<< (std::is_lvalue_reference_v<A> ? "ref " : "") << endl;
tr.quiet().info(type).info("ra").test_eq(ra, ra::is_ra<A>);
tr.quiet().info(type).info("slice").test_eq(slice, ra::SliceConcept<A>);
tr.quiet().info(type).info("Iterator").test_eq(iterator, ra::IteratorConcept<A>);
tr.quiet().info(type).info("scalar").test_eq(scalar, ra::is_scalar<A>);
tr.quiet().info(type).info("fov").test_eq(fov, ra::is_fov<A>);
tr.quiet().info(type).info("std::ranges::range").test_eq(std::ranges::range<A>, std::ranges::range<A>);
}
// Not registered with is_scalar_def.
struct Unreg { int x; };
// prefer decltype(declval(...)) to https://stackoverflow.com/a/13842784 the latter drops const
#define TESTPRED(A, ...) test_predicates <A> (STRINGIZE(A), tr __VA_OPT__(,) __VA_ARGS__)
int main()
{
TestRecorder tr(std::cout, TestRecorder::NOISY);
{
using T = std::chrono::duration<long int, std::ratio<1, 1000000000>>;
ra::Big<T, 1> a;
static_assert(std::is_same_v<ra::value_t<decltype(a)>, T>);
ra::Small<T const, 1> ac;
static_assert(std::is_same_v<ra::value_t<decltype(ac)>, T const>);
static_assert(std::is_same_v<ra::ncvalue_t<decltype(ac)>, T>);
ra::Small<T, 1> an;
static_assert(std::is_same_v<ra::value_t<decltype(an)>, T>);
static_assert(std::is_same_v<ra::ncvalue_t<decltype(an)>, T>);
}
{
TESTPRED(decltype(std::declval<ra::ViewSmall<ra::Dim, int_list<3>, int_list<1>>>()),
true, true, false, false, false);
TESTPRED(int,
false, false, false, true, false);
TESTPRED(ra::int_c<3>,
false, false, false, true, false);
TESTPRED(ra::dim_c<0>,
false, false, false, true, false);
TESTPRED(std::complex<double>,
false, false, false, true, false);
#ifdef __STDCPP_FLOAT128_T__
TESTPRED(std::float128_t,
false, false, false, true, false);
TESTPRED(std::complex<std::float128_t>,
false, false, false, true, false);
#endif // __STDCPP_FLOAT128_T__
TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()),
true, true, false, false, false);
TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()) const,
true, true, false, false, false);
TESTPRED(decltype(std::declval<ra::Unique<int, 2>>()) &,
true, true, false, false, false);
TESTPRED(decltype(std::declval<ra::ViewBig<int, 2>>()),
true, true, false, false, false);
TESTPRED(decltype(ra::Unique<int, 2>().iter()),
true, false, true, false, false);
static_assert(ra::IteratorConcept<decltype(ra::Unique<int, 2>().iter())>);
TESTPRED(decltype(ra::Unique<int, 1>().iter()) &,
true, false, true, false, false);
TESTPRED(decltype(ra::iota(5)),
true, false, true, false, false);
TESTPRED(decltype(ra::iota<0>()),
true, false, true, false, false);
// is_iterator (by RA_IS_DEF) but not IteratorConcept, since it cannot be traversed.
TESTPRED(decltype(ra::iota<0>()) const,
true, false, false, false, false);
TESTPRED(decltype(ra::iota<0>()) &,
true, false, true, false, false);
TESTPRED(decltype(std::declval<ra::Small<int, 2>>()),
true, true, false, false, false);
TESTPRED(decltype(ra::Small<int, 2>().iter()),
true, false, true, false, false);
TESTPRED(decltype(ra::Small<int, 2, 2>()()),
true, true, false, false, false);
TESTPRED(decltype(ra::Small<int, 2, 2>().iter()),
true, false, true, false, false);
TESTPRED(decltype(ra::Small<int, 2>()+3),
true, false, true, false, false);
TESTPRED(decltype(3+ra::Big<int>()),
true, false, true, false, false);
TESTPRED(std::vector<int>,
false, false, false, false, true);
TESTPRED(decltype(ra::start(std::vector<int> {})),
true, false, true, false, false);
TESTPRED(int *,
false, false, false, false, false);
TESTPRED(decltype(std::ranges::iota_view(-5, 10)),
false, false, false, false, true);
// std::string can be registered as is_scalar or not [ra13]. Use ra::ptr(std::string) or ra::scalar(std::string) to get the other behavior.
if constexpr(ra::is_scalar<std::string>) {
TESTPRED(std::string,
false, false, false, true, false);
} else {
TESTPRED(std::string,
false, false, false, false, true);
}
TESTPRED(Unreg,
false, false, false, false, false);
TESTPRED(int [4],
false, false, false, false, false);
TESTPRED(int (&) [3],
false, false, false, false, false);
TESTPRED(decltype("cstring"),
false, false, false, false, false);
}
tr.section("establish meaning of selectors (TODO / convert to TestRecorder)");
{
static_assert(ra::SliceConcept<ra::ViewBig<int, 0>>);
static_assert(ra::SliceConcept<ra::ViewBig<int, 2>>);
static_assert(ra::SliceConcept<ra::ViewSmall<int, int_list<>, int_list<>>>);
static_assert(ra::is_ra<ra::Small<int>>, "bad is_ra Small");
static_assert(ra::is_ra<ra::ViewSmall<int, nil, nil>>, "bad is_ra ViewSmall");
static_assert(ra::is_ra<ra::Unique<int, 0>>, "bad is_ra Unique");
static_assert(ra::is_ra<ra::ViewBig<int, 0>>, "bad is_ra View");
static_assert(ra::is_ra<ra::Small<int, 1>>, "bad is_ra Small");
static_assert(ra::is_ra<ra::ViewSmall<int, int_list<1>, int_list<1>>>, "bad is_ra ViewSmall");
static_assert(ra::is_ra<ra::Unique<int, 1>>, "bad is_ra Unique");
static_assert(ra::is_ra<ra::ViewBig<int, 1>>, "bad is_ra View");
static_assert(ra::is_ra<ra::ViewBig<int>>, "bad is_ra View");
using Scalar = decltype(ra::scalar(3));
using Vector = decltype(ra::start({1, 2, 3}));
static_assert(ra::is_ra_scalar<Scalar>, "bad is_ra_scalar Scalar");
static_assert(ra::is_ra<decltype(ra::scalar(3))>, "bad is_ra Scalar");
static_assert(ra::is_ra<Vector>, "bad is_ra Vector");
static_assert(!ra::is_ra<int *>, "bad is_ra int *");
static_assert(ra::is_scalar<double>, "bad is_scalar real");
static_assert(ra::is_scalar<std::complex<double>>, "bad is_scalar complex");
static_assert(ra::is_scalar<int>, "bad is_scalar int");
static_assert(!ra::is_scalar<decltype(ra::scalar(3))>, "bad is_scalar Scalar");
static_assert(!ra::is_scalar<Vector>, "bad is_scalar Scalar");
static_assert(!ra::is_scalar<decltype(ra::start(3))>, "bad is_scalar Scalar");
int a = 3;
static_assert(!ra::is_scalar<decltype(ra::start(a))>, "bad is_scalar Scalar");
// a regression.
static_assert(ra::is_zero_or_scalar<ra::Scalar<int>>, "bad");
static_assert(!ra::is_ra_pos<ra::Scalar<int>>, "bad");
static_assert(!ra::is_zero_or_scalar<decltype(ra::iota<0>())>, "bad");
static_assert(ra::is_ra_pos<decltype(ra::iota<0>())>, "bad");
static_assert(ra::is_ra_pos<ra::Expr<std::multiplies<>, std::tuple<decltype(ra::iota<0>()), ra::Scalar<int>>>>, "bad");
static_assert(ra::is_ra_pos<ra::Pick<std::tuple<Vector, Vector, Vector>>>, "bad");
}
tr.section("builtin arrays");
{
int const a[] = {1, 2, 3};
int b[] = {1, 2, 3};
int c[][2] = {{1, 2}, {4, 5}};
static_assert(ra::is_builtin_array<decltype(a) &>);
static_assert(ra::is_builtin_array<decltype(a)>);
static_assert(ra::is_builtin_array<decltype(b) &>);
static_assert(ra::is_builtin_array<decltype(b)>);
static_assert(ra::is_builtin_array<decltype(c) &>);
static_assert(ra::is_builtin_array<decltype(c)>);
tr.test_eq(1, ra::rank_s(a));
tr.test_eq(1, ra::rank_s(b));
tr.test_eq(2, ra::rank_s(c));
tr.test_eq(3, ra::size(a));
tr.test_eq(3, ra::size(b));
tr.test_eq(4, ra::size(c));
tr.test_eq(3, ra::size_s(a));
tr.test_eq(3, ra::size_s(b));
tr.test_eq(4, ra::size_s(c));
}
tr.section("adaptors I");
{
tr.test_eq(2, size_s(ra::start(std::array<int, 2> { 1, 2 })));
tr.test_eq(2, ra::size_s(std::array<int, 2> { 1, 2 }));
tr.test_eq(ra::ANY, ra::size_s(ra::start(std::vector<int> { 1, 2, 3})));
tr.test_eq(ra::ANY, ra::size_s(std::vector<int> { 1, 2, 3}));
tr.test_eq(2, ra::start(std::array<int, 2> { 1, 2 }).len_s(0));
tr.test_eq(ra::ANY, ra::start(std::vector<int> { 1, 2, 3 }).len_s(0));
tr.test_eq(1, ra::rank_s(ra::start(std::array<int, 2> { 1, 2 })));
tr.test_eq(1, ra::rank_s(ra::start(std::vector<int> { 1, 2, 3 })));
tr.test_eq(1, ra::start(std::array<int, 2> { 1, 2 }).rank());
tr.test_eq(1, ra::start(std::vector<int> { 1, 2, 3 }).rank());
}
tr.section("adaptors II");
{
static_assert(ra::is_iterator<decltype(ra::ptr(std::array { 1, 2 }))>);
static_assert(ra::is_iterator<decltype(ra::start(std::array { 1, 2 }))>);
}
return tr.summary();
}