1
2if (getRversion() >= '4.0.0') exit_file("Skip tests for R 4.0.0 or later")
3
4library(digest)
5
6x.numeric <- c(seq(0, 1, length = 4 ^ 3), -Inf, Inf, NA, NaN)
7x.list <- list(letters, x.numeric)
8x.dataframe <- data.frame(X = letters,
9                          Y = x.numeric[2],
10                          Z = factor(letters),
11                          stringsAsFactors = FALSE)
12x.matrix.num <- as.matrix(x.numeric)
13x.matrix.letter <- as.matrix(letters)
14x.dataframe.round <- x.dataframe
15x.dataframe.round$Y <- signif(x.dataframe.round$Y, 14)
16x.factor <- factor(letters)
17x.array.num <- as.array(x.numeric)
18x.formula <- a~b+c|d
19x.paren_formula <- a~(b+c)
20x.no_paren_formula <- a~b+c
21
22test.element <- list(
23    # NULL
24    NULL,
25    # empty classes
26    logical(0), integer(0), numeric(0), character(0), list(), data.frame(),
27    # scalar
28    TRUE, FALSE, 1L, 1, "a",
29    # date. Make sure to add the time zone. Otherwise the test might fail
30    as.POSIXct("2015-01-02 03:04:06.07", tz = "UTC"),
31    # vector
32    c(TRUE, FALSE), 1:3, seq(0, 10, length = 4), letters[1:3],
33    factor(letters[4:6]),
34    as.POSIXct(c("2015-01-02 03:04:06.07", "1960-12-31 23:59:59"), tz = "UTC")
35)
36
37expect_true(
38    identical(
39        sha1(x.matrix.num),
40        {
41            z <- matrix(
42                apply(x.matrix.num, 2, digest:::num2hex),
43                ncol = ncol(x.matrix.num)
44            )
45            z <- digest:::add_attributes(x.matrix.num, z)
46            attr(z, "digest::sha1") <- list(
47                class = "matrix",
48                digits = 14L,
49                zapsmall = 7L
50            )
51            digest(z, algo = "sha1")
52        }
53    )
54)
55
56expect_true(
57    identical(
58        sha1(x.matrix.letter),
59        {
60            z <- x.matrix.letter
61            attr(z, "digest::sha1") <- list(
62                class = "matrix",
63                digits = 14L,
64                zapsmall = 7L
65            )
66            digest(z, algo = "sha1")
67        }
68    )
69)
70
71correct <- c(
72    "8d9c05ec7ae28b219c4c56edbce6a721bd68af82",
73    "d61eeea290dd09c5a3eba41c2b3174b6e4e2366d",
74    "af23305d27f0409c91bdb86ba7c0cdb2e09a5dc6",
75    "0c9ca70ce773deb0d9c0b0579c3b94856edf15cc",
76    "095886422ad26e315c0960ef6b09842a1f9cc0ce",
77    "6cc04c6c432bb91e210efe0b25c6ca809e6df2e3",
78    "c1113ba008a349de64da2a7a724e501c1eb3929b",
79    "6e12370bdc6fc457cc154f0525e22c6aa6439f4d",
80    "1c1b5393c68a643bc79c277c6d7374d0b30cd985",
81    "b48c17a2ac82601ff38df374f87d76005fb61cbd",
82    "35280c99aa6a48bfc2810b72b763ccac0f632207",
83    "f757cc017308d217f35ed8f0c001a57b97308fb7",
84    "cfcf101b8449af67d34cdc1bcb0432fe9e4de08e",
85    "a14384d1997440bad13b97b3ccfb3b8c0392e79a",
86    "555f6bea49e58a2c2541060a21c2d4f9078c3086",
87    "631d18dec342e2cb87614864ba525ebb9ad6a124",
88    "b6c04f16b6fdacc794ea75c8c8dd210f99fafa65",
89    "25485ba7e315956267b3fdc521b421bbb046325d",
90    "6def3ca353dfc1a904bddd00e6a410d41ac7ab01",
91    "cf220bcf84c3d0ab1b01f8f764396941d15ff20f",
92    "2af8021b838f613aee7670bed19d0ddf1d6bc0c1",
93    "270ed85d46524a59e3274d89a1bbf693521cb6af",
94    "60e09482f12fda20f7d4a70e379c969c5a73f512",
95    "10380001af2a541b5feefc7aab9f719b67330a42",
96    "4580ff07f27eb8321421efac1676a80d9239572a",
97    "d3022c5a223caaf77e9c564e024199e5d6f51bd5",
98    "f54742ac61edd8c3980354620816c762b524dfc7"
99)
100
101for (i in seq_along(test.element)) {
102    if (i == 7 || i == 13 || i == 19) next
103    expect_true(
104        identical(
105            sha1(test.element[[i]]),
106            correct[i]
107        )
108    )
109}
110
111expect_true(
112    sha1(matrix(integer(0))) == "e13485e1b995f3e36d43674dcbfedea08ce237bc"
113)
114