File: test_bignum.R

package info (click to toggle)
r-cran-openssl 1.4.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,288 kB
  • sloc: ansic: 3,021; sh: 97; makefile: 5
file content (49 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download | duplicates (4)
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
43
44
45
46
47
48
49
context("Bignum")

test_that("Basic bignum math", {
  # Regular numbers
  x1 <- 123
  y1 <- 456

  # Bignum numbers
  x2 <- bignum(123)
  y2 <- bignum(456)

  expect_true(x1 == x2)
  expect_true(y1 == y2)
  expect_true(x2 != y2)
  expect_true(x2 < y2)
  expect_true(x2 <= y2)
  expect_false(x2 > y2)
  expect_false(x2 >= y2)
  expect_false(x1 == y2)
  expect_true(x1+y1 == x2+y2)
  expect_true(y1-x1 == y2-x2)
  expect_true(x1*y1 == x2*y2)
  expect_true(y2-y2 == 0)
  expect_true(y2 %% x2 == y1 %% x1)
  expect_true(y2 %/% x2 == y1 %/% x1)
  expect_true(x2 %% y2 == x1 %% y1)
  expect_error(x2-y2, "")
  expect_error(x2/y2, "integer")
})

test_that("Bignum arithmetic", {
  x <- bignum(123L)
  y <- bignum("123456789123456789")
  z <- bignum("D41D8CD98F00B204E9800998ECF8427E", hex = TRUE)

  # Basic arithmetic
  div <- z %/% y
  mod <- z %% y
  z2 <- div * y + mod
  expect_equal(z2, z)
  expect_true(div < z)
})

test_that("Bignum to integer", {
  for(x in c(0, 123, .Machine$integer.max)){
    expect_equal(x, as.integer(bignum(x)))
  }
  expect_equal(as.integer(bignum(.Machine$integer.max + 1)), NA_integer_)
})