-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_zfunction.cpp
More file actions
164 lines (121 loc) · 4.76 KB
/
test_zfunction.cpp
File metadata and controls
164 lines (121 loc) · 4.76 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
#include "test_common.hpp"
#include <zarray/zarray.hpp>
#include <xtl/xplatform.hpp>
#include <xtl/xhalf_float.hpp>
TEST_SUITE_BEGIN("zfunction");
namespace xt
{
TEST(zfunction, shape)
{
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> b = {0.5, 1.5};
zarray za(a);
zarray zb(b);
using zfunction_type = zfunction<detail::plus, const zarray&, const zarray&>;
zfunction_type f(zplus(), za, zb);
auto dim = f.dimension();
EXPECT_EQ(dim, a.dimension());
auto shape = f.shape();
EXPECT_EQ(shape, a.shape());
decltype(shape) shape2 = {1u, 2u};
f.broadcast_shape(shape2);
EXPECT_EQ(shape2, a.shape());
}
TEST(zfunction, dispatching)
{
using dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
dispatcher_type::init();
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> expa = {{std::exp(0.5), std::exp(1.5)}, {std::exp(2.5), std::exp(3.5)}};
auto res = xarray<double>::from_shape({2, 2});
zarray za(a);
zarray zres(res);
zassign_args args;
dispatcher_type::dispatch(za.get_implementation(), zres.get_implementation(), args);
EXPECT_EQ(expa, res);
}
TEST(zfunction, assign_to)
{
using exp_dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
exp_dispatcher_type::init();
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
using nested_zfunction_type = zfunction<math::exp_fun, const zarray&>;
using zfunction_type = zfunction<detail::plus, const zarray&, nested_zfunction_type>;
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> b = {{-0.2, 2.4}, {1.3, 4.7}};
auto res = xarray<double>::from_shape({2, 2});
zarray za(a);
zarray zb(b);
zarray zres(res);
zfunction_type f(zplus(), za, nested_zfunction_type(zexp(), zb));
zassign_args args;
f.assign_to(zres.get_implementation(), args);
auto expected = xarray<double>::from_shape({2, 2});
std::transform(a.cbegin(), a.cend(), b.cbegin(), expected.begin(),
[](const double& lhs, const double& rhs) { return lhs + std::exp(rhs); });
EXPECT_TRUE(all(isclose(res, expected)));
size_t res_index = f.get_result_type_index();
EXPECT_EQ(res_index, ztyped_array<double>::get_class_static_index());
}
TEST(zfunction, math_operator)
{
using exp_dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
exp_dispatcher_type::init();
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> b = {{-0.2, 2.4}, {1.3, 4.7}};
auto res = xarray<double>::from_shape({2, 2});
zarray za(a);
zarray zb(b);
zarray zres(res);
auto f = za + xt::exp(zb);
zassign_args args;
f.assign_to(zres.get_implementation(), args);
auto expected = xarray<double>::from_shape({2, 2});
std::transform(a.cbegin(), a.cend(), b.cbegin(), expected.begin(),
[](const double& lhs, const double& rhs) { return lhs + std::exp(rhs); });
EXPECT_TRUE(all(isclose(res, expected)));
}
TEST(zfunction, scalar)
{
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
xarray<double> a = {{1., 2.}, {3., 4.}};
xarray<double> res;
zarray za(a);
zarray zres(res);
auto f = za + 2.;
using expected_type = zfunction<detail::plus, const zarray&, zscalar_wrapper<xscalar<double>>>;
EXPECT_TRUE((std::is_same<decltype(f), expected_type>::value));
zres = f;
xarray<double> expected = {{3., 4.}, {5., 6.}};
EXPECT_EQ(zres.get_array<double>(), expected);
}
TEST(zfunction, broadcasting)
{
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
xarray<double> a = {{1., 2.}, {3., 4.}};
xarray<double> b = {1., 2.};
xarray<double> expected = a + b;
zarray za(a);
zarray zb(b);
zarray zres = za + zb;
EXPECT_EQ(zres.get_array<double>(), expected);
}
TEST_CASE("bad_function_call")
{
zdispatcher_t<detail::plus, 2>::init();
zdispatcher_t<detail::xassign_dummy_functor, 1>::init();
auto x0 = xarray<int>::from_shape({2,2});
auto x1 = xarray<int>::from_shape({2,2});
zarray z0(x0);
zarray z1(x1);
auto res = xarray<double>::from_shape({2,2});
zarray zres(res);
zres = z0 + z1;
}
}
TEST_SUITE_END();