@@ -57,7 +57,7 @@ isIn(x, LETTERS)
57
57
```
58
58
59
59
```
60
- ## [1] "U " "S " "L " "D " "O "
60
+ ## [1] "B " "Y " "A " "T " "J "
61
61
```
62
62
But
63
63
@@ -68,7 +68,7 @@ isIn(c(x, "a"), LETTERS)
68
68
```
69
69
70
70
```
71
- ## [1] "U " "S " "L " "D " "O " NA
71
+ ## [1] "B " "Y " "A " "T " "J " NA
72
72
```
73
73
74
74
### Solution
@@ -323,9 +323,43 @@ ifcond(3:1, c(2, 2, 2))
323
323
## [1] 5 0 -3
324
324
```
325
325
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 )
327
356
357
+ test_ifcond()
358
+ ```
328
359
360
+ ```
361
+ ## [1] TRUE
362
+ ```
329
363
330
364
## Know your inputs
331
365
@@ -353,29 +387,29 @@ y <- rnorm(5)
353
387
```
354
388
355
389
```
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
362
396
```
363
397
364
398
``` r
365
399
(p <- m [1 , ])
366
400
```
367
401
368
402
```
369
- ## x y
370
- ## 0.4386955 -0.3399889
403
+ ## x y
404
+ ## 2.3011991 0.6093975
371
405
```
372
406
373
407
``` r
374
408
distances(p , m )
375
409
```
376
410
377
411
```
378
- ## [1] 0.0000000 0.7578129 1.8617414 0.2778016 0.6051608
412
+ ## [1] 0.000000 1.280700 1.063910 4.162217 3.750287
379
413
```
380
414
381
415
``` r
@@ -384,21 +418,21 @@ distances(p, m)
384
418
```
385
419
386
420
```
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
393
427
```
394
428
395
429
``` r
396
430
(q <- dd [1 , ])
397
431
```
398
432
399
433
```
400
- ## x y
401
- ## 1 0.4386955 -0.3399889
434
+ ## x y
435
+ ## 1 2.301199 0.6093975
402
436
```
403
437
404
438
``` r
@@ -410,9 +444,49 @@ distances(q, dd)
410
444
## 1 0
411
445
```
412
446
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
+ }
414
483
484
+ test_distances()
485
+ ```
415
486
487
+ ```
488
+ ## [1] TRUE
489
+ ```
416
490
417
491
## Iterate on 0 length
418
492
@@ -448,9 +522,49 @@ sqrtabs(numeric())
448
522
## numeric(0)
449
523
```
450
524
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
+ ```
452
537
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
+ ```
453
564
565
+ ```
566
+ ## [1] TRUE
567
+ ```
454
568
455
569
# Unit testing in a package
456
570
0 commit comments