| 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 | :- module(self_check, | |
| 6 | [assert_pre/2, assert_post/2,pp_mnf/1, pp_cll/1, mnf/1, mnf/2, | |
| 7 | mnf_det/1, det_call/1, force_det_call/1, | |
| 8 | assert_must_succeed/1,assert_must_fail/1, | |
| 9 | assert_must_succeed_multiple/1, assert_must_succeed_any/1, | |
| 10 | must_fail/1, must_succeed/1, must_succeed_without_residue/1, | |
| 11 | must_succeed_without_residue_and_time_out/1, | |
| 12 | ||
| 13 | run_time_type_check_mode/1, | |
| 14 | turn_on_run_time_type_checks/0,turn_off_run_time_type_checks/0, | |
| 15 | ||
| 16 | get_module_list/1, | |
| 17 | ||
| 18 | perform_self_check/0, perform_self_check/1, perform_self_check/2, | |
| 19 | perform_verbose_self_check/0, perform_verbose_self_check/1, | |
| 20 | portray_self_checks/0, | |
| 21 | ||
| 22 | disable_interaction_on_errors/0, | |
| 23 | ||
| 24 | % time_out_call/2, time_out_with_factor_call/3, time_out_call/1, % now in tools_timeout | |
| 25 | ||
| 26 | check_deterministic/1, check_det/1, | |
| 27 | ||
| 28 | list_skeleton/1, | |
| 29 | ||
| 30 | self_checks_exist/0, | |
| 31 | check_eq/2, | |
| 32 | check_eqeq/2 | |
| 33 | ||
| 34 | ]). | |
| 35 | ||
| 36 | :- meta_predicate nonvar_call(0). | |
| 37 | :- meta_predicate assert_post(0,0). | |
| 38 | :- meta_predicate(get_call_module(0, *)). | |
| 39 | :- meta_predicate assert_must_succeed_any(0). | |
| 40 | :- meta_predicate assert_must_succeed(0). | |
| 41 | :- meta_predicate assert_must_succeed_multiple(0). | |
| 42 | :- meta_predicate assert_must_fail(0). | |
| 43 | %:- meta_predicate kernel_waitflags:assert_must_abort_wf(0,*). | |
| 44 | :- meta_predicate pp_mnf(0), pp_cll(0), mnf(0), mnf(-,0), det_call(0). | |
| 45 | :- meta_predicate mnf_call(0), mnf_call_with_pp(-,0), prepost_mnf_call(0). | |
| 46 | :- meta_predicate prepost_call(0). | |
| 47 | :- meta_predicate check_exception_call(0). | |
| 48 | :- meta_predicate rt_timeout_call(0). | |
| 49 | :- meta_predicate mnf_det(0). | |
| 50 | :- meta_predicate force_det_call(0). | |
| 51 | :- meta_predicate residue_check_call(0). | |
| 52 | :- meta_predicate must_fail(0). | |
| 53 | :- meta_predicate must_succeed(0). | |
| 54 | :- meta_predicate must_succeed_without_residue_and_time_out(0). | |
| 55 | :- meta_predicate must_succeed_without_residue(0). | |
| 56 | :- meta_predicate must_succeed_multiple_without_residue(0). | |
| 57 | :- meta_predicate safe_call(0). | |
| 58 | :- meta_predicate check_deterministic(0). | |
| 59 | :- meta_predicate check_det(0). | |
| 60 | :- meta_predicate check_det2(0,-). | |
| 61 | ||
| 62 | :- use_module(debugging_calls). | |
| 63 | %:- disable_debugging_calls. | |
| 64 | ||
| 65 | :- use_module(module_information,[module_info/2]). | |
| 66 | :- module_info(group,testing). | |
| 67 | :- module_info(description,'This module provides predicates to define and run unit tests.'). | |
| 68 | ||
| 69 | :- use_module(tools_printing,[format_with_colour/4, print_goal/1]). | |
| 70 | :- use_module(tools_strings,[ajoin/2]). | |
| 71 | ||
| 72 | % first code which asserts pre-, post-conditions and self-checks | |
| 73 | ||
| 74 | % first a copy of print_error; so that this bit of self-check does not rely on other modules | |
| 75 | my_print_error(Error) :- | |
| 76 | (var(Error) | |
| 77 | -> print_message(error,'_') | |
| 78 | ; write(user_error,'! '),write_term(user_error,Error,[max_depth(50),numbervars(true)]),nl(user_error) | |
| 79 | ). | |
| 80 | ||
| 81 | my_portray_error(_:Conj) :- nonvar(Conj), Conj=(_,_),!, | |
| 82 | my_portray_error(Conj). % peel top-level module prefix for better pretty printing | |
| 83 | my_portray_error(Query) :- | |
| 84 | portray_clause((call :- Query)), | |
| 85 | fail. | |
| 86 | %safe_numbervars(Query,0,_), | |
| 87 | %write_term(user_error,Query,[portrayed(true),numbervars(true),max_depth(50),indented(true)]),fail. | |
| 88 | my_portray_error(_). | |
| 89 | %my_portray_error(Query) :- portray_clause(user_error,Query). % goes into loop for cyclic terms | |
| 90 | ||
| 91 | :- dynamic pre_condition/2, post_condition/2. | |
| 92 | :- meta_predicate assert_pre(0,0). | |
| 93 | assert_pre(X,Pre) :- % print(adding_pre(X)),nl, | |
| 94 | (nonvar_call(X),nonvar_call(Pre) | |
| 95 | -> retractall(pre_condition(X,_)),assertz(pre_condition(X,Pre)) | |
| 96 | ; my_print_error('### illegal variable(s) in: '), | |
| 97 | my_print_error(assert_pre(X,Pre)),fail | |
| 98 | ). | |
| 99 | assert_post(X,Post) :- | |
| 100 | (nonvar_call(X),nonvar_call(Post) | |
| 101 | -> retractall(post_condition(X,_)),assertz(post_condition(X,Post)) | |
| 102 | ; my_print_error('### illegal variable(s) in: '), | |
| 103 | my_print_error(assert_post(X,Post)),fail | |
| 104 | ). | |
| 105 | ||
| 106 | nonvar_call(Call) :- | |
| 107 | nonvar(Call), | |
| 108 | (Call = M:P -> nonvar(M), nonvar(P) ; true). | |
| 109 | ||
| 110 | :- dynamic self_check/4. | |
| 111 | :- dynamic self_check_module/2. | |
| 112 | :- volatile self_check/4, self_check_module/2. % this means that self-check cannot be run in the compiled versions ! | |
| 113 | ||
| 114 | self_check_module(Module) :- self_check_module(Module,_NrTests). | |
| 115 | ||
| 116 | % increase number of tests for a module and return new number as id | |
| 117 | inc_self_check_module(Module,Res) :- | |
| 118 | (retract(self_check_module(Module,N1)) -> true ; N1=0), | |
| 119 | Nr is N1+1, | |
| 120 | assertz(self_check_module(Module,Nr)), | |
| 121 | Res = Nr. | |
| 122 | ||
| 123 | % used to check if there are self checks. if not, tcl/tk interface does not show the button | |
| 124 | self_checks_exist :- self_check(_,_,_,_). | |
| 125 | ||
| 126 | % has a strange behaviour; investigate :- meta_predicate self_check(:,*). | |
| 127 | ||
| 128 | self_check(X,Module) :- self_check(X,Module,_,_). | |
| 129 | ||
| 130 | :- load_files(library(system), [when(compile_time), imports([environ/2])]). | |
| 131 | :- if(environ(prob_release,true)). | |
| 132 | add_self_check(_,_) :- !. % comment in to not include self-checks in distribution | |
| 133 | :- endif. | |
| 134 | add_self_check(Module,X) :- | |
| 135 | (nonvar(X) -> | |
| 136 | (self_check_already_registered(X,Module) -> true % e.g., when re-loading a file | |
| 137 | ; inc_self_check_module(Module,Nr), | |
| 138 | get_current_term_position(Line,_Col), | |
| 139 | assertz(self_check(X,Module,Nr,Line)) | |
| 140 | ) | |
| 141 | ; | |
| 142 | my_print_error('### trying to assert variable as self_check: '),my_print_error(X),fail | |
| 143 | ). | |
| 144 | ||
| 145 | % check if an identical test (up to variable renaming) is already registered | |
| 146 | % note: just calling self_check(X,Module) would also drop tests that merely unify with an earlier test, | |
| 147 | % e.g., must_succeed(p(a,_)) after must_succeed(p(a,b)) | |
| 148 | self_check_already_registered(X,Module) :- | |
| 149 | ? | \+ \+ ( copy_term(X,Y), |
| 150 | clause(self_check(Y,Module,_,_),true,Ref), % unification with Y: use indexing to find candidates | |
| 151 | clause(self_check(Z,_,_,_),true,Ref), % get the test as it was stored | |
| 152 | subsumes_term(X,Z), subsumes_term(Z,X) % X and Z are variants | |
| 153 | ). | |
| 154 | ||
| 155 | get_current_term_position(Line,Col) :- | |
| 156 | %prolog_load_context(file, File), | |
| 157 | prolog_load_context(term_position,TPos), | |
| 158 | !, | |
| 159 | (stream_position_data(line_count,TPos,LineM1) -> Line is LineM1+1 ; Line='?'), | |
| 160 | (stream_position_data(line_position,TPos,Col) -> true ; Col='?'). | |
| 161 | get_current_term_position('?','?'). | |
| 162 | ||
| 163 | ||
| 164 | :- if(predicate_property(current_logtalk_flag(_, _), _)). | |
| 165 | get_call_module(_, Module) :- | |
| 166 | sender(Module). | |
| 167 | :- else. | |
| 168 | get_call_module(Module:_, Module). | |
| 169 | :- endif. | |
| 170 | ||
| 171 | assert_must_succeed_any(Call) :- | |
| 172 | get_call_module(Call,M), | |
| 173 | add_self_check(M,must_succeed(Call)). | |
| 174 | assert_must_succeed(Call) :- | |
| 175 | get_call_module(Call,M), | |
| 176 | add_self_check(M,must_succeed_without_residue(Call)). | |
| 177 | assert_must_succeed_multiple(Call) :- | |
| 178 | get_call_module(Call,M), | |
| 179 | add_self_check(M,must_succeed_multiple_without_residue(Call)). | |
| 180 | assert_must_fail(Call) :- | |
| 181 | get_call_module(Call,M), | |
| 182 | add_self_check(M,must_fail(Call)). | |
| 183 | ||
| 184 | ||
| 185 | % ------------------------ | |
| 186 | ||
| 187 | :- use_module(debug). | |
| 188 | :- use_module(tools_printing). | |
| 189 | :- use_module(error_manager). | |
| 190 | :- use_module(junit_tests). | |
| 191 | :- use_module(library(lists)). | |
| 192 | :- use_module(tools_meta,[call_residue/2,safe_numbervars/3]). | |
| 193 | ||
| 194 | /* Example use: | |
| 195 | ||
| 196 | :- assert_pre(user:b_fd_type(_G,_L,_U),true). | |
| 197 | :- assert_post(user:b_fd_type(G,L,U),(atomic(G),(integer(L),integer(U)))). | |
| 198 | ||
| 199 | */ | |
| 200 | ||
| 201 | ||
| 202 | ||
| 203 | /* some auxilary predicates that can be used for pre,post conditions: */ | |
| 204 | ||
| 205 | % used in some pre- and post-conditions | |
| 206 | list_skeleton(X) :- | |
| 207 | (nonvar(X),list_skel2(X) -> true | |
| 208 | ; (my_print_error('### not list skeleton: '), my_print_error(X),fail)). | |
| 209 | list_skel2([]). | |
| 210 | list_skel2([_H|T]) :- nonvar(T), list_skel2(T). | |
| 211 | ||
| 212 | ||
| 213 | ||
| 214 | :- dynamic run_time_type_check_mode/1. | |
| 215 | run_time_type_check_mode(on). | |
| 216 | ||
| 217 | turn_on_run_time_type_checks :- | |
| 218 | retract(run_time_type_check_mode(_)), | |
| 219 | assertz(run_time_type_check_mode(on)). | |
| 220 | ||
| 221 | turn_off_run_time_type_checks :- | |
| 222 | retract(run_time_type_check_mode(_)), | |
| 223 | assertz(run_time_type_check_mode(off)). | |
| 224 | ||
| 225 | ||
| 226 | /* ===================================================== */ | |
| 227 | ||
| 228 | :- dynamic prepost_no_error_so_far/0. | |
| 229 | ||
| 230 | prepost_no_error_so_far. % :- fail. | |
| 231 | /* as errors are now displayed by error_manager, no need for interaction ?? */ | |
| 232 | ||
| 233 | disable_interaction_on_errors :- | |
| 234 | retractall(prepost_no_error_so_far). | |
| 235 | ||
| 236 | ||
| 237 | /* ===================================================== */ | |
| 238 | ||
| 239 | :- register_debugging_calls([pp_mnf(*), pp_cll(*), mnf(*), mnf(-,*), det_call(*)]). | |
| 240 | ||
| 241 | pp_mnf(X) :- prepost_mnf_call(X). | |
| 242 | pp_cll(X) :- prepost_call(X). | |
| 243 | mnf(X) :- mnf_call(X). | |
| 244 | mnf(ProgramPoint,X) :- mnf_call_with_pp(ProgramPoint,X). | |
| 245 | ||
| 246 | ||
| 247 | /* ===================================================== */ | |
| 248 | ||
| 249 | ||
| 250 | :- use_module(library(timeout)). | |
| 251 | ||
| 252 | rt_timeout_call(Call) :- run_time_type_check_mode(off),!,check_exception_call(Call). | |
| 253 | rt_timeout_call(Call) :- time_out(check_exception_call(Call),5000,TimeOutRes), | |
| 254 | (TimeOutRes = success -> true ; inc_error_count, add_error(self_check,'### TIMEOUT: ',Call),fail). | |
| 255 | ||
| 256 | % just catch exceptions and print them nicely | |
| 257 | check_exception_call(X) :- | |
| 258 | catch(call(X), Error, ( | |
| 259 | flush_output, | |
| 260 | format_with_colour(user_error,[red,bold],'! *** EXCEPTION *** :~n',[]), | |
| 261 | write('! '),print_numbervars(X),nl, | |
| 262 | write_exception_error(Error),nl, | |
| 263 | flush_output, | |
| 264 | throw(Error) | |
| 265 | )). | |
| 266 | ||
| 267 | write_exception_error(error(existence_error(procedure,CALL),_)) :- | |
| 268 | format_with_colour(user_error,[red,bold],'! EXISTENCE ERROR: ~w~n',[CALL]),fail. | |
| 269 | write_exception_error(Error) :- write('! '),write_term(Error,[max_depth(20),numbervars(true)]),nl. | |
| 270 | ||
| 271 | print_numbervars(X) :- safe_numbervars(X,0,_), print(X),fail. | |
| 272 | print_numbervars(_). | |
| 273 | ||
| 274 | prepost_call(X) :- | |
| 275 | /* print(pre(X)),nl, */ | |
| 276 | (verify_pre(X) -> true ; my_print_error(verify_pre_failed(X))), | |
| 277 | rt_timeout_call(X), | |
| 278 | /* print(post(X)),nl, */ | |
| 279 | (verify_post(X) -> true ; my_print_error(verify_post_failed(X))). | |
| 280 | ||
| 281 | verify_pre(Call) :- | |
| 282 | (run_time_type_check_mode(off) -> true | |
| 283 | ; pre_condition(Call,Pre) -> | |
| 284 | ( \+ rt_timeout_call(Pre) | |
| 285 | -> get_predicate_arity(Call,Pred,Arity), | |
| 286 | add_error(verify_pre,'### PRE-CONDITION ERROR OCCURRED: ',Pred/Arity), | |
| 287 | add_error(verify_pre,'### CALL: ',Call),nl, | |
| 288 | prepost_user_interaction | |
| 289 | ; true | |
| 290 | ) | |
| 291 | ; get_predicate_arity(Call,Pred,Arity), | |
| 292 | add_error(verify_pre,'### No PRE-CONDITION for ',Pred/Arity), | |
| 293 | print_term_summary(Call), | |
| 294 | prepost_user_interaction | |
| 295 | ). | |
| 296 | ||
| 297 | verify_post(Call) :- | |
| 298 | (run_time_type_check_mode(off) -> true | |
| 299 | ; post_condition(Call,Post) -> | |
| 300 | ( \+ rt_timeout_call(Post) | |
| 301 | -> get_predicate_arity(Call,Pred,Arity), | |
| 302 | add_error(verify_post,'### POST-CONDITION ERROR OCCURRED: ',Pred/Arity), | |
| 303 | add_error(verify_post,'### CALL: ',Call),nl, | |
| 304 | prepost_user_interaction | |
| 305 | ; true | |
| 306 | ) | |
| 307 | ; get_predicate_arity(Call,Pred,Arity), | |
| 308 | add_error(verify_post,'### No POST-CONDITION for ',Pred/Arity), | |
| 309 | prepost_user_interaction | |
| 310 | ). | |
| 311 | ||
| 312 | ||
| 313 | prepost_user_interaction :- prepost_no_error_so_far,!, | |
| 314 | my_print_error('### => Stop at next error (y/n/halt/trace) => '), | |
| 315 | read(Answer), | |
| 316 | (Answer='y' -> true | |
| 317 | ; Answer='halt' -> halt | |
| 318 | ; Answer='trace' -> trace | |
| 319 | ; retract(prepost_no_error_so_far)). | |
| 320 | prepost_user_interaction. | |
| 321 | ||
| 322 | ||
| 323 | get_predicate_arity(':'(Module,Call),Res,Arity) :- !,Res=Module:Pred, | |
| 324 | functor(Call,Pred,Arity). | |
| 325 | get_predicate_arity(Call,Pred,Arity) :- | |
| 326 | functor(Call,Pred,Arity). | |
| 327 | ||
| 328 | /* ===================================================== */ | |
| 329 | ||
| 330 | prepost_mnf_call(X) :- % print(prepost_mnf_call(X)),nl, | |
| 331 | (run_time_type_check_mode(off) | |
| 332 | -> call(X) | |
| 333 | ; if(prepost_call(X),true, | |
| 334 | (add_error(mnf,'### WARNING CALL HAS FAILED: ',X), | |
| 335 | print_call(X), | |
| 336 | prepost_user_interaction, | |
| 337 | fail) | |
| 338 | ) | |
| 339 | ). | |
| 340 | ||
| 341 | mnf_call(X) :- | |
| 342 | (run_time_type_check_mode(off) | |
| 343 | -> call(X) | |
| 344 | ; if(call(X),true, | |
| 345 | (add_error(mnf,'### WARNING CALL HAS FAILED: ',X), | |
| 346 | print_call(X), | |
| 347 | prepost_user_interaction, | |
| 348 | fail) | |
| 349 | ) | |
| 350 | ). | |
| 351 | ||
| 352 | print_call(X) :- | |
| 353 | print('### Call: '),print_quoted_with_max_depth(X,10),nl, | |
| 354 | print('### Summary: '),print_term_summary(X),nl, | |
| 355 | print('### Predicate: '), | |
| 356 | (X = ':'(M,C) -> (print(M),print(':')) ; C=X), | |
| 357 | functor(C,F,N), | |
| 358 | print(F), print('/'), print(N),nl. | |
| 359 | ||
| 360 | ||
| 361 | mnf_call_with_pp(ProgramPoint,X) :- | |
| 362 | (run_time_type_check_mode(off) | |
| 363 | -> call(X) | |
| 364 | ; if(rt_timeout_call(X),true, | |
| 365 | (add_error(mnf,'### WARNING CALL HAS FAILED: ',(X,ProgramPoint)), | |
| 366 | print_call(X), | |
| 367 | print('### at program point:'),print(ProgramPoint),nl, | |
| 368 | prepost_user_interaction, | |
| 369 | fail) | |
| 370 | ) | |
| 371 | ). | |
| 372 | ||
| 373 | :- volatile found_det_sol/4. | |
| 374 | :- dynamic found_det_sol/4. | |
| 375 | ||
| 376 | mnf_det(X) :- | |
| 377 | (run_time_type_check_mode(off) | |
| 378 | -> call(X) | |
| 379 | ; (get_functor_module(X,Module,F,Arity), | |
| 380 | retractall(found_det_sol(F,Arity,Module,_)), /* cannot be nested for same predicate !*/ | |
| 381 | copy_term(X,CopyX), | |
| 382 | mnf_call(X), | |
| 383 | (found_det_sol(F,Arity,Module,Previous) -> | |
| 384 | (add_error(mnf,'### WARNING CALL HAS MULTIPLE SOLUTIONS: ',CopyX),nl, | |
| 385 | print('### Solution 1: '),print_quoted_with_max_depth(Previous,60),nl,nl, | |
| 386 | print('### Solution 2: '),print_quoted_with_max_depth(X,60),nl,nl, | |
| 387 | prepost_user_interaction) | |
| 388 | ; assertz(found_det_sol(F,Arity,Module,X)) | |
| 389 | ) | |
| 390 | ) | |
| 391 | ). | |
| 392 | ||
| 393 | % det_call(';'(X=a,X=b)). | |
| 394 | get_functor_module(_:Call,Module,F,Arity) :- Call=(A,_),!, | |
| 395 | get_functor_module(A,Module,F,Arity). | |
| 396 | get_functor_module(Module:Call,Module,F,Arity) :- !, functor(Call,F,Arity). | |
| 397 | get_functor_module(Call,unknown,F,Arity) :- functor(Call,F,Arity). | |
| 398 | ||
| 399 | det_call(X) :- | |
| 400 | (run_time_type_check_mode(off) | |
| 401 | -> call(X) | |
| 402 | ; force_det_call(X) | |
| 403 | ). | |
| 404 | ||
| 405 | residue_check_call(X) :- | |
| 406 | copy_term(X,CX), | |
| 407 | call_residue(X,CallResidue), | |
| 408 | (maplist(acceptable_residue,CallResidue) -> true | |
| 409 | ; add_error(must_succeed_without_residue,'### Call has residue: ',X), | |
| 410 | add_error(must_succeed_without_residue,'### Residue: ',CallResidue), | |
| 411 | inc_error_count, | |
| 412 | print_goal(CallResidue),nl, | |
| 413 | safe_numbervars(CX,0,_), | |
| 414 | format('### Original call: ~w~n',[CX]) | |
| 415 | ). | |
| 416 | ||
| 417 | % if we use ground_det_wait_flag: we can have these residues | |
| 418 | acceptable_residue(kernel_waitflags:copy_wfe_to_inner(_,_)). | |
| 419 | % Default representation returned by SWI for attributes from modules | |
| 420 | % that don't define attribute_goals//1. | |
| 421 | acceptable_residue(put_attr(_,_,_)). | |
| 422 | acceptable_residue((A,B)) :- acceptable_residue(A),acceptable_residue(B). | |
| 423 | ||
| 424 | force_det_call(X) :- | |
| 425 | get_functor_module(X,Module,F,Arity), | |
| 426 | retractall(found_det_sol(F,Arity,Module,_)), /* cannot be nested for same predicate !*/ | |
| 427 | copy_term(X,CopyX), | |
| 428 | %print(det_call(X)),nl, | |
| 429 | residue_check_call(X), % was rt_timeout_call(X) | |
| 430 | (found_det_sol(F,Arity,Module,Previous) -> | |
| 431 | (add_error(mnf,'### WARNING CALL HAS MULTIPLE SOLUTIONS: ',CopyX),nl, | |
| 432 | print('### Solution 1: '),print_quoted_with_max_depth(Previous,60),nl,nl, | |
| 433 | print('### Solution 2: '),print_quoted_with_max_depth(X,60),nl,nl, | |
| 434 | prepost_user_interaction) | |
| 435 | ; assertz(found_det_sol(F,Arity,Module,X)) | |
| 436 | ). | |
| 437 | ||
| 438 | /* ===================================================== */ | |
| 439 | ||
| 440 | must_fail(X) :- | |
| 441 | copy_term(X,Y), | |
| 442 | rt_timeout_call(X),!, | |
| 443 | %(safe_numbervars(Y,0,_) -> true ; true), % portray already does a numbervars | |
| 444 | inc_error_count(Module,_NrErr), | |
| 445 | add_error(must_fail,'Unit Test Failed, a call unexpectedly succeeded in module:',Module), | |
| 446 | my_print_error('! The call: '), | |
| 447 | my_portray_error(Y), | |
| 448 | my_print_error('! should have failed but succeeded with:'), | |
| 449 | my_portray_error(X). | |
| 450 | must_fail(_X). | |
| 451 | ||
| 452 | ||
| 453 | get_self_check_test_nr(Nr) :- (bb_get(self_check_test_nr,N) -> Nr=N ; Nr='?'). | |
| 454 | get_self_check_test_line(Nr) :- (bb_get(self_check_test_line,N) -> Nr=N ; Nr='?'). | |
| 455 | ||
| 456 | must_succeed(X) :- %print(must_suceed(X)),nl, | |
| 457 | \+ rt_timeout_call(X),!, | |
| 458 | %(safe_numbervars(X,0,_) -> true ; true), % portray already does a numbervars | |
| 459 | inc_error_count(Module,_NrErr), | |
| 460 | get_functor_module(X,_,F,Arity), | |
| 461 | get_self_check_test_nr(TestNr), | |
| 462 | get_self_check_test_line(LineNr), | |
| 463 | ajoin(['Unit Test ',TestNr, ' on line ',LineNr, | |
| 464 | ' failed, a call to ',F,'/',Arity,' unexpectedly failed in module:'],Msg), | |
| 465 | add_error(must_succeed,Msg,Module), | |
| 466 | my_print_error('! The call failed but should have succeeded: '), | |
| 467 | my_portray_error(X), | |
| 468 | %trace, X, % to directly re-run test in trace mode (and comment out safe_numbervars above) | |
| 469 | nl. | |
| 470 | must_succeed(_X). % :- print(ok_must_succeed(_X)),nl. | |
| 471 | ||
| 472 | :- volatile found_must_succeed_sol/1, found_id/1. | |
| 473 | :- dynamic found_must_succeed_sol/1, found_id/1. | |
| 474 | found_id(0). | |
| 475 | get_found_id(Nr) :- retract(found_id(Nr)), N1 is Nr+1, assertz(found_id(N1)). | |
| 476 | reset_found_id :- retractall(found_must_succeed_sol(_)), | |
| 477 | retractall(found_id(_)), assertz(found_id(0)). | |
| 478 | ||
| 479 | must_succeed_without_residue_and_time_out(X) :- | |
| 480 | time_out(check_exception_call(must_succeed(X)),2000,TimeOutRes), | |
| 481 | (TimeOutRes = success -> true | |
| 482 | ; inc_error_count, | |
| 483 | add_error(self_check,'### TIMEOUT: ',X), portray_clause(X),nl, | |
| 484 | fail). | |
| 485 | ||
| 486 | must_succeed_without_residue(X) :- get_found_id(ID), | |
| 487 | must_succeed(residue_check_call(X)), | |
| 488 | retractall(found_must_succeed_sol(ID)), | |
| 489 | %residue_check_call(X), | |
| 490 | (found_must_succeed_sol(ID) | |
| 491 | -> add_error(must_succeed_without_residue,'### Self-Check has multiple solutions: ',X), | |
| 492 | inc_error_count, | |
| 493 | ! | |
| 494 | ; assertz(found_must_succeed_sol(ID)),fail | |
| 495 | ). | |
| 496 | must_succeed_without_residue(_). | |
| 497 | ||
| 498 | :- public must_succeed_multiple_without_residue/1. % used by assert_must_succeed_multiple | |
| 499 | must_succeed_multiple_without_residue(X) :- get_found_id(ID), | |
| 500 | must_succeed(X), | |
| 501 | retractall(found_must_succeed_sol(ID)), | |
| 502 | call_residue(X,CallResidue), | |
| 503 | (maplist(acceptable_residue,CallResidue) -> true -> true | |
| 504 | ; add_error(must_succeed_multiple_without_residue,'### Self-Check has residue: ',X), | |
| 505 | add_error(must_succeed_multiple_without_residue,'### Residue: ',CallResidue), | |
| 506 | inc_error_count, | |
| 507 | print_goal(CallResidue),nl | |
| 508 | ), | |
| 509 | (found_must_succeed_sol(ID) | |
| 510 | -> (true,!) | |
| 511 | ; assertz(found_must_succeed_sol(ID)),fail | |
| 512 | ). | |
| 513 | must_succeed_multiple_without_residue(X) :- | |
| 514 | inc_error_count, | |
| 515 | add_error(must_succeed_multiple_without_residue, | |
| 516 | '### Self-Check did not succeed multiple times: ',X). | |
| 517 | ||
| 518 | ||
| 519 | ||
| 520 | safe_call(X) :- | |
| 521 | catch(call(X), Exception, ( | |
| 522 | print(exception(X,Exception)),nl,nl, | |
| 523 | inc_error_count, | |
| 524 | add_error(safe_call,'### Exception occurred during self-check: ',X:Exception) | |
| 525 | )). | |
| 526 | ||
| 527 | :- dynamic starttime/1. | |
| 528 | ||
| 529 | get_module_list(ML) :- findall(Module,self_check_module(Module),Modules), sort(Modules,ML). | |
| 530 | ||
| 531 | ||
| 532 | ||
| 533 | ||
| 534 | :- dynamic errors_in_module/2, current_module_under_test/1, tests_in_module/2. | |
| 535 | set_current_module(M) :- var(M),!, my_print_error('Module is variable'). | |
| 536 | set_current_module(M) :- | |
| 537 | retractall(current_module_under_test(_)), | |
| 538 | assertz(current_module_under_test(M)). | |
| 539 | get_error_count(Module,Nr) :- errors_in_module(Module,Nr), !. | |
| 540 | get_error_count(_,0). | |
| 541 | inc_error_count :- inc_error_count(_,_). | |
| 542 | inc_error_count(Module,N1) :- current_module_under_test(Module),!, | |
| 543 | (retract(errors_in_module(Module,Nr)) -> true ; Nr=0), | |
| 544 | N1 is Nr+1, | |
| 545 | assertz(errors_in_module(Module,N1)). | |
| 546 | inc_error_count(unknown,1) :- print('No current module.'),nl. | |
| 547 | ||
| 548 | :- dynamic unit_test_failed_in_module/3. | |
| 549 | register_failure(Module,TestNr,LineNr) :- assertz(unit_test_failed_in_module(Module,TestNr,LineNr)). | |
| 550 | ||
| 551 | print_error_summary :- errors_in_module(Module,Nr), | |
| 552 | findall(TestNr,unit_test_failed_in_module(Module,TestNr,_),List), | |
| 553 | findall(LineNr,unit_test_failed_in_module(Module,_,LineNr),Lines), | |
| 554 | length(List,Len), | |
| 555 | format('Module ~w has ~w errors and ~w failed test(s): ~w at lines ~w.~n',[Module,Nr,Len,List,Lines]), | |
| 556 | fail. | |
| 557 | print_error_summary. | |
| 558 | ||
| 559 | print_summary :- findall(T,tests_in_module(_,T),NrTests), | |
| 560 | length(NrTests,NrModules), | |
| 561 | sumlist(NrTests,TotTests), | |
| 562 | format('Number of Unit Tests: ~w in ~w modules~n',[TotTests,NrModules]). | |
| 563 | ||
| 564 | reset_error_summary :- | |
| 565 | retractall(errors_in_module(_,_)), | |
| 566 | retractall(unit_test_failed_in_module(_,_,_)), | |
| 567 | retractall(tests_in_module(_,_)). | |
| 568 | ||
| 569 | run_module_tests(Module,Results,Options) :- | |
| 570 | findall(testcase(Module,TestNr,Line,X), included_self_check(X,Module,TestNr,Line,Options), Calls), | |
| 571 | length(Calls,Len), | |
| 572 | assertz(tests_in_module(Module,Len)), | |
| 573 | %maplist(check_test_case, Calls, Results). | |
| 574 | check_test_cases(Calls,Results,0,Module,Options). | |
| 575 | ||
| 576 | included_self_check(X,Module,TestNr,Line,Options) :- | |
| 577 | self_check(X,Module,TestNr,Line), | |
| 578 | (member(run_only_nr(Nr),Options) -> TestNr=Nr ; true). | |
| 579 | ||
| 580 | ||
| 581 | check_test_cases([],[],Nr,Module,_Options) :- | |
| 582 | format('Number of unit tests run in ~w: ~w~n',[Module,Nr]). | |
| 583 | check_test_cases([H|T],[V|VT],Nr,Module,Options) :- | |
| 584 | N1 is Nr+1, | |
| 585 | get_error_count(Module,NrBefore), | |
| 586 | check_test_case(H,N1,V,Options), | |
| 587 | get_error_count(Module,NrAfter), | |
| 588 | (NrBefore=NrAfter -> true | |
| 589 | % ; NrBefore > 0 -> true % we already have a previous unit test failure | |
| 590 | ; H=testcase(_,TestNr,LineNr,_Call), | |
| 591 | format_with_colour(user_error,[red,bold], | |
| 592 | '~n*** UNIT TEST ~w FAILED in module ~w (line ~w)~n',[TestNr,Module,LineNr]), | |
| 593 | format_with_colour(user_error,[red,bold], | |
| 594 | '*** Rerun this test with: probcli -selfcheck_module ~w:~w~n~n',[Module,TestNr]), | |
| 595 | register_failure(Module,TestNr,LineNr) | |
| 596 | %,portray_testcase(H) | |
| 597 | %%% ,trace %%% | |
| 598 | ), | |
| 599 | check_test_cases(T,VT,N1,Module,Options). | |
| 600 | ||
| 601 | :- use_module(tools_printing,[start_terminal_colour/2, reset_terminal_colour/1]). | |
| 602 | portray_testcase(testcase(Module,TestNr,Line,Call)) :- !, | |
| 603 | start_terminal_colour([blue],user_error), | |
| 604 | format(user_error,'UNIT TEST ~w in module ~w at line ~w:~n :- ',[TestNr,Module,Line]), | |
| 605 | portray_clause(user_error,Call),nl, | |
| 606 | reset_terminal_colour(user_error). | |
| 607 | portray_testcase(X) :- print(unknown_testcase(X)),nl. | |
| 608 | ||
| 609 | ||
| 610 | check_test_case(testcase(Module,Nr,Line,Call), TotNr, Verdict,Options) :- | |
| 611 | (member(silent,Options) -> true | |
| 612 | ; format('Running unit test ~w in module ~w (line ~w)~n',[Nr,Module,Line])), | |
| 613 | set_error_context(unit_test_context(Module,TotNr,Line,Call)), | |
| 614 | bb_put(self_check_test_nr,TotNr), | |
| 615 | bb_put(self_check_test_line,Line), | |
| 616 | statistics(runtime,[T1,_]), | |
| 617 | (member(verbose,Options) -> portray_clause(Call) ; true), | |
| 618 | safe_call(Call), | |
| 619 | flush_output, | |
| 620 | statistics(runtime,[T2,_]), | |
| 621 | Time is T2-T1, | |
| 622 | ((member(verbose,Options) ; Time > 100, nonmember(silent,Options)) | |
| 623 | -> format('Runtime for unit test ~w: ~w ms~n',[TotNr,Time]) | |
| 624 | ; true), | |
| 625 | (Time>2000 -> format_with_colour(user_output,[blue],'Warning: long unit test ~w: ~w ms in module ~w (line ~w)~n ~w~n',[TotNr,Time,Module,Line,Call]) ; true), | |
| 626 | (get_all_errors_and_reset(Errors) -> V=error(Errors) ; V=pass), | |
| 627 | create_junit_result(Call, Time, V, Verdict). | |
| 628 | ||
| 629 | ||
| 630 | perform_self_check(M) :- perform_self_check(M,[]). | |
| 631 | perform_self_check :- perform_self_check(_). | |
| 632 | perform_verbose_self_check :- perform_verbose_self_check(_). | |
| 633 | perform_verbose_self_check(M) :- perform_self_check(M,[verbose]). | |
| 634 | ||
| 635 | :- use_module(eventhandling,[announce_event/1]). | |
| 636 | perform_self_check(Module,Options) :- nl, reset_found_id, | |
| 637 | reset_error_summary, | |
| 638 | announce_event(start_unit_tests), | |
| 639 | flush_output, | |
| 640 | print('% '), | |
| 641 | retractall(starttime(_)), | |
| 642 | statistics(runtime,[Start,_]), assertz(starttime(Start)), | |
| 643 | if(self_check_module(Module,NrTests),true, | |
| 644 | (my_print_error(illegal_module(Module)),inc_error_count, | |
| 645 | fail) | |
| 646 | ), | |
| 647 | set_current_module(Module), | |
| 648 | (member(silent,Options) -> true | |
| 649 | ; member(run_only_nr(Nr),Options) | |
| 650 | -> format_with_colour(user_output,[blue],'~n~nPerforming unit test ~w for module ~w~n',[Nr,Module]) | |
| 651 | ; format_with_colour(user_output,[blue],'~n~nPerforming ~w unit tests for module ~w~n',[NrTests,Module])), | |
| 652 | run_module_tests(Module,TestResults,Options), | |
| 653 | full_junit_test_suite_name(['Selfcheck',Module], SuiteName), | |
| 654 | print_junit(SuiteName, TestResults), | |
| 655 | fail. | |
| 656 | perform_self_check(_,_) :- | |
| 657 | statistics(runtime,[End,_]), | |
| 658 | starttime(Start), Tot is End-Start, | |
| 659 | nl, print('Runtime for Performing Unit Tests: '), print(Tot), print(' ms'), | |
| 660 | nl, | |
| 661 | announce_event(stop_unit_tests), | |
| 662 | clear_error_context, | |
| 663 | print_summary, | |
| 664 | (errors_in_module(_,_) | |
| 665 | -> format_with_colour(user_error,[red,bold],'~n! Unit Tests FAILED !!!~n',[]), | |
| 666 | print_error_summary, | |
| 667 | fail | |
| 668 | ; true), | |
| 669 | format_with_colour(user_output,[green],'~nUnit Tests Successful.~n',[]), | |
| 670 | flush_output. | |
| 671 | ||
| 672 | ||
| 673 | portray_self_checks :- portray_self_checks(_). | |
| 674 | ||
| 675 | /* self_check:portray_self_checks */ | |
| 676 | portray_self_checks(Module) :- | |
| 677 | self_check_module(Module,NrTests), | |
| 678 | format_with_colour(user_output,[blue],'~n~nUnit Tests (~w) for Module ~w~n',[NrTests,Module]), | |
| 679 | self_check(X,Module,Nr,Line), | |
| 680 | format_with_colour(user_output,[blue],' ~w (line ~w): ',[Nr, Line]), | |
| 681 | portray_clause(X), | |
| 682 | fail. | |
| 683 | portray_self_checks(_) :- nl. | |
| 684 | ||
| 685 | % ------------------------ | |
| 686 | ||
| 687 | :- dynamic det_counter/1. | |
| 688 | det_counter(0). | |
| 689 | check_deterministic(Call) :- | |
| 690 | retract(det_counter(X)), X1 is X+1, assertz(det_counter(X1)), | |
| 691 | check_det2(Call,X). | |
| 692 | check_det(Call) :- check_deterministic(Call). | |
| 693 | ||
| 694 | :- volatile calling/1. | |
| 695 | :- dynamic calling/1. | |
| 696 | check_det2(Call,X) :- assertz(calling(X)), | |
| 697 | call(Call), | |
| 698 | (retract(calling(X)) -> true | |
| 699 | ; nl, | |
| 700 | print('### Call has multiple solutions: '),nl, | |
| 701 | print('### '), print(Call),nl, | |
| 702 | nl | |
| 703 | ). | |
| 704 | check_det2(Call,X) :- (retract(calling(X)) -> print(fails(Call)),nl ; fail). | |
| 705 | ||
| 706 | ||
| 707 | % ------------------------ | |
| 708 | ||
| 709 | check_eq(A,B) :- (A=B -> true ; format(user_error,'! Not unifiable:~n ~w~n ~w~n',[A,B]),fail). | |
| 710 | check_eqeq(A,B) :- (A==B -> true ; format(user_error,'! Not identical:~n ~w~n ~w~n',[A,B]),fail). |