forked from lloda/ra-ra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-iterator-small.C
96 lines (87 loc) · 3.33 KB
/
test-iterator-small.C
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
// (c) Daniel Llorens - 2014, 2016
// 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.
/// @file test-iterator-small.C
/// @brief WIP: creating a higher-rank iterator for SmallArray/SmallView.
#include <iostream>
#include <iterator>
#include "ra/complex.H"
#include "ra/small.H"
#include "ra/operators.H"
#include "ra/io.H"
#include "ra/big.H"
#include "ra/small.H"
#include "ra/format.H"
#include "ra/test.H"
#include "ra/mpdebug.H"
using std::cout; using std::endl; using std::flush;
int main()
{
TestRecorder tr;
{
tr.section("fiddling");
{
using A = ra::Small<int, 2, 3>;
A a = {{1, 2, 3}, {4, 5, 6}};
cout << a << endl;
using AI0 = typename A::iterator<0>;
cout << "AI0 " << std::is_same<int, AI0>::value << endl;
cout << AI0::rank() << endl;
using AI1 = typename A::iterator<1>;
cout << "AI1 " << std::is_same<int, AI1>::value << endl;
cout << AI1::rank() << endl;
AI0 bi(a.data());
cout << bi.c.p << endl;
}
tr.section("STL style, should be a pointer for default strides");
{
using A = ra::Small<int, 2, 3>;
A a = {{1, 2, 3}, {4, 5, 6}};
tr.test(std::is_same_v<int *, decltype(a.begin())>);
int i = 0;
for (auto && ai: a) {
tr.test_eq(i+1, ai);
++i;
}
}
tr.section("STL style with non-default strides, which keeps indices (as internal detail)");
{
ra::Small<int, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
auto b = transpose<1, 0>(a);
tr.test_eq(ra::start({0, 0}), ra::start(b.begin().i));
tr.test_eq(ra::start({0, 1}), ra::start((++b.begin()).i));
tr.test(!std::is_same_v<int *, decltype(b.begin())>);
int bref[6] = {1, 4, 2, 5, 3, 6};
int i = 0;
for (auto && bi: b) {
tr.test_eq(bref[i], bi);
++i;
}
}
tr.section("rank>0");
{
auto test_over
= [&](auto && a)
{
auto test = [&](std::string ref, auto cr)
{
std::ostringstream o;
for_each([&o](auto && a) { o << a << "|"; }, iter<decltype(cr)::value>(a));
tr.test_eq(ra::scalar(ref), ra::scalar(o.str()));
};
test("1|2|3|4|5|6|", mp::int_t<-2>());
test("1 2 3|4 5 6|", mp::int_t<-1>());
test("1|2|3|4|5|6|", mp::int_t<0>());
test("1 2 3|4 5 6|", mp::int_t<1>());
test("1 2 3\n4 5 6|", mp::int_t<2>());
};
tr.section("default strides");
test_over(ra::Small<int, 2, 3> {{1, 2, 3}, {4, 5, 6}});
tr.section("non-default strides");
test_over(ra::transpose<1, 0>(ra::Small<int, 3, 2> {{1, 4}, {2, 5}, {3, 6}}));
}
}
return tr.summary();
}