SOPT
Sparse OPTimisation
wrapper.cc
Go to the documentation of this file.
1 #include <catch2/catch_all.hpp>
2 #include <random>
3 
4 #include "sopt/wrapper.h"
5 
6 TEST_CASE("Function wrappers", "[utility]") {
7  using namespace sopt;
8  using t_Array = Array<int>;
9  using t_RefArray = t_Array&;
10  using t_ConstRefArray = t_Array const;
11 
12  SECTION("Square function") {
13  auto func = [](t_RefArray output, t_ConstRefArray const &input) { output = input * 2 + 1; };
14 
15  t_Array const x = t_Array::Random(5);
16  auto const A = details::wrap<t_Array>(func);
17  // Expected result
18  t_Array const expected = (x * 2 + 1).eval();
19 
20  CHECK((A * x).matrix() == expected.matrix());
21  CHECK(A(x).matrix() == expected.matrix());
22  }
23 
24  SECTION("Rectangular function") {
25  auto func = [](t_RefArray output, t_ConstRefArray const &input) {
26  output = input.head(input.size() / 2) * 2 + 1;
27  };
28 
29  t_Array const x = t_Array::Random(5);
30  auto const A = details::wrap<t_Array>(func, {{1, 2, 0}});
31  // Expected result
32  t_Array const expected = (x.head(x.size() / 2) * 2 + 1).eval();
33 
34  CHECK((A * x).cols() == 1);
35  CHECK((A * x).rows() == 2);
36  CHECK((A * x).matrix() == expected.matrix());
37  CHECK(A(x).matrix() == expected.matrix());
38  }
39 
40  SECTION("Fixed output-size functions") {
41  auto func = [](t_RefArray output, t_ConstRefArray const &input) {
42  output = input.head(3) * 2 + 1;
43  };
44 
45  t_Array const x = t_Array::Random(5);
46  auto const A = details::wrap<t_Array>(func, {{0, 1, 3}});
47  // Expected result
48  t_Array const expected = (x.head(3) * 2 + 1).eval();
49 
50  CHECK((A * x).cols() == 1);
51  CHECK((A * x).rows() == 3);
52  CHECK((A * x).matrix() == expected.matrix());
53  CHECK(A(x).matrix() == expected.matrix());
54  }
55 }
t_uint rows
t_uint cols
Eigen::Array< T, Eigen::Dynamic, 1 > Array
A 1-dimensional list of elements of given type.
Definition: types.h:34
TEST_CASE("Function wrappers", "[utility]")
Definition: wrapper.cc:6