Skip to content

Commit cb437aa

Browse files
committed
Adds test cases.
1 parent 95b5fe6 commit cb437aa

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test_cachematrix.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
library(testthat)
2+
3+
source("cachematrix.R")
4+
5+
context('cachematrix')
6+
7+
test_that("makeCacheMatrix() creates a list of functions", {
8+
m <- makeCacheMatrix()
9+
expect_equal(typeof(m),'list')
10+
})
11+
12+
test_that("makeCacheMatrix().get returns a matrix", {
13+
m <- makeCacheMatrix()
14+
expect_identical(m$get(),matrix())
15+
})
16+
17+
test_that("makeCacheMatrix(x) and .get returns a matrix", {
18+
m <- makeCacheMatrix(matrix(2,1,2))
19+
expect_identical(m$get(),matrix(2,1,2))
20+
})
21+
22+
test_that("makeCacheMatrix()$set() updates the matrix", {
23+
m <- makeCacheMatrix()
24+
m$set(matrix(2,1,2))
25+
expect_identical(m$get(),matrix(2,1,2))
26+
})
27+
28+
test_that("cacheSolve() returns an inverse, and caches it", {
29+
m <- makeCacheMatrix(matrix(data=c(1,0,0,1),2,2))
30+
expect_identical(cacheSolve(m),matrix(data=c(1,0,0,1),2,2))
31+
# expect_identical(m$getinverse(),matrix(data=c(1,0,0,1),2,2))
32+
})
33+
34+
test_that("When makeCacheMatrix()$set() is called getinverse() is reset", {
35+
m <- makeCacheMatrix()
36+
m$setinverse(matrix(2,1,2))
37+
expect_identical(m$getinverse(),matrix(2,1,2))
38+
m$set(matrix(2,1,2))
39+
expect_identical(m$getinverse(),NULL)
40+
m$setinverse(matrix(2,1,2))
41+
expect_identical(m$getinverse(),matrix(2,1,2))
42+
})

0 commit comments

Comments
 (0)