Skip to content

Commit 76ad433

Browse files
committed
derive padding from largest ID
1 parent f509c7c commit 76ad433

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

R/utils.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ generate_file_names <-
266266
new_names <- paste0(new_names, "-", stamp)
267267
}
268268
if (!is.null(ids)) {
269-
new_names <- paste0(new_names, "-", sprintf("%02d", ids))
269+
width <- max(2L, nchar(sprintf("%d", max(ids))))
270+
new_names <- paste0(new_names, "-", sprintf("%0*d", width, ids))
270271
}
271272
if (random) {
272273
rand_num_pid <- as.integer(stats::runif(1, min = 0, max = 1E7)) + Sys.getpid()

tests/testthat/test-utils.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,28 @@ test_that("get_standalone_hpp() suggests formatting deprecated syntax", {
182182

183183
# misc --------------------------------------------------------------------
184184

185-
test_that("generate_file_names() zero-pads IDs", {
185+
test_that("generate_file_names() zero-pads IDs for lexicographic sorting", {
186186
expect_equal(
187187
generate_file_names(
188188
basename = "output",
189-
ids = c(1, 9, 10, 100),
189+
ids = 1:10,
190190
timestamp = FALSE,
191191
random = FALSE
192192
),
193-
paste0("output-", c("01", "09", "10", "100"), ".csv")
193+
paste0("output-", sprintf("%02d", 1:10), ".csv")
194194
)
195+
196+
file_names <- generate_file_names(
197+
basename = "output",
198+
ids = 1:100,
199+
timestamp = FALSE,
200+
random = FALSE
201+
)
202+
expect_equal(
203+
file_names[c(1, 9, 10, 100)],
204+
paste0("output-", c("001", "009", "010", "100"), ".csv")
205+
)
206+
expect_equal(sort(file_names), file_names)
195207
})
196208

197209
test_that("copy_temp_files retains sources if any copy fails", {

0 commit comments

Comments
 (0)