subsetting.R 7.04 KB
Newer Older
1 2
# Test possible bugs involving subset and subassign.
#
3
# Added for pqR, 2014, 2017, Radford M. Neal.
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
# General tests of subset extraction with scalar indexes, not array.

vi <- 11:15
vr <- (11:15) + 0.1
L <- list(11.1,12L,13.1,14L,TRUE)
Ln <- list(e1=11.1,e2=12L,e3=13.1,e4=14L,e5=TRUE)

vin <- vi
names(vin) <- paste0("a",1:length(vin))
vrn <- vr
names(vrn) <- paste0("a",1:length(vrn))

a <- vi[3L]
stopifnot(identical(a,13L))
a <- vi[3]
stopifnot(identical(a,13L))
a <- vr[3]
stopifnot(identical(a,13.1))
a <- vin[3]
stopifnot(identical(a,c(a3=13L)))
a <- vrn[3]
stopifnot(identical(a,c(a3=13.1)))
a <- L[3]
stopifnot(identical(a,list(13.1)))
a <- Ln[3]
stopifnot(identical(a,list(e3=13.1)))

a <- vi[[3]]
stopifnot(identical(a,13L))
a <- vr[[3]]
stopifnot(identical(a,13.1))
a <- vin[[3]]
stopifnot(identical(a,13L))
a <- vrn[[3]]
stopifnot(identical(a,13.1))
a <- L[[3]]
stopifnot(identical(a,13.1))
a <- Ln[[3]]
stopifnot(identical(a,13.1))

a <- vin["a3"]
stopifnot(identical(a,c(a3=13L)))
a <- vin[["a3"]]
stopifnot(identical(a,13L))
a <- Ln[["e3"]]
stopifnot(identical(a,13.1))
a <- Ln$e3
stopifnot(identical(a,13.1))

j <- 2

a <- vi[j+1]
stopifnot(identical(a,13L))
a <- vr[j+1]
stopifnot(identical(a,13.1))
a <- vin[j+1]
stopifnot(identical(a,c(a3=13L)))
a <- vrn[j+1]
stopifnot(identical(a,c(a3=13.1)))
a <- L[j+1]
stopifnot(identical(a,list(13.1)))
a <- Ln[j+1]
stopifnot(identical(a,list(e3=13.1)))

a <- vi[[j+1]]
stopifnot(identical(a,13L))
a <- vr[[j+1]]
stopifnot(identical(a,13.1))
a <- vin[[j+1]]
stopifnot(identical(a,13L))
a <- vrn[[j+1]]
stopifnot(identical(a,13.1))
a <- L[[j+1]]
stopifnot(identical(a,13.1))
a <- Ln[[j+1]]
stopifnot(identical(a,13.1))


# General tests of subset replacement with scalar indexes, not array.

vi <- 11:15
vr <- (11:15) + 0.1
L <- list(11.1,12L,13.1,14L,TRUE)
Ln <- list(e1=11.1,e2=12L,e3=13.1,e4=14L,e5=TRUE)

vin <- vi
names(vin) <- paste0("a",1:length(vin))
vrn <- vr
names(vrn) <- paste0("a",1:length(vrn))

vi[3] <- 20L
stopifnot(identical(vi,c(11L,12L,20L,14L,15L)))
vi[2] <- 30
stopifnot(identical(vi,c(11,30,20,14,15)))
vr[3] <- 20.2
vr[2] <- 30.2
stopifnot(identical(vr,c(11.1,30.2,20.2,14.1,15.1)))

vin[3] <- 20L
stopifnot(identical(vin,c(a1=11L,a2=12L,a3=20L,a4=14L,a5=15L)))
vin[2] <- 30
stopifnot(identical(vin,c(a1=11,a2=30,a3=20,a4=14,a5=15)))
vrn[3] <- 20.2
vrn[2] <- 30.2
stopifnot(identical(vrn,c(a1=11.1,a2=30.2,a3=20.2,a4=14.1,a5=15.1)))

L[3] <- list(FALSE)
stopifnot(identical(L,list(11.1,12L,FALSE,14L,TRUE)))
L[[2]] <- 1.1
stopifnot(identical(L,list(11.1,1.1,FALSE,14L,TRUE)))
Ln[3] <- list(FALSE)
stopifnot(identical(Ln,list(e1=11.1,e2=12L,e3=FALSE,e4=14L,e5=TRUE)))
Ln$e5 <- "abc"
stopifnot(identical(Ln,list(e1=11.1,e2=12L,e3=FALSE,e4=14L,e5="abc")))
Ln$e1 <- c(1,2)
stopifnot(identical(Ln,list(e1=c(1,2),e2=12L,e3=FALSE,e4=14L,e5="abc")))

vi <- 11:15
vr <- (11:15) + 0.1
L <- list(11.1,12L,13.1,14L,TRUE)
Ln <- list(e1=11.1,e2=12L,e3=13.1,e4=14L,e5=TRUE)

vin <- vi
names(vin) <- paste0("a",1:length(vin))
vrn <- vr
names(vrn) <- paste0("a",1:length(vrn))

vi[[3]] <- 20L
stopifnot(identical(vi,c(11L,12L,20L,14L,15L)))
vi[[2]] <- 30
stopifnot(identical(vi,c(11,30,20,14,15)))
vr[[3]] <- 20.2
vr[[2]] <- 30.2
stopifnot(identical(vr,c(11.1,30.2,20.2,14.1,15.1)))

vin[[3]] <- 20L
stopifnot(identical(vin,c(a1=11L,a2=12L,a3=20L,a4=14L,a5=15L)))
vin[[2]] <- 30
stopifnot(identical(vin,c(a1=11,a2=30,a3=20,a4=14,a5=15)))
vrn[[3]] <- 20.2
vrn[[2]] <- 30.2
stopifnot(identical(vrn,c(a1=11.1,a2=30.2,a3=20.2,a4=14.1,a5=15.1)))

vin["a1"] <- 10L
stopifnot(identical(vin,c(a1=10,a2=30,a3=20,a4=14,a5=15)))
vin[["a1"]] <- 12L
stopifnot(identical(vin,c(a1=12,a2=30,a3=20,a4=14,a5=15)))
# vin$a4 <- 50L
# stopifnot(identical(vin,c(a1=12,a2=30,a3=20,a4=50,a5=15)))

vi <- 11:15
vr <- (11:15) + 0.1
L <- list(11.1,12L,13.1,14L,TRUE)
Ln <- list(e1=11.1,e2=12L,e3=13.1,e4=14L,e5=TRUE)

vin <- vi
names(vin) <- paste0("a",1:length(vin))
vrn <- vr
names(vrn) <- paste0("a",1:length(vrn))

j <- 2
k <- 20L

vi[[j+1]] <- k
stopifnot(identical(vi,c(11L,12L,20L,14L,15L)))
vi[[j]] <- k+10
stopifnot(identical(vi,c(11,30,20,14,15)))
vr[[j+1]] <- k+0.2
vr[[j]] <- k+10.2
stopifnot(identical(vr,c(11.1,30.2,20.2,14.1,15.1)))

vin[[j+1]] <- k
stopifnot(identical(vin,c(a1=11L,a2=12L,a3=20L,a4=14L,a5=15L)))
vin[[j]] <- k+10
stopifnot(identical(vin,c(a1=11,a2=30,a3=20,a4=14,a5=15)))
vrn[[j+1]] <- k+0.2
vrn[[j]] <- k+10.2
stopifnot(identical(vrn,c(a1=11.1,a2=30.2,a3=20.2,a4=14.1,a5=15.1)))

vin["a1"] <- k-10L
stopifnot(identical(vin,c(a1=10,a2=30,a3=20,a4=14,a5=15)))
vin[["a1"]] <- k-8L
stopifnot(identical(vin,c(a1=12,a2=30,a3=20,a4=14,a5=15)))
# vin$a4 <- k+30L
# stopifnot(identical(vin,c(a1=12,a2=30,a3=20,a4=50,a5=15)))
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

# Replacement of pairlist or language elements.

L <- pairlist(1,2,3)
L[[2]] <- 9
stopifnot(identical(L,pairlist(1,9,3)))
L[[2]] <- NULL
stopifnot(identical(L,pairlist(1,3)))
L[[1]] <- NULL
stopifnot(identical(L,pairlist(3)))

L <- pairlist(a=1,b=2,c=3)
L$b <- 9
stopifnot(identical(L,pairlist(a=1,b=9,c=3)))
L$b <- NULL
stopifnot(identical(L,pairlist(a=1,c=3)))
L$a <- NULL
stopifnot(identical(L,pairlist(c=3)))

L <- parse(text="f(1,2)")[[1]]
L[[2]] <- 3
stopifnot(identical(L,parse(text="f(3,2)")[[1]]))
L[[1]] <- NULL
stopifnot(identical(L,pairlist(3,2)))
215 216


217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
# Tests with ...

M <- matrix(101:112,3,4)

f <- function (w,...) stopifnot(identical(M[...],w))
f(104L,4)
f(104L,1,2)
f(105L,2,2)
f(M[])

f <- function (w,x,...) stopifnot(identical(M[x,...],w))
f(104L,4)
f(104L,1,2)
f(105L,2,2)

f <- function (w,...) stopifnot(identical(M[[...]],w))
f(104L,4)
f(104L,1,2)
f(105L,2,2)

f <- function (w,x,...) stopifnot(identical(M[[x,...]],w))
f(104L,4)
f(104L,1,2)
f(105L,2,2)

M2 <- M
M2[2,3] <- 9L

f <- function (M,W,...) { M[...] <- 9L; stopifnot(identical(M,W)) }
f(M,M2,2,3)

f <- function (M,W,x,...) { M[x,...] <- 9L; stopifnot(identical(M,W)) }
f(M,M2,2,3)

f <- function (M,W,...) { M[[...]] <- 9L; stopifnot(identical(M,W)) }
f(M,M2,2,3)

f <- function (M,W,x,...) { M[[x,...]] <- 9L; stopifnot(identical(M,W)) }
f(M,M2,2,3)


258 259 260 261 262 263 264 265 266 267 268 269 270 271
# Check that name after $ gets dispatched as a string.

a <- list(p=3,q=4)
class(a) <- "fred"
`$.fred` <-
  function (x,n)
    { stopifnot(identical(n,substitute(n))); x[[n]] }
`$<-.fred` <-
  function (x,n,value) 
    { stopifnot(identical(n,substitute(n))); x[[n]] <- value; x }
a$q <- a$q + 1
stopifnot(identical(a,structure(list(p=3,q=5),class="fred")))

cat("DONE\n")
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288


# Check how dimnames are converted to names when result is a vector.

M <- matrix(3,2,2)
rownames(M) <- c("a","b")
stopifnot(identical(M[2,2],c(b=3)))
colnames(M) <- c("x","y")
stopifnot(identical(M[2,2],3))
stopifnot(identical(M[array(2),2],c(b=3)))
stopifnot(identical(M[2,array(2)],c(y=3)))
A <- M[1..1,_]
stopifnot(identical(A[,2],3))
stopifnot(identical(A[_,2],c(a=3)))
A <- M[_,1..1]
stopifnot(identical(A[2,],3))
stopifnot(identical(A[2,_],c(x=3)))
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305


# Check array indexing.

a <- array(1:120,c(2,3,4,5))
a[2,3,4,5] <- 9999L
stopifnot(a[2,3,4,5]==9999L)
stopifnot(a[length(a)]==9999L)
a[2,3,4,5] <- 120L
stopifnot(all(c(a)==1:120))

a <- array(1:120,c(2,3,4,5))
a[[2,3,4,5]] <- 9999L
stopifnot(a[[2,3,4,5]]==9999L)
stopifnot(a[length(a)]==9999L)
a[[2,3,4,5]] <- 120L
stopifnot(all(c(a)==1:120))