Skip to content

Commit a7b7d95

Browse files
committed
unit test solutions
1 parent 112052c commit a7b7d95

File tree

2 files changed

+143
-29
lines changed

2 files changed

+143
-29
lines changed

Diff for: unittesting.Rmd

+7-7
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ ifcond(1, 2)
210210
ifcond(3:1, c(2, 2, 2))
211211
```
212212

213-
<!-- ### Solution -->
213+
### Solution
214214

215-
```{r, echo=FALSE, eval=FALSE}
215+
```{r}
216216
## Unit test:
217217
library("RUnit")
218218
test_ifcond <- function() {
@@ -264,9 +264,9 @@ distances(p, m)
264264
distances(q, dd)
265265
```
266266

267-
<!-- ### Solution -->
267+
### Solution
268268

269-
```{r, echo=FALSE, eval=FALSE}
269+
```{r}
270270
## Unit test:
271271
library("RUnit")
272272
test_distances <- function() {
@@ -315,9 +315,9 @@ all(sqrtabs(c(-4, 0, 4)) == c(2, 0, 2))
315315
sqrtabs(numeric())
316316
```
317317

318-
<!-- ### Solution -->
318+
### Solution
319319

320-
```{r, echo=FALSE, eval=FALSE}
320+
```{r}
321321
## Unit test:
322322
library(RUnit)
323323
test_sqrtabs <- function() {
@@ -456,4 +456,4 @@ coverage <- package_coverage("/path/to/package/source", type="all")
456456
shine(coverage)
457457
```
458458

459-
[Coverage for all Bioconductor packages](https://codecov.io/github/Bioconductor-mirror).
459+
[Coverage for all Bioconductor packages](https://codecov.io/github/Bioconductor-mirror).

Diff for: unittesting.md

+136-22
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ isIn(x, LETTERS)
5757
```
5858

5959
```
60-
## [1] "U" "S" "L" "D" "O"
60+
## [1] "B" "Y" "A" "T" "J"
6161
```
6262
But
6363

@@ -68,7 +68,7 @@ isIn(c(x, "a"), LETTERS)
6868
```
6969

7070
```
71-
## [1] "U" "S" "L" "D" "O" NA
71+
## [1] "B" "Y" "A" "T" "J" NA
7272
```
7373

7474
### Solution
@@ -323,9 +323,43 @@ ifcond(3:1, c(2, 2, 2))
323323
## [1] 5 0 -3
324324
```
325325

326-
<!-- ### Solution -->
326+
### Solution
327+
328+
329+
```r
330+
## Unit test:
331+
library("RUnit")
332+
test_ifcond <- function() {
333+
checkIdentical(5, ifcond(3, 2))
334+
checkIdentical(8, ifcond(2, 2))
335+
checkIdentical(5, ifcond(1, 2))
336+
checkIdentical(c(5, 8, 5), ifcond(3:1, c(2, 2, 2)))
337+
}
338+
339+
test_ifcond()
340+
```
341+
342+
```
343+
## Warning in if (x > y) {: the condition has length > 1 and only the first
344+
## element will be used
345+
```
346+
347+
```
348+
## Error in checkIdentical(c(5, 8, 5), ifcond(3:1, c(2, 2, 2))): FALSE
349+
##
350+
```
351+
352+
```r
353+
## updated function:
354+
ifcond <- function(x, y)
355+
ifelse(x > y, x*x - y*y, x*x + y*y)
327356

357+
test_ifcond()
358+
```
328359

360+
```
361+
## [1] TRUE
362+
```
329363

330364
## Know your inputs
331365

@@ -353,29 +387,29 @@ y <- rnorm(5)
353387
```
354388

355389
```
356-
## x y
357-
## [1,] 0.43869553 -0.3399889
358-
## [2,] -0.06406455 0.2270320
359-
## [3,] -1.27446180 0.3888273
360-
## [4,] 0.66013277 -0.5077365
361-
## [5,] 0.08794117 -0.8331329
390+
## x y
391+
## [1,] 2.301199 0.60939751
392+
## [2,] 1.380715 1.49984771
393+
## [3,] 1.237685 0.58034293
394+
## [4,] -1.723762 -0.45066731
395+
## [5,] -1.389801 -0.05480788
362396
```
363397

364398
```r
365399
(p <- m[1, ])
366400
```
367401

368402
```
369-
## x y
370-
## 0.4386955 -0.3399889
403+
## x y
404+
## 2.3011991 0.6093975
371405
```
372406

373407
```r
374408
distances(p, m)
375409
```
376410

377411
```
378-
## [1] 0.0000000 0.7578129 1.8617414 0.2778016 0.6051608
412+
## [1] 0.000000 1.280700 1.063910 4.162217 3.750287
379413
```
380414

381415
```r
@@ -384,21 +418,21 @@ distances(p, m)
384418
```
385419

386420
```
387-
## x y
388-
## 1 0.43869553 -0.3399889
389-
## 2 -0.06406455 0.2270320
390-
## 3 -1.27446180 0.3888273
391-
## 4 0.66013277 -0.5077365
392-
## 5 0.08794117 -0.8331329
421+
## x y
422+
## 1 2.301199 0.60939751
423+
## 2 1.380715 1.49984771
424+
## 3 1.237685 0.58034293
425+
## 4 -1.723762 -0.45066731
426+
## 5 -1.389801 -0.05480788
393427
```
394428

395429
```r
396430
(q <- dd[1, ])
397431
```
398432

399433
```
400-
## x y
401-
## 1 0.4386955 -0.3399889
434+
## x y
435+
## 1 2.301199 0.6093975
402436
```
403437

404438
```r
@@ -410,9 +444,49 @@ distances(q, dd)
410444
## 1 0
411445
```
412446

413-
<!-- ### Solution -->
447+
### Solution
448+
449+
450+
```r
451+
## Unit test:
452+
library("RUnit")
453+
test_distances <- function() {
454+
x <- y <- c(0, 1, 2)
455+
m <- cbind(x, y)
456+
p <- m[1, ]
457+
dd <- data.frame(x, y)
458+
q <- dd[1, ]
459+
expct <- c(0, sqrt(c(2, 8)))
460+
checkIdentical(expct, distances(p, m))
461+
checkIdentical(expct, distances(q, dd))
462+
}
463+
464+
test_distances()
465+
```
466+
467+
```
468+
## Error in checkIdentical(expct, distances(q, dd)): FALSE
469+
##
470+
```
471+
472+
```r
473+
## updated function
474+
distances <- function(point, pointVec) {
475+
point <- as.numeric(point)
476+
x <- point[1]
477+
y <- point[2]
478+
xVec <- pointVec[,1]
479+
yVec <- pointVec[,2]
480+
dist <- sqrt((xVec - x)^2 + (yVec - y)^2)
481+
return(dist)
482+
}
414483

484+
test_distances()
485+
```
415486

487+
```
488+
## [1] TRUE
489+
```
416490

417491
## Iterate on 0 length
418492

@@ -448,9 +522,49 @@ sqrtabs(numeric())
448522
## numeric(0)
449523
```
450524

451-
<!-- ### Solution -->
525+
### Solution
526+
527+
528+
```r
529+
## Unit test:
530+
library(RUnit)
531+
test_sqrtabs <- function() {
532+
checkIdentical(c(2, 0, 2), sqrtabs(c(-4, 0, 4)))
533+
checkIdentical(numeric(), sqrtabs(numeric()))
534+
}
535+
test_sqrtabs()
536+
```
452537

538+
```
539+
## Error in checkIdentical(numeric(), sqrtabs(numeric())): FALSE
540+
##
541+
```
542+
543+
```r
544+
## updated function:
545+
sqrtabs <- function(x) {
546+
v <- abs(x)
547+
sapply(seq_along(v), function(i) sqrt(v[i]))
548+
}
549+
test_sqrtabs() # nope!
550+
```
551+
552+
```
553+
## Error in checkIdentical(numeric(), sqrtabs(numeric())): FALSE
554+
##
555+
```
556+
557+
```r
558+
sqrtabs <- function(x) {
559+
v <- abs(x)
560+
vapply(seq_along(v), function(i) sqrt(v[i]), 0)
561+
}
562+
test_sqrtabs() # yes!
563+
```
453564

565+
```
566+
## [1] TRUE
567+
```
454568

455569
# Unit testing in a package
456570

0 commit comments

Comments
 (0)