Skip to content

Commit 541a633

Browse files
committed
De-bugged code and added unit tests
1 parent 07a2203 commit 541a633

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.Rhistory

cachematrix.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ cacheSolve <- function(x, ...) {
2727
return(i)
2828
}
2929
data <- x$get()
30-
i <- resolve(data, ...)
30+
i <- solve(data, ...)
3131
x$setinverse(i)
3232
i
3333
}

test_cachematrix.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Test suite for cachematrix.R
2+
3+
library(testthat)
4+
source("cachematrix.R")
5+
6+
test_that("Inverse of 2x2 matrix", {
7+
m <- matrix(c(2, 0, 0, 2), nrow=2, ncol=2)
8+
cm <- makeCacheMatrix(m)
9+
im <- cacheSolve(cm)
10+
11+
expected <- matrix(c(0.5, 0, 0, 0.5), nrow=2, ncol=2)
12+
expect_equal(expected, im)
13+
})
14+
15+
test_that("Inverse of identity is identity", {
16+
m <- matrix(c(1, 0, 0, 1), nrow=2, ncol=2)
17+
cm <- makeCacheMatrix(m)
18+
im <- cacheSolve(cm)
19+
20+
expect_equal(m, im)
21+
})

0 commit comments

Comments
 (0)