Skip to content

Commit 54959dc

Browse files
committed
Fix inverse reset bug
Post-submission, just for correctness
1 parent 1b21e2a commit 54959dc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cachematrix.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ makeCacheMatrix <- function(x = matrix()) {
1111
## cached inverse
1212
set <- function(y) {
1313
x <<- y
14-
inv <- NULL
14+
inv <<- NULL
1515
}
1616

1717
## Return the current matrix

test/test_cachematrix.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ test_that("makeCacheMatrix(m) sets/gets the cached inverse", {
3131
expect_identical(x$getInverse(), im)
3232
})
3333

34+
test_that("makeCacheMatrix(m)$set resets the cached inverse", {
35+
x <- makeCacheMatrix()
36+
37+
## bogus inverse, just for unit testing
38+
im <- matrix(1,1)
39+
x$setInverse(im)
40+
41+
m <- matrix(2,3)
42+
x$set(m)
43+
44+
expect_identical(x$getInverse(), NULL)
45+
})
46+
3447
test_that("cacheSolve(x) uses the cached inverse", {
3548
x <- makeCacheMatrix()
3649

0 commit comments

Comments
 (0)