The R Project SVN R-packages

Rev

Rev 3456 | Rev 3458 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3456 Rev 3457
Line 14... Line 14...
14
 
14
 
15
# test classes
15
# test classes
16
stopifnot(class(u1.sdf) == "sqlite.data.frame",
16
stopifnot(class(u1.sdf) == "sqlite.data.frame",
17
          class(u2.sdf) == "sqlite.data.frame")
17
          class(u2.sdf) == "sqlite.data.frame")
18
 
18
 
19
# test row.names
19
# test row.names, head, tail
20
stopifnot(all(as.character(row.names(u1.sdf))==row.names(iris)))
20
stopifnot(all(as.character(row.names(u1.sdf))==row.names(iris)),
21
stopifnot(all(as.character(row.names(u2.sdf))==row.names(attenu)))
21
          all(as.character(row.names(u2.sdf))==row.names(attenu)),
-
 
22
          all(head(u1.sdf) == head(iris)),
-
 
23
          all(head(u2.sdf) == head(attenu)),
-
 
24
          all(tail(u1.sdf) == tail(iris)),
-
 
25
          all(tail(u2.sdf) == tail(attenu)))
-
 
26
 
-
 
27
 
-
 
28
compareSdfToDf <- function(sdf, df, with.names=TRUE) {
-
 
29
    ncols <- ncol(df)
-
 
30
    nrows <- nrow(df)
-
 
31
    for (i in 1:nrows) { for (j in 1:ncols) {
-
 
32
        if (df[i,j] != sdf[i,j]) stop("Not equal on ", i, ",", j, "\n")
-
 
33
    }}
-
 
34
    for (j in 1:ncols) { 
-
 
35
        sv <- sdf[[j]]; # test sqlite.vector
-
 
36
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
-
 
37
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
-
 
38
        for (i in 1:nrows)
-
 
39
            if (sv[i] != df[i,j]) stop("Not equal on", i, "on col", j, "\n")
-
 
40
    }
-
 
41
    if (with.names) for (j in names(df)) { 
-
 
42
        sv <- sdf[[j]]; # test sqlite.vector
-
 
43
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
-
 
44
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
-
 
45
        for (i in 1:nrows)
-
 
46
            if (sv[i] != df[i,j]) stop("Not equal on", i, "on col", j, "\n")
-
 
47
    }
-
 
48
}
22
 
49
 
23
# test creating named sdfs
50
# test creating named sdfs
24
iris.sdf <- sqlite.data.frame(iris, "iris")
51
iris.sdf <- sqlite.data.frame(iris, "iris")
25
stopifnot(file.exists("iris.db"))
52
stopifnot(file.exists("iris.db"))
26
iris2.sdf <- sqlite.data.frame(iris, "iris")
53
iris2.sdf <- sqlite.data.frame(iris, "iris")
Line 41... Line 68...
41
          length(row.names(iris.sdf)) == length(row.names(iris)),
68
          length(row.names(iris.sdf)) == length(row.names(iris)),
42
          all(dim(iris.sdf) == dim(iris)),
69
          all(dim(iris.sdf) == dim(iris)),
43
          all(names(iris.sdf) == names(iris)))
70
          all(names(iris.sdf) == names(iris)))
44
 
71
 
45
# test sdf indexers
72
# test sdf indexers
46
ncols <- length(iris)
-
 
47
nrows <- length(row.names(iris))
73
compareSdfToDf(iris.sdf, iris)
48
for (i in 1:nrows) { for (j in 1:ncols) {
-
 
49
    if (iris[i,j] != iris.sdf[i,j]) stop("Not equal on ", i, ",", j, "\n")
-
 
50
}}
-
 
51
for (j in 1:ncols) { 
-
 
52
    sv <- iris.sdf[[j]]; # test sqlite.vector
-
 
53
    stopifnot(class(sv) == "sqlite.vector", is.typeSvec(sv, class(iris[[j]])[1]))
-
 
54
    if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
-
 
55
    for (i in 1:nrows)
-
 
56
        if (sv[i] != iris[i,j]) stop("Not equal on", i, "on col", j, "\n")
-
 
57
}
-
 
58
for (j in names(iris)) { 
-
 
59
    sv <- iris.sdf[[j]]; # test sqlite.vector
-
 
60
    stopifnot(class(sv) == "sqlite.vector", is.typeSvec(sv, class(iris[[j]])[1]))
-
 
61
    if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
-
 
62
    for (i in 1:nrows)
-
 
63
        if (sv[i] != iris[i,j]) stop("Not equal on", i, "on col", j, "\n")
-
 
64
}
-
 
65
 
74
 
66
# test operators
75
# test operators
67
stopifnot(all((iris.sdf[,1] + iris.sdf[,2]) == (iris[,1] + iris[,2])))
76
stopifnot(all((iris.sdf[,1] + iris.sdf[,2]) == (iris[,1] + iris[,2])))
68
stopifnot(all((iris.sdf[,1]*10 %/% iris.sdf[,2]) == (iris[,1]*10 %/% iris[,2])))
77
stopifnot(all((iris.sdf[,1]*10 %/% iris.sdf[,2]) == (iris[,1]*10 %/% iris[,2])))
69
stopifnot(all(signif(log(iris.sdf[,1],17)) == signif(log(iris[,1],17))))
78
stopifnot(all(signif(log(iris.sdf[,1],17)) == signif(log(iris[,1],17))))
70
stopifnot(all(round(log(iris.sdf[,1]),5) == round(log(iris[,1]),5)))
79
stopifnot(all(round(log(iris.sdf[,1]),5) == round(log(iris[,1]),5)))
71
stopifnot(all(with(iris.sdf, Sepal.Length*Sepal.Width - Petal.Length/Petal.Width) == with(iris, Sepal.Length*Sepal.Width - Petal.Length/Petal.Width)))
80
stopifnot(all(with(iris.sdf, Sepal.Length*Sepal.Width - Petal.Length/Petal.Width) == with(iris, Sepal.Length*Sepal.Width - Petal.Length/Petal.Width)))
72
 
81
 
73
stopifnot(sapply(iris.sdf[,1:4],sum) == sapply(iris[,1:4],sum))
82
stopifnot(sapply(iris.sdf[,1:4],sum) == sapply(iris[,1:4],sum))
74
 
83
 
-
 
84
if (require(RSQLite)) {
-
 
85
    dr <- SQLite()
-
 
86
    con <- dbConnect(dr, dbname="example.db")
-
 
87
    i1 <- sdfImportDBI(con, "select * from iris")
-
 
88
    compareSdfToDf(i1[,1:4], iris[,1:4], with.names=FALSE)
-
 
89
    stopifnot(all(as.character(i1[,5]) == as.character(iris[,5])))
-
 
90
 
-
 
91
    i2 <- sdfImportDBI(con, "select * from iris", 30)  # test rbindSdf
-
 
92
    compareSdfToDf(i2[,1:4], iris[,1:4], with.names=FALSE)
-
 
93
    stopifnot(all(as.character(i2[,5]) == as.character(iris[,5])))
-
 
94
}
-
 
95
 
75
# test summary
96
# test summary
76
#for (j in 1:5) stopifnot(all(summary(iris.sdf[,j]) == summary(iris[,j])))
97
#for (j in 1:5) stopifnot(all(summary(iris.sdf[,j]) == summary(iris[,j])))
77
#iris.sdf.summary <- summary(iris.sdf)
98
#iris.sdf.summary <- summary(iris.sdf)
78
#iris.summary <- summary(iris)
99
#iris.summary <- summary(iris)
79
#stopifnot(all(iris.sdf.summary[,1:4] == iris.summary[,1:4]))
100
#stopifnot(all(iris.sdf.summary[,1:4] == iris.summary[,1:4]))