Debian Codegraph @

test/fpm_test/test_backend.f90

raw
1 !> Define tests for the `fpm_backend` module (build scheduling)
2 module test_backend
3 use testsuite, only : new_unittest, unittest_t, error_t, test_failed
4 use test_module_dependencies, only: operator(.in.)
5 use fpm_filesystem, only: exists, mkdir, get_temp_filename
6 use fpm_targets, only: build_target_t, build_target_ptr, &
7 FPM_TARGET_OBJECT, FPM_TARGET_ARCHIVE, FPM_TARGET_SHARED, &
8 add_target, add_dependency
9 use fpm_backend, only: sort_target, schedule_targets
10 use fpm_strings, only: string_t
11 use fpm_environment, only: OS_LINUX
12 use fpm_compile_commands, only: compile_command_t, compile_command_table_t
13 implicit none
14 private
15
16 public :: collect_backend
17
18 contains
19
20
21 !> Collect all exported unit tests
22 subroutine collect_backend(testsuite)
23
24 !> Collection of tests
25 type(unittest_t), allocatable, intent(out) :: testsuite(:)
26
27 testsuite = [ &
28 & new_unittest("target-sort", test_target_sort), &
29 & new_unittest("target-sort-skip-all", test_target_sort_skip_all), &
30 & new_unittest("target-sort-rebuild-all", test_target_sort_rebuild_all), &
31 & new_unittest("target-shared-sort", test_target_shared), &
32 & new_unittest("schedule-targets", test_schedule_targets), &
33 & new_unittest("schedule-targets-empty", test_schedule_empty), &
34 & new_unittest("serialize-compile-commands", compile_commands_roundtrip), &
35 & new_unittest("compile-commands-write", compile_commands_register_from_cmd), &
36 & new_unittest("compile-commands-register-string", compile_commands_register_from_string) &
37 ]
38
39 end subroutine collect_backend
40
41
42 !> Check scheduling of objects with dependencies
43 subroutine test_target_sort(error)
44
45 !> Error handling
46 type(error_t), allocatable, intent(out) :: error
47
48 type(build_target_ptr), allocatable :: targets(:)
49
50 integer :: i
51
52 targets = new_test_package()
53
54 ! Perform depth-first topological sort of targets
55 do i=1,size(targets)
56
57 call sort_target(targets(i)%ptr)
58
59 end do
60
61 ! Check target states: all targets scheduled
62 do i=1,size(targets)
63
64 if (.not.targets(i)%ptr%touched) then
65 call test_failed(error,"Target touched flag not set")
66 return
67 end if
68
69 if (.not.targets(i)%ptr%sorted) then
70 call test_failed(error,"Target sort flag not set")
71 return
72 end if
73
74 if (targets(i)%ptr%skip) then
75 call test_failed(error,"Target skip flag set incorrectly")
76 return
77 end if
78
79 if (targets(i)%ptr%schedule < 0) then
80 call test_failed(error,"Target schedule not set")
81 return
82 end if
83
84 end do
85
86 ! Check all objects sheduled before library
87 do i=2,size(targets)
88
89 if (targets(i)%ptr%schedule >= targets(1)%ptr%schedule) then
90 call test_failed(error,"Object dependency scheduled after dependent library target")
91 return
92 end if
93
94 end do
95
96 ! Check target 4 schedule before targets 2 & 3
97 do i=2,3
98 if (targets(4)%ptr%schedule >= targets(i)%ptr%schedule) then
99 call test_failed(error,"Object dependency scheduled after dependent object target")
100 return
101 end if
102 end do
103
104 end subroutine test_target_sort
105
106
107
108 !> Check incremental rebuild for existing archive
109 !> all object sources are unmodified: all objects should be skipped
110 subroutine test_target_sort_skip_all(error)
111
112 !> Error handling
113 type(error_t), allocatable, intent(out) :: error
114
115 type(build_target_ptr), allocatable :: targets(:)
116
117 integer :: fh, i
118
119 targets = new_test_package()
120
121 do i=2,size(targets)
122
123 ! Mimick unmodified sources
124 allocate(targets(i)%ptr%source)
125 targets(i)%ptr%source%digest = i
126 targets(i)%ptr%digest_cached = i
127
128 end do
129
130 ! Mimick archive already exists
131 open(newunit=fh,file=targets(1)%ptr%output_file,status="unknown")
132 close(fh)
133
134 ! Perform depth-first topological sort of targets
135 do i=1,size(targets)
136
137 call sort_target(targets(i)%ptr)
138
139 end do
140
141 ! Check target states: all targets skipped
142 do i=1,size(targets)
143
144 if (.not.targets(i)%ptr%touched) then
145 call test_failed(error,"Target touched flag not set")
146 return
147 end if
148
149 if (targets(i)%ptr%sorted) then
150 call test_failed(error,"Target sort flag set incorrectly")
151 return
152 end if
153
154 if (.not.targets(i)%ptr%skip) then
155 call test_failed(error,"Target skip flag set incorrectly")
156 return
157 end if
158
159 end do
160
161 end subroutine test_target_sort_skip_all
162
163
164 !> Check incremental rebuild for existing archive
165 !> all but lowest source modified: all objects should be rebuilt
166 subroutine test_target_sort_rebuild_all(error)
167
168 !> Error handling
169 type(error_t), allocatable, intent(out) :: error
170
171 type(build_target_ptr), allocatable :: targets(:)
172
173 integer :: fh, i
174
175 targets = new_test_package()
176
177 do i=2,3
178
179 ! Mimick unmodified sources
180 allocate(targets(i)%ptr%source)
181 targets(i)%ptr%source%digest = i
182 targets(i)%ptr%digest_cached = i
183
184 end do
185
186 ! Mimick archive already exists
187 open(newunit=fh,file=targets(1)%ptr%output_file,status="unknown")
188 close(fh)
189
190 ! Perform depth-first topological sort of targets
191 do i=1,size(targets)
192
193 call sort_target(targets(i)%ptr)
194
195 end do
196
197 ! Check target states: all targets scheduled
198 do i=1,size(targets)
199
200 if (.not.targets(i)%ptr%sorted) then
201 call test_failed(error,"Target sort flag not set")
202 return
203 end if
204
205 if (targets(i)%ptr%skip) then
206 call test_failed(error,"Target skip flag set incorrectly")
207 return
208 end if
209
210 end do
211
212 end subroutine test_target_sort_rebuild_all
213
214
215 !> Check construction of target queue and schedule
216 subroutine test_schedule_targets(error)
217
218 !> Error handling
219 type(error_t), allocatable, intent(out) :: error
220
221 type(build_target_ptr), allocatable :: targets(:)
222
223 integer :: i, j
224 type(build_target_ptr), allocatable :: queue(:)
225 integer, allocatable :: schedule_ptr(:)
226
227 targets = new_test_package()
228
229 ! Perform depth-first topological sort of targets
230 do i=1,size(targets)
231
232 call sort_target(targets(i)%ptr)
233
234 end do
235
236 ! Construct build schedule queue
237 call schedule_targets(queue, schedule_ptr, targets)
238
239 ! Check all targets enqueued
240 do i=1,size(targets)
241
242 if (.not.(targets(i)%ptr.in.queue)) then
243
244 call test_failed(error,"Target not found in build queue")
245 return
246
247 end if
248
249 end do
250
251 ! Check schedule structure
252 if (schedule_ptr(1) /= 1) then
253
254 call test_failed(error,"schedule_ptr(1) does not point to start of the queue")
255 return
256
257 end if
258
259 if (schedule_ptr(size(schedule_ptr)) /= size(queue)+1) then
260
261 call test_failed(error,"schedule_ptr(end) does not point to end of the queue")
262 return
263
264 end if
265
266 do i=1,size(schedule_ptr)-1
267
268 do j=schedule_ptr(i),(schedule_ptr(i+1)-1)
269
270 if (queue(j)%ptr%schedule /= i) then
271
272 call test_failed(error,"Target scheduled in the wrong region")
273 return
274
275 end if
276
277 end do
278
279 end do
280
281 end subroutine test_schedule_targets
282
283
284 !> Check construction of target queue and schedule
285 !> when there's nothing to do (all targets skipped)
286 subroutine test_schedule_empty(error)
287
288 !> Error handling
289 type(error_t), allocatable, intent(out) :: error
290
291 type(build_target_ptr), allocatable :: targets(:)
292
293 integer :: i
294 type(build_target_ptr), allocatable :: queue(:)
295 integer, allocatable :: schedule_ptr(:)
296
297 targets = new_test_package()
298
299 do i=1,size(targets)
300
301 targets(i)%ptr%skip = .true.
302
303 end do
304
305 ! Perform depth-first topological sort of targets
306 do i=1,size(targets)
307
308 call sort_target(targets(i)%ptr)
309
310 end do
311
312 ! Construct build schedule queue
313 call schedule_targets(queue, schedule_ptr, targets)
314
315 ! Check queue is empty
316 if (size(queue) > 0) then
317
318 call test_failed(error,"Expecting an empty build queue, but not empty")
319 return
320
321 end if
322
323 ! Check schedule loop is not entered
324 do i=1,size(schedule_ptr)-1
325
326 call test_failed(error,"Attempted to run an empty schedule")
327 return
328
329 end do
330
331 end subroutine test_schedule_empty
332
333
334 !> Helper to generate target objects with dependencies
335 function new_test_package() result(targets)
336
337 type(build_target_ptr), allocatable :: targets(:)
338 integer :: i
339
340 call add_target(targets,'test-package',FPM_TARGET_ARCHIVE,get_temp_filename())
341
342 call add_target(targets,'test-package',FPM_TARGET_OBJECT,get_temp_filename())
343
344 call add_target(targets,'test-package',FPM_TARGET_OBJECT,get_temp_filename())
345
346 call add_target(targets,'test-package',FPM_TARGET_OBJECT,get_temp_filename())
347
348 ! Library depends on all objects
349 call add_dependency(targets(1)%ptr,targets(2)%ptr)
350 call add_dependency(targets(1)%ptr,targets(3)%ptr)
351 call add_dependency(targets(1)%ptr,targets(4)%ptr)
352
353 ! Inter-object dependency
354 ! targets 2 & 3 depend on target 4
355 call add_dependency(targets(2)%ptr,targets(4)%ptr)
356 call add_dependency(targets(3)%ptr,targets(4)%ptr)
357
358 end function new_test_package
359
360 subroutine compile_commands_roundtrip(error)
361
362 !> Error handling
363 type(error_t), allocatable, intent(out) :: error
364
365 type(compile_command_t) :: cmd
366 type(compile_command_table_t) :: cc
367 integer :: i
368
369 call cmd%test_serialization('compile_command: empty', error)
370 if (allocated(error)) return
371
372 cmd = compile_command_t(directory = string_t("/test/dir"), &
373 arguments = [string_t("gfortran"), &
374 string_t("-c"), string_t("main.f90"), &
375 string_t("-o"), string_t("main.o")], &
376 file = string_t("main.f90"))
377
378 call cmd%test_serialization('compile_command: non-empty', error)
379 if (allocated(error)) return
380
381 call cc%test_serialization('compile_command_table: empty', error)
382 if (allocated(error)) return
383
384 do i=1,10
385 call cc%register(cmd,error)
386 if (allocated(error)) return
387 end do
388
389 call cc%test_serialization('compile_command_table: non-empty', error)
390 if (allocated(error)) return
391
392 end subroutine compile_commands_roundtrip
393
394 subroutine compile_commands_register_from_cmd(error)
395 type(error_t), allocatable, intent(out) :: error
396
397 type(compile_command_table_t) :: table
398 type(compile_command_t) :: cmd
399 integer :: i
400
401 cmd = compile_command_t(directory = string_t("/src"), &
402 arguments = [string_t("gfortran"), &
403 string_t("-c"), string_t("example.f90"), &
404 string_t("-o"), string_t("example.o")], &
405 file = string_t("example.f90"))
406
407 call table%register(cmd, error)
408 if (allocated(error)) return
409
410 if (.not.allocated(table%command)) then
411 call test_failed(error, "Command table not allocated after registration")
412 return
413 endif
414
415 if (size(table%command) /= 1) then
416 call test_failed(error, "Expected one registered command")
417 return
418 endif
419
420 if (table%command(1)%file%s /= "example.f90") then
421 call test_failed(error, "Registered file mismatch")
422 return
423 endif
424
425 end subroutine compile_commands_register_from_cmd
426
427 subroutine compile_commands_register_from_string(error)
428 type(error_t), allocatable, intent(out) :: error
429
430 type(compile_command_table_t) :: table
431 character(len=*), parameter :: cmd_line = "gfortran -c example.f90 -o example.o"
432
433 ! Register a raw command line string
434 call table%register(cmd_line, OS_LINUX, error)
435 if (allocated(error)) return
436
437 if (.not.allocated(table%command)) then
438 call test_failed(error, "Command table not allocated after string registration")
439 return
440 end if
441
442 if (size(table%command) /= 1) then
443 call test_failed(error, "Expected one registered command after string registration")
444 return
445 end if
446
447 if (.not.allocated(table%command(1)%arguments)) then
448 call test_failed(error, "Command arguments not allocated")
449 return
450 end if
451
452 if (size(table%command(1)%arguments) /= 5) then
453 call test_failed(error, "Wrong number of parsed arguments, should be 5")
454 return
455 end if
456
457 if (table%command(1)%arguments(1)%s /= "gfortran") then
458 call test_failed(error, "Expected 'gfortran' as first argument")
459 return
460 end if
461
462 if (table%command(1)%arguments(3)%s /= "example.f90") then
463 call test_failed(error, "Expected 'example.f90' as third argument")
464 return
465 end if
466
467 end subroutine compile_commands_register_from_string
468
469 !> Check sorting and scheduling for shared library targets
470 subroutine test_target_shared(error)
471
472 !> Error handling
473 type(error_t), allocatable, intent(out) :: error
474
475 type(build_target_ptr), allocatable :: targets(:)
476 integer :: i
477
478 ! Create a new test package with a shared library
479 call add_target(targets, 'test-shared', FPM_TARGET_SHARED, get_temp_filename())
480 call add_target(targets, 'test-shared', FPM_TARGET_OBJECT, get_temp_filename())
481 call add_target(targets, 'test-shared', FPM_TARGET_OBJECT, get_temp_filename())
482
483 ! Shared library depends on the two object files
484 call add_dependency(targets(1)%ptr, targets(2)%ptr)
485 call add_dependency(targets(1)%ptr, targets(3)%ptr)
486
487 ! Perform topological sort
488 do i = 1, size(targets)
489 call sort_target(targets(i)%ptr)
490 end do
491
492 ! Check scheduling and flags
493 do i = 1, size(targets)
494 if (.not.targets(i)%ptr%touched) then
495 call test_failed(error, "Shared: Target not touched")
496 return
497 end if
498 if (.not.targets(i)%ptr%sorted) then
499 call test_failed(error, "Shared: Target not sorted")
500 return
501 end if
502 if (targets(i)%ptr%skip) then
503 call test_failed(error, "Shared: Target incorrectly skipped")
504 return
505 end if
506 end do
507
508 ! Check dependencies scheduled before the shared lib
509 if (targets(2)%ptr%schedule >= targets(1)%ptr%schedule) then
510 call test_failed(error, "Shared: Object 2 scheduled after shared lib")
511 return
512 end if
513 if (targets(3)%ptr%schedule >= targets(1)%ptr%schedule) then
514 call test_failed(error, "Shared: Object 3 scheduled after shared lib")
515 return
516 end if
517
518 end subroutine test_target_shared
519
520
521
522
523 end module test_backend