-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_xvariable_function.cpp
More file actions
235 lines (204 loc) · 8.28 KB
/
test_xvariable_function.cpp
File metadata and controls
235 lines (204 loc) · 8.28 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* *
* 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 "test_fixture.hpp"
namespace xf
{
struct xfunction_features
{
variable_type m_a;
variable_type m_b;
xfunction_features();
};
xfunction_features::xfunction_features()
{
m_a = make_test_variable();
m_b = make_test_variable2();
}
TEST(xvariable_function, size)
{
xfunction_features f;
std::size_t s1 = (f.m_a + f.m_a).size();
EXPECT_EQ(s1, 9u);
std::size_t s2 = (f.m_a + f.m_b).size();
EXPECT_EQ(s2, 12u);
std::size_t s3 = (f.m_a + f.m_a).template size<join::outer>();
EXPECT_EQ(s3, 9u);
std::size_t s4 = (f.m_a + f.m_b).template size<join::outer>();
EXPECT_EQ(s4, 48u);
}
TEST(xvariable_function, dimension)
{
xfunction_features f;
std::size_t s1 = (f.m_a + f.m_a).dimension();
EXPECT_EQ(s1, 2u);
std::size_t s2 = (f.m_a + f.m_b).dimension();
EXPECT_EQ(s2, 3u);
std::size_t s3 = (f.m_a + f.m_a).template dimension<join::outer>();
EXPECT_EQ(s3, 2u);
std::size_t s4 = (f.m_a + f.m_b).template dimension<join::outer>();
EXPECT_EQ(s4, 3u);
}
TEST(xvariable_function, dimension_labels)
{
xfunction_features f;
auto l1 = (f.m_a + f.m_a).dimension_labels();
EXPECT_EQ(l1, f.m_a.dimension_labels());
auto l2 = (f.m_a + f.m_a).dimension_labels<join::outer>();
EXPECT_EQ(l2, f.m_a.dimension_labels());
auto l3 = (f.m_a + f.m_b).dimension_labels();
EXPECT_EQ(l3, f.m_b.dimension_labels());
auto l4 = (f.m_a + f.m_b).dimension_labels<join::outer>();
EXPECT_EQ(l4, f.m_b.dimension_labels());
}
TEST(xvariable_function, coordinates)
{
xfunction_features f;
auto c1 = (f.m_a + f.m_a).coordinates();
EXPECT_EQ(c1, f.m_a.coordinates());
auto c2 = (f.m_a + f.m_a).coordinates<join::outer>();
EXPECT_EQ(c2, f.m_a.coordinates());
auto c3 = (f.m_a + f.m_b).coordinates();
EXPECT_EQ(c3, make_intersect_coordinate());
auto c4 = (f.m_a + f.m_b).coordinates<join::outer>();
EXPECT_EQ(c4, make_merge_coordinate());
}
TEST(xvariable_function, dimension_mapping)
{
xfunction_features f;
auto d1 = (f.m_a + f.m_a).dimension_mapping();
EXPECT_EQ(d1, f.m_a.dimension_mapping());
auto d2 = (f.m_a + f.m_a).dimension_mapping<join::outer>();
EXPECT_EQ(d2, f.m_a.dimension_mapping());
auto d3 = (f.m_a + f.m_b).dimension_mapping();
EXPECT_EQ(d3, f.m_b.dimension_mapping());
auto d4 = (f.m_a + f.m_b).dimension_mapping<join::outer>();
EXPECT_EQ(d4, f.m_b.dimension_mapping());
}
TEST(xvariable_function, shape)
{
xfunction_features f;
using shape_type = std::decay_t<decltype(f.m_a.shape())>;
auto s1 = (f.m_a + f.m_a).shape();
EXPECT_EQ(s1, shape_type({ 3, 3 }));
auto s2 = (f.m_a + f.m_b).shape();
EXPECT_EQ(s2, shape_type({ 3, 3, 3 }));
}
TEST(xvariable_function, access)
{
xfunction_features f;
std::size_t i = f.m_a.data().shape()[0] - 1;
std::size_t j = f.m_a.data().shape()[1] - 1;
{
SCOPED_TRACE("same shape");
xtl::xoptional<double> a = (f.m_a + f.m_a)(i, j);
xtl::xoptional<double> b = f.m_a(i, j) + f.m_a(i, j);
EXPECT_EQ(a, b);
}
{
SCOPED_TRACE("different shape");
xtl::xoptional<double> a = (f.m_a + f.m_b)(1, i, j);
xtl::xoptional<double> b = f.m_a(1, i, j) + f.m_b(1, i, j);
EXPECT_EQ(a, b);
}
}
TEST(xvariable_function, broadcast_coordinate)
{
xfunction_features f;
coordinate_type c1;
xtrivial_broadcast res1 = (f.m_a + f.m_a).broadcast_coordinates(c1);
EXPECT_EQ(c1, f.m_a.coordinates());
EXPECT_TRUE(res1.m_same_dimensions);
EXPECT_TRUE(res1.m_same_labels);
coordinate_type c2;
xtrivial_broadcast res2 = (f.m_a + f.m_a).broadcast_coordinates<join::outer>(c2);
EXPECT_EQ(c2, f.m_a.coordinates());
EXPECT_TRUE(res2.m_same_dimensions);
EXPECT_TRUE(res2.m_same_labels);
coordinate_type c3;
xtrivial_broadcast res3 = (f.m_a + f.m_b).broadcast_coordinates(c3);
EXPECT_EQ(c3, make_intersect_coordinate());
EXPECT_FALSE(res3.m_same_dimensions);
EXPECT_FALSE(res3.m_same_labels);
coordinate_type c4;
xtrivial_broadcast res4 = (f.m_a + f.m_b).broadcast_coordinates<join::outer>(c4);
EXPECT_EQ(c4, make_merge_coordinate());
EXPECT_FALSE(res4.m_same_dimensions);
EXPECT_FALSE(res4.m_same_labels);
}
TEST(xvariable_function, select_inner)
{
xfunction_features f;
{
SCOPED_TRACE("same shape");
xtl::xoptional<double> a = (f.m_a + f.m_a).select({{"abscissa", "d"}, {"ordinate", 4}});
xtl::xoptional<double> b = 2 * f.m_a.select({{"abscissa", "d"}, {"ordinate", 4}});
EXPECT_EQ(a, b);
EXPECT_ANY_THROW((f.m_a + f.m_a).select({{"abscissa", "e"}, {"ordinate", 4}}));
}
{
SCOPED_TRACE("different shape");
xtl::xoptional<double> a = (f.m_a + f.m_b).select({{"abscissa", "d"}, {"ordinate", 4}, {"altitude", 2}});
xtl::xoptional<double> b = f.m_a.select({{"abscissa", "d"}, {"ordinate", 4}, {"altitude", 2}}) +
f.m_b.select({{"abscissa", "d"}, {"ordinate", 4}, {"altitude", 2}});
EXPECT_EQ(a, b);
EXPECT_ANY_THROW((f.m_a + f.m_b).select({{"abscissa", "e"}, {"ordinate", 4}, {"altitude", 2}}));
}
}
TEST(xvariable_function, select_outer)
{
xfunction_features f;
{
SCOPED_TRACE("same shape");
xtl::xoptional<double> a = (f.m_a + f.m_a).select<join::outer>({{"abscissa", "d"}, {"ordinate", 4}});
xtl::xoptional<double> b = 2 * f.m_a.select<join::outer>({{"abscissa", "d"}, {"ordinate", 4}});
EXPECT_EQ(a, b);
EXPECT_EQ((f.m_a + f.m_a).select<join::outer>({{"abscissa", "e"}, {"ordinate", 4}}), f.m_a.missing());
}
{
SCOPED_TRACE("different shape");
xtl::xoptional<double> a = (f.m_a + f.m_b).select<join::outer>({{"abscissa", "d"}, {"ordinate", 4}, {"altitude", 2}});
xtl::xoptional<double> b = f.m_a.select<join::outer>({{"abscissa", "d"}, {"ordinate", 4}, {"altitude", 2}}) +
f.m_b.select<join::outer>({{"abscissa", "d"}, {"ordinate", 4}, {"altitude", 2}});
EXPECT_EQ(a, b);
EXPECT_EQ((f.m_a + f.m_b).select<join::outer>({{"abscissa", "e"}, {"ordinate", 4}, {"altitude", 2}}), f.m_a.missing());
}
}
TEST(xvariable_function, print)
{
auto a = variable_type(
make_test_data(),
{
{ "day", xf::axis({ "Monday", "Tuesday", "Wednesday" }) },
{ "city", xf::axis({ "London", "Paris", "Brussels" }) }
}
);
data_type db = { 1., 2., 3. };
auto b = variable_type(
db,
{
{ "city", xf::axis({ "Paris", "London", "Brussels" }) }
}
);
auto f = a + b;
std::string expected =
R"variable({{ 3, 3, N/A},
{N/A, 6, 9},
{ 9, 9, 12}}
Coordinates:
day : (Monday, Tuesday, Wednesday, )
city: (London, Paris, Brussels, )
)variable";
std::ostringstream oss;
oss << f;
std::string res = oss.str();
EXPECT_EQ(res, expected);
}
}