Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5cf489d

Browse files
committed
testing the functions
1 parent be5b987 commit 5cf489d

File tree

1 file changed

+48
-48
lines changed

1 file changed

+48
-48
lines changed

cachematrix.R

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,52 +56,52 @@ cacheSolve <- function(x, ...) {
5656

5757
## Tests the functions
5858
run.tests <- function() {
59-
message("Test #1: with a 2x2 matrix")
60-
m <- matrix(c(4, 2, 7, 6), 2, 2)
61-
62-
# The expected inverse of the above matrix
63-
mi <- matrix(c(0.6, -0.2, -0.7, 0.4), 2, 2)
64-
65-
# Initializes the closure with the above matrix
66-
cm <- makeCacheMatrix(m)
67-
print(cm$get())
68-
69-
# Gets the inverse of the above function
70-
xi <- cacheSolve(cm)
71-
print(cm$getSolve())
72-
73-
## Calculates the inverse again, it should return the cached value
74-
xi <- cacheSolve(cm)
75-
76-
## Is this really the inverse matrix?
77-
## This tests compares the inverse matrix returned with the one
78-
## that was expected
79-
if(isTRUE(all.equal(xi, mi))) {
80-
message("Test #1 OK")
81-
} else {
82-
message("Test #1 FAIL")
83-
}
84-
85-
# Test #2: With a 3x3 matrix
86-
message("Test #2: with a 3x3 matrix")
87-
m <- matrix(c(3, 2, 0, 0, 0, 1, 2, -2, 1), 3, 3)
88-
89-
cm <- makeCacheMatrix(m)
90-
print(cm$get())
91-
92-
## Calculates the inverse matrix. It should not use the chache
93-
xi <- cacheSolve(cm)
94-
print(cm$getSolve())
95-
96-
## Once again requests the inverse, this time from the cache
97-
xi <- cacheSolve(cm)
98-
99-
## Is the returned matrix really the inverse?
100-
## This actually tests if the matrix multiplied by its inverse
101-
## is equal to a 3x3 identity matrix
102-
if(isTRUE(all.equal(m %*% xi, diag(3)))) {
103-
message("Test #2 OK")
104-
} else {
105-
message("Test #2 FAIL")
106-
}
59+
message("Test #1: with a 2x2 matrix")
60+
m <- matrix(c(4, 2, 7, 6), 2, 2)
61+
62+
# The expected inverse of the above matrix
63+
mi <- matrix(c(0.6, -0.2, -0.7, 0.4), 2, 2)
64+
65+
# Initializes the closure with the above matrix
66+
cm <- makeCacheMatrix(m)
67+
print(cm$get())
68+
69+
# Gets the inverse of the above function
70+
xi <- cacheSolve(cm)
71+
print(cm$getSolve())
72+
73+
## Calculates the inverse again, it should return the cached value
74+
xi <- cacheSolve(cm)
75+
76+
## Is this really the inverse matrix?
77+
## This tests compares the inverse matrix returned with the one
78+
## that was expected
79+
if(isTRUE(all.equal(xi, mi))) {
80+
message("Test #1 OK")
81+
} else {
82+
message("Test #1 FAIL")
83+
}
84+
85+
# Test #2: With a 3x3 matrix
86+
message("Test #2: with a 3x3 matrix")
87+
m <- matrix(c(3, 2, 0, 0, 0, 1, 2, -2, 1), 3, 3)
88+
89+
cm <- makeCacheMatrix(m)
90+
print(cm$get())
91+
92+
## Calculates the inverse matrix. It should not use the chache
93+
xi <- cacheSolve(cm)
94+
print(cm$getSolve())
95+
96+
## Once again requests the inverse, this time from the cache
97+
xi <- cacheSolve(cm)
98+
99+
## Is the returned matrix really the inverse?
100+
## This actually tests if the matrix multiplied by its inverse
101+
## is equal to a 3x3 identity matrix
102+
if(isTRUE(all.equal(m %*% xi, diag(3)))) {
103+
message("Test #2 OK")
104+
} else {
105+
message("Test #2 FAIL")
106+
}
107107
}

0 commit comments

Comments
 (0)