|
4 | 4 |
|
5 | 5 | #include <testthat.h>
|
6 | 6 |
|
| 7 | +#include <algorithm> // for max_element |
| 8 | + |
| 9 | +#ifdef _WIN32 |
| 10 | +#include "Rversion.h" |
| 11 | +#define CPP11_HAS_IS_UTILITIES R_VERSION >= R_Version(4, 0, 0) |
| 12 | +#else |
| 13 | +#define CPP11_HAS_IS_UTILITIES 1 |
| 14 | +#endif |
| 15 | + |
| 16 | +#if CPP11_HAS_IS_UTILITIES |
| 17 | +context("r_vector-capabilities-C++") { |
| 18 | + test_that("read only vector capabilities") { |
| 19 | + using cpp11::integers; |
| 20 | + |
| 21 | + expect_true(std::is_destructible<integers>::value); |
| 22 | + expect_true(std::is_default_constructible<integers>::value); |
| 23 | + expect_true(std::is_nothrow_default_constructible<integers>::value); |
| 24 | + expect_true(std::is_copy_constructible<integers>::value); |
| 25 | + expect_true(std::is_move_constructible<integers>::value); |
| 26 | + expect_true(std::is_copy_assignable<integers>::value); |
| 27 | + expect_true(std::is_move_assignable<integers>::value); |
| 28 | + } |
| 29 | + |
| 30 | + test_that("writable vector capabilities") { |
| 31 | + using cpp11::writable::integers; |
| 32 | + |
| 33 | + expect_true(std::is_destructible<integers>::value); |
| 34 | + expect_true(std::is_default_constructible<integers>::value); |
| 35 | + expect_true(std::is_nothrow_default_constructible<integers>::value); |
| 36 | + expect_true(std::is_copy_constructible<integers>::value); |
| 37 | + expect_true(std::is_move_constructible<integers>::value); |
| 38 | + expect_true(std::is_copy_assignable<integers>::value); |
| 39 | + expect_true(std::is_move_assignable<integers>::value); |
| 40 | + } |
| 41 | + |
| 42 | + test_that("read only const_iterator capabilities") { |
| 43 | + using cpp11::integers; |
| 44 | + |
| 45 | + expect_true(std::is_destructible<integers::const_iterator>::value); |
| 46 | + expect_true(std::is_trivially_destructible<integers::const_iterator>::value); |
| 47 | + expect_true(std::is_copy_constructible<integers::const_iterator>::value); |
| 48 | + expect_true(std::is_move_constructible<integers::const_iterator>::value); |
| 49 | + expect_true(std::is_copy_assignable<integers::const_iterator>::value); |
| 50 | + expect_true(std::is_trivially_copy_assignable<integers::const_iterator>::value); |
| 51 | + expect_true(std::is_move_assignable<integers::const_iterator>::value); |
| 52 | + expect_true(std::is_trivially_move_assignable<integers::const_iterator>::value); |
| 53 | + } |
| 54 | + |
| 55 | + test_that("writable iterator capabilities") { |
| 56 | + using cpp11::writable::integers; |
| 57 | + |
| 58 | + expect_true(std::is_destructible<integers::iterator>::value); |
| 59 | + expect_true(std::is_trivially_destructible<integers::iterator>::value); |
| 60 | + expect_true(std::is_copy_constructible<integers::iterator>::value); |
| 61 | + expect_true(std::is_move_constructible<integers::iterator>::value); |
| 62 | + expect_true(std::is_copy_assignable<integers::iterator>::value); |
| 63 | + expect_true(std::is_trivially_copy_assignable<integers::iterator>::value); |
| 64 | + expect_true(std::is_move_assignable<integers::iterator>::value); |
| 65 | + expect_true(std::is_trivially_move_assignable<integers::iterator>::value); |
| 66 | + } |
| 67 | + |
| 68 | + test_that("writable proxy capabilities") { |
| 69 | + using cpp11::writable::integers; |
| 70 | + |
| 71 | + expect_true(std::is_destructible<integers::proxy>::value); |
| 72 | + expect_true(std::is_trivially_destructible<integers::proxy>::value); |
| 73 | + expect_true(std::is_copy_constructible<integers::proxy>::value); |
| 74 | + expect_true(std::is_move_constructible<integers::proxy>::value); |
| 75 | + |
| 76 | + // Should these be true? Does it affect anything in practice? |
| 77 | + expect_false(std::is_copy_assignable<integers::proxy>::value); |
| 78 | + expect_false(std::is_trivially_copy_assignable<integers::proxy>::value); |
| 79 | + expect_false(std::is_move_assignable<integers::proxy>::value); |
| 80 | + expect_false(std::is_trivially_move_assignable<integers::proxy>::value); |
| 81 | + } |
| 82 | +} |
| 83 | +#endif |
| 84 | + |
7 | 85 | context("r_vector-C++") {
|
8 | 86 | test_that("writable vector temporary isn't leaked (integer) (#338)") {
|
9 | 87 | R_xlen_t before = cpp11::detail::store::count();
|
@@ -396,4 +474,26 @@ context("r_vector-C++") {
|
396 | 474 |
|
397 | 475 | UNPROTECT(2);
|
398 | 476 | }
|
| 477 | + |
| 478 | + test_that("std::max_element works on read only vectors") { |
| 479 | + SEXP foo_sexp = PROTECT(Rf_allocVector(INTSXP, 5)); |
| 480 | + SET_INTEGER_ELT(foo_sexp, 0, 1); |
| 481 | + SET_INTEGER_ELT(foo_sexp, 1, 2); |
| 482 | + SET_INTEGER_ELT(foo_sexp, 2, 5); |
| 483 | + SET_INTEGER_ELT(foo_sexp, 3, 4); |
| 484 | + SET_INTEGER_ELT(foo_sexp, 4, 3); |
| 485 | + cpp11::integers foo(foo_sexp); |
| 486 | + |
| 487 | + auto element = std::max_element(foo.begin(), foo.end()); |
| 488 | + |
| 489 | + expect_true(*element == 5); |
| 490 | + |
| 491 | + UNPROTECT(1); |
| 492 | + } |
| 493 | + |
| 494 | + test_that("std::max_element works on writable vectors (#334)") { |
| 495 | + cpp11::writable::integers foo = {1, 2, 5, 4, 3}; |
| 496 | + auto element = std::max_element(foo.begin(), foo.end()); |
| 497 | + expect_true(*element == 5); |
| 498 | + } |
399 | 499 | }
|
0 commit comments