File: test-convert.R

package info (click to toggle)
r-cran-dqrng 0.2.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 452 kB
  • sloc: cpp: 615; ansic: 154; sh: 14; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download | duplicates (3)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
context("conversion")

safe_expect_error <- function(..., msg) {
  os.type <- Sys.info()["sysname"]
  if (os.type == "Darwin") {
    expect_error(...)
  } else {
    expect_error(..., msg)
  }
}

Rcpp::sourceCpp("cpp/convert.cpp") # Warnings about shifts can be ignored.

test_that("conversion to 16-bit integers works correctly", {
    expect_identical(convert_16(0), "0")
    expect_identical(convert_16(65535), "65535")

    # Handles vectors.
    expect_identical(convert_16(c(0, 0, 0)), "0")
    expect_identical(convert_16(c(0, 65535)), "65535")
    expect_identical(convert_16(c(0, 0, 65535)), "65535")

    # Reports errors.
    skip_on_os("solaris")
    safe_expect_error(convert_16(-1), msg = "seed element out of range")
    safe_expect_error(convert_16(NA_integer_), msg = "seed element out of range")
    safe_expect_error(convert_16(65536), msg = "seed element out of range")
    safe_expect_error(convert_16(c(1, 0)), msg = "vector implies an out-of-range seed")
})

test_that("conversion to 32-bit integers works correctly", {
    expect_identical(convert_32(0), "0")
    expect_identical(convert_32(.Machine$integer.max), "2147483647")
    expect_identical(convert_32(NA_integer_), "2147483648")
    expect_identical(convert_32(-1), "4294967295")

    # Handles vectors.
    expect_identical(convert_32(c(0, 0, 0)), "0")
    expect_identical(convert_32(c(0, .Machine$integer.max)), "2147483647")
    expect_identical(convert_32(c(0, 0, -1)), "4294967295")

    # Reports errors.
    skip_on_os("solaris")
    safe_expect_error(convert_32(c(1, 0)), msg = "vector implies an out-of-range seed")
})

test_that("conversion to 64-bit integers works correctly", {
    expect_identical(convert_64(0), "0")
    expect_identical(convert_64(c(0, 0)), "0")
    expect_identical(convert_64(c(0, 0, 0)), "0")

    expect_identical(convert_64(c(0, .Machine$integer.max)), "2147483647")
    expect_identical(convert_64(c(0, 0, NA_integer_)), "2147483648")
    expect_identical(convert_64(c(0, -1)), "4294967295")
    expect_identical(convert_64(c(0, 0, NA_integer_)), "2147483648")

    expect_identical(convert_64(c(1, 0)), "4294967296")
    expect_identical(convert_64(c(1, .Machine$integer.max)), "6442450943")
    expect_identical(convert_64(c(1, NA_integer_)), "6442450944")
    expect_identical(convert_64(c(1, -1)), "8589934591")

    expect_identical(convert_64(c(-1, 0)), "18446744069414584320")
    expect_identical(convert_64(c(-1, -1)), "18446744073709551615")

    # Reports errors.
    skip_on_os("solaris")
    safe_expect_error(convert_64(c(1, 1, 0)), msg = "vector implies an out-of-range seed")
})

test_that("unsigned/signed methods are consistent", {
    extremes <- c(0L, 1L, .Machine$integer.max, NA_integer_, -1L)
    for (x in extremes) {
        for (y in extremes) {
            expect_true(is_signed_consistent(c(x, y)))
        }
    }
})