forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix-test.R
69 lines (60 loc) · 1.7 KB
/
cachematrix-test.R
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
#
# Tests for R Programming / Programming Assignment 2: Lexical Scoping
#
# Source the implementation
source("cachematrix.R")
# Contain tests in an anonymous function to avoid polluting the global environment
(function() {
# Runs the specified test function and prints out the result
run_test <- function(test) {
result <- test()
if(identical(result$expected, result$observed)) {
print(paste("PASS", result$test, collapse=" "))
} else {
print(paste("FAIL", result$test, collapse=" "))
print(result[c("expected", "observed")])
}
}
# Define tests
tests <- c(
function () {
list(
test = "makeCacheMatrix returns a list",
expected = class(list()),
observed = class(makeCacheMatrix())
)
},
function () {
list(
test = "solution is initially not cached",
expected = FALSE,
observed = makeCacheMatrix()$is_cached()
)
},
function () {
list(
test = "decorator's solve() returns inverse of a 2x2 matrix",
expected = matrix(c(-2,3,3,-4), 2, 2),
observed = makeCacheMatrix(matrix(c(4,3,3,2), 2, 2))$solve()
)
},
function () {
cacheMatrix <- makeCacheMatrix(matrix(c(4,3,3,2), 2, 2))
cacheMatrix$solve()
list(
test="decorator's solve() result is cached",
expected = TRUE,
observed = cacheMatrix$is_cached()
)
},
function () {
list(
test = "cacheSolve returns the inverse of a decorated 2x2 matrix",
expected = matrix(c(-2,3,3,-4), 2, 2),
observed = cacheSolve(makeCacheMatrix(matrix(c(4,3,3,2), 2, 2)))
)
}
)
# Run tests
lapply(tests, run_test)
})()