-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest-compiler.R
More file actions
461 lines (389 loc) · 12.8 KB
/
Copy pathtest-compiler.R
File metadata and controls
461 lines (389 loc) · 12.8 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Unit tests for compiler selection helpers
skip_on_cran()
test_that("quickr_r_cmd_config_value captures only stdout", {
expect_identical(
deparse(formals(quickr:::quickr_r_cmd_config_value)$system2),
"base::system2"
)
observed_stdout <- NULL
observed_stderr <- NULL
system2_stub <- function(
command,
args,
stdout = "",
stderr = "",
...
) {
observed_stdout <<- stdout
observed_stderr <<- stderr
" value "
}
expect_identical(
quickr:::quickr_r_cmd_config_value(
"CC",
r_cmd = "R",
system2 = system2_stub
),
"value"
)
expect_identical(observed_stdout, TRUE)
expect_identical(observed_stderr, FALSE)
})
test_that("quickr_r_cmd_config_value returns empty on command failure", {
system2_stub <- function(command, args, stdout = "", stderr = "", ...) {
structure(" value ", status = 1L)
}
expect_identical(
quickr:::quickr_r_cmd_config_value(
"CC",
r_cmd = "R",
system2 = system2_stub
),
""
)
})
test_that("quickr_flang_path prefers flang-new", {
which_stub <- function(x) {
if (x == "flang-new") {
"/tmp/flang-new"
} else if (x == "flang") {
"/tmp/flang"
} else {
""
}
}
local_mocked_bindings(Sys.which = which_stub, .package = "base")
expect_identical(quickr_flang_path(), "/tmp/flang-new")
})
test_that("flang probe helpers do not expose test-only process hooks", {
expect_false("which" %in% names(formals(quickr_flang_path)))
expect_false("which" %in% names(formals(quickr_flang_available)))
expect_false("system2" %in% names(formals(quickr_flang_available)))
expect_false("which" %in% names(formals(quickr_cached_flang_available)))
expect_false("system2" %in% names(formals(quickr_cached_flang_available)))
expect_false("which" %in% names(formals(quickr:::quickr_fcompiler_env)))
expect_false("system2" %in% names(formals(quickr:::quickr_fcompiler_env)))
})
test_that("quickr_prefer_flang respects quickr.fortran_compiler", {
withr::local_options(quickr.fortran_compiler = "flang")
expect_true(quickr_prefer_flang())
withr::local_options(quickr.fortran_compiler = "gfortran")
expect_false(quickr_prefer_flang())
})
test_that("quickr_default_fortran_makevars_lines relaxes gfortran cost model", {
expect_equal(
quickr:::quickr_default_fortran_makevars_lines(
config_value = function(name) {
if (identical(name, "FC")) "gfortran -m64" else ""
}
),
"PKG_FFLAGS += -fvect-cost-model=cheap"
)
expect_equal(
quickr:::quickr_default_fortran_makevars_lines(
config_value = function(name) {
if (identical(name, "FC")) "/usr/bin/gfortran-13" else ""
}
),
"PKG_FFLAGS += -fvect-cost-model=cheap"
)
expect_identical(
quickr:::quickr_default_fortran_makevars_lines(
config_value = function(name) {
if (identical(name, "FC")) "flang-new" else ""
}
),
character()
)
})
test_that("quickr_fortran_compiler_option validates values", {
withr::local_options(quickr.fortran_compiler = "auto")
expect_null(quickr:::quickr_fortran_compiler_option())
withr::local_options(quickr.fortran_compiler = "flang")
expect_identical(quickr:::quickr_fortran_compiler_option(), "flang")
withr::local_options(quickr.fortran_compiler = "gfortran")
expect_identical(quickr:::quickr_fortran_compiler_option(), "gfortran")
withr::local_options(quickr.fortran_compiler = "nope")
expect_error(
quickr:::quickr_fortran_compiler_option(),
"options(quickr.fortran_compiler)",
fixed = TRUE
)
})
test_that("quickr_fcompiler_env writes Makevars when flang is usable", {
temp <- withr::local_tempdir()
prefix <- file.path(temp, "flang")
dir.create(file.path(prefix, "bin"), recursive = TRUE)
dir.create(file.path(prefix, "lib"), recursive = TRUE)
flang <- file.path(prefix, "bin", "flang-new")
file.create(flang)
file.create(file.path(prefix, "lib", "libflang_rt.runtime.dylib"))
cache_env <- environment(quickr:::quickr_flang_runtime_flags)
old_cache <- cache_env$cache
cache_env$cache <- NULL
on.exit(cache_env$cache <- old_cache, add = TRUE)
build_dir <- file.path(temp, "build")
dir.create(build_dir)
local_mocked_bindings(
Sys.which = function(x) if (x == "flang-new") flang else "",
system2 = function(...) "",
.package = "base"
)
withr::local_options(quickr.fortran_compiler = "flang")
env <- quickr:::quickr_fcompiler_env(
build_dir = build_dir,
sysname = "Darwin"
)
expect_true(startsWith(env, "R_MAKEVARS_USER="))
expect_true(file.exists(sub("R_MAKEVARS_USER=", "", env, fixed = TRUE)))
})
test_that("quickr_fcompiler_env writes Makevars for default gfortran flags", {
build_dir <- withr::local_tempdir()
withr::local_options(quickr.fortran_compiler = "gfortran")
env <- quickr:::quickr_fcompiler_env(
build_dir = build_dir,
sysname = "Linux",
config_value = function(name) {
if (identical(name, "FC")) "gfortran -m64" else ""
}
)
expect_true(startsWith(env, "R_MAKEVARS_USER="))
makevars_path <- sub("^R_MAKEVARS_USER=", "", env)
expect_equal(
readLines(makevars_path),
"PKG_FFLAGS += -fvect-cost-model=cheap"
)
})
test_that("quickr_fcompiler_env errors when flang is explicitly requested but unavailable", {
build_dir <- withr::local_tempdir()
local_mocked_bindings(Sys.which = function(cmd) "", .package = "base")
withr::local_options(quickr.fortran_compiler = "flang")
expect_error(
quickr:::quickr_fcompiler_env(
build_dir = build_dir
),
"configured to use flang",
fixed = TRUE
)
})
test_that("quickr_flang_available returns unavailable when system2 fails", {
which_stub <- function(x) if (x == "flang-new") "/tmp/flang-new" else ""
system2_fail <- function(...) structure("error", status = 1L)
local_mocked_bindings(
Sys.which = which_stub,
system2 = system2_fail,
.package = "base"
)
result <- quickr:::quickr_flang_available()
expect_identical(result$path, "/tmp/flang-new")
expect_false(result$available)
})
test_that("quickr_disable_flang_auto sets auto_disabled flag", {
state <- new.env(parent = emptyenv())
state$auto_disabled <- FALSE
state$fallback_warned <- FALSE
result <- quickr:::quickr_disable_flang_auto(state = state)
expect_true(result)
expect_true(state$auto_disabled)
})
test_that("quickr_warn_flang_fallback_once warns only once", {
state <- new.env(parent = emptyenv())
state$fallback_warned <- FALSE
expect_warning(
quickr:::quickr_warn_flang_fallback_once(state = state),
"flang compilation failed"
)
expect_true(state$fallback_warned)
# Second call should not warn
expect_silent(quickr:::quickr_warn_flang_fallback_once(state = state))
})
test_that("quickr_warn_compiler_failure_once warns only once", {
state <- new.env(parent = emptyenv())
state$warned <- FALSE
expect_warning(
quickr:::quickr_warn_compiler_failure_once("test error", state = state),
"test error"
)
expect_true(state$warned)
# Second call should not warn
expect_silent(quickr:::quickr_warn_compiler_failure_once(
"test error 2",
state = state
))
})
test_that("quickr_flang_runtime_flags returns empty for non-Darwin", {
result <- quickr:::quickr_flang_runtime_flags(
"/usr/bin/flang",
sysname = "Linux"
)
expect_identical(result, character())
})
test_that("quickr_flang_runtime_flags returns empty for empty flang path", {
result <- quickr:::quickr_flang_runtime_flags("", sysname = "Darwin")
expect_identical(result, character())
})
test_that("quickr_flang_runtime_flags returns empty when flang does not exist", {
# Clear cache
cache_env <- environment(quickr:::quickr_flang_runtime_flags)
old_cache <- cache_env$cache
cache_env$cache <- NULL
on.exit(cache_env$cache <- old_cache, add = TRUE)
result <- quickr:::quickr_flang_runtime_flags(
"/nonexistent/path/to/flang",
sysname = "Darwin"
)
expect_identical(result, character())
})
test_that("quickr_flang_runtime_flags returns empty when runtime lib not found", {
temp <- withr::local_tempdir()
flang <- file.path(temp, "bin", "flang-new")
dir.create(dirname(flang), recursive = TRUE)
file.create(flang)
# Create lib dir but no runtime lib
dir.create(file.path(temp, "lib"))
# Clear cache
cache_env <- environment(quickr:::quickr_flang_runtime_flags)
old_cache <- cache_env$cache
cache_env$cache <- NULL
on.exit(cache_env$cache <- old_cache, add = TRUE)
result <- quickr:::quickr_flang_runtime_flags(flang, sysname = "Darwin")
expect_identical(result, character())
})
test_that("quickr_fortran_compiler_option errors for non-string", {
withr::local_options(quickr.fortran_compiler = 123)
expect_error(
quickr:::quickr_fortran_compiler_option(),
"must be a single string"
)
})
test_that("quickr_prefer_flang returns FALSE when flang_auto_disabled", {
state <- new.env(parent = emptyenv())
state$auto_disabled <- TRUE
state$fallback_warned <- FALSE
withr::local_options(quickr.fortran_compiler = "auto")
# Inject the state
local_mocked_bindings(
quickr_flang_auto_disabled = function(...) TRUE,
.package = "quickr"
)
expect_false(quickr_prefer_flang())
})
test_that("quickr_fcompiler_env handles flang unavailable for non-explicit request", {
build_dir <- withr::local_tempdir()
withr::local_options(quickr.fortran_compiler = "auto")
# Mock flang as preferred but unavailable
local_mocked_bindings(
quickr_prefer_flang = function(...) TRUE,
quickr_cached_flang_available = function(...) {
list(path = "/tmp/flang", available = FALSE)
},
.package = "quickr"
)
# Should return character() since not explicit and flang unavailable
result <- quickr:::quickr_fcompiler_env(
build_dir = build_dir,
sysname = "Darwin",
config_value = function(name) if (identical(name, "FC")) "clang" else ""
)
expect_identical(result, character())
})
test_that("quickr_fcompiler_env errors when flang runtime not found on Darwin explicit", {
temp <- withr::local_tempdir()
flang <- file.path(temp, "bin", "flang-new")
dir.create(dirname(flang), recursive = TRUE)
file.create(flang)
# Clear cache
cache_env <- environment(quickr:::quickr_flang_runtime_flags)
old_cache <- cache_env$cache
cache_env$cache <- NULL
on.exit(cache_env$cache <- old_cache, add = TRUE)
build_dir <- file.path(temp, "build")
dir.create(build_dir)
withr::local_options(quickr.fortran_compiler = "flang")
local_mocked_bindings(
Sys.which = function(x) if (x == "flang-new") flang else "",
system2 = function(...) "",
.package = "base"
)
expect_error(
quickr:::quickr_fcompiler_env(
build_dir = build_dir,
sysname = "Darwin"
),
"could not locate the flang runtime library"
)
})
test_that("quickr_fcompiler_env falls back when flang runtime not found non-explicit", {
temp <- withr::local_tempdir()
flang <- file.path(temp, "bin", "flang-new")
dir.create(dirname(flang), recursive = TRUE)
file.create(flang)
dir.create(file.path(temp, "lib"))
# Clear cache
cache_env <- environment(quickr:::quickr_flang_runtime_flags)
old_cache <- cache_env$cache
cache_env$cache <- NULL
on.exit(cache_env$cache <- old_cache, add = TRUE)
build_dir <- file.path(temp, "build")
dir.create(build_dir)
withr::local_options(quickr.fortran_compiler = "auto")
local_mocked_bindings(
quickr_prefer_flang = function(...) TRUE,
quickr_cached_flang_available = function(...) {
list(path = flang, available = TRUE)
},
.package = "quickr"
)
result <- quickr:::quickr_fcompiler_env(
build_dir = build_dir,
sysname = "Darwin",
config_value = function(name) if (identical(name, "FC")) "clang" else ""
)
# Should fall back to character() since runtime not found and not explicit
expect_identical(result, character())
})
test_that("compile cleans existing build directories and reports failures", {
fsub <- r2f(function(x) {
declare(type(x = double(1)))
x + 1
})
build_dir <- withr::local_tempdir()
file.create(file.path(build_dir, "stale.txt"))
calls <- 0L
system2_stub <- function(
command,
args,
stdout = TRUE,
stderr = TRUE,
env = character(),
...
) {
if (
length(args) >= 3L &&
identical(args[[1L]], "CMD") &&
identical(args[[2L]], "config")
) {
return("")
}
calls <<- calls + 1L
if (calls == 1L) {
return(structure("flang fail", status = 1))
}
structure("fallback fail", status = 2)
}
local_mocked_bindings(
system2 = system2_stub,
.package = "base"
)
local_mocked_bindings(
quickr_fcompiler_env = function(...) "ENV=1",
.package = "quickr"
)
expect_error(
suppressWarnings(quickr:::compile(fsub, build_dir = build_dir)),
"Compilation Error",
fixed = TRUE
)
expect_true(dir.exists(build_dir))
expect_false(file.exists(file.path(build_dir, "stale.txt")))
})