Skip to content

Commit d59ae3b

Browse files
TESTS: Add more tests - slight increase in test coverage
1 parent 24aa23b commit d59ae3b

File tree

4 files changed

+171
-3
lines changed

4 files changed

+171
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: parallelly
2-
Version: 1.46.1-9032
2+
Version: 1.46.1-9033
33
Title: Enhancing the 'parallel' Package
44
Imports:
55
parallel,

inst/testme/test-freeCores.R

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
library(parallelly)
22

3-
message("*** freeLoad() ...")
3+
message("*** freeCores() ...")
44

55
free <- freeCores()
66
print(free)
@@ -11,4 +11,46 @@ stopifnot(
1111
!is.na(free), free >= 1L
1212
)
1313

14-
message("*** freeLoad() ... DONE")
14+
## Attributes
15+
stopifnot(
16+
!is.null(attr(free, "loadavg")),
17+
!is.null(attr(free, "maxCores")),
18+
!is.null(attr(free, "memory")),
19+
!is.null(attr(free, "fraction"))
20+
)
21+
22+
for (memory in c("1min", "5min", "15min")) {
23+
message(sprintf("- freeCores(memory = '%s')", memory))
24+
free <- freeCores(memory = memory)
25+
print(free)
26+
stopifnot(
27+
is.integer(free),
28+
length(free) == 1L,
29+
!is.na(free), free >= 1L,
30+
identical(attr(free, "memory"), memory)
31+
)
32+
}
33+
34+
message("- freeCores(fraction = 0.5)")
35+
free_half <- freeCores(fraction = 0.5)
36+
print(free_half)
37+
stopifnot(
38+
is.integer(free_half),
39+
length(free_half) == 1L,
40+
!is.na(free_half), free_half >= 1L,
41+
identical(attr(free_half, "fraction"), 0.5)
42+
)
43+
44+
45+
message("- freeCores() - exceptions")
46+
47+
res <- tryCatch(freeCores(fraction = 0), error = identity)
48+
stopifnot(inherits(res, "error"))
49+
50+
res <- tryCatch(freeCores(fraction = 1.5), error = identity)
51+
stopifnot(inherits(res, "error"))
52+
53+
res <- tryCatch(freeCores(fraction = NA_real_), error = identity)
54+
stopifnot(inherits(res, "error"))
55+
56+
message("*** freeCores() ... DONE")

inst/testme/test-utils,cluster.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,54 @@ for (os in list(NULL, "unix", "windows")) {
5656
}
5757

5858

59+
message("- is_localhost()")
60+
61+
is_localhost <- parallelly:::is_localhost
62+
## Reset cache
63+
is_localhost(worker = NULL, hostname = NULL)
64+
65+
## Known localhost names
66+
stopifnot(
67+
is_localhost("localhost"),
68+
is_localhost("127.0.0.1")
69+
)
70+
71+
## Current hostname is localhost
72+
hostname <- Sys.info()[["nodename"]]
73+
stopifnot(is_localhost(hostname))
74+
75+
## After being identified, hostname should be cached
76+
stopifnot(is_localhost(hostname))
77+
78+
## A clearly non-local host
79+
stopifnot(!is_localhost("this-host-does-not-exist-12345.example.org"))
80+
81+
## Reset cache for clean state
82+
is_localhost(worker = NULL, hostname = NULL)
83+
84+
85+
message("- is_ip_number()")
86+
87+
is_ip_number <- parallelly:::is_ip_number
88+
stopifnot(
89+
is_ip_number("192.168.1.1"),
90+
is_ip_number("0.0.0.0"),
91+
is_ip_number("255.255.255.255"),
92+
!is_ip_number("256.0.0.0"),
93+
!is_ip_number("not-an-ip"),
94+
!is_ip_number("192.168.1"),
95+
!is_ip_number("192.168.1.1.1")
96+
)
97+
98+
99+
message("- is_fqdn()")
100+
101+
is_fqdn <- parallelly:::is_fqdn
102+
stopifnot(
103+
is_fqdn("host.example.com"),
104+
is_fqdn("a.b"),
105+
!is_fqdn("localhost"),
106+
!is_fqdn("singlename")
107+
)
108+
59109
message("*** utils,cluster ... DONE")

inst/testme/test-utils.R

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,80 @@ message("*** inRCmdCheck() ...")
9191
cat(sprintf("R CMD check is running: %s\n", inRCmdCheck()))
9292
message("*** inRCmdCheck() ... DONE")
9393

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+
94170
message("*** utils ... DONE")

0 commit comments

Comments
 (0)