Skip to content

Commit b8a6435

Browse files
committed
simplify headers
1 parent d91ddb0 commit b8a6435

30 files changed

+658
-1694
lines changed

extended-tests/cpp4rtest/R/cpp4r.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ matrix_add <- function(x, y) {
247247
.Call(`_cpp4rtest_matrix_add`, x, y)
248248
}
249249

250+
matrix_add_coerce_test <- function(x, y) {
251+
.Call(`_cpp4rtest_matrix_add_coerce_test`, x, y)
252+
}
253+
254+
matrix_mixed_add <- function(int_mat, dbl_mat) {
255+
.Call(`_cpp4rtest_matrix_mixed_add`, int_mat, dbl_mat)
256+
}
257+
250258
#' @title Protect functions
251259
#' @rdname testing-protect
252260
#' @keywords internal

extended-tests/cpp4rtest/src/cpp4r.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,20 @@ extern "C" SEXP _cpp4rtest_matrix_add(SEXP x, SEXP y) {
403403
return cpp4r::as_sexp(matrix_add(cpp4r::as_cpp<cpp4r::decay_t<const cpp4r::doubles_matrix<>&>>(x), cpp4r::as_cpp<cpp4r::decay_t<const cpp4r::doubles_matrix<>&>>(y)));
404404
END_CPP4R
405405
}
406+
// matrix.h
407+
cpp4r::doubles_matrix<> matrix_add_coerce_test(const cpp4r::doubles_matrix<>& x, const cpp4r::doubles_matrix<>& y);
408+
extern "C" SEXP _cpp4rtest_matrix_add_coerce_test(SEXP x, SEXP y) {
409+
BEGIN_CPP4R
410+
return cpp4r::as_sexp(matrix_add_coerce_test(cpp4r::as_cpp<cpp4r::decay_t<const cpp4r::doubles_matrix<>&>>(x), cpp4r::as_cpp<cpp4r::decay_t<const cpp4r::doubles_matrix<>&>>(y)));
411+
END_CPP4R
412+
}
413+
// matrix.h
414+
cpp4r::doubles_matrix<> matrix_mixed_add(const cpp4r::doubles_matrix<>& int_mat, const cpp4r::doubles_matrix<>& dbl_mat);
415+
extern "C" SEXP _cpp4rtest_matrix_mixed_add(SEXP int_mat, SEXP dbl_mat) {
416+
BEGIN_CPP4R
417+
return cpp4r::as_sexp(matrix_mixed_add(cpp4r::as_cpp<cpp4r::decay_t<const cpp4r::doubles_matrix<>&>>(int_mat), cpp4r::as_cpp<cpp4r::decay_t<const cpp4r::doubles_matrix<>&>>(dbl_mat)));
418+
END_CPP4R
419+
}
406420
// protect.h
407421
void protect_one_(SEXP x, int n);
408422
extern "C" SEXP _cpp4rtest_protect_one_(SEXP x, SEXP n) {
@@ -915,6 +929,8 @@ static const R_CallMethodDef CallEntries[] = {
915929
{"_cpp4rtest_mat_mat_create_dimnames", (DL_FUNC) &_cpp4rtest_mat_mat_create_dimnames, 0},
916930
{"_cpp4rtest_mat_sexp_copy_dimnames", (DL_FUNC) &_cpp4rtest_mat_sexp_copy_dimnames, 1},
917931
{"_cpp4rtest_matrix_add", (DL_FUNC) &_cpp4rtest_matrix_add, 2},
932+
{"_cpp4rtest_matrix_add_coerce_test", (DL_FUNC) &_cpp4rtest_matrix_add_coerce_test, 2},
933+
{"_cpp4rtest_matrix_mixed_add", (DL_FUNC) &_cpp4rtest_matrix_mixed_add, 2},
918934
{"_cpp4rtest_my_message", (DL_FUNC) &_cpp4rtest_my_message, 2},
919935
{"_cpp4rtest_my_message_n1", (DL_FUNC) &_cpp4rtest_my_message_n1, 1},
920936
{"_cpp4rtest_my_message_n1fmt", (DL_FUNC) &_cpp4rtest_my_message_n1fmt, 1},

extended-tests/cpp4rtest/src/matrix.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,40 @@
119119

120120
return result;
121121
}
122+
123+
// Test function specifically for integer matrix coercion
124+
// This takes doubles_matrix<> but should accept integer matrices via implicit coercion
125+
[[cpp4r::register]] cpp4r::doubles_matrix<>
126+
matrix_add_coerce_test(const cpp4r::doubles_matrix<>& x,
127+
const cpp4r::doubles_matrix<>& y) {
128+
int nrow = x.nrow();
129+
int ncol = x.ncol();
130+
131+
cpp4r::writable::doubles_matrix<> result(nrow, ncol);
132+
133+
for (int i = 0; i < nrow; i++) {
134+
for (int j = 0; j < ncol; j++) {
135+
result(i, j) = x(i, j) + y(i, j);
136+
}
137+
}
138+
139+
return result;
140+
}
141+
142+
// Test function for integer + double mixed coercion
143+
[[cpp4r::register]] cpp4r::doubles_matrix<>
144+
matrix_mixed_add(const cpp4r::doubles_matrix<>& int_mat,
145+
const cpp4r::doubles_matrix<>& dbl_mat) {
146+
int nrow = int_mat.nrow();
147+
int ncol = int_mat.ncol();
148+
149+
cpp4r::writable::doubles_matrix<> result(nrow, ncol);
150+
151+
for (int i = 0; i < nrow; i++) {
152+
for (int j = 0; j < ncol; j++) {
153+
result(i, j) = int_mat(i, j) + dbl_mat(i, j);
154+
}
155+
}
156+
157+
return result;
158+
}

extended-tests/cpp4rtest/src/test-matrix.h

Lines changed: 48 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ context("matrix-C++") {
152152
expect_error(cpp4r::writable::integers_matrix<cpp4r::by_column>(x));
153153
}
154154

155-
test_that("as_doubles_matrix coerces integer matrix to double") {
155+
test_that("doubles_matrix implicitly coerces integer matrix to double") {
156156
// Create an integer matrix
157157
SEXP int_mat = PROTECT(Rf_allocMatrix(INTSXP, 2, 3));
158158
INTEGER(int_mat)[0] = 1;
@@ -162,8 +162,8 @@ context("matrix-C++") {
162162
INTEGER(int_mat)[4] = 5;
163163
INTEGER(int_mat)[5] = 6;
164164

165-
// Coerce to doubles
166-
cpp4r::doubles_matrix<> result = cpp4r::as_doubles_matrix<>(int_mat);
165+
// Implicit coercion via constructor
166+
cpp4r::doubles_matrix<> result(int_mat);
167167

168168
expect_true(result.nrow() == 2);
169169
expect_true(result.ncol() == 3);
@@ -180,14 +180,14 @@ context("matrix-C++") {
180180
UNPROTECT(1);
181181
}
182182

183-
test_that("as_doubles_matrix handles NA values correctly") {
183+
test_that("doubles_matrix implicit coercion handles NA values correctly") {
184184
SEXP int_mat = PROTECT(Rf_allocMatrix(INTSXP, 2, 2));
185185
INTEGER(int_mat)[0] = 1;
186186
INTEGER(int_mat)[1] = NA_INTEGER;
187187
INTEGER(int_mat)[2] = 3;
188188
INTEGER(int_mat)[3] = 4;
189189

190-
cpp4r::doubles_matrix<> result = cpp4r::as_doubles_matrix<>(int_mat);
190+
cpp4r::doubles_matrix<> result(int_mat);
191191

192192
expect_true(result(0, 0) == 1.0);
193193
expect_true(cpp4r::is_na(result(1, 0)));
@@ -197,7 +197,7 @@ context("matrix-C++") {
197197
UNPROTECT(1);
198198
}
199199

200-
test_that("as_doubles_matrix preserves dimnames") {
200+
test_that("doubles_matrix implicit coercion preserves dimnames") {
201201
// Create integer matrix with dimnames
202202
SEXP int_mat = PROTECT(Rf_allocMatrix(INTSXP, 2, 2));
203203
INTEGER(int_mat)[0] = 1;
@@ -218,7 +218,7 @@ context("matrix-C++") {
218218
SET_VECTOR_ELT(dimnames, 1, colnames);
219219
Rf_setAttrib(int_mat, R_DimNamesSymbol, dimnames);
220220

221-
cpp4r::doubles_matrix<> result = cpp4r::as_doubles_matrix<>(int_mat);
221+
cpp4r::doubles_matrix<> result(int_mat);
222222

223223
// Check dimnames are preserved
224224
SEXP result_dimnames = Rf_getAttrib(result.data(), R_DimNamesSymbol);
@@ -235,14 +235,14 @@ context("matrix-C++") {
235235
UNPROTECT(4);
236236
}
237237

238-
test_that("as_doubles_matrix handles logical matrix") {
238+
test_that("doubles_matrix implicitly coerces logical matrix") {
239239
SEXP lgl_mat = PROTECT(Rf_allocMatrix(LGLSXP, 2, 2));
240240
LOGICAL(lgl_mat)[0] = TRUE;
241241
LOGICAL(lgl_mat)[1] = FALSE;
242242
LOGICAL(lgl_mat)[2] = TRUE;
243243
LOGICAL(lgl_mat)[3] = NA_LOGICAL;
244244

245-
cpp4r::doubles_matrix<> result = cpp4r::as_doubles_matrix<>(lgl_mat);
245+
cpp4r::doubles_matrix<> result(lgl_mat);
246246

247247
expect_true(result(0, 0) == 1.0);
248248
expect_true(result(1, 0) == 0.0);
@@ -252,109 +252,79 @@ context("matrix-C++") {
252252
UNPROTECT(1);
253253
}
254254

255-
test_that("as_doubles_matrix rejects non-matrix types") {
255+
test_that("doubles_matrix rejects incompatible types") {
256256
SEXP str_mat = PROTECT(Rf_allocMatrix(STRSXP, 2, 2));
257-
expect_error(cpp4r::as_doubles_matrix<>(str_mat));
257+
expect_error(cpp4r::doubles_matrix<>(str_mat));
258258
UNPROTECT(1);
259259
}
260260

261-
test_that("as_integers_matrix coerces integer-like doubles") {
262-
SEXP dbl_mat = PROTECT(Rf_allocMatrix(REALSXP, 2, 2));
263-
REAL(dbl_mat)[0] = 1.0;
264-
REAL(dbl_mat)[1] = 2.0;
265-
REAL(dbl_mat)[2] = 3.0;
266-
REAL(dbl_mat)[3] = 4.0;
261+
test_that("integers_matrix implicitly coerces logical matrix") {
262+
SEXP lgl_mat = PROTECT(Rf_allocMatrix(LGLSXP, 2, 2));
263+
LOGICAL(lgl_mat)[0] = TRUE;
264+
LOGICAL(lgl_mat)[1] = FALSE;
265+
LOGICAL(lgl_mat)[2] = TRUE;
266+
LOGICAL(lgl_mat)[3] = NA_LOGICAL;
267267

268-
cpp4r::integers_matrix<> result = cpp4r::as_integers_matrix<>(dbl_mat);
268+
cpp4r::integers_matrix<> result(lgl_mat);
269269

270270
expect_true(result(0, 0) == 1);
271-
expect_true(result(1, 0) == 2);
272-
expect_true(result(0, 1) == 3);
273-
expect_true(result(1, 1) == 4);
274-
275-
expect_true(cpp4r::detail::r_typeof(result.data()) == INTSXP);
271+
expect_true(result(1, 0) == 0);
272+
expect_true(result(0, 1) == 1);
273+
expect_true(result(1, 1) == NA_INTEGER);
276274

277275
UNPROTECT(1);
278276
}
279277

280-
test_that("as_integers_matrix rejects non-integer-like doubles") {
278+
test_that("integers_matrix rejects double matrix (narrowing)") {
281279
SEXP dbl_mat = PROTECT(Rf_allocMatrix(REALSXP, 2, 2));
282280
REAL(dbl_mat)[0] = 1.0;
283-
REAL(dbl_mat)[1] = 2.5; // Has fractional part!
281+
REAL(dbl_mat)[1] = 2.0;
284282
REAL(dbl_mat)[2] = 3.0;
285283
REAL(dbl_mat)[3] = 4.0;
286284

287-
expect_error(cpp4r::as_integers_matrix<>(dbl_mat));
285+
// integers_matrix should NOT accept doubles (would be narrowing conversion)
286+
expect_error(cpp4r::integers_matrix<>(dbl_mat));
288287

289288
UNPROTECT(1);
290289
}
291290

292-
test_that("as_integers_matrix handles NA values") {
291+
test_that("complexes_matrix implicitly coerces double matrix") {
293292
SEXP dbl_mat = PROTECT(Rf_allocMatrix(REALSXP, 2, 2));
294293
REAL(dbl_mat)[0] = 1.0;
295-
REAL(dbl_mat)[1] = NA_REAL;
294+
REAL(dbl_mat)[1] = 2.0;
296295
REAL(dbl_mat)[2] = 3.0;
297296
REAL(dbl_mat)[3] = 4.0;
298297

299-
cpp4r::integers_matrix<> result = cpp4r::as_integers_matrix<>(dbl_mat);
298+
cpp4r::complexes_matrix<> result(dbl_mat);
300299

301-
expect_true(result(0, 0) == 1);
302-
expect_true(result(1, 0) == NA_INTEGER);
303-
expect_true(result(0, 1) == 3);
304-
expect_true(result(1, 1) == 4);
300+
expect_true(result(0, 0).real() == 1.0);
301+
expect_true(result(1, 0).real() == 2.0);
302+
expect_true(result(0, 1).real() == 3.0);
303+
expect_true(result(1, 1).real() == 4.0);
305304

306-
UNPROTECT(1);
307-
}
305+
// Imaginary parts should be 0
306+
expect_true(result(0, 0).imag() == 0.0);
307+
expect_true(result(1, 1).imag() == 0.0);
308308

309-
test_that("as_integers_matrix from logical matrix") {
310-
SEXP lgl_mat = PROTECT(Rf_allocMatrix(LGLSXP, 2, 2));
311-
LOGICAL(lgl_mat)[0] = TRUE;
312-
LOGICAL(lgl_mat)[1] = FALSE;
313-
LOGICAL(lgl_mat)[2] = TRUE;
314-
LOGICAL(lgl_mat)[3] = NA_LOGICAL;
315-
316-
cpp4r::integers_matrix<> result = cpp4r::as_integers_matrix<>(lgl_mat);
317-
318-
expect_true(result(0, 0) == 1);
319-
expect_true(result(1, 0) == 0);
320-
expect_true(result(0, 1) == 1);
321-
expect_true(result(1, 1) == NA_INTEGER);
309+
expect_true(cpp4r::detail::r_typeof(result.data()) == CPLXSXP);
322310

323311
UNPROTECT(1);
324312
}
325313

326-
test_that("as_integers_matrix preserves dimnames") {
327-
SEXP dbl_mat = PROTECT(Rf_allocMatrix(REALSXP, 2, 2));
328-
REAL(dbl_mat)[0] = 10.0;
329-
REAL(dbl_mat)[1] = 20.0;
330-
REAL(dbl_mat)[2] = 30.0;
331-
REAL(dbl_mat)[3] = 40.0;
332-
333-
SEXP dimnames = PROTECT(Rf_allocVector(VECSXP, 2));
334-
SEXP rownames = PROTECT(Rf_allocVector(STRSXP, 2));
335-
SEXP colnames = PROTECT(Rf_allocVector(STRSXP, 2));
336-
337-
SET_STRING_ELT(rownames, 0, Rf_mkChar("row1"));
338-
SET_STRING_ELT(rownames, 1, Rf_mkChar("row2"));
339-
SET_STRING_ELT(colnames, 0, Rf_mkChar("col1"));
340-
SET_STRING_ELT(colnames, 1, Rf_mkChar("col2"));
341-
342-
SET_VECTOR_ELT(dimnames, 0, rownames);
343-
SET_VECTOR_ELT(dimnames, 1, colnames);
344-
Rf_setAttrib(dbl_mat, R_DimNamesSymbol, dimnames);
345-
346-
cpp4r::integers_matrix<> result = cpp4r::as_integers_matrix<>(dbl_mat);
347-
348-
// Check dimnames are preserved
349-
SEXP result_dimnames = Rf_getAttrib(result.data(), R_DimNamesSymbol);
350-
expect_true(result_dimnames != R_NilValue);
314+
test_that("complexes_matrix implicitly coerces integer matrix") {
315+
SEXP int_mat = PROTECT(Rf_allocMatrix(INTSXP, 2, 2));
316+
INTEGER(int_mat)[0] = 1;
317+
INTEGER(int_mat)[1] = 2;
318+
INTEGER(int_mat)[2] = 3;
319+
INTEGER(int_mat)[3] = 4;
351320

352-
SEXP result_rownames = VECTOR_ELT(result_dimnames, 0);
353-
SEXP result_colnames = VECTOR_ELT(result_dimnames, 1);
321+
cpp4r::complexes_matrix<> result(int_mat);
354322

355-
expect_true(strcmp(CHAR(STRING_ELT(result_rownames, 0)), "row1") == 0);
356-
expect_true(strcmp(CHAR(STRING_ELT(result_colnames, 1)), "col2") == 0);
323+
expect_true(result(0, 0).real() == 1.0);
324+
expect_true(result(1, 0).real() == 2.0);
325+
expect_true(result(0, 1).real() == 3.0);
326+
expect_true(result(1, 1).real() == 4.0);
357327

358-
UNPROTECT(4);
328+
UNPROTECT(1);
359329
}
360330
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
cpp4r version: 0.4.0
2-
vendored on: 2025-11-30
2+
vendored on: 2025-12-02

0 commit comments

Comments
 (0)