tests/testthat/test-imap.r

context("imap iterator")

pow <- function(x, y) {
  x^y
}

test_that("imap over two numeric vectors of equal length", {
  it <- imap(pow, c(2, 3, 10), c(5, 2, 3))

  expect_equal(nextElem(it), 32)
  expect_equal(nextElem(it), 9)
  expect_equal(nextElem(it), 1000)

  expect_error(nextElem(it), "StopIteration")
})

test_that("imap over two numeric vectors of unequal length", {
  it <- imap(pow, c(2, 3, 10), c(5, 2, 3, 42))

  expect_equal(nextElem(it), 32)
  expect_equal(nextElem(it), 9)
  expect_equal(nextElem(it), 1000)

  expect_error(nextElem(it), "StopIteration")
})

test_that("imap over two lists of equal length", {
  it <- imap(pow, list(2, 3, 10), list(5, 2, 3))

  expect_equal(nextElem(it), 32)
  expect_equal(nextElem(it), 9)
  expect_equal(nextElem(it), 1000)

  expect_error(nextElem(it), "StopIteration")
})

test_that("imap over two lists of unequal length", {
  it <- imap(pow, list(2, 3, 10), list(5, 2, 3, 42))

  expect_equal(nextElem(it), 32)
  expect_equal(nextElem(it), 9)
  expect_equal(nextElem(it), 1000)

  expect_error(nextElem(it), "StopIteration")
})

Try the itertools2 package in your browser

Any scripts or data that you put into this service are public.

itertools2 documentation built on May 2, 2019, 3:37 p.m.