Debian Codegraph @

inst/share/R/compareRUnitTestData.r

raw symbols SCIP
1 ######################################################################
2 ## RUnit : A unit test framework for the R programming language
3 ## Copyright (C) 2003-2009 Thomas Koenig, Matthias Burger, Klaus Juenemann
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; version 2 of the License.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; if not, write to the Free Software
16 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18 ## $Id$
19
20 compare <- function(td1, td2, tolerance=100) {
21 ##@bdescr
22 ## compare two test suite result data objects obtained
23 ## on the same test suite
24 ## identify timing differences exceeding 'tolerance' [seconds]
25 ##@edescr
26 ##
27 ##
28 ##@in td1 : [list] of S3 class 'RUnitTestData'
29 ##@in td2 : [list] of S3 class 'RUnitTestData'
30 ##@in tolerance : [numeric] positive scalar
31 ##@ret : [data.frame]
32 ##
33 ##@codestatus : untested
34
35 ## preconditions
36 if (!is(td1, "RUnitTestData")) {
37 stop("argument 'td1' has to be of class 'RUnitTestData'.")
38 }
39 if (!is(td2, "RUnitTestData")) {
40 stop("argument 'td2' has to be of class 'RUnitTestData'.")
41 }
42 if (length(tolerance) != 1 || is.na(tolerance) || tolerance < 0) {
43 stop("argument 'tolerance' has to be positive scalar.")
44 }
45
46 ## helper functions
47 commonNames <- function(x1, x2) {
48 return(intersect(names(x1), names(x2)))
49 }
50 compareTiming <- function(x1, x2, tol=0) {
51 d <- x1 - x2
52 if (abs(d) > tol) {
53 return(d)
54 } else {
55 return(as.numeric(0))
56 }
57 }
58
59 comparePerSourceFile <- function(sf1,sf2, tol=0) {
60 ## FIXME
61 ## check if test case file was considered in this suite
62 ## i.e. test case file name can be in list albeit it
63 ## was not executed in the scenario
64 ## thus list is empty
65 if (length(sf1) == 0 | length(sf2) == 0) {
66 cat("\n skipped empty result set:", sf1)
67 return(NULL)
68 }
69 commonTests <- commonNames(sf1, sf2)
70 t(sapply(commonTests, function(x, obj1, obj2) {
71 ##cat("\n test:", x, "\n")
72
73 if(obj1[[x]][["kind"]] == obj2[[x]][["kind"]]) {
74 if (obj1[[x]][["kind"]] == "success") {
75 return(c(x, obj1[[x]][["kind"]], obj1[[x]][["time"]],
76 obj2[[x]][["kind"]], obj2[[x]][["time"]],
77 compareTiming(obj1[[x]][["time"]],
78 obj2[[x]][["time"]],
79 tol=tol)))
80 } else {
81 return(c(x, obj1[[x]][["kind"]], as.numeric(NA),
82 obj2[[x]][["kind"]], as.numeric(NA), as.numeric(NA)))
83 }
84 } else {
85 ## no timing delta
86 ## should check for timing in second case
87 ## obj2[[x]][["time"]])
88 return(c(x, obj1[[x]][["kind"]], as.numeric(NA),
89 obj2[[x]][["kind"]], as.numeric(NA), as.numeric(NA)))
90
91 }
92
93 }, obj1=sf1, obj2=sf2))
94 }
95
96 comparePerSuite <- function(s1,s2, tol=0) {
97 ## absolute file names recorded, strip path
98 commonFiles <- intersect(basename(names(s1[["sourceFileResults"]])),
99 basename(names(s2[["sourceFileResults"]])))
100 do.call("rbind", sapply(commonFiles, function(x, obj1, obj2) {
101 ## match exact file name in abs. name
102 idx1 <- match(x, basename(names(obj1)))
103 idx2 <- match(x, basename(names(obj2)))
104 if (length(idx1) != 1 || is.na(idx1) || length(idx2) != 1 || is.na(idx2)) {
105 stop("ambiguous file name.")
106 next;
107 }
108 comparePerSourceFile(obj1[[idx1]], obj2[[idx2]], tol=tol)
109 }, obj1=s1[["sourceFileResults"]], obj2=s2[["sourceFileResults"]]) )
110
111 }
112
113
114 ## main
115 ## test suites to compare
116 commonTestSuites <- commonNames(td1, td2)
117
118 res <- matrix(ncol=6, nrow=0)
119 colnames(res) <- c("TestCase", "Suite1 State", "Suite1 Timing", "Suite2 State", "Suite2 Timing", "Delta")
120 for (ti in seq_along(commonTestSuites)) {
121 res <- rbind(res, comparePerSuite(td1[[commonTestSuites[ti]]], td2[[commonTestSuites[ti]]],
122 tol=tolerance))
123 }
124
125 ## postcondition
126 return(res)
127 }
128