@@ -55,3 +55,38 @@ test_that("reductions over vectors still use intrinsics", {
5555 expect_identical(fn(x2 ), 1L )
5656 expect_quick_identical(fn , x1 , x2 )
5757})
58+
59+
60+ test_that(" nested scalar min/max compiles and runs" , {
61+ fn <- function (m ) {
62+ declare(type(m = integer(2 , 2 )))
63+ lo <- 1L
64+ hi <- 2L
65+ min(max(m [1 , 1 ], lo ), hi )
66+ }
67+
68+ m1 <- matrix (c(0L , 3L , 1L , 2L ), nrow = 2L , byrow = TRUE )
69+ m2 <- matrix (c(10L , 3L , 1L , 2L ), nrow = 2L , byrow = TRUE )
70+ expect_identical(fn(m1 ), 1L )
71+ expect_identical(fn(m2 ), 2L )
72+ expect_quick_identical(fn , m1 , m2 )
73+ })
74+
75+
76+ test_that(" clamp on a 1d array works with nested scalar min/max" , {
77+ clamp <- function (x , lo , hi ) {
78+ declare(type(x = double(n )), type(lo = double(1 )), type(hi = double(1 )))
79+ out <- double(length(x ))
80+ for (i in seq_along(x )) {
81+ out [i ] <- min(max(x [i ], lo ), hi )
82+ }
83+ out
84+ }
85+
86+ x <- c(- 2.0 , - 0.5 , 0.25 , 1.25 , 10.0 )
87+ lo <- - 0.25
88+ hi <- 1.0
89+
90+ expect_identical(clamp(x , lo , hi ), pmin(pmax(x , lo ), hi ))
91+ expect_quick_identical(clamp , list (x , lo , hi ))
92+ })
0 commit comments