@@ -6,18 +6,47 @@ test.MCM <- function() {
6
6
testMatrix <- matrix (c(4 ,2 ,7 ,6 ),2 ,2 )
7
7
newmat <- matrix (c(1 ,2 ,3 ,4 ),2 ,2 )
8
8
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
11
13
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
14
18
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
17
24
testMCM $ set(testMatrix )
18
- checkEquals(NULL ,testMCM $ getInverse())
25
+ # check that setting the matrix resets inverse to NULL
26
+ checkEquals(NULL ,testMCM $ getInverse()) # 7
19
27
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
23
52
}
0 commit comments