Skip to content

Commit df9f289

Browse files
committed
unit test solutions
1 parent a7b7d95 commit df9f289

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

unittesting.Rmd

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ isExactIn("a", c("abc", letters))
164164
isExactIn(c("a", "z"), c("abc", letters))
165165
```
166166

167-
<!-- ### Solution -->
167+
### Solution
168168

169-
```{r, echo=FALSE, eval=FALSE}
169+
```{r}
170170
## Unit test:
171171
library("RUnit")
172172
test_isExactIn <- function() {

unittesting.md

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

5959
```
60-
## [1] "B" "Y" "A" "T" "J"
60+
## [1] "I" "K" "S" "Y" "W"
6161
```
6262
But
6363

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

7070
```
71-
## [1] "B" "Y" "A" "T" "J" NA
71+
## [1] "I" "K" "S" "Y" "W" NA
7272
```
7373

7474
### Solution
@@ -263,9 +263,37 @@ isExactIn(c("a", "z"), c("abc", letters))
263263
## [1] "abc" "a"
264264
```
265265

266-
<!-- ### Solution -->
266+
### Solution
267+
267268

269+
```r
270+
## Unit test:
271+
library("RUnit")
272+
test_isExactIn <- function() {
273+
checkIdentical("a", isExactIn("a", letters))
274+
checkIdentical("a", isExactIn("a", c("abc", letters)))
275+
checkIdentical(c("a", "z"), isExactIn(c("a", "z"), c("abc", letters)))
276+
}
268277

278+
test_isExactIn()
279+
```
280+
281+
```
282+
## Error in checkIdentical("a", isExactIn("a", c("abc", letters))): FALSE
283+
##
284+
```
285+
286+
```r
287+
## updated function:
288+
isExactIn <- function(x, y)
289+
x[x %in% y]
290+
291+
test_isExactIn()
292+
```
293+
294+
```
295+
## [1] TRUE
296+
```
269297

270298
## If conditions with length > 1
271299

@@ -387,12 +415,12 @@ y <- rnorm(5)
387415
```
388416

389417
```
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
418+
## x y
419+
## [1,] -3.1164934 -1.81741540
420+
## [2,] 1.5741209 -0.05377479
421+
## [3,] -0.6298675 0.68829382
422+
## [4,] -1.3595685 -1.09019047
423+
## [5,] 0.9666774 0.76398426
396424
```
397425

398426
```r
@@ -401,15 +429,15 @@ y <- rnorm(5)
401429

402430
```
403431
## x y
404-
## 2.3011991 0.6093975
432+
## -3.116493 -1.817415
405433
```
406434

407435
```r
408436
distances(p, m)
409437
```
410438

411439
```
412-
## [1] 0.000000 1.280700 1.063910 4.162217 3.750287
440+
## [1] 0.000000 5.011217 3.530140 1.901484 4.830725
413441
```
414442

415443
```r
@@ -418,21 +446,21 @@ distances(p, m)
418446
```
419447

420448
```
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
449+
## x y
450+
## 1 -3.1164934 -1.81741540
451+
## 2 1.5741209 -0.05377479
452+
## 3 -0.6298675 0.68829382
453+
## 4 -1.3595685 -1.09019047
454+
## 5 0.9666774 0.76398426
427455
```
428456

429457
```r
430458
(q <- dd[1, ])
431459
```
432460

433461
```
434-
## x y
435-
## 1 2.301199 0.6093975
462+
## x y
463+
## 1 -3.116493 -1.817415
436464
```
437465

438466
```r

0 commit comments

Comments
 (0)