Skip to content

Commit 511a7ed

Browse files
committed
re-add complex matrix with tests
1 parent a91c3ed commit 511a7ed

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

cpp11test/src/test-matrix.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "cpp11/complexes.hpp"
12
#include "cpp11/doubles.hpp"
23
#include "cpp11/function.hpp"
34
#include "cpp11/integers.hpp"
@@ -24,6 +25,7 @@ context("matrix-C++") {
2425
expect_true(x[1].size() == 2);
2526
expect_true(x[1].stride() == 5);
2627
}
28+
2729
test_that("matrix dim attributes are correct for read only matrices") {
2830
auto getExportedValue = cpp11::package("base")["getExportedValue"];
2931

@@ -41,6 +43,7 @@ context("matrix-C++") {
4143
expect_true(x[1].size() == 61);
4244
expect_true(x[1].stride() == 87);
4345
}
46+
4447
test_that("matrix<by_column> attributes are correct") {
4548
cpp11::doubles_matrix<cpp11::by_column> x(getExportedValue("datasets", "volcano"));
4649

@@ -156,4 +159,40 @@ context("matrix-C++") {
156159
cpp11::writable::doubles_matrix<cpp11::by_row> x(5, 2);
157160
expect_error(cpp11::writable::integers_matrix<cpp11::by_column>(x));
158161
}
162+
163+
test_that("complex objects can be created, filled, and copied") {
164+
// vector
165+
166+
cpp11::writable::complexes v(2);
167+
v[0] = std::complex<double>(1, 2);
168+
v[1] = std::complex<double>(3, 4);
169+
170+
cpp11::complexes vc = v;
171+
172+
expect_true(v.size() == vc.size());
173+
174+
for (int i = 0; i < 2; ++i) {
175+
expect_true(v[i] == vc[i]);
176+
}
177+
178+
// matrix
179+
180+
cpp11::writable::complexes_matrix<cpp11::by_row> m(5, 2);
181+
182+
for (int i = 0; i < 5; ++i) {
183+
for (int j = 0; j < 2; ++j) {
184+
m(i, j) = std::complex<double>(i, j);
185+
}
186+
}
187+
188+
cpp11::complexes_matrix<> mc = m;
189+
expect_true(m.nrow() == mc.nrow());
190+
expect_true(m.ncol() == mc.ncol());
191+
192+
for (int i = 0; i < 5; ++i) {
193+
for (int j = 0; j < 2; ++j) {
194+
expect_true(m(i, j) == mc(i, j));
195+
}
196+
}
197+
}
159198
}

inst/include/cpp11/matrix.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
#include <iterator>
44
#include <string> // for string
55

6-
#include "cpp11/R.hpp" // for SEXP, SEXPREC, R_xlen_t, INT...
7-
#include "cpp11/r_bool.hpp" // for r_bool
8-
#include "cpp11/r_string.hpp" // for r_string
9-
#include "cpp11/r_vector.hpp" // for r_vector
10-
#include "cpp11/sexp.hpp" // for sexp
6+
#include "cpp11/R.hpp" // for SEXP, SEXPREC, R_xlen_t, INT...
7+
#include "cpp11/r_bool.hpp" // for r_bool
8+
#include "cpp11/r_complex.hpp" // for r_complex
9+
#include "cpp11/r_string.hpp" // for r_string
10+
#include "cpp11/r_vector.hpp" // for r_vector
11+
#include "cpp11/sexp.hpp" // for sexp
1112

1213
namespace cpp11 {
1314

@@ -214,6 +215,8 @@ template <typename S = by_column>
214215
using logicals_matrix = matrix<r_vector<r_bool>, r_bool, S>;
215216
template <typename S = by_column>
216217
using strings_matrix = matrix<r_vector<r_string>, r_string, S>;
218+
template <typename S = by_column>
219+
using complexes_matrix = matrix<r_vector<r_complex>, r_complex, S>;
217220

218221
namespace writable {
219222
template <typename S = by_column>
@@ -224,6 +227,8 @@ template <typename S = by_column>
224227
using logicals_matrix = matrix<r_vector<r_bool>, r_vector<r_bool>::proxy, S>;
225228
template <typename S = by_column>
226229
using strings_matrix = matrix<r_vector<r_string>, r_vector<r_string>::proxy, S>;
230+
template <typename S = by_column>
231+
using complexes_matrix = matrix<r_vector<r_complex>, r_vector<r_complex>::proxy, S>;
227232
} // namespace writable
228233

229234
// TODO: Add tests for Matrix class

0 commit comments

Comments
 (0)