-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest-matrix.cpp
More file actions
56 lines (42 loc) · 1.6 KB
/
test-matrix.cpp
File metadata and controls
56 lines (42 loc) · 1.6 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
#include <testthat.h>
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/matrix.hpp"
context("matrix-C++") {
test_that("matrix dim attributes are correct for writable matrices") {
cpp11::writable::doubles_matrix x(5, 2);
cpp11::integers dim(SEXP(x.attr("dim")));
expect_true(dim[0] == 5);
expect_true(dim[1] == 2);
expect_true(x.nrow() == 5);
expect_true(x.ncol() == 2);
}
test_that("matrix dim attributes are correct for read only matrices") {
auto getExportedValue = cpp11::package("base")["getExportedValue"];
cpp11::doubles_matrix x(SEXP(getExportedValue("datasets", "volcano")));
expect_true(x.size() == 5307);
expect_true(x.nrow() == 87);
expect_true(x.ncol() == 61);
}
test_that("row based subsetting works") {
auto getExportedValue = cpp11::package("base")["getExportedValue"];
cpp11::doubles_matrix x(SEXP(getExportedValue("datasets", "volcano")));
auto r = x[0];
expect_true(r[0] == 100);
expect_true(r[60] == 103);
}
test_that("index based subsetting works") {
auto getExportedValue = cpp11::package("base")["getExportedValue"];
cpp11::doubles_matrix x(SEXP(getExportedValue("datasets", "volcano")));
expect_true(x(0, 0) == 100);
expect_true(x(0, 60) == 103);
}
test_that("copy constructor works") {
auto getExportedValue = cpp11::package("base")["getExportedValue"];
cpp11::doubles_matrix x(SEXP(getExportedValue("datasets", "volcano")));
cpp11::doubles_matrix y(x);
expect_true(x.nrow() == y.nrow());
expect_true(SEXP(x) == SEXP(y));
}
}