1 2 3 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
|
library(iterators)
test00 <- function() {}
# test vector iterator creation
test01 <- function() {
x <- iter(1:10)
}
# test hasNext, nextElem
test02 <- function() {
x <- iter(1:10)
checkEquals(nextElem(x), 1)
for(i in 1:9) nextElem(x)
checkException(nextElem(x))
}
# check checkFunc
test03 <- function() {
x <- iter(1:100, checkFunc=function(i) i%%10==0)
checkEquals(nextElem(x), 10)
for(i in 1:9) nextElem(x)
checkException(nextElem(x))
}
# test matrix iterator creation
test04 <- function() {
x <- matrix(1:10,ncol=2)
}
# test hasNext, nextElem
test05 <- function() {
x <- matrix(1:10,ncol=2)
# by cell
y <- iter(x,by='cell')
checkEquals(nextElem(y), 1)
for(i in 1:9) nextElem(y)
checkException(nextElem(y))
# by col
y <- iter(x,by='column')
checkEquals(nextElem(y), matrix(1:5, ncol=1))
nextElem(y)
checkException(nextElem(y))
# by row
y <- iter(x,by='row')
checkEquals(nextElem(y), matrix(c(1,6),nrow=1))
for(i in 1:4) nextElem(y)
checkException(nextElem(y))
}
# test checkFunc
test06 <- function() {
# create a larger matrix
x <- matrix(1:100, ncol=20)
# by cell
y <- iter(x, by='cell', checkFunc=function(i) i%%10==0)
checkEquals(nextElem(y), 10)
for(i in 1:9) nextElem(y)
checkException(nextElem(y))
# by col
y <- iter(x, by='column', checkFunc=function(i) i[5]%%10==0)
checkEquals(nextElem(y), as.matrix(x[,2]))
for(i in 1:9) nextElem(y)
checkException(nextElem(y))
# by row
# create an easier matrix to deal with
x <- matrix(1:100, nrow=20, byrow=TRUE)
y <- iter(x, by='row', checkFunc=function(i) i[5]%%10==0)
checkEquals(as.vector(nextElem(y)), x[2,])
for(i in 1:9) nextElem(y)
checkException(nextElem(y))
}
# test data frame iterator creation
test07 <- function() {
x <- data.frame(1:10, 11:20)
y <- iter(x)
}
# test hasNext, nextElem
test08 <- function() {
x <- data.frame(1:10, 11:20)
# by row
y <- iter(x, by='row')
checkEquals(nextElem(y), x[1,])
for(i in 1:9) nextElem(y)
checkException(nextElem(y))
# by col
y <- iter(x, by='column')
checkEquals(nextElem(y), x[,1])
nextElem(y)
checkException(nextElem(y))
}
# test checkFunc
test09 <- function() {
x <- data.frame(1:10, 11:20)
# by row
y <- iter(x, by='row', checkFunc=function(i) i[[1]][1]%%2==0)
checkEquals(nextElem(y),x[2,])
for(i in 1:4) nextElem(y)
checkException(nextElem(y))
# by col
y <- iter(x, by='column', checkFunc=function(i) i[[1]][1]%%11==0)
checkEquals(nextElem(y), x[,2])
checkException(nextElem(y))
}
# test function iterator creation
# we need to test a function that takes no arguement as
# well as one that takes the index
test10 <- function() {
noArgFunc <- function() 1
needArgFunc <- function(i)
if(i>100)
stop('too high')
else
i
}
# test hasNext, nextElem
test11 <- function() {
noArgFunc <- function() 1
needArgFunc <- function(i) if(i>100) stop('too high') else i
y <- iter(noArgFunc)
checkEquals(nextElem(y), 1)
nextElem(y)
y <- iter(needArgFunc)
checkEquals(nextElem(y), 1)
for (i in 1:99) nextElem(y)
checkException(nextElem(y))
}
# test checkFunc
test12 <- function() {
noArgFunc <- function() 1
needArgFunc <- function(i)
if(i>100)
stop('too high')
else
i
y <- iter(noArgFunc, checkFunc=function(i) i==1)
checkEquals(nextElem(y), 1)
nextElem(y)
y <- iter(needArgFunc, checkFunc=function(i) i%%10==0)
checkEquals(nextElem(y), 10)
for(i in 1:9) nextElem(y)
checkException(nextElem(y))
}
|