forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixTest.R
42 lines (30 loc) · 919 Bytes
/
matrixTest.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
## Simple test wrapper for the programming assignment
## author: mig
library(testthat)
source("cacheMatrix.R")
# create and return a random invertible matrix of dimension nxn
makeRandomInvertibleMatrix <- function(n = 2) {
repeat {
v <- sample(n^2, replace = TRUE)
m <- matrix(v, nrow=n, ncol=n)
if(det(m) != 0)
return(m)
}
}
context("cachesolve")
# Make sure that cachesolve actually returns the inverted matrix...
test_that("cachesolve inverts", {
m <- makeRandomInvertibleMatrix(3)
minv <- solve(m)
mc <- makeCacheMatrix(m)
minvc <- cacheSolve(mc)
expect_equal(minv, minvc)
})
# Test that the cached value is returned
test_that("caching works", {
m <- makeRandomInvertibleMatrix(6)
mc <- makeCacheMatrix(m)
minvc <- cacheSolve(mc)
msg <- tryCatch(minvc <- cacheSolve(mc), message = function(c) c[1])
expect_equal(msg$message, "getting cached data\n")
})