tests/testthat/test-rational-operators.R

testthat::context("test-rational-operators")

testthat::test_that("Test Addition", {
  expect_error(rational(c(1L,2L),c(3L,5L), "S4") + rational(1L, 2L, "S4"))
  expect_true(is.na(rational(1L, 2L, "S3") + "test"))
  expect_true(is.na(rational(1L, 2L, "R6") + "test"))

  a <- rational(1L,2L,"S4")
  b <- rational(3L,5L,"S4")
  d <- a + b
  expect_true(d@n == 11)
  expect_true(d@d == 10)
  a <- 7L
  b <- rational(3L,5L,"S4")
  d <- a + b
  expect_true(d@n == 38)
  expect_true(d@d == 5)
  a <- rational(1L,2L,"S4")
  b <- 7L
  d <- a + b
  expect_true(d@n == 15)
  expect_true(d@d == 2)
  a <- 7
  b <- rational(3L,5L,"S4")
  d <- a + b
  expect_true(abs(d - 7.6) < 1E-12)
  a <- rational(1L,2L,"S4")
  b <- 7
  d <- a + b
  expect_true(abs(d - 7.5) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- rational(3L,5L,"S3")
  d <- a + b
  expect_true(d$n == 11)
  expect_true(d$d == 10)
  a <- 7L
  b <- rational(3L,5L,"S3")
  d <- a + b
  expect_true(d$n == 38)
  expect_true(d$d == 5)
  a <- rational(1L,2L,"S3")
  b <- 7L
  d <- a + b
  expect_true(d$n == 15)
  expect_true(d$d == 2)
  a <- 7
  b <- rational(3L,5L,"S3")
  d <- a + b
  expect_true(abs(d - 7.6) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- 7
  d <- a + b
  expect_true(abs(d - 7.5) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  d <- a + b
  expect_true(d$getNumerator() == 11)
  expect_true(d$getDenominator() == 10)
  a <- 7L
  b <- rational(3L,5L,"R6")
  d <- a + b
  expect_true(d$getNumerator() == 38)
  expect_true(d$getDenominator() == 5)
  a <- rational(1L,2L,"R6")
  b <- 7L
  d <- a + b
  expect_true(d$getNumerator() == 15)
  expect_true(d$getDenominator() == 2)
  a <- 7
  b <- rational(3L,5L,"R6")
  d <- a + b
  expect_true(abs(d - 7.6) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- 7
  d <- a + b
  expect_true(abs(d - 7.5) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  a$add(b)
  expect_true(a$getNumerator() == 11)
  expect_true(a$getDenominator() == 10)
  a <- rational(1L,2L,"R6")
  b <- 7L
  a$add(b)
  expect_true(a$getNumerator() == 15)
  expect_true(a$getDenominator() == 2)
})

testthat::test_that("Test Multiply", {
  expect_error(rational(c(1L,2L),c(3L,5L), "S4") * rational(1L, 2L, "S4"))
  expect_true(is.na(rational(1L, 2L, "S3") * "test"))
  expect_true(is.na(rational(1L, 2L, "R6") * "test"))

  a <- rational(1L,2L,"S4")
  b <- rational(3L,5L,"S4")
  d <- a * b
  expect_true(d@n == 3)
  expect_true(d@d == 10)
  a <- 7L
  b <- rational(3L,5L,"S4")
  d <- a * b
  expect_true(d@n == 21)
  expect_true(d@d == 5)
  a <- rational(1L,2L,"S4")
  b <- 7L
  d <- a * b
  expect_true(d@n == 7)
  expect_true(d@d == 2)
  a <- 7
  b <- rational(3L,5L,"S4")
  d <- a * b
  expect_true(abs(d - 4.2) < 1E-12)
  a <- rational(1L,2L,"S4")
  b <- 7
  d <- a * b
  expect_true(abs(d - 3.5) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- rational(3L,5L,"S3")
  d <- a * b
  expect_true(d$n == 3)
  expect_true(d$d == 10)
  a <- 7L
  b <- rational(3L,5L,"S3")
  d <- a * b
  expect_true(d$n == 21)
  expect_true(d$d == 5)
  a <- rational(1L,2L,"S3")
  b <- 7L
  d <- a * b
  expect_true(d$n == 7)
  expect_true(d$d == 2)
  a <- 7
  b <- rational(3L,5L,"S3")
  d <- a * b
  expect_true(abs(d - 4.2) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- 7
  d <- a * b
  expect_true(abs(d - 3.5) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  d <- a * b
  expect_true(d$getNumerator() == 3)
  expect_true(d$getDenominator() == 10)
  a <- 7L
  b <- rational(3L,5L,"R6")
  d <- a * b
  expect_true(d$getNumerator() == 21)
  expect_true(d$getDenominator() == 5)
  a <- rational(1L,2L,"R6")
  b <- 7L
  d <- a * b
  expect_true(d$getNumerator() == 7)
  expect_true(d$getDenominator() == 2)
  a <- 7
  b <- rational(3L,5L,"R6")
  d <- a * b
  expect_true(abs(d - 4.2) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- 7
  d <- a * b
  expect_true(abs(d - 3.5) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  a$multiply(b)
  expect_true(a$getNumerator() == 3)
  expect_true(a$getDenominator() == 10)
  a <- rational(1L,2L,"R6")
  b <- 7L
  a$multiply(b)
  expect_true(a$getNumerator() == 7)
  expect_true(a$getDenominator() == 2)
})

testthat::test_that("Test Divide", {
  expect_true(is.na(rational(1L, 2L, "S3") / "test"))
  expect_true(is.na(rational(1L, 2L, "R6") / "test"))

  a <- rational(1L,2L,"S4")
  b <- rational(3L,5L,"S4")
  d <- a / b
  expect_true(d@n == 5)
  expect_true(d@d == 6)
  a <- 7L
  b <- rational(3L,5L,"S4")
  d <- a / b
  expect_true(d@n == 35)
  expect_true(d@d == 3)
  a <- rational(1L,2L,"S4")
  b <- 7L
  d <- a / b
  expect_true(d@n == 1)
  expect_true(d@d == 14)
  a <- 7
  b <- rational(3L,5L,"S4")
  d <- a / b
  expect_true(abs(d - 7*5/3) < 1E-12)
  a <- rational(1L,2L,"S4")
  b <- 7
  d <- a / b
  expect_true(abs(d - 1/14) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- rational(3L,5L,"S3")
  d <- a / b
  expect_true(d$n == 5)
  expect_true(d$d == 6)
  a <- 7L
  b <- rational(3L,5L,"S3")
  d <- a / b
  expect_true(d$n == 35)
  expect_true(d$d == 3)
  a <- rational(1L,2L,"S3")
  b <- 7L
  d <- a / b
  expect_true(d$n == 1)
  expect_true(d$d == 14)
  a <- 7
  b <- rational(3L,5L,"S3")
  d <- a / b
  expect_true(abs(d - 7*5/3) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- 7
  d <- a / b
  expect_true(abs(d - 1/14) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  d <- a / b
  expect_true(d$getNumerator() == 5)
  expect_true(d$getDenominator() == 6)
  a <- 7L
  b <- rational(3L,5L,"R6")
  d <- a / b
  expect_true(d$getNumerator() == 35)
  expect_true(d$getDenominator() == 3)
  a <- rational(1L,2L,"R6")
  b <- 7L
  d <- a / b
  expect_true(d$getNumerator() == 1)
  expect_true(d$getDenominator() == 14)
  a <- 7
  b <- rational(3L,5L,"R6")
  d <- a / b
  expect_true(abs(d - 7*5/3) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- 7
  d <- a / b
  expect_true(abs(d - 1/14) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  a$divide(b)
  expect_true(a$getNumerator() == 5)
  expect_true(a$getDenominator() == 6)
  a <- rational(1L,2L,"R6")
  b <- 7L
  a$divide(b)
  expect_true(a$getNumerator() == 1)
  expect_true(a$getDenominator() == 14)
})

testthat::test_that("Test Subtract", {
  expect_true(is.na(rational(1L, 2L, "S3") - "test"))
  expect_true(is.na(rational(1L, 2L, "R6") - "test"))

  a <- rational(1L,2L,"S4")
  b <- rational(3L,5L,"S4")
  d <- a - b
  expect_true(d@n == -1)
  expect_true(d@d == 10)
  a <- 7L
  b <- rational(3L,5L,"S4")
  d <- a - b
  expect_true(d@n == 32)
  expect_true(d@d == 5)
  a <- rational(1L,2L,"S4")
  b <- 7L
  d <- a - b
  expect_true(d@n == -13)
  expect_true(d@d == 2)
  a <- 7
  b <- rational(3L,5L,"S4")
  d <- a - b
  expect_true(abs(d - 6.4) < 1E-12)
  a <- rational(1L,2L,"S4")
  b <- 7
  d <- a - b
  expect_true(abs(d - -6.5) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- rational(3L,5L,"S3")
  d <- a - b
  expect_true(d$n == -1)
  expect_true(d$d == 10)
  a <- 7L
  b <- rational(3L,5L,"S3")
  d <- a - b
  expect_true(d$n == 32)
  expect_true(d$d == 5)
  a <- rational(1L,2L,"S3")
  b <- 7L
  d <- a - b
  expect_true(d$n == -13)
  expect_true(d$d == 2)
  a <- 7
  b <- rational(3L,5L,"S3")
  d <- a - b
  expect_true(abs(d - 6.4) < 1E-12)
  a <- rational(1L,2L,"S3")
  b <- 7
  d <- a - b
  expect_true(abs(d - -6.5) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  d <- a - b
  expect_true(d$getNumerator() == -1)
  expect_true(d$getDenominator() == 10)
  a <- 7L
  b <- rational(3L,5L,"R6")
  d <- a - b
  expect_true(d$getNumerator() == 32)
  expect_true(d$getDenominator() == 5)
  a <- rational(1L,2L,"R6")
  b <- 7L
  d <- a - b
  expect_true(d$getNumerator() == -13)
  expect_true(d$getDenominator() == 2)
  a <- 7
  b <- rational(3L,5L,"R6")
  d <- a - b
  expect_true(abs(d - 6.4) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- 7
  d <- a - b
  expect_true(abs(d - -6.5) < 1E-12)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  a$subtract(b)
  expect_true(a$getNumerator() == -1)
  expect_true(a$getDenominator() == 10)
  a <- rational(1L,2L,"R6")
  b <- 7L
  a$subtract(b)
  expect_true(a$getNumerator() == -13)
  expect_true(a$getDenominator() == 2)

})

testthat::test_that("Test Integer Divistion", {
  expect_true(is.na(rational(1L, 2L, "S3") %/% "test"))
  expect_true(is.na(rational(1L, 2L, "R6") %/% "test"))

  a <- rational(3L,1L,"S4")
  b <- rational(2L,1L,"S4")
  expect_equal(1, a %/% b)
  a <- 3L
  b <- rational(2L,1L,"S4")
  expect_equal(1, a %/% b)
  a <- rational(3L,1L,"S4")
  b <- 2L
  expect_equal(1, a %/% b)
  a <- 3.5
  b <- rational(2L,1L,"S4")
  expect_equal(1, a %/% b)
  a <- rational(3L,1L,"S4")
  b <- 2.1
  expect_equal(1, a %/% b)
  a <- rational(1L,2L,"S3")
  b <- rational(3L,5L,"S3")
  d <- 3L
  e <- 1.5
  expect_equal(0, a %/% b)
  expect_equal(5, d %/% b)
  expect_equal(0, a %/% d)
  expect_equal(2, e %/% b)
  expect_equal(2, d %/% e)
  a <- rational(1L,2L,"R6")
  b <- rational(3L,5L,"R6")
  d <- 3L
  e <- 1.5
  expect_equal(0, a %/% b)
  expect_equal(5, d %/% b)
  expect_equal(0, a %/% d)
  expect_equal(2, e %/% b)
  expect_equal(2, d %/% e)

})

testthat::test_that("Test Mod", {
  a <- rational(5L, 1L, "S4")
  b <- rational(2L, 1L, "S4")
  d <- rational(3L, 2L, "S4")
  e <- rational(1L, 2L, "S4")
  expect_true(rational(1L, 1L, "S4") == a %% b)
  expect_true(rational(0L, 1L, "S4") == d %% e)
  expect_true(rational(1L, 1L, "S4") == 5L %% b)
  expect_true(rational(1L, 1L, "S4") == a %% 2L)
  expect_true(rational(0L, 1L, "S4") == 2L %% e)
  expect_equal(1, 5 %% b)
  expect_equal(1, a %% 2)

  a <- rational(5L, 1L, "S3")
  b <- rational(2L, 1L, "S3")
  d <- rational(3L, 2L, "S3")
  e <- rational(1L, 2L, "S3")
  expect_true(rational(1L, 1L, "S3") == a %% b)
  expect_true(rational(0L, 1L, "S3") == d %% e)
  expect_true(rational(1L, 1L, "S3") == 5L %% b)
  expect_true(rational(1L, 1L, "S3") == a %% 2L)
  expect_true(rational(0L, 1L, "S3") == 2L %% e)
  expect_equal(1, 5 %% b)
  expect_equal(1, a %% 2)

  a <- rational(5L, 1L, "R6")
  b <- rational(2L, 1L, "R6")
  d <- rational(3L, 2L, "R6")
  e <- rational(1L, 2L, "R6")
  expect_true(rational(1L, 1L, "R6") == a %% b)
  expect_true(rational(0L, 1L, "R6") == d %% e)
  expect_true(rational(1L, 1L, "R6") == 5L %% b)
  expect_true(rational(1L, 1L, "R6") == a %% 2L)
  expect_true(rational(0L, 1L, "R6") == 2L %% e)
  expect_equal(1, 5 %% b)
  expect_equal(1, a %% 2)
})
bertcarnell/rational documentation built on May 10, 2021, 8:32 p.m.