1 % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 %:- use_module('../extensions/profiler/profiler.pl').
6
7 :- module(test_runner, [
8 test_repl/0,
9 run_silently/0,
10 run_safely/0,
11 run_last_test/0, last/0,
12 run_tests_by_id/1, ids/1, run_id/1,
13 run_tests_all/0, run_tests_all_except/1,
14 run_tests_by_category/1, run_tests_by_category/2, category/1,
15 run_tests_by_first_category/1,
16 makefile_by_category/2, copy/1,
17 generate_makefile_all_tests/0,
18 generate_makefile_for_coverage/0,
19 full_flush/0,
20 jenkins_sanity_check/0,
21 sanity_check/1,
22 v/0, vv/0,
23 cache/0, % enable caching
24 all_categories/1,
25 all_ids/1,
26 ids_in_category/2,
27 set_prob_examples_location/1,
28 halt_tests/0 % use to call halt with proper exit code; however, normally test predicates will call halt1 directly, mainly useful in junit mode
29 ]).
30
31 :- if(predicate_property(expects_dialect(_), _)).
32 :- expects_dialect(sicstus4).
33 :- endif.
34
35 % Some errors, such as syntax errors, are only printed through the message mechanism and not thrown as exceptions.
36 % These errors also don't terminate SICStus or affect the exit code,
37 % which makes it easy for them to go unnoticed in CI runs.
38 % As a workaround, add a portray_message hook to detect error messages
39 % and halt the test runner at an appropriate time.
40
41 :- dynamic(prolog_error_occurred/0).
42
43 :- multifile(user:portray_message/2).
44 user:portray_message(error, _) :-
45 \+ prolog_error_occurred,
46 assertz(prolog_error_occurred),
47 fail. % to let SICStus portray the error
48
49 check_no_prolog_startup_error :-
50 prolog_error_occurred,
51 !,
52 format('*** Error(s) from Prolog while starting test runner! Aborting. ***~n', []),
53 halt(1).
54 check_no_prolog_startup_error.
55 :- initialization(check_no_prolog_startup_error).
56
57 :- use_module('../src/pathes', []). % set up library search paths
58
59 % comment in to ensure that plspec loaded first:
60 % :- use_module('../extensions/plspec/plspec/plspec_core').
61
62 :- use_module(probsrc(prob_cli)). % we call go_cli
63
64 %:- set_prolog_flag(compiling,debugcode).
65 %:- set_prolog_flag(source_info,on).
66 %:- set_prolog_flag(profiling,on).
67
68 /*
69 :- use_module('../extensions/profiler/profiler.pl'). % (adds the profiler himself)
70 :- use_module('../extensions/profiler/profiler_te.pl'). % (adds the term expander)
71 %:- use_module('../extensions/profiler/profiler_gui.pl'). % (adds the term expander)
72 :- enable_profiling_all(b_expression_sharing).
73 :- enable_profiling(bsyntaxtree:transform_bexpr_with_acc/5).
74 :- enable_profiling(bsyntaxtree:transform_bexpr/3).
75 :- enable_profiling(b_ast_cleanup:recompute_used_ids_info/2).
76 :- enable_profiling(bsyntaxtree:sub_expression_contains_wd_condition/1).
77
78 for starting from Spider:
79 set_prob_examples_location('../../prob_examples'), run_silently, test_repl.
80 or use test_runner_cov.pl
81 */
82
83
84 :- use_module(library(system)).
85 :- use_module(library(lists)).
86 :- use_module(library(sets), [intersect/2]).
87 :- use_module(library(file_systems)).
88
89 :- use_module(probsrc(junit_tests),[set_junit_dir/1,create_and_print_junit_result/4]).
90 :- use_module(testcases).
91 :- use_module(probsrc(system_call)).
92 :- use_module(probsrc(tools),[safe_number_codes/2,get_options/5]).
93 :- use_module(probsrc(tools_commands),[edit_file/1]).
94 :- use_module(probsrc(tools_printing), [start_terminal_colour/2, reset_terminal_colour/1, format_with_colour/4, format_with_colour_nl/4]).
95 :- use_module(probsrc(error_manager),[get_total_number_of_errors/1, get_all_errors/1]).
96 :- use_module(probsrc(debug),[formatsilent/3, debug_mode/1]).
97 :- use_module(probsrc(performance_messages),[toggle_perfmessages/0]).
98
99 :- use_module(probsrc(module_information)).
100 :- module_info(group,testing).
101 :- module_info(description,'This module runs the tests stored in testcases.pl.').
102
103 :- set_prolog_flag(double_quotes, codes).
104
105
106 :- use_module(probsrc(tools), [ajoin_with_sep/3]).
107 generate_makefile(RegenerateGoal,Filename,TargetCommand) :-
108 tell(Filename),
109 all_first_categories(AllTestCategories),
110 ajoin_with_sep(AllTestCategories, ' \\\n\t', JoinedCategories),
111 write('# This Makefile is generated automatically. DO NOT EDIT.\n'),
112 write('# If you have added/removed a test category, regenerate using this command:\n'),
113 format('# ./prolog.sh --file tests/test_runner.pl --goal "~q, halt ; halt(1)."\n\n', [RegenerateGoal]),
114 format('categories = \\\n\t~w\n\n', [JoinedCategories]),
115 write('.PHONY: test_all_source\n'),
116 write('test_all_source: $(categories)\n\n'),
117 write('.PHONY: $(categories)\n'),
118 format('$(categories):\n\t~w\n', [TargetCommand]),
119 told.
120
121 %write_subtargets([G]) :- write(G).
122 %write_subtargets([G|Gs]) :-
123 % write(G), write(' '),
124 % write_subtargets(Gs).
125
126 generate_makefile_all_tests :-
127 % run tests where category G is the first category to avoid running tests multiple times
128 % also avoids concurrency issues when test run multiple times in parallel with diff checking of output
129 generate_makefile(generate_makefile_all_tests, 'tests/Makefile_All_Tests',
130 % Makefile_All_Tests is (indirectly) included from the top-level Makefile,
131 % so the working directory is the repository root
132 % and the test_runner.pl path must include the tests/ directory prefix.
133 './prolog.sh --file tests/test_runner.pl --goal "run_silently, run_tests_by_first_category($@), halt_tests ; halt(1)."').
134
135 :- use_module(probsrc(tools_platform), [host_platform/1]).
136 % TODO Do we want to install SWI-Prolog on all CI runners?
137 group_cannot_be_checked_on_ci_server(setlog).
138 group_cannot_be_checked_on_ci_server(smt_solver_integration) :-
139 host_platform(windows). % TODO Install Z3 on GitLab CI Windows runner
140 group_cannot_be_checked_on_ci_server(cdclt) :-
141 \+ (current_prolog_flag(dialect, sicstus),
142 current_prolog_flag(version_data, sicstus(4,VN,VM,_,_)),
143 VN>=10, VM >= 2). % disable cdclt tests due to segmentation faults (SPRM-21543), fixed in 4.10.2
144
145 generate_makefile_for_coverage :-
146 generate_makefile(generate_makefile_for_coverage, 'Makefile_Coverage_Groups', './prolog.sh --file tests/prob_cov_runner.pl --goal "coverage_group_data($@), halt ; halt(1)."').
147
148 all_first_categories(X) :- % only categories that appear as first category
149 findall(Group1,
150 cli_testcase(_Id,[Group1|_],_Conf,_Args,_Desc),
151 ListOfGroups),
152 remove_dups(ListOfGroups,X).
153 all_categories(X) :-
154 findall(Groups,
155 cli_testcase(_Id,Groups,_Conf,_Args,_Desc),
156 List),
157 append(List,ListOfGroups),
158 remove_dups(ListOfGroups,X).
159
160 all_ids(X) :-
161 findall(Id,
162 cli_testcase(Id,_Groups,_Conf,_Args,_Desc),
163 List),
164 remove_dups(List,X).
165
166 ids_in_category(Category,IDs) :-
167 findall(Id,
168 (cli_testcase(Id,Groups,_Conf,_Args,_Desc),member(Category,Groups)),
169 List),
170 remove_dups(List,IDs).
171
172 prob_junit_dir(JUnitDir) :- environ('PROB_JUNIT_DIR', JUnitDir).
173
174 prob_junit_args(JUnitArgs) :-
175 prob_junit_dir(JUnitDir), !,
176 JUnitArgs = ['-junit', JUnitDir].
177 prob_junit_args([]).
178
179 halt1_allowed :- \+ repl_mode.
180
181 tests_multiply_timeout(Var) :-
182 environ('TESTS_MULTIPLY_TIMEOUT', Var2), !, tools:arg_is_number(Var2,Var).
183 tests_multiply_timeout(Var) :- multiply_timeout(Var).
184
185 :- dynamic multiply_timeout/1.
186
187 % call updir from within Spider, when you are in the src subdirectory
188 set_cur_dir :- (directory_exists('./src') -> true /* we seem to be in the right directory */
189 ; directory_exists('./cia') -> current_directory(_Old,'./..')
190 ; print('Please start tests from ProB directory or ProB/src directory'),nl,
191 print('Current directory: '),
192 current_directory(CD), print(CD), nl).
193
194 reset_test_runner(SessionInfo) :-
195 reset_nr_of_tests, set_cur_dir,
196 retractall(test_skipped(_)), retractall(test_failed(_)), retractall(last_test_failed(_)),
197 retractall(test_diff_failed(_,_,_)),
198 statistics(walltime,[WStart,_]),
199 bb_put(reset_test_runner_wtime,WStart),
200 bb_put(reset_test_runner_info,SessionInfo).
201
202
203 ids(Nr) :- show_command(Nr).
204 show_command(Nr) :- cli_testcase(Nr,_,_,L,Desc),
205 print('# '), print(Desc),nl, l_pr([probcli|L]).
206 l_pr([]) :- nl.
207 l_pr([H|T]) :- print(' '),print(H), l_pr(T).
208
209 :- volatile sanity_check_failed/0.
210 :- dynamic sanity_check_failed/0.
211 jenkins_sanity_check :-
212 sanity_check(true),
213 (sanity_check_failed -> halt1 ; halt(0)).
214 sanity_check(_Jenkins) :-
215 cli_testcase(Id,Categories,_,Args,_Comm1),
216 \+ member('-strict',Args),
217 format_warning('*** test does not use -strict option: ',[]), print_test(Id,Categories),nl,
218 assertz(sanity_check_failed), fail.
219 sanity_check(Jenkins) :-
220 Jenkins == false,
221 cli_testcase_diff_check_output(Id,_F1,_F2),
222 cli_testcase(Id,Categories,_I1,_A1,_Comm1),
223 (Categories = [_]
224 -> true
225 ; format_warning('*** multiple categories for test with output file; can generate race conditions when run in parallel: ',[]),
226 print_test(Id,Categories),
227 nl /* can generate race conditions when run in parallel */),
228 assertz(sanity_check_failed), fail.
229 sanity_check(_Jenkins) :-
230 cli_testcase_diff_check_output(Id,_F1,_F2),
231 \+ (cli_testcase(Id,_Categories,_I1,_A1,_Comm1)),
232 format_warning_nl('*** no cli_testcase, but diff output defined for id: ~w',[Id]),
233 assertz(sanity_check_failed), fail.
234 sanity_check(_Jenkins) :-
235 cli_testcase(Id,C1,I1,A1,Comm1),
236 cli_testcase(Id,C2,I2,A2,Comm2),
237 [C1,I1,A1,Comm1] \= [C2,I2,A2,Comm2],
238 format_warning_nl('*** multiple cli_testcases for id: ~w',[Id]),
239 assertz(sanity_check_failed), fail.
240 sanity_check(_Jenkins).
241
242
243 format_error_nl(F,Args) :- format_with_colour_nl(user_output,[red,bold],F,Args).
244 format_warning(F,Args) :- format_with_colour(user_output,[red],F,Args).
245 format_warning_nl(F,Args) :- format_with_colour_nl(user_output,[red],F,Args).
246 format_progress_nl(F,Args) :- format_with_colour_nl(user_output,[blue],F,Args).
247
248 print_test(Id,Comment) :- print(Id),print(' : '), print(Comment).
249
250 % run the latest test (with highest id)
251 last :- run_last_test.
252 run_last_test :- get_last_test_id(Id),
253 run_tests_by_id(Id,_,no_skipping).
254
255 get_last_test_id(Id) :- set_last_test_id, last_test_id(Id).
256
257 set_last_test_id :-
258 retractall(last_test_id(_)), assertz(last_test_id(-1)),
259 cli_testcase(Id,C1,I1,A1,Comm1),
260 (cli_testcase(Id,C2,I2,A2,Comm2),
261 (C1,I1,A1,Comm1) \= (C2,I2,A2,Comm2)
262 -> format_warning_nl('~n*** multiple entries for test id: ~w',[Id])
263 ; true),
264 update_last(Id),
265 fail.
266 set_last_test_id.
267
268 :- volatile last_test_id/1.
269 :- dynamic last_test_id/1.
270 update_last(Id) :- number(Id),!, % debug_print(9,Id), debug_print(9,' '),
271 retract(last_test_id(L)),
272 (Id>L -> Max = Id ; Max = L),
273 assertz(last_test_id(Max)).
274 update_last(Id) :- format_warning_nl('~n*** test id not a number: ~w',[Id]).
275
276 repeat_id(ID,Nr) :- repeat_id_aux(ID,0,Nr,success,10000000,0,0).
277 repeat_id_aux(ID,Nr,TotNr,Status1,Min,Max,Tot) :- Nr<TotNr, !, N1 is Nr+1,
278 statistics(walltime,[Start,_]),
279 run_tests_by_id(ID,Status,no_skipping),
280 (Status=success -> Status2=Status1, Col=blue ; Status2=Status, Col=red),
281 statistics(walltime,[Stop,_]),
282 WT is Stop - Start,
283 NewMin is min(Min,WT), NewMax is max(Max,WT), NewTot is Tot+WT,
284 Average is NewTot / N1,
285 format_with_colour_nl(user_output,[Col],'Test ~w repeated ~w/~w times~n Walltime Minumum = ~w ms, Maximum = ~w ms, Average = ~w ms~n Status=~w',[ID,N1,TotNr,NewMin,NewMax,Average,Status]),
286 repeat_id_aux(ID,N1,TotNr,Status2,NewMin,NewMax,NewTot).
287 repeat_id_aux(_,_,_TotNr,_Status,_Min,_Max,_).
288
289 % run a test with a specific id
290 run_id(X) :- run_tests_by_id(X,_,no_skipping).
291 run_tests_by_id(X) :- run_tests_by_id(X,_,allow_skipping).
292 run_tests_by_id(X,Status,AllowSkipping) :- reset_test_runner(id(X)),
293 phrase(tests_by_id(X), Testcases),
294 run_list(Testcases, AllowSkipping),
295 check_failed(Status).
296 tests_by_id([]) --> [].
297 tests_by_id(From-To) --> !,
298 ({From>To} -> []
299 ; tests_by_id(From), {F1 is From+1},
300 tests_by_id(F1-To)).
301 tests_by_id([Id | Ids]) --> !,
302 tests_by_id(Id),
303 tests_by_id(Ids).
304 tests_by_id(Id) -->
305 {cli_testcase(Id,TestCategories,TestInfos,Arguments,Comment)}, !,
306 [testcase(Id,TestCategories,TestInfos,Arguments,Comment)].
307 tests_by_id(Id) -->
308 {\+(cli_testcase(Id,_Category,_Infos,_Arguments,_Comment))}, !,
309 {format_warning_nl('*** No testcase with ID ~w found', [Id])}.
310
311 :- use_module(library(random),[random_select/3]).
312 run_random_tests(Nr) :-
313 findall(testcase(Id,TestCategories,TestInfos,Arguments,Comment),
314 cli_testcase(Id,TestCategories,TestInfos,Arguments,Comment),AllTests),
315 select_random_tests(Nr,AllTests,Tests),
316 reset_test_runner(random(Nr)),
317 run_list(Tests), check_failed(_).
318 select_random_tests(N,_,[]) :- N<1,!.
319 select_random_tests(_,[],[]) :- !.
320 select_random_tests(N,All,[X|Tests]) :-
321 random_select(X,All,Rest),
322 N1 is N-1,
323 select_random_tests(N1,Rest,Tests).
324
325
326 run_tests_all :- run_tests_all_except([]).
327
328 :- use_module(library(ordsets)).
329 run_tests_all_except(Categories) :- reset_test_runner(all_except(Categories)),
330 findall(Test, test_not_in_categories(Categories, Test), Tests),
331 run_list(Tests),
332 check_failed(_).
333
334 test_not_in_categories(Categories, Test) :-
335 sort(Categories, SC),
336 test_not_in_categories_aux(SC, Test).
337
338 :- use_module(library(ordsets), [ord_intersect/2]).
339 test_not_in_categories_aux(SC, testcase(Id,TestCategories,Infos,Arguments,Comment)) :-
340 cli_testcase(Id,TestCategories,Infos,Arguments,Comment),
341 sort(TestCategories,TC),
342 \+ ord_intersect(TC,SC).
343
344
345 :- dynamic silent_running/0, no_strict_running/0.
346 run_silently :- silent_running, !.
347 run_silently :- add_additional_arguments(['-silent']), assertz(silent_running).
348 run_no_strict :- (no_strict_running -> true ; assertz(no_strict_running)). % useful to see all errors of a test
349
350 % perform additional checks (e.g., used_ids info in AST) and throw errors when something is wrong
351 run_safely :- add_additional_preference('PROB_SAFE_MODE','TRUE').
352
353 % run all tests with a specific category
354 category(X) :- run_tests_by_category(X).
355 run_tests_by_category(X) :- run_tests_by_category(X,all).
356 run_tests_by_first_category(X) :- % used as entry in Jenkins when running all tests in parallel by category
357 run_tests_by_category(X,first).
358 run_tests_by_category(X,FirstOnly) :-
359 reset_test_runner(category(X,FirstOnly)),
360 (is_list(X) -> run_tests_by_category_aux(X,FirstOnly) ; run_tests_by_category_aux([X],FirstOnly)),
361 check_failed(_).
362 run_tests_by_category_aux(Categories,FirstOnly) :-
363 get_tests_for_categories(Categories,FirstOnly,List),
364 ( List=[] -> format_warning_nl('*** No testcase with categories ~w found', [Categories])
365 ; run_list(List)).
366
367 get_tests_for_categories(Categories,List) :-
368 get_tests_for_categories(Categories,all,List).
369 get_tests_for_categories(Categories,FirstOnly,List) :-
370 findall(testcase(Id, TestCategories, Infos, Arguments, Comment),
371 (cli_testcase(Id, TestCategories, Infos, Arguments, Comment),
372 (FirstOnly=first -> TestCategories=[C1|_], member(C1,Categories)
373 ; intersect(Categories, TestCategories)) ),
374 List).
375
376 run_tests_using_command(Command) :- reset_test_runner(cmd(Command)),
377 findall(testcase(Id, TestCategories, Infos, Arguments, Comment),
378 (cli_testcase(Id, TestCategories, Infos, Arguments, Comment),
379 member(Command, Arguments)),
380 List),
381 ( List=[] -> format_warning_nl('*** No testcase using command ~w found', [Command])
382 ; run_list(List), check_failed(_)).
383
384 run_tests_using_preference(Pref,Val) :- reset_test_runner(using_pref(Pref,Val)),
385 findall(testcase(Id, TestCategories, Infos, Arguments, Comment),
386 (cli_testcase(Id, TestCategories, Infos, Arguments, Comment),
387 append(_,['-p',CmdPref,Val|_],Arguments),
388 match_preference(Pref,CmdPref) ),
389 List),
390 ( List=[] -> format_warning_nl('*** No testcase using preference ~w with value ~w found', [Pref,Val])
391 ; run_list(List), check_failed(_)).
392
393 :- use_module(probsrc(preferences),[eclipse_preference/2]).
394 match_preference(A,A).
395 match_preference(A,B) :- eclipse_preference(A,B) ; eclipse_preference(B,A).
396
397 % Generate file list (for copying) of a category :
398 show_files(Cat) :-
399 cli_testcase(Id, TestCategories, _Infos, Arguments, _Comment),
400 member(Cat, TestCategories), % print(Arguments),nl,
401 ( file_in_arguments(File,Arguments)
402 ;
403 get_testcase_diff_check_output(Id,_GeneratedFile,File)),
404 format('~w ',[File]),
405 fail.
406 show_files(_) :- nl.
407
408 file_in_arguments(F,['-p',_,_|T]) :- !, file_in_arguments(F,T).
409 file_in_arguments(F,[C,_|T]) :- binary_command(C),!, file_in_arguments(F,T).
410 file_in_arguments(F,[H|T]) :- is_file(H),H=F ; file_in_arguments(F,T).
411 binary_command(eval). binary_command(evalt). binary_command(evalf).
412 binary_command('-mc').
413 is_file(F) :- atom(F), atom_codes(F,Codes), member(47,Codes),!.
414
415
416 % Generate a Makefile for all listed Categories
417 % example: makefile_by_category('AlstomMakefile',[animate,history,sptxt,rel_fnc])
418 % the Makefile can be run to perform the listed tests on a compiled version of probcli
419 makefile_by_category(File, Categories) :-
420 (is_list(Categories) -> Cats = Categories ; Cats = [Categories]),
421 my_open(File, Stream),
422 format(Stream, 'PROBCLI=probcli~n',[]),
423 format(Stream, 'test_all:', []),
424 makefile_write_categories(Stream, Cats),
425 makefile_by_category_aux(Stream, Cats),
426 my_close(Stream).
427 makefile_by_category_aux(_Stream, []).
428 makefile_by_category_aux(Stream, [Cat | Cats]) :-
429 makefile_by_category_single(Stream, Cat),
430 makefile_by_category_aux(Stream, Cats).
431 makefile_by_category_single(Stream, Cat) :-
432 findall(testcase(Id, TestCategories, Infos, Arguments, Comment),
433 (cli_testcase(Id, TestCategories, Infos, Arguments, Comment),
434 member(Cat, TestCategories)),
435 List),
436 (List=[] -> format_warning_nl('*** No testcase with category ~w found', [Cat])
437 ; format(Stream, '~n~w:~n', [Cat]), makefile_write_calls(Stream, List)).
438
439 my_open(user_output,S) :- !, S=user_output.
440 my_open(File,S) :- open(File,write,S).
441 my_close(user_output) :- !.
442 my_close(S) :- close(S).
443
444 makefile_write_categories(Stream, []) :-
445 format(Stream, '~n', []).
446 makefile_write_categories(Stream, [Cat | Cats]) :-
447 format(Stream, ' ~a', [Cat]),
448 makefile_write_categories(Stream, Cats).
449
450 makefile_write_calls(_Stream, []).
451 makefile_write_calls(Stream, [testcase(Id, _TestCategories, _Infos, Arguments, Comment) | Tests]) :-
452 (Comment='' -> true ; format(Stream,'\techo \"Test ~w : ~w\"~n',[Id,Comment])),
453 format(Stream, '\t$(PROBCLI)', []),
454 makefile_write_arguments(Stream, Arguments),
455 makefile_write_diff(Stream, Id),
456 makefile_write_calls(Stream, Tests).
457
458 makefile_write_arguments(Stream, []) :-
459 format(Stream, '~n', []).
460 makefile_write_arguments(Stream, [Arg | Args]) :-
461 format(Stream, ' ~w', [Arg]),
462 (quote_next_arg(Arg) -> makefile_write_arguments_quoted(Stream,Args)
463 ; makefile_write_arguments(Stream, Args)).
464
465 quote_next_arg('-goal'). % this will contain spaces ,... surround in "" for shell
466 quote_next_arg('--check_goal').
467 quote_next_arg('-check_goal').
468 quote_next_arg('-cbc_deadlock_pred').
469 quote_next_arg('-eval').
470 quote_next_arg('-evalt').
471 quote_next_arg('-evalf').
472 quote_next_arg('-cbc_sequence_with_target'). % actually quotes next two !
473 quote_next_arg('-cbc_sequence_with_target_all'). % ditto <- TO DO
474
475
476 makefile_write_arguments_quoted(Stream, []) :-
477 format(Stream, '~n', []).
478 makefile_write_arguments_quoted(Stream, [Arg | Args]) :-
479 format(Stream, ' \"~w\"', [Arg]),
480 makefile_write_arguments(Stream, Args).
481
482 makefile_write_diff(Stream, ID) :-
483 get_testcase_diff_check_output(ID, File1, File2),
484 format(Stream, '\tdiff -b ~w ~w~n', [File1, File2]),
485 fail.
486 makefile_write_diff(_Stream, _ID).
487
488 % -------------------------
489
490 copy(Cat) :- (Cat=[_|_] -> C=Cat ; C=[Cat]),
491 generate_copy_commands(C,'testarchive/').
492
493 :- use_module(probsrc(b_trace_checking),[get_default_trace_file/2]).
494 generate_copy_commands(Categories,Dest) :-
495 cli_testcase(ID, TestCategories, _Infos, Arguments, _Comment),
496 non_empty_inter(Categories,TestCategories), %print(ID),nl,
497 Arguments=[MainFile|_], generate_copy_command(MainFile,Dest), % print(MainFile),nl,
498 additional_testcase_file(ID,MainFile,Arguments,ExtraFile),
499 generate_copy_command(ExtraFile,Dest),
500 fail.
501 generate_copy_commands(_,_).
502
503 additional_testcase_file(ID,_,_,EFile) :- extra_testcase_file(ID,EFile).
504 additional_testcase_file(_ID,File,Arguments,TFile) :- member('-t',Arguments),
505 get_default_trace_file(File,TFile).
506 additional_testcase_file(ID,_,_,RefFile2) :- get_testcase_diff_check_output(ID,_File1,RefFile2).
507
508 non_empty_inter(A,B) :- member(X,A), member(X,B),!.
509
510 :- use_module(probsrc(tools_strings),[string_concatenate/3]).
511 :- use_module(probsrc(tools),[get_parent_directory/2]).
512 generate_copy_command(File,Dest) :-
513 safe_file_exists(File),
514 get_parent_directory(File,Dir),!,
515 string_concatenate(Dest,Dir,DestDir),
516 string_concatenate(Dest,File,DestFile),
517 format(user_output,'\tmkdir -p ~w~n',[DestDir]),
518 get_command_path(mkdir,MkCmdPath),
519 system_call(MkCmdPath, ['-p',DestDir],_Text1,_JExit1),
520 format(user_output,'\tcp ~w ~w~n',[File,DestFile]),
521 get_command_path(cp,CpCmdPath),
522 system_call(CpCmdPath, [File,DestFile],_Text2,_JExit2).
523 generate_copy_command(_,_).
524
525 % -------------------------
526
527 :- dynamic test_failed/1, last_test_failed/1, test_diff_failed/3, test_skipped/1.
528 halt_tests :-
529 prolog_error_occurred,
530 !,
531 halt1.
532 halt_tests :- test_failed(_),!,
533 halt1.
534 halt_tests :-
535 nl,print('TEST RUN SUCCESSFUL'),nl,
536 halt. % regular halt
537 halt1 :-
538 format_error_nl('TEST RUN FAILED',[]),
539 halt(1).
540
541 check_failed(failure) :- test_failed(X),!,print_failed_tests,
542 print('Use the following command to run individual tests: '),nl,
543 print(' ./prolog.sh --file tests/test_runner.pl --goal "run_id('),print(X),print(')."'),nl,
544 (halt1_allowed -> halt1 ; print('halt(1) :: dontstop mode'),nl).
545 check_failed(success) :- number_of_tests_run(Nr),
546 start_terminal_colour([green,bold],user_output),
547 findall(Y,test_skipped(Y),Skips), length(Skips,NrSkips),
548 bb_get(reset_test_runner_info,Info),
549 (Nr=1,NrSkips=0,Info\=category(_,_) -> format(user_output,'Test successful.~n',[])
550 ; NrSkips>0 -> format(user_output,'All ~w tests successful, ~w skipped (for ~w).~n',[Nr,NrSkips,Info])
551 ; format(user_output,'All ~w tests successful (for ~w).~n',[Nr,Info])),
552 reset_terminal_colour(user_output).
553 print_failed_tests :- number_of_tests_run(Nr),
554 findall(Y,test_failed(Y),Fails), length(Fails,NrFails),
555 findall(Y,test_skipped(Y),Skips), length(Skips,NrSkips),
556 start_terminal_colour([red,bold],user_error),
557 bb_get(reset_test_runner_info,Info),
558 format(user_error,'*** Tests run: ~w, skipped: ~w, failed: ~w (for ~w) ***~n*** Failed tests:~n',[Nr,NrSkips,NrFails,Info]),
559 test_failed(X), lookup_test_description(X,Desc),
560 format(user_error,'~w ~w~n',[X,Desc]),
561 fail.
562 print_failed_tests :- nl(user_error),
563 (user_interrupt_signal_received -> format(user_error,'Tests were interrupted by CTRL-C (user_interrupt)~n',[])
564 ; true),
565 reset_terminal_colour(user_error).
566
567 lookup_test_description(Id,Desc) :- cli_testcase(Id,_,_,_,Desc).
568
569 :- dynamic user_interrupt_signal_received/0.
570 run_list(List) :- run_list(List,allow_skipping).
571 run_list(List,AllowSkipping) :-
572 init_test_runner,
573 retractall(user_interrupt_signal_received),
574 length(List,Len),
575 run_single_testcase_list_aux(List,Len,AllowSkipping).
576 run_single_testcase_list_aux([],_,_) :- !.
577 run_single_testcase_list_aux(List,_Len,allow_skipping) :- user_interrupt_signal_received,!,
578 length(List,RestLen),
579 format('Skipping ~w remaining tests~n',[RestLen]),
580 maplist(assert_test_skipped,List).
581 run_single_testcase_list_aux([TC|Tail],Len,AllowSkipping) :-
582 print_progress_stats(Len),
583 run_single_testcase(TC,AllowSkipping),
584 run_single_testcase_list_aux(Tail,Len,AllowSkipping).
585 assert_test_skipped(Id) :- assertz(test_skipped(Id)).
586
587 print_progress_stats(All) :- number_of_tests_run(Nr), Nr>0,!,
588 findall(Y,test_failed(Y),Fails), length(Fails,NrFails),
589 (last_test_failed(Failed) -> ajoin(['(e.g. ', Failed, ')'],FailInfo) ; FailInfo = ''),
590 findall(Y,test_skipped(Y),Skips), length(Skips,NrSkips),
591 bb_get(reset_test_runner_wtime,WStart),
592 statistics(walltime,[WNow,_]), Delta is round((WNow - WStart) / 1000),
593 bb_get(reset_test_runner_info,Info),
594 format_progress_nl('Progress: ~w/~w tests run, ~w skipped, ~w failed ~w (running since ~w sec for ~w)',[Nr,All,NrSkips,NrFails,FailInfo,Delta,Info]).
595 print_progress_stats(_).
596
597 cache :- add_additional_arguments(['-cache','/Users/leuschel/svn_root/NewProB/examples/cache/']).
598 v :- add_additional_arguments(['-v']). % verbose
599 vv :- add_additional_arguments(['-vv']). % very_verbose
600
601 :- volatile additional_arguments/1.
602 :- dynamic additional_arguments/1.
603 % add additional cli arguments when running tests:
604 add_additional_arguments(List) :-
605 (retract(additional_arguments(Old)) -> true ; Old=[]),
606 append(Old,List,New),
607 assertz(additional_arguments(New)).
608
609 reset_additional_arguments :- retractall(additional_arguments(_)).
610
611 % auto complete a preference name and print error if no match found
612 get_preference_name(Prefix,Name) :- auto_complete_preference(Prefix,Name),!.
613 get_preference_name(Prefix,_) :-
614 format_warning_nl('No matching preference found for: ~w',[Prefix]),fail.
615
616 :- use_module(probsrc(preferences),[eclipse_preference/2]).
617 auto_complete_preference(Prefix,Name) :-
618 atom_codes(Prefix,PC),
619 eclipse_preference(EP,Name),
620 (atom_codes(EP,EPC) ; atom_codes(Name,EPC)),
621 append(PC,_,EPC).
622
623 % add additional preference when running tests:
624 add_additional_preference(PREF,PREFVAL) :-
625 (retract(additional_arguments(Old0)) -> remove_matching_pref(Old0,PREF,Old) ; Old=[]),
626 New = ['-p',PREF,PREFVAL|Old],
627 format('New additional arguments: ~w~n',[New]),
628 assertz(additional_arguments(New)).
629
630 % remove all preferences conflicting with PREF
631 remove_matching_pref([],_PREF,[]).
632 remove_matching_pref(['-p',P,OLD|T],PREF,Res) :- !,
633 (P=PREF -> Res=T ; Res = ['-p',P,OLD|RT], remove_matching_pref(T,PREF,RT)).
634 remove_matching_pref([H|T],PREF,[H|RT]) :- remove_matching_pref(T,PREF,RT).
635
636 % remove all preferencs conflicting with other list of prefs
637 remove_matching_prefs([],P,P).
638 remove_matching_prefs(['-cache',_File|T],InPrefs,Res) :- !,
639 remove_matching_prefs(T,InPrefs,Res).
640 remove_matching_prefs(['-p',PREF,_|T],InPrefs,Res) :-
641 remove_matching_pref(InPrefs,PREF,In2),
642 remove_matching_prefs(T,In2,Res).
643 remove_matching_prefs([_|T],InPrefs,Res) :-
644 remove_matching_prefs(T,InPrefs,Res).
645
646 :- use_module(probsrc(pathes_lib), [unavailable_extension/2]).
647 unavailable_extension_for_test(Id,TestCategories,Ext,Reason) :-
648 test_requires_extension(Id,TestCategories,Ext),
649 unavailable_extension(Ext,Reason).
650
651 :- dynamic skip_all_tests/0.
652
653 skip_test(_, _, _, ReasonMsg, ReasonTerm) :-
654 skip_all_tests,
655 !,
656 ReasonMsg = 'skipping all tests',
657 ReasonTerm = skip_all_tests.
658 skip_test(_Id, _TestCategories, TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
659 member(skip,TestConfigurationInfos),
660 !,
661 ReasonMsg = 'test marked as skipped',
662 ReasonTerm = skip.
663 skip_test(_Id, _TestCategories, TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
664 member(conditional_skip(Callable), TestConfigurationInfos),
665 % Evaluate the condition in the context of the testcases module,
666 % where it was defined, so that imports are visible to the condition.
667 % We can't use a meta_predicate declaration here,
668 % because the goal is nested inside a term.
669 call(testcases:Callable),
670 !,
671 ReasonMsg = 'skip condition is true',
672 ReasonTerm = conditional_skip(Callable).
673 skip_test(_Id, TestCategories, _TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
674 member(private, TestCategories),
675 \+ absolute_file_name(prob_examples(examples), _, [access(exist), file_type(directory), file_errors(fail)]),
676 !,
677 ReasonMsg = 'test requires non-public examples which are not available',
678 ReasonTerm = private.
679 skip_test(_Id, TestCategories, _TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
680 member(private_source_not_available, TestCategories),
681 \+ directory_exists('../private_examples'),
682 !,
683 ReasonMsg = 'test requires private examples which are not available',
684 ReasonTerm = private_source_not_available.
685 skip_test(_Id, _TestCategories, TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
686 current_prolog_flag(dialect, swi),
687 % TODO Handle swi_expected_failure differently.
688 % Ideally, tests that are expected to fail on SWI should not be skipped,
689 % but instead should be run normally with any failures ignored.
690 % This would allow detecting when tests unexpectedly succeed
691 % (i. e. have been fixed, but not unmarked as expected failure yet).
692 member(swi_expected_failure, TestConfigurationInfos),
693 !,
694 ReasonMsg = 'test expected to fail on SWI-Prolog',
695 ReasonTerm = swi_expected_failure.
696 skip_test(_Id, TestCategories, _TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
697 ? group_cannot_be_checked_on_ci_server(Category),
698 member(Category, TestCategories),
699 !,
700 ReasonMsg = 'category cannot be checked on CI',
701 ReasonTerm = Category.
702 skip_test(Id, TestCategories, _TestConfigurationInfos, ReasonMsg, ReasonTerm) :-
703 unavailable_extension_for_test(Id,TestCategories,Ext,Reason),
704 !,
705 ajoin(['test requires an unavailable extension (',Reason,')'],ReasonMsg),
706 ReasonTerm = Ext.
707
708
709 % RUNNING SINGLE TESTCASE:
710 % ------------------------
711
712 :- dynamic previous_test_run/1.
713 previous_test_run(none).
714
715 :- use_module(library(timeout), [time_out/3]).
716
717 run_single_testcase(testcase(Id,TestCategories,Infos,Arguments,Comment),AllowSkipping) :-
718 skip_test(Id,TestCategories,Infos,ReasonMsg,ReasonTerm),
719 (AllowSkipping = allow_skipping -> true
720 ; % Skipping disabled - print message, then continue to next clause to run the test anyway.
721 format_progress_nl('Running test that would normally be skipped - ~w: ~q', [ReasonMsg,ReasonTerm]),
722 fail),
723 !,
724 full_flush,
725 format_with_colour_nl(user_output,[blue,bold],'Skipping test ~w (~w) because ~w: ~q', [Id,Comment,ReasonMsg,ReasonTerm]),
726 print_junit_skip(Arguments),
727 assertz(test_skipped(Id)),
728 full_flush.
729 run_single_testcase(testcase(Id,_TestCategories,_Infos,Arguments,Comment),AllowSkipping) :-
730 test_requires_previous_test(Id,ReqPreviousId),
731 previous_test_run(PreviousId),
732 PreviousId \= ReqPreviousId,!,
733 format_warning_nl('~n*** Test id ~w requires test ~w to be run just before, but previous test is ~w',
734 [Id,ReqPreviousId,PreviousId]),
735 (AllowSkipping = allow_skipping -> true
736 ; format_progress_nl('Running test that would normally be skipped', []),fail),
737 !,
738 full_flush,
739 format_with_colour_nl(user_output,[blue,bold],'Skipping test ~w (~w)', [Id,Comment]),
740 print_junit_skip(Arguments),
741 assertz(test_skipped(Id)),
742 full_flush.
743 run_single_testcase(testcase(Id,_TestCategories,_Infos,Arguments,Comment),_) :-
744 full_flush,
745 format_progress_nl('Running test ~w ~n ~w',[Id,Comment]),
746 retractall(previous_test_run(_)), assert(previous_test_run(Id)),
747 (debug_mode(off) -> true ; print('testcase: probcli '), print_args(Arguments),nl),
748 clear_diff_output(Id),
749 clear_logxml_output(Id),
750 prob_junit_args(JUnitArgs),
751 maplist(patch_prob_examples_loc,Arguments,Arguments0), % update path to prob_examples if necessary
752 append(Arguments0, JUnitArgs, Arguments1),
753 (additional_arguments(ExtraArgs)
754 -> remove_matching_prefs(ExtraArgs,Arguments1,RemArguments1), % remove conflicting arguments now overriden
755 append(ExtraArgs,RemArguments1,Arguments2)
756 ; Arguments1=Arguments2),
757 ? (tests_multiply_timeout(Factor) -> modify_timeout(Factor,Arguments2,Arguments3) ; Arguments3 = Arguments2),
758 (no_strict_running, select('-strict',Arguments3,Arguments4) -> true ; Arguments4=Arguments3),
759 (silent_running -> true ; print('executing: probcli '), print_args(Arguments4),nl),
760 full_flush,
761 test_started(Id),
762 time_out(catch(prob_cli:run_probcli(Arguments4,[test_runner]), Exception, true),
763 2147483646,TO_Result), !,
764 test_finished(Id,Walltime),
765 (TO_Result \= success % time_out occured, possibly due to throw(time_out);
766 -> Exception = TO_Result % et Exception to time_out
767 ; true),
768 (test_exception_is_success(Exception)
769 -> check_diff_output(Id),
770 check_logxml(Id)
771 ; format_warning_nl('Exception in test ~w: ~w',[Id,Exception]),
772 handle_test_exception(Exception),
773 print_test_failed(Id,Exception)
774 ),
775 (prolog_error_occurred ->
776 print_test_failed(Id,'Error(s) from Prolog during test! See console log for details. Aborting test run.'),
777 halt1
778 ; true),
779 !,
780 (test_failed(Id) % asserted by check_diff_output/check_logxml/print_test_failed
781 -> true % messages already printed above
782 ; format_with_colour_nl(user_output,[green],'Test ~w completed successfully (in ~w ms)~n',[Id,Walltime])
783 ),
784 safe_reset_cli(Id),
785 full_flush.
786
787 test_exception_is_success(Exception) :- var(Exception), !. % No exception was actually thrown
788 test_exception_is_success(Exception) :- Exception == halt(0). % can be thrown by typechecker_test
789
790 handle_test_exception(Exception) :-
791 ( Exception = error(_,_) -> Type = 'Error'
792 ; Exception = enumeration_warning(_,_,_,_,_) -> Type = 'Enumeration exception'
793 ; Exception = solver_and_provers_too_weak -> Type = 'Solver exception'
794 ; fail
795 ),
796 !,
797 (halt1_allowed
798 -> format_warning_nl('~w during test execution: ~w',[Type,Exception]),
799 halt1
800 ; true).
801 handle_test_exception(halt(1)) :- !.
802 handle_test_exception(parse_errors(_)) :- !.
803 handle_test_exception(time_out) :- !.
804 handle_test_exception(user_interrupt_signal) :- !,
805 assertz(user_interrupt_signal_received),
806 format_warning_nl('CTRL-C received, aborting tests',[]).
807 handle_test_exception(Exception) :-
808 format_warning_nl('Exception not caught in test_runner: ~w',[Exception]),
809 halt1.
810
811 safe_reset_cli(Id) :-
812 catch(prob_cli:reset_cli, % when bundles/extensions are missing we can get exceptions here
813 Exception,
814 (halt1_allowed
815 -> format_warning_nl('Exception while resetting probcli: ~w',[Exception]),
816 halt1
817 ; print_test_failed(Id,Exception)
818 )).
819
820 :- dynamic test_took_aux/2.
821 :- dynamic last_testcase_run/1, number_of_tests_run/1.
822 number_of_tests_run(0).
823 reset_nr_of_tests :- retractall(number_of_tests_run(_)), assertz(number_of_tests_run(0)).
824
825 :- use_module(library(system),[now/1, datime/2]).
826 :- dynamic performance_session_running/1, performance_session_stats/4.
827
828 performance_session_start :-
829 now(When),
830 datime(When,datime(Year,Month,Day,Hour,Min,Sec)),
831 format('~nStarting Codespeed Performance Monitoring session ~w:~w:~w:~w:~w:~w~n',[Year,Month,Day,Hour,Min,Sec]),
832 retractall(performance_session_running(_)),
833 assertz(performance_session_running(When)).
834
835 :- use_module(probsrc(parsercall),[get_parser_version/1]).
836 :- use_module(probsrc(version), [version_str/1, revision/1, lastchangeddate/1,
837 format_prolog_version/1, format_prob_version/1]).
838 performance_session_end(FilePrefix) :-
839 performance_session_running(When),
840 datime(When,datime(Year,Month,Day,Hour,Min,Sec)),
841 %tools:ajoin([FilePrefix,':',Year,Month,Day,Hour,Min,Sec],FileName),
842 format('~nFinishing Codespeed session ~w:~w:~w:~w:~w:~w~n -> File : ~w~n',[Year,Month,Day,Hour,Min,Sec,FilePrefix]),
843 open(FilePrefix,append,S),
844 format(S,'~n/* Codespeed session ~w:~w:~w:~w:~w:~w */~n',[Year,Month,Day,Hour,Min,Sec]),
845 version_str(Vers), portray_clause(S, session_prob_version(When,Vers)),
846 revision(Rev), portray_clause(S, session_prob_revision(When,Rev)),
847 lastchangeddate(DD), portray_clause(S, session_prob_lastchangeddate(When,DD)),
848 get_parser_version(PV), portray_clause(S, session_prob_parser_version(When,PV)),
849 current_prolog_flag(version, PrologVString),
850 portray_clause(S, session_prolog_version_string(When,PrologVString)),
851 current_prolog_flag(version_data, PrologVData),
852 portray_clause(S, session_prolog_version_data(When,PrologVData)),
853 current_prolog_flag(host_type, HostType),
854 portray_clause(S, session_prolog_host_type(When,HostType)),
855 write_perf_data(When,S).
856
857 write_perf_data(When,S) :- additional_arguments(New),
858 portray_clause(S, stored_additional_arguments(When,New)),
859 fail.
860 write_perf_data(When,S) :- performance_session_stats(When,Id,Time,WTime),
861 portray_clause(S, stored_performance_test_stats(When,Id,Time,WTime)),
862 fail.
863 write_perf_data(_When,S) :- nl(S), nl(S), close(S).
864
865
866
867 test_started(Id) :-
868 retractall(last_testcase_run(_)), assertz(last_testcase_run(Id)),
869 retractall(test_took_aux(_,_)),
870 statistics(runtime,[Start,_]),
871 statistics(walltime,[WStart,_]),
872 bb_put(test_started,Start),
873 bb_put(test_started_wtime,WStart),
874 bb_put(test_target_coverage,0). % a special value which can be increased in ProB's source code
875 % useful to measure the number of tests that cover a new feature
876
877 :- public inc_test_target_coverage/0.
878 % call this from prob_prolog if some code point you wish to test is covered
879 % using the stats repl command you can then see how many tests have covered this code point
880 inc_test_target_coverage :-
881 bb_get(test_target_coverage,X),
882 X1 is X+1,
883 bb_put(test_target_coverage,X1).
884
885 :- dynamic test_stats/6.
886 test_finished(Id,WTime) :-
887 statistics(runtime,[End,_]),
888 statistics(walltime,[WEnd,_]),
889 bb_get(test_started,Start),
890 bb_get(test_started_wtime,WStart),
891 bb_get(test_target_coverage,Covered),
892 Time is End - Start, WTime is WEnd- WStart,
893 retractall(test_took_aux(_,_)),
894 assertz(test_took_aux(Time,WTime)),
895 (retract(number_of_tests_run(Nr)) -> N1 is Nr+1 ; N1=1),
896 assertz(number_of_tests_run(N1)),
897 (retract(test_stats(Id,PrevTime,PrevWTime,_,_,_))
898 -> assertz(test_stats(Id,Time,WTime,PrevTime,PrevWTime,Covered))
899 ; assertz(test_stats(Id,Time,WTime,-1,-1,Covered))
900 ),
901 (performance_session_running(When)
902 -> assertz(performance_session_stats(When,Id,Time,WTime))
903 ; true).
904
905 print_delta_stats :- print('Comparing walltimes with previous test run: '),nl,
906 findall(delta(DeltaPerc,DeltaWTime,Id),test_delta_stat(Id,DeltaPerc,DeltaWTime),L),
907 (L=[] -> print('No previous run information available'),nl
908 ; print(' ID | % (delta absolute) | walltime (runtime)~n'),nl,
909 sort(L,SL),
910 maplist(print_delta,SL)).
911 test_delta_stat(Id,DeltaPerc,DeltaWTime) :-
912 test_stats(Id,_RTime,WTime,_PrevRTime,PrevWTime,_),
913 PrevWTime>0,
914 DeltaWTime is WTime - PrevWTime,
915 DeltaPerc is (100*DeltaWTime) / PrevWTime.
916 print_delta(delta(DeltaPerc,DeltaWTime,Id)) :-
917 test_stats(Id,RTime,WTime,_PrevRTime,PrevWTime,_Cov),
918 format(' ~w | ~2f % (~w ms) | ~w ms (~w ms runtime) [~w walltime ms previously]~n',
919 [Id,DeltaPerc,DeltaWTime,WTime,RTime,PrevWTime]).
920
921 :- use_module(probsrc(tools),[print_memory_used_wo_gc/0]).
922 print_current_stats :- print_current_stats(user_output,' | ').
923 print_current_stats(Stream,Sep) :-
924 print_memory_used_wo_gc,nl,
925 bb_put(test_target_coverage_nr,0),
926 bb_put(test_target_coverage_count,0),
927 bb_put(test_counter_nr,0),
928 format(Stream,' ID~wOK~wWALLTIME (ms)~wRUNTIME (ms)~wCOV~wDESCRIPTION~n',[Sep,Sep,Sep,Sep,Sep]),
929 test_stats(Id,RTime,WTime,_PrevRTime,_PrevWTime,NrTargetCoverage),
930 cli_testcase(Id,_Cat,_,_Args,Desc),
931 (test_failed(Id) -> OK = '*FAILED*' ; OK = ' OK '),
932 format(Stream,' ~w~w~w~w~w~w~w~w~w~w~w~n',
933 [Id,Sep,OK,Sep,WTime,Sep,RTime,Sep,NrTargetCoverage,Sep,Desc]),
934 bb_get(test_counter_nr,Nr), Nr1 is Nr+1, bb_put(test_counter_nr,Nr1),
935 (NrTargetCoverage>0
936 -> bb_get(test_target_coverage_nr,TN), TN1 is TN+1, bb_put(test_target_coverage_nr,TN1),
937 bb_get(test_target_coverage_count,NC), NC1 is NC+NrTargetCoverage,
938 bb_put(test_target_coverage_count,NC1)
939 ),
940 fail.
941 print_current_stats(_,_) :-
942 bb_get(test_counter_nr,NrT),
943 format('Number of tests: ~w~n',[NrT]),
944 (bb_get(test_target_coverage_nr,Nr), Nr>0 ->
945 bb_get(test_target_coverage_count,NrC),
946 Perc is (100.0 * Nr) / NrT,
947 format('Number of tests reaching test source code target: ~w (~2f %, total hits: ~w)~n',[Nr, Perc, NrC])
948 ; true).
949
950 test_took(Time,WTime) :- test_took_aux(Time,WTime), !.
951 test_took(0,0) :-
952 format_warning_nl('test_took/2 called before test_finished/2, this should not happen!',[]).
953
954 print_junit_skip(Arguments) :-
955 prob_junit_dir(Dir)
956 -> set_junit_dir(Dir),
957 create_and_print_junit_result(['Integration Tests'],Arguments,0,skip)
958 ; true.
959
960 % if the test expects a time_out error, the timeout is not expanded
961 % otherwise, timeout is increased to allow coverage analysis / junit / etc to finish
962 :- use_module(probsrc(tools_meta), [no_time_out_value/1]).
963 multiply_and_truncate_timeout(OrigTimeout, Factor, NewTimeout) :-
964 no_time_out_value(NoTimeout),
965 % Make sure that the new timeout never exceeds the special "no timeout" value,
966 % otherwise ProB prints lots of warnings about the timeout being too high.
967 (Factor = disable_time_out -> NewTimeout = NoTimeout
968 ; NewTimeout is min(round(Factor * OrigTimeout), NoTimeout)). % Factor could be float
969
970 modify_timeout(_,OldOptions,New) :-
971 append(_,['-expcterr','time_out'|_],OldOptions), !, New=OldOptions. % we expect a time_out
972 modify_timeout(Factor,[],['-p','TIME_OUT',NVal]) :-
973 % timeout was not set at all - set it to Factor*Default
974 % Note: there is a potential problem when the time_out is set inside the machine and not in the test !! TO DO: fix
975 preferences:preference_default_value(time_out,DEFAULT),
976 multiply_and_truncate_timeout(DEFAULT, Factor, NVal).
977 modify_timeout(Factor,[GTC,OLD|T],[GTC,NewT|MT]) :- is_global_time_out_cmd(GTC),!,
978 tools:arg_is_number(OLD,OLDT),
979 multiply_and_truncate_timeout(OLDT,Factor, NewT),
980 modify_timeout(Factor,T,MT).
981 modify_timeout(Factor,['-p','TIME_OUT',OLD|T],['-p','TIME_OUT',NewT|T]) :-
982 tools:arg_is_number(OLD,OLDT), !,
983 multiply_and_truncate_timeout(OLDT, Factor, NewT). % TODO: we currently assume global_time_out appears before
984 ?modify_timeout(Factor,[H|T],[H|MT]) :- modify_timeout(Factor,T,MT).
985
986 is_global_time_out_cmd('-global_time_out').
987 is_global_time_out_cmd('--global_time_out').
988 is_global_time_out_cmd('-time_out'). % old version
989
990 full_flush :- flush_output(user_output), flush_output(user_error).
991
992 print_args(List) :- print_args(List,15,15).
993 print_args([],_,_).
994 print_args([H|T],Nr,MaxPerLine) :- print(H),
995 (Nr<1 -> write(' \\'),nl, N1=MaxPerLine ; N1 is Nr-1),
996 write(' '), print_args(T,N1,MaxPerLine).
997
998 print_test_failed(Id,Msg) :-
999 print_test_failed(Id,Msg,'').
1000 print_test_failed(Id,Msg1,Msg2) :-
1001 (Msg1 = user_interrupt_signal -> assertz(user_interrupt_signal_received) ; true),
1002 test_took(RunTime,WallTime),
1003 cli_testcase(Id,Categories,_Infos,Arguments,Comment),
1004 Categories = [FirstCat|_], !,
1005 ErrorMessage = ['Test with Id',Id,'failed.','Test Categories: ',Categories,'Test Arguments: ',Arguments,'Test Comment: ',Comment,'Error Messages in Test Runner: ',Msg1,Msg2,'Content of Error Manager: '],
1006 (get_all_errors(AllErrors) -> true ; AllErrors = []),
1007 append(ErrorMessage,AllErrors,FullErrorMessage),
1008 create_and_print_junit_result(['Integration Tests',FirstCat],Id,RunTime,error(FullErrorMessage)),
1009 assert_test_failed(Id),
1010 start_terminal_colour(red,user_error),
1011 format(user_error,'*** Test ~w FAILED (after ~w ms runtime, ~w ms walltime): ~w~w~n',
1012 [Id,RunTime,WallTime,Msg1,Msg2]),
1013 write('***'),print_memory_used_wo_gc,nl,
1014 reset_terminal_colour(user_error).
1015
1016 assert_test_failed(Id) :-
1017 assertz(test_failed(Id)),
1018 retractall(last_test_failed(_)),
1019 assertz(last_test_failed(Id)).
1020
1021 diff_failed(Id,F1,F2,DiffOutputText,CmpOutputText) :-
1022 test_took(Time,_),
1023 atom_codes(DiffOutputAtom,DiffOutputText),
1024 atom_codes(CmpOutputAtom,CmpOutputText),
1025 ErrMsg = ['Diff for test with Id\n',Id,'\nfailed:\n','Output file\n',F1,'\ndoes not correspond to stored version\n',F2,
1026 '\nOutput of Diff:\n',DiffOutputAtom,
1027 '\nOutput of Cmp:\n',CmpOutputAtom],
1028 create_and_print_junit_result(['Diff Checking'],Id,Time,error(ErrMsg)),
1029 assert_test_failed(Id),
1030 assertz(test_diff_failed(Id,F1,F2)),
1031 start_terminal_colour(red,user_error),
1032 format(user_error,'*** Test ~w FAILED: Diff failed:~nOutput file ~w~ndoes not correspond to stored version~n~w~n', [Id,F1,F2]),
1033 format(user_error,'Diff:~n~s~n',[DiffOutputText]),
1034 format(user_error,'Cmp:~n~s~n',[CmpOutputText]),
1035 reset_terminal_colour(user_error).
1036
1037 :- use_module(probsrc(tools_commands),[diff_files_with_editor/2]).
1038 diff_in_editor :- findall(I,test_diff_failed(I,_,_),LI), sort(LI,SI),
1039 format('Opening failed diff files in editor: ~w~n',[SI]),
1040 test_diff_failed(Id,F1,F2),
1041 format('Test ~w~n~w ~w~n',[Id,F1,F2]),
1042 diff_files_with_editor(F1,F2),
1043 fail.
1044 diff_in_editor.
1045
1046
1047 clear_diff_output(Id) :- % clear all files that should be generated
1048 get_testcase_diff_check_output(Id,GeneratedFile,_StoredReferenceFile),
1049 safe_file_exists(GeneratedFile),
1050 (get_testcase_do_not_delete(Id,GeneratedFile) -> formatsilent(user_output,'% Keeping: ~w~n',[GeneratedFile])
1051 ; formatsilent(user_output,'% Deleting: ~w~n',[GeneratedFile]),
1052 delete_file(GeneratedFile)
1053 ),fail.
1054 clear_diff_output(_).
1055
1056 check_diff_output(Id) :-
1057 findall(diff(Id,GeneratedFile,StoredReferenceFile),
1058 get_testcase_diff_check_output(Id,GeneratedFile,StoredReferenceFile),
1059 ListOfDiffsToCheck),
1060 maplist(check_diff_output2, ListOfDiffsToCheck).
1061
1062 check_diff_output2(diff(Id,GeneratedFile,StoredReferenceFile)) :-
1063 \+ safe_file_exists(GeneratedFile) -> print_test_failed(Id,'Output file does not exist:',GeneratedFile) ;
1064 \+ safe_file_exists(StoredReferenceFile) -> print_test_failed(Id,'Stored file does not exist:',StoredReferenceFile) ;
1065 diff(Id,GeneratedFile,StoredReferenceFile).
1066
1067 diff(Id,F1,F2) :-
1068 formatsilent(user_output,'% Checking: diff / cmp ~w ~w~n',[F1,F2]),
1069 get_command_path(diff,DiffPath),
1070 get_command_path(cmp,CmpPath),
1071 (system_call(DiffPath,['-b',F1,F2],DiffOutputText,_ErrTextDiff,ExitDiff) % use -q for quiet
1072 -> true
1073 ; DiffOutputText = "*** CALLING DIFF FAILED !", ExitDiff = fail
1074 ),
1075 formatsilent(user_output,'% Checking: cmp ~w ~w~n',[F1,F2]),
1076 (system_call(CmpPath,['-b',F1,F2],CmpOutputText,_ErrTextCmp,_ExitCmp)
1077 -> true
1078 ; CmpOutputText = "*** CALLING CMP FAILED !"
1079 ),
1080 (ExitDiff = exit(0)%, ExitCmp = exit(0)
1081 -> true
1082 ; diff_failed(Id,F1,F2,DiffOutputText,CmpOutputText)).
1083
1084
1085 % ----------------
1086 :- volatile logxml_file/1.
1087 % logxml file used automatically when started by test_runner
1088 :- dynamic logxml_file/1.
1089 clear_logxml_output(_Id) :- logxml_file(GeneratedFile),
1090 safe_file_exists(GeneratedFile),
1091 delete_file(GeneratedFile),
1092 fail.
1093 clear_logxml_output(_).
1094
1095 set_logxml_file(File) :- retractall(logxml_file(_)), assertz(logxml_file(File)),
1096 format('Adding -logxml ~w.~n',[File]),
1097 add_additional_arguments(['-logxml',File]).
1098
1099 :- use_module(probsrc(logger),[read_xml_log_file/2]).
1100 check_logxml(Id) :-
1101 logxml_file(File),!,
1102 xmllint(Id,File),
1103 check_logxml(Id,File).
1104 check_logxml(_).
1105
1106 xmllint(Id,File) :-
1107 (call_xmllint(File,ErrText,ExitLint)
1108 -> (ExitLint = exit(0) -> true
1109 ; print_test_failed(Id,'xmllint reported errors for file:',File),
1110 formatsilent(user_error,'% xmllint result~n~s~n',[ErrText])
1111 )
1112 ; print_test_failed(Id,'Calling xmllint failed for file:',File)
1113 ).
1114
1115 :- dynamic use_logxml_schema/0.
1116 set_use_logxml_schema :- (use_logxml_schema -> true ; assertz(use_logxml_schema)).
1117 call_xmllint(File,ErrText,ExitLint) :- use_logxml_schema,
1118 absolute_file_name(prob_home('doc/logxml_xsd.xml'),SchemaFile),
1119 file_exists(SchemaFile),!,
1120 get_command_path(xmllint,LintPath),
1121 format('Calling ~w with schema ~w for logxml file ~w~n',[LintPath,SchemaFile,File]),
1122 system_call(LintPath,['--schema', SchemaFile, File, '-noout'],_OutputText,ErrText,ExitLint).
1123 call_xmllint(File,ErrText,ExitLint) :-
1124 get_command_path(xmllint,LintPath),
1125 format('Calling ~w without schema for logxml file ~w~n',[LintPath,File]),
1126 system_call(LintPath,[File],_OutputText,ErrText,ExitLint).
1127
1128 check_logxml(Id,File) :-
1129 catch(read_xml_log_file(File,Infos), E,
1130 print_test_failed(Id,'Exception trying to read logxml file:',E)),
1131 !,
1132 format_progress_nl('Read logxml file ~w, contains ~w.',[File,Infos]),
1133 check_error_cound(Id,Infos).
1134 check_logxml(Id,File) :-
1135 print_test_failed(Id,'Logxml file could not be read:',File).
1136
1137 check_error_cound(Id,Infos) :-
1138 testcase_expects_errors(Id),!,
1139 (member(expected_errors/E,Infos),E>0 -> true ; print_test_failed(Id,'Logxml file does not report errors:',Infos)).
1140 check_error_cound(Id,Infos) :-
1141 (member(errors/0,Infos),member(expected_errors/0,Infos) -> true
1142 ; print_test_failed(Id,'Logxml file reports errors:',Infos)).
1143
1144 testcase_expects_errors(Id) :-
1145 cli_testcase(Id,_Cat,_Infos,Arguments,_Comment),
1146 member(A,Arguments), expect_error_arg(A).
1147
1148 expect_error_arg('-expcterr').
1149 expect_error_arg('-expecterr').
1150 expect_error_arg('-expect').
1151 expect_error_arg('-expcterrpos').
1152
1153 % ------------------
1154
1155 :- volatile repl_mode/0.
1156 :- dynamic repl_mode/0.
1157
1158 :- use_module(test_paths, [get_prob_examples_override/1, set_prob_examples_override/1]).
1159
1160 set_prob_examples_location(Dir) :-
1161 (atom(Dir) -> DirAtom = Dir ; atom_codes(DirAtom, Dir)),
1162 format('Setting location of prob_examples directory to: ~s~n',[DirAtom]),
1163 set_prob_examples_override(DirAtom).
1164
1165 % Prefixes of arguments that contain file paths,
1166 % which may need to be patched to an alternate prob_examples location.
1167 path_arg_prefix("#file ").
1168 path_arg_prefix(":cdclt #file ").
1169 path_arg_prefix(":cdclt-file ").
1170 path_arg_prefix(":cdclt-free #file ").
1171 path_arg_prefix(":cdclt-double-check #file ").
1172 path_arg_prefix(":cdclt-free-double-check #file ").
1173 path_arg_prefix(":prob-file ").
1174 path_arg_prefix(":z3 #file ").
1175 path_arg_prefix(":z3-double-check #file ").
1176 path_arg_prefix(":z3-free-double-check #file ").
1177 path_arg_prefix(":z3-file ").
1178 path_arg_prefix(":z3-free #file ").
1179 path_arg_prefix(":z3-free-file ").
1180 path_arg_prefix("").
1181
1182 % update path to prob_examples if necessary:
1183 patch_prob_examples_loc_0(NewLoc,Arg,PatchedArg) :-
1184 path_arg_prefix(Prefix),
1185 append(Prefix, ArgTail, Arg),
1186 append("../prob_examples", PathTail, ArgTail),
1187 !,
1188 append(NewLoc, PathTail, PatchedArgTail),
1189 append(Prefix, PatchedArgTail, PatchedArg).
1190
1191 patch_prob_examples_loc(Arg,PatchedArg) :-
1192 get_prob_examples_override(NewLocAtom),
1193 atom_codes(NewLocAtom, NewLoc),
1194 atom(Arg),
1195 atom_codes(Arg,ArgC),
1196 patch_prob_examples_loc_0(NewLoc,ArgC,PatchedArgC),
1197 !,
1198 atom_codes(PatchedArg,PatchedArgC),
1199 format('Patched ~w to ~w~n',[Arg,PatchedArg]).
1200 patch_prob_examples_loc(A,A).
1201
1202 get_testcase_do_not_delete(Id,PF) :-
1203 cli_testcase_do_not_delete(Id,F),
1204 patch_prob_examples_loc(F,PF).
1205
1206 get_testcase_diff_check_output(Id,PF1,PF2) :-
1207 cli_testcase_diff_check_output(Id,F1,F2),
1208 patch_prob_examples_loc(F1,PF1),
1209 patch_prob_examples_loc(F2,PF2).
1210
1211 :- use_module(extension('counter/counter'),[counter_init/0]).
1212 :- use_module(probsrc(prob_startup), [startup_prob/0]).
1213 init_test_runner :- startup_prob,counter_init.
1214 :- use_module(library(lists),[maplist/2]).
1215 % a minimal shell to execute tests:
1216 test_repl :-
1217 init_test_runner,
1218 format_prob_version(user_output),nl,
1219 format_prolog_version(user_output),nl,
1220 assertz(repl_mode),
1221 current_prolog_flag(argv,ArgV), treat_argv(ArgV),
1222 test_repl_loop,
1223 retractall(repl_mode).
1224
1225
1226
1227 treat_argv(['-prob-examples',Dir|T]) :- !, set_prob_examples_location(Dir),
1228 treat_argv(T).
1229 treat_argv(Args) :- maplist(eval_argv,Args).
1230
1231 % execute tests provided on the command-line:
1232 eval_argv(Cmd) :- format('ARGV ==> ~w~n',[Cmd]),
1233 atom_codes(Cmd,C), safe_number_codes(Nr,C), !, test_eval(Nr).
1234 eval_argv(Cmd) :- test_eval(Cmd),!.
1235
1236 test_repl_loop :- safe_read(T), test_eval(T), !, test_repl_loop.
1237 test_repl_loop.
1238
1239 safe_read(T) :-
1240 catch(
1241 (prompt(OldPrompt, 'TEST ==> '), call_cleanup(read(T), prompt(_, OldPrompt))),
1242 error(syntax_error(E),_),
1243 (format_warning_nl('*** Syntax error: ~w~n*** Type Prolog term followed by a dot(.) and enter.',[E]),
1244 safe_read(T))).
1245
1246 :- use_module(probsrc(translate),[print_open_stream_stats/0]).
1247 :- meta_predicate wall(0).
1248 wall(Call) :-
1249 statistics(walltime,[Start,_]),
1250 call(Call),
1251 statistics(walltime,[Stop,_]), WT is Stop-Start,
1252 format('Walltime: ~w ms~n',[WT]),
1253 print_memory_used_wo_gc,nl,
1254 (debug_mode(off) -> true ; print_open_stream_stats).
1255
1256 % -------------------------
1257
1258 :- use_module(library(file_systems)).
1259 :- use_module(probsrc(tools),[get_options/5]).
1260 % true for test_files
1261 test_file(Id,File,AbsFileName) :- cli_testcase(Id,_Cat,_Infos,Arguments,_Comment),
1262 get_options(Arguments,prob_cli:recognised_cli_option,_Options,Files,fail),
1263 member(File,Files),
1264 is_existing_file(File),
1265 absolute_file_name(File,AbsFileName).
1266
1267 is_existing_file(X) :- \+ number(X), atom(X),
1268 atom_codes(X,Codes),[BS] = "/", (member(BS,Codes) -> true),
1269 file_exists(X).
1270
1271 % obtain a list of all files used in tests
1272 all_files(Files) :- findall(F,test_file(_,_,F),A), sort(A,Files).
1273
1274 % a test file that can be loaded:
1275 valid_test_file(Id,File,AbsFileName,XT) :-
1276 test_file(Id,File,AbsFileName),
1277 cli_testcase(Id,_Cat,_Infos,Arguments,_Comment),
1278 \+ append(_,['-expcterr', load_main_file |_],Arguments),
1279 tools:get_filename_extension(File,XT).
1280 all_valid_files(Files,Mode) :-
1281 findall(F, (valid_test_file(_,_,F,XT),
1282 relevant_extension(XT,Mode)),
1283 A),
1284 sort(A,Files).
1285
1286 % traverse a directory and indicate which specification files are used in tests and which ones not
1287 traverse :- traverse('../prob_examples/public_examples/').
1288 traverse(SD) :- all_files(Files), absolute_file_name(SD,StartDir),
1289 format('Examining files in ~w~n + means file is used in some test~n~n',[StartDir]),
1290 traverse(StartDir,Files).
1291
1292 traverse(Dir,AllFiles) :- file_member_of_directory(Dir,_,FullFile),
1293 tools:get_filename_extension(FullFile,XT),
1294 (member(FullFile,AllFiles) -> format(' + ~w~n',[FullFile])
1295 ; relevant_extension(XT,_) -> format('--- ~w~n',[FullFile])),
1296 fail.
1297 traverse(Dir,AllFiles) :- directory_member_of_directory(Dir,_,SubDir),
1298 %format('~nSTART ~w~n',[SubDir]),
1299 traverse(SubDir,AllFiles),
1300 %format('~n END ~w~n',[SubDir]),
1301 fail.
1302 traverse(_,_).
1303
1304 relevant_extension('mch',b).
1305 relevant_extension('ref',b).
1306 relevant_extension('imp',b).
1307 relevant_extension('tla',tla).
1308 relevant_extension('fuzz',z).
1309 relevant_extension('tex',z).
1310 relevant_extension('csp',csp).
1311 relevant_extension('cspm',csp).
1312 relevant_extension('eventb',eventb).
1313
1314 % --------------------------
1315
1316 test_eval(quit) :- !,fail.
1317 test_eval(q) :- !,fail.
1318 test_eval(end_of_file) :- !,fail. % Ctrl-D
1319 test_eval(Cmd) :- test_eval1(Cmd),!.
1320 test_eval(Cmd) :-
1321 format_warning_nl('Error executing command: ~w',[Cmd]).
1322
1323 test_eval1(N) :- number(N),!, wall(run_id(N)).
1324 test_eval1(last) :- !, wall(run_last_test).
1325 test_eval1(N-M) :- number(N), number(M),!, wall(run_tests_by_id(N-M)).
1326 test_eval1('..'(N,M)) :- !, test_eval1(N-M).
1327 test_eval1(repeat(ID,M)) :- !, repeat_id(ID,M).
1328 test_eval1(r) :- !, run_random_tests(25).
1329 test_eval1(v) :- !,v.
1330 test_eval1(verbose) :- !,v.
1331 test_eval1(all_files) :- !, all_files(Files), length(Files,Len),
1332 format('~nFiles = ~n~w~n # Files = ~w~n',[Files,Len]).
1333 test_eval1(valid_files(Mode)) :- !, all_valid_files(Files,Mode), length(Files,Len),
1334 format('~nValid ~w Files = ~n~w~n # Files = ~w~n',[Mode,Files,Len]).
1335 test_eval1(files) :- !, traverse.
1336 test_eval1(files(Dir)) :- !, traverse(Dir).
1337 test_eval1(ex(Dir)) :- !, set_prob_examples_location(Dir).
1338 test_eval1(cache) :- !,print('Enabling cache'),nl,
1339 cache.
1340 test_eval1(debug) :- !,print('Enabling Prolog debugging mode (use -v or -vv for ProB debugging info)'),nl,
1341 debug,
1342 retractall(multiply_timeout(_)),
1343 assertz(multiply_timeout(10)).
1344 test_eval1(factor(X)) :- !,
1345 retractall(multiply_timeout(_)),
1346 format('Setting timeout factor to ~w~n',[X]),
1347 assertz(multiply_timeout(X)).
1348 test_eval1(disable_time_out) :- !, test_eval1(factor(disable_time_out)).
1349 test_eval1(timeout(X)) :- !,
1350 format('Adding -timeout ~w for model checking, disproving~nUse factor(X) to set TIME_OUT factor.~n',[X]),
1351 add_additional_arguments(['-timeout',X]).
1352 test_eval1(coverage) :- !,
1353 format('Adding -coverage to all commands.~n',[]),
1354 add_additional_arguments(['-coverage']).
1355 test_eval1(opreuse) :- !,
1356 format('Adding operation reuse to all commands.~n',[]),
1357 add_additional_arguments(['-p', 'OPERATION_REUSE',true]).
1358 test_eval1(opc) :- !,
1359 format('Adding operation reuse and compression to all commands.~n',[]),
1360 add_additional_arguments(['-p', 'COMPRESSION', 'TRUE', '-p', 'OPERATION_REUSE',true]).
1361 test_eval1(opcf) :- !,
1362 format('Adding operation reuse and compression to all commands.~n',[]),
1363 add_additional_arguments(['-p', 'COMPRESSION', 'TRUE', '-p', 'OPERATION_REUSE',full]).
1364 test_eval1(gc_trace) :- !,
1365 format('Adding -gc_trace to all commands.~n',[]),
1366 add_additional_arguments(['-gc_trace']).
1367 test_eval1(reset) :- !, reset_additional_arguments.
1368 test_eval1(logxml) :- !, File = './test_runner_logxml.xml',
1369 set_logxml_file(File).
1370 test_eval1(xsd) :- !, File = './test_runner_logxml.xml',
1371 set_logxml_file(File), set_use_logxml_schema.
1372 test_eval1(debug_off) :- !,print('Disabling Prolog debugging mode'),nl,
1373 nodebug,
1374 retractall(multiply_timeout(_)).
1375 test_eval1(fast) :- !,print('Enabling jvm_parser_fastrw'),nl,
1376 % TODO: only works at the very beginning; we need to be able to switch parser?
1377 add_additional_preference('jvm_parser_fastrw','true'),
1378 add_additional_preference('jvm_parser_force_parsing','true').
1379 test_eval1(force) :- !,print('Setting jvm_parser_force_parsing'),nl,
1380 add_additional_preference('jvm_parser_force_parsing','true').
1381 test_eval1(clpfd) :- !,print('Enabling CLPFD'),nl,
1382 add_additional_preference('CLPFD','TRUE').
1383 test_eval1(clpfd_off) :- !,print('Disabling CLPFD'),nl,
1384 add_additional_preference('CLPFD','FALSE').
1385 test_eval1(smt) :- !,print('Enabling SMT'),nl,
1386 add_additional_preference('SMT','TRUE').
1387 test_eval1(smt_off) :- !,print('Disabling SMT'),nl,
1388 add_additional_preference('SMT','FALSE').
1389 test_eval1(chr) :- !,print('Enabling CHR'),nl,
1390 add_additional_preference('CHR','TRUE').
1391 test_eval1(chr_off) :- !,print('Disabling CHR'),nl,
1392 add_additional_preference('CHR','FALSE').
1393 test_eval1(cse_off) :- !,print('Disabling CSE'),nl,
1394 add_additional_preference('CSE','FALSE').
1395 test_eval1(cse) :- !,print('Enabling CSE'),nl,
1396 add_additional_preference('CSE','TRUE').
1397 test_eval1(cse_subst) :- !,print('Enabling CSE_SUBST'),nl,
1398 add_additional_preference('CSE','TRUE'),
1399 add_additional_preference('CSE_SUBST','TRUE').
1400 test_eval1(trace_info) :- !,print('Enabling TRACE_INFO'),nl,
1401 add_additional_preference('TRACE_INFO','TRUE').
1402 %
1403 test_eval1(p(PREF)) :- !,
1404 get_preference_name(PREF,PName),
1405 print('Enabling Preference '),print(PName),nl,
1406 add_additional_preference(PName,'true').
1407 test_eval1(p(PREF,VAL)) :- !,
1408 get_preference_name(PREF,PName),
1409 print('Setting Preference '),print(PName),nl,
1410 add_additional_preference(PName,VAL).
1411 test_eval1(random) :- !,print('Enabling RANDOMISE_ENUMERATION_ORDER'),nl,
1412 add_additional_preference('RANDOMISE_ENUMERATION_ORDER','TRUE').
1413 test_eval1(random_off) :- !,print('Enabling RANDOMISE_ENUMERATION_ORDER'),nl,
1414 add_additional_preference('RANDOMISE_ENUMERATION_ORDER','FALSE').
1415 test_eval1(safe) :- !,print('Setting PROB_SAFE_MODE'),nl,
1416 run_safely.
1417 test_eval1(sanity_check) :- !, sanity_check(false).
1418 test_eval1(sc) :- !, sanity_check(false).
1419 test_eval1(trace) :- !, print('Enabling TRACE_UPON_ERROR'),nl,
1420 add_additional_preference('TRACE_UPON_ERROR','TRUE').
1421 test_eval1(trace_off) :- !, print('Disabling TRACE_UPON_ERROR'),nl,
1422 add_additional_preference('TRACE_UPON_ERROR','FALSE').
1423 test_eval1(raise) :- !,print('Enabling STRICT_RAISE_ENUM_WARNINGS'),nl,
1424 add_additional_preference('STRICT_RAISE_ENUM_WARNINGS','TRUE').
1425 test_eval1(nopt) :- !,print('Disabling OPTIMIZE_AST'),nl,
1426 add_additional_preference('OPTIMIZE_AST','FALSE').
1427 test_eval1(vv) :- !,vv.
1428 test_eval1(version(X)) :- !, print_version(X). %cpp, java, full, lib
1429 test_eval1(version) :- !, print_version(full_verbose).
1430 test_eval1(silent) :- !, run_silently.
1431 test_eval1(nostrict) :- !, run_no_strict.
1432 test_eval1(no_strict) :- !, run_no_strict.
1433 test_eval1(strict) :- !, retractall(no_strict_running).
1434 test_eval1(skip) :- !, (retract(skip_all_tests) -> X=false ; assertz(skip_all_tests),X=true),
1435 format('Marking all tests as skipped: ~w~n',[X]).
1436 test_eval1(x) :- !,halt.
1437 test_eval1(reload) :- !,use_module(probsrc(test_runner)), use_module(probsrc(testcases)).
1438 :- if(predicate_property(make, _)).
1439 test_eval1(make) :- !, make.
1440 :- else.
1441 test_eval1(make) :- !,
1442 print('make/0 is only supported on SWI-Prolog - reloading just the testcases instead.'),nl,
1443 test_eval1(reload).
1444 :- endif.
1445 test_eval1(edit) :- last_testcase_run(Id),
1446 get_file_for_testcase(Id,File),!,
1447 edit_file(File).
1448 test_eval1(e) :- !, test_eval1(edit).
1449 test_eval1(diff) :- !, diff_in_editor.
1450 test_eval1(halt) :- !,halt.
1451 test_eval1(info) :- !, get_total_number_of_errors(X), format('~nTotal number of errors: ~w~n~n',[X]).
1452 test_eval1(cat) :- !, print('Categories: '),
1453 findall(Cat,
1454 (cli_testcase(_, TestCategories, _, _, _), member(Cat, TestCategories)), List),
1455 sort(List,SL), print(SL),nl,
1456 format('Type cat(Cat) or name of category to run it.~n',[]),
1457 format('Note: priv is an alias for private_source_not_available.~n',[]).
1458 test_eval1(all) :- !, wall(run_tests_all).
1459 test_eval1(cata) :- !, category_analysis.
1460 test_eval1(cat(Category)) :- !,
1461 wall(run_tests_by_category(Category,all)).
1462 test_eval1(priv) :- !, test_eval1(cat(private_source_not_available)).
1463 test_eval1(first(Category)) :- !,
1464 wall(run_tests_by_first_category(Category)).
1465 test_eval1(list(Category)) :- !,
1466 get_tests_for_categories([Category],TList),
1467 findall(Id,member(testcase(Id, _, _, _, _),TList),List),
1468 format('Tests for category: ~w~n',[List]).
1469 test_eval1(make(Categories)) :- !,
1470 wall(makefile_by_category(user_output,Categories)).
1471 test_eval1(make(File,Categories)) :- !,
1472 wall(makefile_by_category(File,Categories)).
1473 test_eval1(files(Category)) :- !, show_files(Category).
1474 test_eval1(uses(Command)) :- !, wall(run_tests_using_command(Command)).
1475 test_eval1(uses(Pref,Val)) :- !, wall(run_tests_using_preference(Pref,Val)).
1476 test_eval1(p) :- !, test_eval1(profile).
1477 test_eval1(ps) :- !, test_eval1(print_profile).
1478 test_eval1(pc) :- !, test_eval1(print_coverage).
1479 test_eval1(delta) :- !, print_delta_stats.
1480 test_eval1(stats) :- !, print_current_stats.
1481 test_eval1(statscsv) :- !, print_current_stats(user_output,',').
1482 test_eval1(start) :- !, performance_session_start.
1483 test_eval1(stop) :- !, performance_session_end('log/test_runner_performance_log.pl').
1484 test_eval1(codespeed) :- !,
1485 performance_session_start,
1486 test_eval1(cat(codespeed)),
1487 performance_session_end('log/test_runner_performance_codespeed_log.pl').
1488 test_eval1(prob_profile) :- !,
1489 cli_print_statistics(prob_profile),
1490 cli_print_statistics(disprover_profile).
1491 test_eval1(profile) :- !, print('PROFILING : '), %spy([avl:avl_size/2]),
1492 (current_prolog_flag(profiling,on)
1493 -> set_prolog_flag(profiling,off), print('OFF') ;
1494 set_prolog_flag(profiling,on), print('ON')),
1495 nl,print('USE ps to print_profile or pc to print_coverage info'),nl.
1496 test_eval1(profile_stats) :- !, test_eval1(print_profile).
1497 test_eval1(print_profile) :- !, nl,print('PROFILE INFORMATION:'), nl,
1498 catch(print_profile,
1499 error(existence_error(_,_),_),
1500 print('CAN ONLY BE USED WHEN RUNNING PROB FROM SOURCE')),
1501 nl, debug:timer_statistics.
1502 test_eval1(print_coverage) :- !, nl,print('COVERAGE INFORMATION:'), nl,
1503 (current_prolog_flag(source_info,on) -> true ; format_warning_nl('Only useful when current_prolog_flag(source_info,on)!',[])),
1504 catch(print_coverage,
1505 error(existence_error(_,_),_),
1506 print('CAN ONLY BE USED WHEN RUNNING PROB FROM SOURCE')),
1507 nl.
1508 test_eval1(profile_reset) :- !, nl,print('RESETTING PROFILE INFORMATION'), nl,
1509 catch(profile_reset,
1510 error(existence_error(_,_),_),
1511 print('CAN ONLY BE USED WHEN RUNNING PROB FROM SOURCE')),
1512 nl.
1513 test_eval1(perf) :- !, toggle_perfmessages.
1514 test_eval1(Category) :- valid_category(Category),!,
1515 wall(run_tests_by_category(Category,all)).
1516
1517 test_eval1(codespeed(Id)) :- !, codespeed_id(Id).
1518 test_eval1(module(Module)) :- !,
1519 prob_cli:run_probcli(['-selfcheck_module',Module],[test_runner]), safe_reset_cli(Module), full_flush.
1520 test_eval1(cs(Id)) :- !, codespeed_id(Id).
1521 test_eval1('-'(ProBCLICommand)) :- atom(ProBCLICommand),
1522 atom_concat('-',ProBCLICommand,Atom),
1523 (prob_cli:recognised_option(Atom,Call) -> true % from probcli.pl
1524 ; format('Unknown probcli command: -~w~n',[ProBCLICommand]),fail),
1525 !,
1526 format('Executing probcli command ~w~n',[Call]),
1527 call(prob_cli:Call).
1528 test_eval1([H|T]) :- number(H),!, wall(run_tests_by_id([H|T],_,no_skipping)).
1529 test_eval1([Category|T]) :- valid_category(Category),!,
1530 wall(run_tests_by_category([Category|T],all)).
1531 test_eval1(C) :-
1532 (C=help -> true ; format('*** Unknown command ~w~n',[C])),
1533 print(' Commands: Nr, Nr-Nr, last, q, x, v, vv, uses(Cmd), uses(Pref,Val),'),nl,
1534 print(' repeat(id,nr), timeout(ms),factor(f), e,edit,diff, r (for 25 random tests),'),nl,
1535 print(' cat, cat(Cats),make(Cats),make(File,Cats),files(Cat),'),nl,
1536 print(' profile, profile_stats, (to turn Prolog profiling on and print info)'),nl,
1537 print(' debug,debug_off, (to turn Prolog debug mode on or off)'),nl,
1538 print(' perf, reload, sc,'),nl,
1539 print(' -CMD, (for probcli commands like -profile)'),nl,
1540 print(' * for setting preferences:'),nl,
1541 print(' p(PREF), p(PREF,VAL),'),nl,
1542 print(' clpfd,clpfd_off, smt,smt_off, chr,chr_off, cse,cse_subst,cse_off,'),nl,
1543 print(' random,random_off, trace_info, nopt,'),nl,
1544 print(' cache, perf, (turn ProB caching or performance messages on)'),nl,
1545 print(' trace,trace_off, (set TRACE_UPON_ERROR preference)'),nl,
1546 print(' * statistics:'),nl,
1547 print(' delta, stats, info.'),nl.
1548
1549 safe_file_exists(F) :- atom(F), file_exists(F).
1550
1551 get_file_for_testcase(Id,File) :-
1552 cli_testcase(Id,_,_Infos,Arguments,_Comment),
1553 member(File,Arguments), safe_file_exists(File).
1554
1555 valid_category(Cat) :-
1556 cli_testcase(_Id, TestCategories, _Infos, _Arguments, _Comment),
1557 member(Cat, TestCategories).
1558
1559
1560 % ---------------------------
1561
1562 % run test using binary
1563 % binaries are registered in a file probcli_bak/codespeed_versions.pl
1564 % results for test ID are written to probcli_bak/codespeed/res_ID.csv
1565
1566 :- use_module(probsrc(tools), [ajoin/2,ajoin_with_sep/3]).
1567 codespeed_id(Ids) :- codespeed_id(Ids,[min_date(date(2020,1,1))]).
1568 codespeed_id([],_) :- !.
1569 codespeed_id([H|T],Options) :- !, codespeed_id(H,Options),!, codespeed_id(T,Options).
1570 codespeed_id(Cat,Options) :- atom(Cat),!,
1571 get_tests_for_categories([Cat],List),
1572 codespeed_id(List,Options).
1573 codespeed_id(testcase(Id, _, _, _, _),Options) :- !, codespeed_id(Id,Options).
1574 codespeed_id(Id,Options) :-
1575 use_module('probcli_bak/codespeed_versions.pl'), % contains probcli_binary
1576 cli_testcase(Id,_TestCategories,_TestInfos,Arguments,Comment),
1577 format('Benchmarking test ~w using probcli binaries ~w~nTest Description: ~w~n',[Id,Options,Comment]),
1578 maplist(convert_arg_to_atom,Arguments,Args2),
1579 findall(probcli_binary(V1,V2,V3,F,Path,Sics,Date),
1580 get_probcli_binary_info(Options,V1,V2,V3,F,Path,_Hash,Sics,Date),Binaries),
1581 Repeats=3,
1582 maplist(bench_probcli_binary_testcase(Id,Args2,Repeats),Binaries,Walltimes,Oks),
1583
1584 ajoin(['codespeed/res_',Id,'.csv'],FName),
1585 absolute_file_name(FName,AF,[relative_to(probcli_bak)]),
1586 format('Writing codespeed results for ~w to ~w~n',[Id,AF]),
1587 open(AF,write,Stream),
1588 format(Stream,'\"Test ~w\"~n',[Id]),
1589 format(Stream,'\"~w\"~n',[Comment]),
1590 ajoin_with_sep(Arguments,' ',ArgsStr),
1591 format(Stream,'Command:,\"probcli ~w\"~n~n',[ArgsStr]),
1592 format(Stream,'\"~w.~w.~w-~w\",~w,~w,~w,~w,~w,~w,\"~w\"~n',[v1,v2,v3,f,sics,date,avg,min,max,ok,walltimes]),
1593 maplist(print_codespeed_results(Stream,Repeats),Binaries,Walltimes,Oks),
1594 close(Stream),
1595 maplist(print_codespeed_results(user_output,Repeats),Binaries,Walltimes,Oks).
1596
1597 get_probcli_binary_info(Options,V1,V2,V3,F,Path,Hash,Sics,Date) :-
1598 probcli_binary(V1,V2,V3,F,Path,Hash,Sics,Date), % from codespeed_versions.pl
1599 (( member(min_date(D2),Options), D2 @> Date
1600 ; member(max_date(D3),Options), D3 @< Date
1601 ; member(min_version(V12,V22,V32),Options), v(V12,V22,V32) @> v(V1,V2,V3)
1602 )
1603 -> format('Excluding version ~w.~w.~w-~w ~w~n',[V1,V2,V3,F,Options]),
1604 fail
1605 ; % format('Including version ~w.~w.~w-~w ~w~n',[V1,V2,V3,F,Options]),
1606 true).
1607
1608
1609 %:- use_module(library(statistics),[min_max/3]).
1610 print_codespeed_results(Stream,Repeats,probcli_binary(V1,V2,V3,F,_Path,sicstus(S1,S2,S3),date(Y,M,D)),Walltimes,Ok) :-
1611 sumlist(Walltimes,Sum),
1612 Average is Sum / Repeats,
1613 min_member(Min,Walltimes),
1614 max_member(Max,Walltimes),
1615 ajoin([S1,'.',S2,'.',S3],Sics),
1616 ajoin([Y,'/',M,'/',D],Date),
1617 format(Stream,'\"~w.~w.~w-~w\",~w,~w,~w,~w,~w,~w,\"~w\"~n',[V1,V2,V3,F,Sics,Date,Average,Min,Max,Ok,Walltimes]).
1618
1619 %bench_probcli_binary_testcase(Id,_Args,_Repeats,probcli_binary(V1,V2,V3,F,Path,_,_),Walltimes,Ok) :-
1620 % format('~nDry run test ~w using version ~w.~w.~w-~w (~w)~n',[Id,V1,V2,V3,F,Path]),!, Ok=skipped, Walltimes=[0].
1621 bench_probcli_binary_testcase(Id,Args,Repeats,probcli_binary(V1,V2,V3,F,Path,_,_),Walltimes,Ok) :-
1622 format('~nRunning test ~w using version ~w.~w.~w-~w (~w)~n',[Id,V1,V2,V3,F,Path]),
1623 run_probcli_binary_testcase(Id,Path,Args,_WT,Ok), % run once for parser
1624 rep_bench(Repeats,Id,Path,Args,Walltimes).
1625
1626 rep_bench(0,_,_,_,[]).
1627 rep_bench(Nr,Id,Path,Args,[WT1|WTR]) :- Nr>0,
1628 run_probcli_binary_testcase(Id,Path,Args,WT1,_),
1629 N1 is Nr-1,
1630 rep_bench(N1,Id,Path,Args,WTR).
1631
1632 :- use_module(probsrc(system_call), [system_call/5]).
1633 run_probcli_binary_testcase(Id,Path,Arguments,WT,Ok) :-
1634 statistics(walltime,[Start,_]),
1635 % absolute_file_name('probcli_bak/',BakPath),
1636 % atom_concat(BakPath,Path,Cmd),
1637 absolute_file_name(Path,Cmd,[relative_to(probcli_bak)]),
1638 format(' Test ~w :: ~w ~w~n',[Id,Cmd,Arguments]),
1639 system_call(Cmd,Arguments,_OutputText,ErrText,Exit),
1640 statistics(walltime,[Stop,_]), WT is Stop-Start,
1641 format(' Walltime: ~w ms; ~w~n',[WT,Exit]),
1642 (Exit=exit(0), ErrText = [] -> Ok=true
1643 ; format_error_nl('STD-ERROR (~w):~n~s',[Exit,ErrText]), Ok=false
1644 ).
1645
1646 % convert for system_call/process_create which do not accept numbers:
1647 convert_arg_to_atom(Nr,Atom) :- number(Nr), number_codes(Nr,C),!,atom_codes(Atom,C).
1648 convert_arg_to_atom(A,A).
1649
1650
1651 % --------------------
1652
1653 category_analysis :-
1654 format('Analysing test categories:~n',[]),
1655 findall(Cat-Id,(cli_testcase(Id,Categories,_,_Args,_Comm1),member(Cat,Categories)),L),
1656 sort(L,SL),
1657 keyclumped(SL,Groups), % Groups = [ Cat1 - [TestNr1, ...], Cat2 - [...], ...]
1658 maplist(print_cat_group,Groups).
1659
1660 nr_of_files(Cat,Id,Nr,DistinctNr) :-
1661 findall(File,(cli_testcase(Id,Categories,_,Args,_Comm1),
1662 member(Cat,Categories),
1663 file_in_arguments(File,Args)),Files),
1664 length(Files,Nr),
1665 sort(Files,SFile),
1666 length(SFile,DistinctNr).
1667
1668 print_cat_group(Cat-Tests) :-
1669 Tests = [First|T],
1670 nr_of_files(Cat,_,NrFiles,Distinct),
1671 (T=[]
1672 -> format(' ~w : 1 test : [~w] : ~w files, ~w distinct~n',[Cat,First,NrFiles,Distinct])
1673 ; length(Tests,Len),
1674 last(T,Last),
1675 format(' ~w : ~w tests : [~w .. ~w] : ~w files, ~w distinct~n',[Cat,Len,First,Last,NrFiles,Distinct])
1676 ).
1677