Debian Codegraph @

inst/testme/test-utils.R

raw
1 message("*** utils ...")
2
3 message("*** hpaste() ...")
4
5 # Some vectors
6 x <- 1:6
7 y <- 10:1
8 z <- LETTERS[x]
9
10 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
11 # Abbreviation of output vector
12 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13 printf("x = %s.\n", hpaste(x))
14 ## x = 1, 2, 3, ..., 6.
15
16 printf("x = %s.\n", hpaste(x, maxHead = 2))
17 ## x = 1, 2, ..., 6.
18
19 printf("x = %s.\n", hpaste(x, maxHead = 3)) # Default
20 ## x = 1, 2, 3, ..., 6.
21
22 # It will never output 1, 2, 3, 4, ..., 6
23 printf("x = %s.\n", hpaste(x, maxHead = 4))
24 ## x = 1, 2, 3, 4, 5 and 6.
25
26 # Showing the tail
27 printf("x = %s.\n", hpaste(x, maxHead = 1, maxTail = 2))
28 ## x = 1, ..., 5, 6.
29
30 # Turning off abbreviation
31 printf("y = %s.\n", hpaste(y, maxHead = Inf))
32 ## y = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
33
34 ## ...or simply
35 printf("y = %s.\n", paste(y, collapse = ", "))
36 ## y = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
37
38 # Change last separator
39 printf("x = %s.\n", hpaste(x, lastCollapse = " and "))
40 ## x = 1, 2, 3, 4, 5 and 6.
41
42 # No collapse
43 stopifnot(all(hpaste(x, collapse = NULL) == x))
44
45 # Empty input
46 stopifnot(identical(hpaste(character(0)), character(0)))
47
48 message("*** hpaste() ... DONE")
49
50
51 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52 # debug()
53 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54 message("*** mdebug() ...")
55
56 mdebug("Hello #", 1)
57 mdebugf("Hello #%d", 1)
58 options(parallelly.debug = TRUE)
59
60 mdebug("Hello #", 2)
61 mdebugf("Hello #%d", 2)
62 options(parallelly.debug = FALSE)
63
64 mdebug("Hello #", 3)
65 mdebugf("Hello #%d", 3)
66
67 message("*** mdebug() ... DONE")
68
69
70 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71 # pid_exists()
72 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73 message("*** pid_exists() ...")
74 options(parallelly.debug = TRUE)
75
76 pid <- Sys.getpid()
77 printf("Current PID: %d\n", pid)
78
79 exists <- pid_exists(pid)
80 printf("Does it exist: %s\n", exists)
81
82 ## Either pid_exists() works and return TRUE here, or it fails
83 ## to query the process information at all in case it returns NA
84 ## However, it should never return FALSE.
85 stopifnot(is.logical(exists), length(exists) == 1L,
86 isTRUE(exists) || is.na(exists))
87
88 message("*** pid_exists() ... DONE")
89
90 message("*** inRCmdCheck() ...")
91 cat(sprintf("R CMD check is running: %s\n", inRCmdCheck()))
92 message("*** inRCmdCheck() ... DONE")
93
94
95 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96 # trim()
97 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98 message("*** trim() ...")
99
100 trim <- parallelly:::trim
101
102 stopifnot(
103 identical(trim(" hello "), "hello"),
104 identical(trim("hello"), "hello"),
105 identical(trim("\t hello \n"), "hello"),
106 identical(trim(""), ""),
107 identical(trim(" "), "")
108 )
109
110 message("*** trim() ... DONE")
111
112
113 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114 # commaq()
115 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116 message("*** commaq() ...")
117
118 commaq <- parallelly:::commaq
119
120 res <- commaq("a")
121 stopifnot(identical(res, sQuote("a")))
122
123 res <- commaq(c("a", "b"))
124 stopifnot(identical(res, paste(sQuote(c("a", "b")), collapse = ", ")))
125
126 res <- commaq(character(0))
127 stopifnot(identical(res, ""))
128
129 message("*** commaq() ... DONE")
130
131
132 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133 # hpaste() - lastCollapse
134 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135 message("*** hpaste() - lastCollapse ...")
136
137 ## Two-element vector with lastCollapse
138 res <- hpaste(c("a", "b"), lastCollapse = " and ")
139 stopifnot(identical(res, "a and b"))
140
141 ## Single-element vector
142 res <- hpaste("a", lastCollapse = " and ")
143 stopifnot(identical(res, "a"))
144
145 message("*** hpaste() - lastCollapse ... DONE")
146
147
148 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149 # pid_exists() - argument validation
150 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151 message("*** pid_exists() - argument validation ...")
152
153 ## Non-existent PID
154 result <- pid_exists(2^31 - 1L)
155 message("pid_exists(non-existent PID): ", result)
156 stopifnot(is.logical(result), length(result) == 1L,
157 isFALSE(result) || is.na(result))
158
159 ## Invalid arguments
160 res <- tryCatch(pid_exists(-1L), error = identity)
161 stopifnot(inherits(res, "error"))
162
163 res <- tryCatch(pid_exists(NA_real_), error = identity)
164 stopifnot(inherits(res, "error"))
165
166 message("*** pid_exists() - argument validation ... DONE")
167
168 options(parallelly.debug = FALSE)
169
170 message("*** utils ... DONE")