File: whenTest.R

package info (click to toggle)
r-cran-foreach 1.3.0-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 628 kB
  • ctags: 4
  • sloc: sh: 76; makefile: 1
file content (42 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
test01 <- function() {
  actual <-
    foreach(i=1:5) %:%
      when(i %% 2 == 1) %:%
        foreach(j=1:5) %:%
          when(j %% 2 == 1 && i != j) %do%
            c(i, j)
  expected <- list(list(c(1, 3), c(1, 5)),
                   list(c(3, 1), c(3, 5)),
                   list(c(5, 1), c(5, 3)))
  checkEquals(actual, expected)

  actual <-
    foreach(i=1:5, .combine='c') %:%
      when(i %% 2 == 1) %:%
        foreach(j=1:5) %:%
          when(j %% 2 == 1 && i != j) %do%
            c(i, j)
  expected <- list(c(1, 3), c(1, 5),
                   c(3, 1), c(3, 5),
                   c(5, 1), c(5, 3))
  checkEquals(actual, expected)
}

test02 <- function() {
  qsort <- function(x) {
    n <- length(x)
    if (n == 0) {
      x
    } else {
      p <- sample(n, 1)
      smaller <- foreach(y=x[-p], .combine=c) %:% when(y <= x[p]) %do% y
      larger  <- foreach(y=x[-p], .combine=c) %:% when(y >  x[p]) %do% y
      c(qsort(smaller), x[p], qsort(larger))
    }
  }

  x <- runif(100)
  a <- qsort(x)
  b <- sort(x)
  checkEquals(a, b)
}