@@ -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}
0 commit comments