Skip to content

Commit 82d9038

Browse files
committed
added some more unit tests and comments explaining the parts of the function that had been confusing to me
1 parent 33ab759 commit 82d9038

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

cachematrix.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
## This function takes a matrix as an argument, and contains
77
## getters and setters for both the matrix itself, and the
8-
## inverse of the matrix.
8+
## inverse of the matrix. When the matrix is changed the
9+
## cache of the inverse matrix is cleared
910

1011
makeCacheMatrix <- function(x = matrix()) {
12+
# I'll confess some minor scoping confusion. I don't
13+
# really understand what environment inverse is being
14+
# created in and why it doesn't end up being shared
15+
# by all instances of makeCacheMatrix, but I tested
16+
# and it doesn't
1117
inverse <- NULL
1218
set <- function(y) {
1319
x <<- y
@@ -22,6 +28,11 @@ makeCacheMatrix <- function(x = matrix()) {
2228
getInverse <- function() {
2329
inverse
2430
}
31+
# this list below was confusing to me before so I'm commenting
32+
# to explain it to future me. The last call in a function
33+
# is the one that gets returned, so by returning this list,
34+
# we are providing all the functions to the new object
35+
# being created.
2536
list(set = set, get = get,
2637
setInverse = setInverse,
2738
getInverse = getInverse)

tests/1.R

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,47 @@ test.MCM <- function() {
66
testMatrix <- matrix(c(4,2,7,6),2,2)
77
newmat <- matrix(c(1,2,3,4),2,2)
88
testMCM <- makeCacheMatrix(testMatrix)
9-
checkEquals(testMatrix, testMCM$get())
10-
checkEquals(NULL,testMCM$getInverse())
9+
#check that matrix is being set properly
10+
checkEquals(testMatrix, testMCM$get()) #1
11+
# check that inverse variable properly initialized to NULL
12+
checkEquals(NULL,testMCM$getInverse()) #2
1113
testMCM$set(newmat)
12-
checkEquals(newmat, testMCM$get())
13-
checkEquals(NULL, testMCM$getInverse())
14+
# check that setter is working properly
15+
checkEquals(newmat, testMCM$get()) #3
16+
# check that inverse stays null after set
17+
checkEquals(NULL, testMCM$getInverse()) #4
1418
newSol <- cacheSolve(testMCM)
15-
checkEquals(matrix(c(-2,1,1.5,-0.5),2,2),newSol)
16-
checkEquals(newSol, testMCM$getInverse())
19+
#check that cacheSolve gives the correct answer (matrix newmat)
20+
checkEquals(matrix(c(-2,1,1.5,-0.5),2,2),newSol) #5
21+
# check that there is now a value for inverse and that
22+
# getInverse works properly
23+
checkEquals(newSol, testMCM$getInverse()) #6
1724
testMCM$set(testMatrix)
18-
checkEquals(NULL,testMCM$getInverse())
25+
# check that setting the matrix resets inverse to NULL
26+
checkEquals(NULL,testMCM$getInverse()) #7
1927
testSol <- cacheSolve(testMCM)
20-
checkEquals(matrix(c(0.6,-0.2,-0.7,0.4),2,2),testSol)
21-
checkTrue(!is.null(testMCM$getInverse()))
22-
checkEquals(testMCM$getInverse(),testSol)
28+
# check for the correct answer with another matrix (testMatrix)
29+
checkEquals(matrix(c(0.6,-0.2,-0.7,0.4),2,2),testSol) #8
30+
# check that the inverse cache is now set
31+
checkTrue(!is.null(testMCM$getInverse())) #9
32+
# check that the cached value is correct
33+
checkEquals(testMCM$getInverse(),testSol) #10
34+
# this set of checks creates two makeCacheMatrix objects and
35+
# checks to make sure their caches don't interfere
36+
testMCM$set(testMatrix)
37+
newMCM <- makeCacheMatrix(newmat)
38+
checkTrue(is.null(testMCM$getInverse())) #11
39+
checkTrue(is.null(newMCM$getInverse())) #12
40+
checkTrue(is.null(newMCM$inverse)) #13
41+
checkTrue(is.null(testMCM$inverse)) #14
42+
cacheSolve(testMCM)
43+
checkTrue(!is.null(testMCM$getInverse())) #15
44+
checkTrue(is.null(newMCM$getInverse())) #16
45+
# now we've confirmed that testMCM has a cached inverse
46+
# and newMCM doesn't, and that the value of xxxMCM$inverse
47+
# was null before, now we'll check that they were null
48+
# because you can't pull the data out that way, not
49+
# because the cache hadn't been set yet
50+
checkTrue(is.null(testMCM$inverse)) #17
51+
checkTrue(is.null(newMCM$inverse)) #18
2352
}

0 commit comments

Comments
 (0)