0025758: Visualization, TKOpenGl - texture initialization fails on Intel HD 4600...
[occt.git] / src / DrawResources / TestCommands.tcl
CommitLineData
d5f74e42 1# Copyright (c) 2013-2014 OPEN CASCADE SAS
40093367 2#
973c2be1 3# This file is part of Open CASCADE Technology software library.
40093367 4#
d5f74e42 5# This library is free software; you can redistribute it and/or modify it under
6# the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 7# by the Free Software Foundation, with special exception defined in the file
8# OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9# distribution for complete text of the license and disclaimer of any warranty.
40093367 10#
973c2be1 11# Alternatively, this file may be used under the terms of Open CASCADE
12# commercial license or contractual agreement.
40093367 13
14############################################################################
15# This file defines scripts for execution of OCCT tests.
16# It should be loaded automatically when DRAW is started, and provides
351bbcba 17# top-level commands starting with 'test'. Type 'help test' to get their
18# synopsys.
40093367 19# See OCCT Tests User Guide for description of the test system.
20#
21# Note: procedures with names starting with underscore are for internal use
22# inside the test system.
23############################################################################
24
25# Default verbose level for command _run_test
26set _tests_verbose 0
27
28# regexp for parsing test case results in summary log
29set _test_case_regexp {^CASE\s+([\w.-]+)\s+([\w.-]+)\s+([\w.-]+)\s*:\s*([\w]+)(.*)}
30
31# Basic command to run indicated test case in DRAW
b725d7c5 32help test {
33 Run specified test case
34 Use: test group grid casename [echo=0]
35 - If echo is set to 0 (default), log is stored in memory and only summary
36 is output (the log can be obtained with command "dlog get")
37 - If echo is set to 1 or "-echo", all commands and results are echoed
38 immediately, but log is not saved and summary is not produced
39}
5df3a117 40proc test {group grid casename {echo 0}} {
40093367 41 # get test case paths (will raise error if input is invalid)
42 _get_test $group $grid $casename dir gridname casefile
43
b725d7c5 44 # if echo specified as "-echo", convert it to bool
45 if { "$echo" == "-echo" } { set echo t }
46
40093367 47 # run test
c97067ad 48 uplevel _run_test $dir $group $gridname $casefile $echo
40093367 49
50 # check log
5df3a117 51 if { ! $echo } {
52 _check_log $dir $group $gridname $casename [dlog get]
53 }
40093367 54
55 return
56}
57
58# Basic command to run indicated test case in DRAW
b725d7c5 59help testgrid {
60 Run all tests, or specified group, or one grid
87f42a3f 61 Use: testgrid [groupmask [gridmask [casemask]]] [options...]
b725d7c5 62 Allowed options are:
63 -parallel N: run N parallel processes (default is number of CPUs, 0 to disable)
9753e6de 64 -refresh N: save summary logs every N seconds (default 600, minimal 1, 0 to disable)
b725d7c5 65 -outdir dirname: set log directory (should be empty or non-existing)
66 -overwrite: force writing logs in existing non-empty directory
67 -xml filename: write XML report for Jenkins (in JUnit-like format)
87f42a3f 68 Groups, grids, and test cases to be executed can be specified by list of file
69 masks, separated by spaces or comma; default is all (*).
40093367 70}
b725d7c5 71proc testgrid {args} {
40093367 72 global env tcl_platform _tests_verbose
73
74 ######################################################
75 # check arguments
76 ######################################################
77
78 # check that environment variable defining paths to test scripts is defined
79 if { ! [info exists env(CSF_TestScriptsPath)] ||
80 [llength $env(CSF_TestScriptsPath)] <= 0 } {
81 error "Error: Environment variable CSF_TestScriptsPath is not defined"
82 }
83
84 # treat options
b725d7c5 85 set parallel [_get_nb_cpus]
40093367 86 set refresh 60
b725d7c5 87 set logdir ""
40093367 88 set overwrite 0
89 set xmlfile ""
90 for {set narg 0} {$narg < [llength $args]} {incr narg} {
91 set arg [lindex $args $narg]
92
93 # parallel execution
94 if { $arg == "-parallel" } {
95 incr narg
b725d7c5 96 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
40093367 97 set parallel [expr [lindex $args $narg]]
98 } else {
b725d7c5 99 error "Option -parallel requires argument"
40093367 100 }
101 continue
102 }
103
104 # refresh logs time
105 if { $arg == "-refresh" } {
106 incr narg
b725d7c5 107 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
40093367 108 set refresh [expr [lindex $args $narg]]
109 } else {
b725d7c5 110 error "Option -refresh requires argument"
111 }
112 continue
113 }
114
115 # output directory
116 if { $arg == "-outdir" } {
117 incr narg
118 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
119 set logdir [lindex $args $narg]
120 } else {
121 error "Option -outdir requires argument"
40093367 122 }
123 continue
124 }
125
126 # allow overwrite logs
127 if { $arg == "-overwrite" } {
128 set overwrite 1
129 continue
130 }
131
132 # refresh logs time
133 if { $arg == "-xml" } {
134 incr narg
b725d7c5 135 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
40093367 136 set xmlfile [lindex $args $narg]
137 }
138 if { $xmlfile == "" } {
139 set xmlfile TESTS-summary.xml
140 }
141 continue
142 }
143
144 # unsupported option
145 if { [regexp {^-} $arg] } {
146 error "Error: unsupported option \"$arg\""
147 }
148
149 # treat arguments not recognized as options as group and grid names
87f42a3f 150 if { ! [info exists groupmask] } {
151 set groupmask [split $arg ,]
152 } elseif { ! [info exists gridmask] } {
153 set gridmask [split $arg ,]
154 } elseif { ! [info exists casemask] } {
155 set casemask [split $arg ,]
40093367 156 } else {
87f42a3f 157 error "Error: cannot interpret argument $narg ($arg)"
40093367 158 }
159 }
160
161 # check that target log directory is empty or does not exist
162 set logdir [file normalize [string trim $logdir]]
163 if { $logdir == "" } {
b725d7c5 164 # if specified logdir is empty string, generate unique name like
ffc26091 165 # results/<branch>_<timestamp>
166 set prefix ""
b725d7c5 167 if { ! [catch {exec git branch} gitout] &&
168 [regexp {[*] ([\w]+)} $gitout res branch] } {
ffc26091 169 set prefix "${branch}_"
b725d7c5 170 }
ffc26091 171 set logdir "results/${prefix}[clock format [clock seconds] -format {%Y-%m-%dT%H%M}]"
40093367 172 set logdir [file normalize $logdir]
173 }
174 if { [file isdirectory $logdir] && ! $overwrite && ! [catch {glob -directory $logdir *}] } {
175 error "Error: Specified log directory \"$logdir\" is not empty; please clean it before running tests"
176 }
177 if { [catch {file mkdir $logdir}] || ! [file writable $logdir] } {
178 error "Error: Cannot create directory \"$logdir\", or it is not writable"
179 }
180
87f42a3f 181 # masks for search of test groups, grids, and cases
182 if { ! [info exists groupmask] } { set groupmask * }
183 if { ! [info exists gridmask ] } { set gridmask * }
184 if { ! [info exists casemask ] } { set casemask * }
185
40093367 186 ######################################################
187 # prepare list of tests to be performed
188 ######################################################
189
190 # list of tests, each defined by a list of:
191 # test scripts directory
192 # group (subfolder) name
193 # grid (subfolder) name
194 # test case name
195 # path to test case file
196 set tests_list {}
197
198 # iterate by all script paths
199 foreach dir [_split_path $env(CSF_TestScriptsPath)] {
200 # protection against empty paths
201 set dir [string trim $dir]
202 if { $dir == "" } { continue }
203
204 if { $_tests_verbose > 0 } { _log_and_puts log "Examining tests directory $dir" }
205
206 # check that directory exists
207 if { ! [file isdirectory $dir] } {
208 _log_and_puts log "Warning: directory $dir listed in CSF_TestScriptsPath does not exist, skipped"
209 continue
210 }
211
87f42a3f 212 # search all directories in the current dir with specified mask
213 if [catch {glob -directory $dir -tail -types d {*}$groupmask} groups] { continue }
40093367 214
215 # iterate by groups
216 if { $_tests_verbose > 0 } { _log_and_puts log "Groups to be executed: $groups" }
217 foreach group [lsort -dictionary $groups] {
218 if { $_tests_verbose > 0 } { _log_and_puts log "Examining group directory $group" }
219
220 # file grids.list must exist: it defines sequence of grids in the group
221 if { ! [file exists $dir/$group/grids.list] } {
222 _log_and_puts log "Warning: directory $dir/$group does not contain file grids.list, skipped"
223 continue
224 }
225
226 # read grids.list file and make a list of grids to be executed
227 set gridlist {}
228 set fd [open $dir/$group/grids.list]
229 set nline 0
230 while { [gets $fd line] >= 0 } {
231 incr nline
232
233 # skip comments and empty lines
234 if { [regexp "\[ \t\]*\#.*" $line] } { continue }
235 if { [string trim $line] == "" } { continue }
236
237 # get grid id and name
238 if { ! [regexp "^\(\[0-9\]+\)\[ \t\]*\(\[A-Za-z0-9_.-\]+\)\$" $line res gridid grid] } {
239 _log_and_puts log "Warning: cannot recognize line $nline in file $dir/$group/grids.list as \"gridid gridname\"; ignored"
240 continue
241 }
242
87f42a3f 243 # check that grid fits into the specified mask
244 foreach mask $gridmask {
245 if { $mask == $gridid || [string match $mask $grid] } {
246 lappend gridlist $grid
247 }
40093367 248 }
249 }
250 close $fd
251
252 # iterate by all grids
253 foreach grid $gridlist {
254
255 # check if this grid is aliased to another one
256 set griddir $dir/$group/$grid
257 if { [file exists $griddir/cases.list] } {
258 set fd [open $griddir/cases.list]
259 if { [gets $fd line] >= 0 } {
260 set griddir [file normalize $dir/$group/$grid/[string trim $line]]
261 }
262 close $fd
263 }
264
265 # check if grid directory actually exists
266 if { ! [file isdirectory $griddir] } {
267 _log_and_puts log "Error: tests directory for grid $grid ($griddir) is missing; skipped"
268 continue
269 }
270
271 # create directory for logging test results
272 if { $logdir != "" } { file mkdir $logdir/$group/$grid }
273
274 # iterate by all tests in the grid directory
87f42a3f 275 if { [catch {glob -directory $griddir -type f {*}$casemask} testfiles] } { continue }
40093367 276 foreach casefile [lsort -dictionary $testfiles] {
87f42a3f 277 # filter out files with reserved names
40093367 278 set casename [file tail $casefile]
87f42a3f 279 if { $casename == "begin" || $casename == "end" ||
280 $casename == "parse.rules" } {
281 continue
282 }
40093367 283
284 lappend tests_list [list $dir $group $grid $casename $casefile]
285 }
286 }
287 }
288 }
289 if { [llength $tests_list] < 1 } {
290 error "Error: no tests are found, check you input arguments and variable CSF_TestScriptsPath!"
291 }
292
293 ######################################################
294 # run tests
295 ######################################################
296
297 # log command arguments and environment
9753e6de 298 lappend log "Command: testgrid $args"
299 lappend log "Host: [info hostname]"
300 lappend log "Started on: [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S}]"
301 catch {lappend log "DRAW build:\n[dversion]" }
302 lappend log "Environment:"
8a262fa1 303 foreach envar [lsort [array names env]] {
9753e6de 304 lappend log "$envar=\"$env($envar)\""
40093367 305 }
9753e6de 306 lappend log ""
40093367 307
308 set refresh_timer [clock seconds]
309 uplevel dchrono _timer reset
310 uplevel dchrono _timer start
311
312 # if parallel execution is requested, allocate thread pool
313 if { $parallel > 0 } {
314 if { ! [info exists tcl_platform(threaded)] || [catch {package require Thread}] } {
315 _log_and_puts log "Warning: Tcl package Thread is not available, running in sequential mode"
316 set parallel 0
317 } else {
318 set worker [tpool::create -minworkers $parallel -maxworkers $parallel]
319 # suspend the pool until all jobs are posted, to prevent blocking of the process
320 # of starting / processing jobs by running threads
b725d7c5 321 catch {tpool::suspend $worker}
40093367 322 if { $_tests_verbose > 0 } { _log_and_puts log "Executing tests in (up to) $parallel threads" }
9753e6de 323 # limit number of jobs in the queue by reasonable value
324 # to prevent slowdown due to unnecessary queue processing
325 set nbpooled 0
326 set nbpooled_max [expr 10 * $parallel]
327 set nbpooled_ok [expr 5 * $parallel]
40093367 328 }
329 }
330
331 # start test cases
8a262fa1 332 set userbreak 0
40093367 333 foreach test_def $tests_list {
8a262fa1 334 # check for user break
9753e6de 335 if { $userbreak || "[info commands dbreak]" == "dbreak" && [catch dbreak] } {
8a262fa1 336 set userbreak 1
337 break
338 }
339
40093367 340 set dir [lindex $test_def 0]
341 set group [lindex $test_def 1]
342 set grid [lindex $test_def 2]
343 set casename [lindex $test_def 3]
344 set casefile [lindex $test_def 4]
345
346 # command to set tests for generation of image in results directory
347 set imgdir_cmd ""
348 if { $logdir != "" } { set imgdir_cmd "set imagedir $logdir/$group/$grid" }
349
350 # prepare command file for running test case in separate instance of DRAW
351 set fd_cmd [open $logdir/$group/$grid/${casename}.tcl w]
352 puts $fd_cmd "$imgdir_cmd"
353 puts $fd_cmd "set test_image $casename"
b725d7c5 354 puts $fd_cmd "_run_test $dir $group $grid $casefile t"
5df3a117 355
40093367 356 # use dlog command to obtain complete output of the test when it is absent (i.e. since OCCT 6.6.0)
5df3a117 357 # note: this is not needed if echo is set to 1 in call to _run_test above
40093367 358 if { ! [catch {dlog get}] } {
359 puts $fd_cmd "puts \[dlog get\]"
360 } else {
361 # else try to use old-style QA_ variables to get more output...
362 set env(QA_DUMP) 1
363 set env(QA_DUP) 1
364 set env(QA_print_command) 1
365 }
5df3a117 366
40093367 367 # final 'exit' is needed when running on Linux under VirtualGl
368 puts $fd_cmd "exit"
369 close $fd_cmd
8418c617 370
371 # commant to run DRAW with a command file;
372 # note that empty string is passed as standard input to avoid possible
373 # hang-ups due to waiting for stdin of the launching process
374 set command "exec <<{} DRAWEXE -f $logdir/$group/$grid/${casename}.tcl"
375
40093367 376 # alternative method to run without temporary file; disabled as it needs too many backslashes
377# else {
8418c617 378# set command "exec <<\"\" DRAWEXE -c $imgdir_cmd\\\; set test_image $casename\\\; \
40093367 379# _run_test $dir $group $grid $casefile\\\; \
380# puts \\\[dlog get\\\]\\\; exit"
381# }
382
383 # run test case, either in parallel or sequentially
384 if { $parallel > 0 } {
385 # parallel execution
386 set job [tpool::post -nowait $worker "catch \"$command\" output; return \$output"]
387 set job_def($job) [list $logdir $dir $group $grid $casename]
9753e6de 388 incr nbpooled
389 if { $nbpooled > $nbpooled_max } {
390 _testgrid_process_jobs $worker $nbpooled_ok
391 }
40093367 392 } else {
393 # sequential execution
394 catch {eval $command} output
395 _log_test_case $output $logdir $dir $group $grid $casename log
396
397 # update summary log with requested period
398 if { $logdir != "" && $refresh > 0 && [expr [clock seconds] - $refresh_timer > $refresh] } {
399 # update and dump summary
400 _log_summarize $logdir $log
401 set refresh_timer [clock seconds]
402 }
403 }
404 }
405
406 # get results of started threads
407 if { $parallel > 0 } {
9753e6de 408 _testgrid_process_jobs $worker
40093367 409 # release thread pool
9753e6de 410 if { $nbpooled > 0 } {
411 tpool::cancel $worker [array names job_def]
412 }
413 catch {tpool::resume $worker}
40093367 414 tpool::release $worker
415 }
416
417 uplevel dchrono _timer stop
418 set time [lindex [split [uplevel dchrono _timer show] "\n"] 0]
419
8a262fa1 420 if { $userbreak } {
9753e6de 421 _log_and_puts log "*********** Stopped by user break ***********"
8a262fa1 422 set time "${time} \nNote: the process is not finished, stopped by user break!"
423 }
424
40093367 425 ######################################################
426 # output summary logs and exit
427 ######################################################
428
429 _log_summarize $logdir $log $time
430 if { $logdir != "" } {
431 puts "Detailed logs are saved in $logdir"
432 }
433 if { $logdir != "" && $xmlfile != "" } {
434 # XML output file is assumed relative to log dir unless it is absolute
435 if { [ file pathtype $xmlfile] == "relative" } {
436 set xmlfile [file normalize $logdir/$xmlfile]
437 }
438 _log_xml_summary $logdir $xmlfile $log 0
439 puts "XML summary is saved to $xmlfile"
440 }
441
442 return
443}
444
22db40eb 445# Procedure to regenerate summary log from logs of test cases
446help testsummarize {
447 Regenerate summary log in the test directory from logs of test cases.
448 This can be necessary if test grids are executed separately (e.g. on
449 different stations) or some grids have been re-executed.
450 Use: testsummarize dir
451}
452proc testsummarize {dir} {
453 global _test_case_regexp
454
455 if { ! [file isdirectory $dir] } {
456 error "Error: \"$dir\" is not a directory"
457 }
458
459 # get summary statements from all test cases in one log
9753e6de 460 set log {}
22db40eb 461
462 # to avoid huge listing of logs, first find all subdirectories and iterate
463 # by them, parsing log files in each subdirectory independently
464 foreach grid [glob -directory $dir -types d -tails */*] {
465 foreach caselog [glob -nocomplain -directory [file join $dir $grid] -types f -tails *.log] {
466 set file [file join $dir $grid $caselog]
467 set nbfound 0
468 set fd [open $file r]
469 while { [gets $fd line] >= 0 } {
470 if { [regexp $_test_case_regexp $line res grp grd cas status message] } {
471 if { "[file join $grid $caselog]" != "[file join $grp $grd ${cas}.log]" } {
472 puts "Error: $file contains status line for another test case ($line)"
473 }
9753e6de 474 lappend log $line
22db40eb 475 incr nbfound
476 }
477 }
478 close $fd
479
480 if { $nbfound != 1 } {
481 puts "Error: $file contains $nbfound status lines, expected 1"
482 }
483 }
484 }
485
486 _log_summarize $dir $log "Summary regenerated from logs at [clock format [clock seconds]]"
487 return
488}
489
cc6a292d 490# Procedure to compare results of two runs of test cases
b725d7c5 491help testdiff {
492 Compare results of two executions of tests (CPU times, ...)
22db40eb 493 Use: testdiff dir1 dir2 [groupname [gridname]] [options...]
b725d7c5 494 Where dir1 and dir2 are directories containing logs of two test runs.
495 Allowed options are:
22db40eb 496 -save filename: save resulting log in specified file (default name is
60874ff8 497 <dir1>/diff-<dir2>.log); HTML log is saved with same name
22db40eb 498 and extension .html
b725d7c5 499 -status {same|ok|all}: filter cases for comparing by their status:
500 same - only cases with same status are compared (default)
501 ok - only cases with OK status in both logs are compared
502 all - results are compared regardless of status
503 -verbose level:
504 1 - output only differences
22db40eb 505 2 - output also list of logs and directories present in one of dirs only
506 3 - (default) output also progress messages
cc6a292d 507}
508proc testdiff {dir1 dir2 args} {
509 if { "$dir1" == "$dir2" } {
510 error "Input directories are the same"
511 }
512
513 ######################################################
514 # check arguments
515 ######################################################
516
517 # treat options
22db40eb 518 set logfile [file join $dir1 "diff-[file tail $dir2].log"]
cc6a292d 519 set basename ""
520 set status "same"
521 set verbose 3
522 for {set narg 0} {$narg < [llength $args]} {incr narg} {
523 set arg [lindex $args $narg]
524
525 # log file name
526 if { $arg == "-save" } {
527 incr narg
22db40eb 528 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
cc6a292d 529 set logfile [lindex $args $narg]
530 } else {
531 error "Error: Option -save must be followed by log file name"
532 }
533 continue
534 }
535
cc6a292d 536 # status filter
537 if { $arg == "-status" } {
538 incr narg
22db40eb 539 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
cc6a292d 540 set status [lindex $args $narg]
541 } else { set status "" }
542 if { "$status" != "same" && "$status" != "all" && "$status" != "ok" } {
543 error "Error: Option -status must be followed by one of \"same\", \"all\", or \"ok\""
544 }
545 continue
546 }
547
548 # verbose level
549 if { $arg == "-verbose" } {
550 incr narg
22db40eb 551 if { $narg < [llength $args] && ! [regexp {^-} [lindex $args $narg]] } {
cc6a292d 552 set verbose [expr [lindex $args $narg]]
22db40eb 553 } else {
554 error "Error: Option -verbose must be followed by integer verbose level"
cc6a292d 555 }
556 continue
557 }
558
22db40eb 559 if { [regexp {^-} $arg] } {
cc6a292d 560 error "Error: unsupported option \"$arg\""
22db40eb 561 }
562
563 # non-option arguments form a subdirectory path
564 set basename [file join $basename $arg]
cc6a292d 565 }
566
567 # run diff procedure (recursive)
568 _test_diff $dir1 $dir2 $basename $status $verbose log
569
570 # save result to log file
571 if { "$logfile" != "" } {
9753e6de 572 _log_save $logfile [join $log "\n"]
22db40eb 573 _log_html_diff "[file rootname $logfile].html" $log $dir1 $dir2
574 puts "Log is saved to $logfile (and .html)"
cc6a292d 575 }
576
577 return
578}
579
351bbcba 580# Procedure to check data file before adding it to repository
581help testfile {
582 Check data file and prepare it for putting to test data files repository.
583 Use: testfile [filelist]
584
585 Will report if:
586 - data file (non-binary) is in DOS encoding (CR/LF)
587 - same data file (with same or another name) already exists in the repository
588 - another file with the same name already exists
589 Note that names are assumed to be case-insensitive (for Windows).
590
591 Unless the file is already in the repository, tries to load it, reports
592 the recognized file format, file size, number of faces and edges in the
60874ff8 593 loaded shape (if any), and makes snapshot (in the temporary directory).
351bbcba 594 Finally it advises whether the file should be put to public section of the
595 repository.
596}
597proc testfile {filelist} {
598 global env
599
600 # check that CSF_TestDataPath is defined
601 if { ! [info exists env(CSF_TestDataPath)] } {
602 error "Environment variable CSF_TestDataPath must be defined!"
603 }
604
605 # build registry of existing data files (name -> path) and (size -> path)
606 puts "Checking available test data files..."
607 foreach dir [_split_path $env(CSF_TestDataPath)] {
608 while {[llength $dir] != 0} {
609 set curr [lindex $dir 0]
610 set dir [lrange $dir 1 end]
611 eval lappend dir [glob -nocomplain -directory $curr -type d *]
612 foreach file [glob -nocomplain -directory $curr -type f *] {
613 set name [file tail $file]
614 set name_lower [string tolower $name]
615
616 # check that the file is not in DOS encoding
617 if { [_check_dos_encoding $file] } {
618 puts "Warning: file $file is in DOS encoding; was this intended?"
619 }
620 _check_file_format $file
621
622 # check if file with the same name is present twice or more
623 if { [info exists names($name_lower)] } {
624 puts "Error: more than one file with name $name is present in the repository:"
625 if { [_diff_files $file $names($name_lower)] } {
626 puts "(files are different by content)"
627 } else {
628 puts "(files are same by content)"
629 }
630 puts "--> $file"
631 puts "--> $names($name_lower)"
632 continue
633 }
634
635 # check if file with the same content exists
636 set size [file size $file]
637 if { [info exists sizes($size)] } {
638 foreach other $sizes($size) {
639 if { ! [_diff_files $file $other] } {
640 puts "Warning: two files with the same content found:"
641 puts "--> $file"
642 puts "--> $other"
643 }
644 }
645 }
646
647 # add the file to the registry
648 set names($name_lower) $file
649 lappend sizes($size) $file
650 }
651 }
652 }
653 if { [llength $filelist] <= 0 } { return }
654
655 # check the new files
656 set has_images f
657 puts "Checking new file(s)..."
658 foreach file $filelist {
659 # check for DOS encoding
660 if { [_check_dos_encoding $file] } {
661 puts "$file: Warning: DOS encoding detected"
662 }
663
664 set name [file tail $file]
665 set name_lower [string tolower $name]
666
667 # check for presence of the file with same name
668 if { [info exists names($name_lower)] } {
669 if { [_diff_files $file $names($name_lower)] } {
670 puts "$file: Error: name is already used by existing file\n--> $names($name_lower)"
671 } else {
672 puts "$file: OK: already in the repository \n--> $names($name_lower)"
673 continue
674 }
675 }
676
677 # check if file with the same content exists
678 set size [file size $file]
679 if { [info exists sizes($size)] } {
680 set found f
681 foreach other $sizes($size) {
682 if { ! [_diff_files $file $other] } {
683 puts "$file: OK: the same file is already present under name [file tail $other]\n--> $other"
684 set found t
685 break
686 }
687 }
688 if { $found } { continue }
689 }
690
691 # try to read the file
692 set format [_check_file_format $file]
693 if { [catch {uplevel load_data_file $file $format a}] } {
694 puts "$file: Error: Cannot read as $format file"
695 continue
696 }
697
698 # get number of faces and edges
699 set edges 0
700 set faces 0
701 set nbs [uplevel nbshapes a]
702 regexp {EDGE[ \t:]*([0-9]+)} $nbs res edges
703 regexp {FACE[ \t:]*([0-9]+)} $nbs res faces
704
705 # classify; first check file size and number of faces and edges
706 if { $size < 95000 && $faces < 20 && $edges < 100 } {
707 set dir public
708 } else {
709 set dir private
710 # check if one of names of that file corresponds to typical name for
711 # MDTV bugs or has extension .rle, this should be old model
712 if { [regexp -nocase {.*(cts|ats|pro|buc|ger|fra|usa|uki)[0-9]+.*} $name] ||
713 [regexp -nocase {[.]rle\y} $name] } {
714 set dir old
715 }
716 }
717
718 # add stats
719 puts "$file: $format size=[expr $size / 1024] KiB, nbfaces=$faces, nbedges=$edges -> $dir"
720
60874ff8 721 set tmpdir [_get_temp_dir]
722 file mkdir $tmpdir/$dir
351bbcba 723
724 # make snapshot
725 pload AISV
726 uplevel vdisplay a
727 uplevel vfit
728 uplevel vzfit
60874ff8 729 uplevel vdump $tmpdir/$dir/[file rootname [file tail $file]].png
351bbcba 730 set has_images t
731 }
732 if { $has_images } {
60874ff8 733 puts "Snapshots are saved in subdirectory [_get_temp_dir]"
351bbcba 734 }
735}
736
22db40eb 737# Procedure to locate data file for test given its name.
738# The search is performed assuming that the function is called
739# from the test case script; the search order is:
740# - subdirectory "data" of the test script (grid) folder
741# - subdirectories in environment variable CSF_TestDataPath
742# - subdirectory set by datadir command
743# If file is not found, raises Tcl error.
744proc locate_data_file {filename} {
745 global env groupname gridname casename
746
747 # check if the file is located in the subdirectory data of the script dir
748 set scriptfile [info script]
749 if { $scriptfile != "" } {
750 set path [file join [file dirname $scriptfile] data $filename]
751 if { [file exists $path] } {
752 return [file normalize $path]
753 }
754 }
755
756 # check sub-directories in paths indicated by CSF_TestDataPath
757 if { [info exists env(CSF_TestDataPath)] } {
758 foreach dir [_split_path $env(CSF_TestDataPath)] {
759 while {[llength $dir] != 0} {
760 set name [lindex $dir 0]
761 set dir [lrange $dir 1 end]
80482e01 762 # skip directories starting with dot
763 if { [regexp {^[.]} $name] } { continue }
22db40eb 764 if { [file exists $name/$filename] } {
765 return [file normalize $name/$filename]
766 }
80482e01 767 eval lappend dir [glob -nocomplain -directory $name -type d *]
22db40eb 768 }
769 }
770 }
771
772 # check current datadir
773 if { [file exists [uplevel datadir]/$filename] } {
774 return [file normalize [uplevel datadir]/$filename]
775 }
776
777 # raise error
c97067ad 778 error [join [list "File $filename could not be found" \
22db40eb 779 "(should be in paths indicated by CSF_TestDataPath environment variable, " \
780 "or in subfolder data in the script directory)"] "\n"]
781}
782
40093367 783# Internal procedure to find test case indicated by group, grid, and test case names;
784# returns:
785# - dir: path to the base directory of the tests group
786# - gridname: actual name of the grid
787# - casefile: path to the test case script
788# if no such test is found, raises error with appropriate message
789proc _get_test {group grid casename _dir _gridname _casefile} {
790 upvar $_dir dir
791 upvar $_gridname gridname
792 upvar $_casefile casefile
793
794 global env
795
796 # check that environment variable defining paths to test scripts is defined
797 if { ! [info exists env(CSF_TestScriptsPath)] ||
798 [llength $env(CSF_TestScriptsPath)] <= 0 } {
799 error "Error: Environment variable CSF_TestScriptsPath is not defined"
800 }
801
802 # iterate by all script paths
803 foreach dir [_split_path $env(CSF_TestScriptsPath)] {
804 # protection against empty paths
805 set dir [string trim $dir]
806 if { $dir == "" } { continue }
807
808 # check that directory exists
809 if { ! [file isdirectory $dir] } {
810 puts "Warning: directory $dir listed in CSF_TestScriptsPath does not exist, skipped"
811 continue
812 }
813
814 # check if test group with given name exists in this dir
815 # if not, continue to the next test dir
816 if { ! [file isdirectory $dir/$group] } { continue }
817
818 # check that grid with given name (possibly alias) exists; stop otherwise
819 set gridname $grid
820 if { ! [file isdirectory $dir/$group/$gridname] } {
821 # check if grid is named by alias rather than by actual name
822 if { [file exists $dir/$group/grids.list] } {
823 set fd [open $dir/$group/grids.list]
824 while { [gets $fd line] >= 0 } {
825 if { [regexp "\[ \t\]*\#.*" $line] } { continue }
826 if { [regexp "^$grid\[ \t\]*\(\[A-Za-z0-9_.-\]+\)\$" $line res gridname] } {
827 break
828 }
829 }
830 close $fd
831 }
832 }
833 if { ! [file isdirectory $dir/$group/$gridname] } { continue }
834
835 # get actual file name of the script; stop if it cannot be found
836 set casefile $dir/$group/$gridname/$casename
837 if { ! [file exists $casefile] } {
838 # check if this grid is aliased to another one
839 if { [file exists $dir/$group/$gridname/cases.list] } {
840 set fd [open $dir/$group/$gridname/cases.list]
841 if { [gets $fd line] >= 0 } {
842 set casefile [file normalize $dir/$group/$gridname/[string trim $line]/$casename]
843 }
844 close $fd
845 }
846 }
847 if { [file exists $casefile] } {
848 # normal return
849 return
850 }
851 }
852
853 # coming here means specified test is not found; report error
854 error [join [list "Error: test case $group / $grid / $casename is not found in paths listed in variable" \
855 "CSF_TestScriptsPath (current value is \"$env(CSF_TestScriptsPath)\")"] "\n"]
856}
857
858# Internal procedure to run test case indicated by base directory,
859# grid and grid names, and test case file path.
860# The log can be obtained by command "dlog get".
5df3a117 861proc _run_test {scriptsdir group gridname casefile echo} {
40093367 862 global env
863
864 # start timer
865 uplevel dchrono _timer reset
866 uplevel dchrono _timer start
22db40eb 867 catch {uplevel meminfo w} membase
40093367 868
869 # enable commands logging; switch to old-style mode if dlog command is not present
870 set dlog_exists 1
871 if { [catch {dlog reset}] } {
872 set dlog_exists 0
5df3a117 873 } elseif { $echo } {
874 decho on
40093367 875 } else {
876 dlog reset
877 dlog on
878 rename puts puts-saved
879 proc puts args {
880 global _tests_verbose
881
882 # log only output to stdout and stderr, not to file!
883 if {[llength $args] > 1} {
d33dea30
PK
884 set optarg [lindex $args end-1]
885 if { $optarg == "stdout" || $optarg == "stderr" || $optarg == "-newline" } {
40093367 886 dlog add [lindex $args end]
c97067ad 887 } else {
888 eval puts-saved $args
40093367 889 }
890 } else {
891 dlog add [lindex $args end]
892 }
40093367 893 }
894 }
895
40093367 896 # evaluate test case
897 if [catch {
60874ff8 898 # set variables identifying test case
40093367 899 uplevel set casename [file tail $casefile]
8418c617 900 uplevel set groupname $group
901 uplevel set gridname $gridname
c97067ad 902 uplevel set dirname $scriptsdir
40093367 903
a3ef6605 904 # set path for saving of log and images (if not yet set) to temp dir
60874ff8 905 if { ! [uplevel info exists imagedir] } {
60874ff8 906 uplevel set test_image \$casename
a3ef6605 907
908 # create subdirectory in temp named after group and grid with timestamp
909 set rootlogdir [_get_temp_dir]
910
911 set imagedir "${group}-${gridname}-${::casename}-[clock format [clock seconds] -format {%Y-%m-%dT%Hh%Mm%Ss}]"
912 set imagedir [file normalize ${rootlogdir}/$imagedir]
913
914 if { [catch {file mkdir $imagedir}] || ! [file writable $imagedir] ||
915 ! [catch {glob -directory $imagedir *}] } {
916# puts "Warning: Cannot create directory \"$imagedir\", or it is not empty; \"${rootlogdir}\" is used"
917 set imagedir $rootlogdir
918 }
919
d16b64f1 920 uplevel set imagedir \"$imagedir\"
60874ff8 921 }
922
923 # execute test scripts
40093367 924 if { [file exists $scriptsdir/$group/begin] } {
925 puts "Executing $scriptsdir/$group/begin..."; flush stdout
926 uplevel source $scriptsdir/$group/begin
927 }
928 if { [file exists $scriptsdir/$group/$gridname/begin] } {
929 puts "Executing $scriptsdir/$group/$gridname/begin..."; flush stdout
930 uplevel source $scriptsdir/$group/$gridname/begin
931 }
932
933 puts "Executing $casefile..."; flush stdout
934 uplevel source $casefile
935
936 if { [file exists $scriptsdir/$group/$gridname/end] } {
937 puts "Executing $scriptsdir/$group/$gridname/end..."; flush stdout
938 uplevel source $scriptsdir/$group/$gridname/end
939 }
940 if { [file exists $scriptsdir/$group/end] } {
941 puts "Executing $scriptsdir/$group/end..."; flush stdout
942 uplevel source $scriptsdir/$group/end
943 }
944 } res] {
945 puts "Tcl Exception: $res"
946 }
947
40093367 948 # stop logging
949 if { $dlog_exists } {
5df3a117 950 if { $echo } {
951 decho off
952 } else {
953 rename puts {}
954 rename puts-saved puts
955 dlog off
956 }
40093367 957 }
958
8418c617 959 # stop cpulimit killer if armed by the test
960 cpulimit
961
22db40eb 962 # add memory and timing info
963 set stats ""
964 if { ! [catch {uplevel meminfo w} memuse] } {
965 set stats "MEMORY DELTA: [expr ($memuse - $membase) / 1024] KiB\n"
966 }
40093367 967 uplevel dchrono _timer stop
968 set time [uplevel dchrono _timer show]
969 if [regexp -nocase {CPU user time:[ \t]*([0-9.e-]+)} $time res cpu] {
22db40eb 970 set stats "${stats}TOTAL CPU TIME: $cpu sec\n"
971 }
972 if { $dlog_exists && ! $echo } {
973 dlog add $stats
974 } else {
975 puts $stats
40093367 976 }
977}
978
979# Internal procedure to check log of test execution and decide if it passed or failed
980proc _check_log {dir group gridname casename log {_summary {}} {_html_log {}}} {
981 global env
982 if { $_summary != "" } { upvar $_summary summary }
983 if { $_html_log != "" } { upvar $_html_log html_log }
9753e6de 984 set summary {}
985 set html_log {}
40093367 986
987if [catch {
988
989 # load definition of 'bad words' indicating test failure
990 # note that rules are loaded in the order of decreasing priority (grid - group - common),
991 # thus grid rules will override group ones
992 set badwords {}
993 foreach rulesfile [list $dir/$group/$gridname/parse.rules $dir/$group/parse.rules $dir/parse.rules] {
994 if [catch {set fd [open $rulesfile r]}] { continue }
995 while { [gets $fd line] >= 0 } {
996 # skip comments and empty lines
997 if { [regexp "\[ \t\]*\#.*" $line] } { continue }
998 if { [string trim $line] == "" } { continue }
999 # extract regexp
1000 if { ! [regexp {^([^/]*)/([^/]*)/(.*)$} $line res status rexp comment] } {
1001 puts "Warning: cannot recognize parsing rule \"$line\" in file $rulesfile"
1002 continue
1003 }
1004 set status [string trim $status]
1005 if { $comment != "" } { set status "$status ([string trim $comment])" }
1006 set rexp [regsub -all {\\b} $rexp {\\y}] ;# convert regexp from Perl to Tcl style
1007 lappend badwords [list $status $rexp]
1008 }
1009 close $fd
1010 }
1011 if { [llength $badwords] <= 0 } {
1012 puts "Warning: no definition of error indicators found (check files parse.rules)"
1013 }
1014
1015 # analyse log line-by-line
1016 set todos {}
1017 set status ""
1018 foreach line [split $log "\n"] {
1019 # check if line defines specific treatment of some messages
b58dfc5e 1020 set deb_info [dversion]
1021 if [regexp -nocase {^[ \s]*TODO ([^:]*):(.*)$} $line res platforms pattern] {
1022 if { [regexp {DEBUG_} $platforms] != 1 } {
1023 if { ! [regexp -nocase {\mAll\M} $platforms] &&
1024 ! [regexp -nocase "\\m$env(os_type)\\M" $platforms] } {
1025 lappend html_log $line
1026 continue ;# TODO statement is for another platform
1027 }
1028
1029 # record TODOs that mark unstable cases
1030 if { [regexp {[\?]} $platforms] } {
1031 set todos_unstable([llength $todos]) 1
1032 }
1033
1034 lappend todos [regsub -all {\\b} [string trim $pattern] {\\y}] ;# convert regexp from Perl to Tcl style
1035 lappend html_log [_html_highlight BAD $line]
1036 continue
1037 }
1038
1039 if { [regexp "Debug mode" $deb_info] != 1 && [regexp {DEBUG_} $platforms] == 1 } {
1040 continue
40093367 1041 }
1042
b58dfc5e 1043 if { [regexp "Debug mode" $deb_info] == 1 && [regexp {DEBUG_} $platforms] == 1 } {
1044 if { ! [regexp -nocase {\mAll\M} $platforms] &&
1045 ! [regexp -nocase "\\m$env(os_type)\\M" $platforms] } {
1046 lappend html_log $line
1047 continue ;# TODO statement is for another platform
1048 }
40093367 1049
b58dfc5e 1050 # record TODOs that mark unstable cases
1051 if { [regexp {[\?]} $platforms] } {
1052 set todos_unstable([llength $todos]) 1
1053 }
1054
1055 lappend todos [regsub -all {\\b} [string trim $pattern] {\\y}] ;# convert regexp from Perl to Tcl style
1056 lappend html_log [_html_highlight BAD $line]
1057 continue
1058 }
40093367 1059 }
b58dfc5e 1060
40093367 1061 # check for presence of messages indicating test result
1062 set ismarked 0
1063 foreach bw $badwords {
1064 if { [regexp [lindex $bw 1] $line] } {
1065 # check if this is known bad case
1066 set is_known 0
1067 for {set i 0} {$i < [llength $todos]} {incr i} {
1068 if { [regexp [lindex $todos $i] $line] } {
1069 set is_known 1
1070 incr todo_count($i)
9753e6de 1071 lappend html_log [_html_highlight BAD $line]
40093367 1072 break
1073 }
1074 }
1075
1076 # if it is not in todo, define status
1077 if { ! $is_known } {
1078 set stat [lindex $bw 0 0]
9753e6de 1079 lappend html_log [_html_highlight $stat $line]
40093367 1080 if { $status == "" && $stat != "OK" && ! [regexp -nocase {^IGNOR} $stat] } {
1081 set status [lindex $bw 0]
1082 }
1083 }
1084 set ismarked 1
1085 break
1086 }
1087 }
1088 if { ! $ismarked } {
9753e6de 1089 lappend html_log $line
40093367 1090 }
1091 }
1092
1093 # check for presence of TEST COMPLETED statement
1094 if { $status == "" && ! [regexp {TEST COMPLETED} $log] } {
1095 # check whether absence of TEST COMPLETED is known problem
1096 set i [lsearch $todos "TEST INCOMPLETE"]
1097 if { $i >= 0 } {
1098 incr todo_count($i)
1099 } else {
1100 set status "FAILED (no final message is found)"
1101 }
1102 }
1103
1104 # check declared bad cases and diagnose possible improvement
1105 # (bad case declared but not detected).
1106 # Note that absence of the problem marked by TODO with question mark
1107 # (unstable) is not reported as improvement.
1108 if { $status == "" } {
1109 for {set i 0} {$i < [llength $todos]} {incr i} {
1110 if { ! [info exists todos_unstable($i)] &&
1111 (! [info exists todo_count($i)] || $todo_count($i) <= 0) } {
1112 set status "IMPROVEMENT (expected problem TODO no. [expr $i + 1] is not detected)"
1113 break;
1114 }
1115 }
1116 }
1117
1118 # report test as known bad if at least one of expected problems is found
1119 if { $status == "" && [llength [array names todo_count]] > 0 } {
1120 set status "BAD (known problem)"
1121 }
1122
1123 # report normal OK
1124 if { $status == "" } {set status "OK" }
1125
1126} res] {
1127 set status "FAILED ($res)"
1128}
1129
1130 # put final message
1131 _log_and_puts summary "CASE $group $gridname $casename: $status"
9753e6de 1132 set summary [join $summary "\n"]
1133 set html_log "[_html_highlight [lindex $status 0] $summary]\n[join $html_log \n]"
40093367 1134}
1135
1136# Auxiliary procedure putting message to both cout and log variable (list)
1137proc _log_and_puts {logvar message} {
1138 if { $logvar != "" } {
1139 upvar $logvar log
9753e6de 1140 lappend log $message
40093367 1141 }
1142 puts $message
1143}
1144
1145# Auxiliary procedure to log result on single test case
1146proc _log_test_case {output logdir dir group grid casename logvar} {
1147 upvar $logvar log
1148
1149 # check result and make HTML log
1150 _check_log $dir $group $grid $casename $output summary html_log
9753e6de 1151 lappend log $summary
40093367 1152
1153 # save log to file
1154 if { $logdir != "" } {
1155 _log_html $logdir/$group/$grid/$casename.html $html_log "Test $group $grid $casename"
1156 _log_save $logdir/$group/$grid/$casename.log "$output\n$summary" "Test $group $grid $casename"
1157 }
1158}
1159
1160# Auxiliary procedure to save log to file
1161proc _log_save {file log {title {}}} {
1162 # create missing directories as needed
1163 catch {file mkdir [file dirname $file]}
1164
1165 # try to open a file
1166 if [catch {set fd [open $file w]} res] {
1167 error "Error saving log file $file: $res"
1168 }
1169
1170 # dump log and close
1171 puts $fd "$title\n"
1172 puts $fd $log
1173 close $fd
1174 return
1175}
1176
22db40eb 1177# Auxiliary procedure to make a (relative if possible) URL to a file for
1178# inclusion a reference in HTML log
1179proc _make_url {htmldir file} {
1180 set htmlpath [file split [file normalize $htmldir]]
1181 set filepath [file split [file normalize $file]]
1182 for {set i 0} {$i < [llength $htmlpath]} {incr i} {
1183 if { "[lindex $htmlpath $i]" != "[lindex $filepath $i]" } {
1184 if { $i == 0 } { break }
1185 return "[string repeat "../" [expr [llength $htmlpath] - $i - 1]][eval file join [lrange $filepath $i end]]"
1186 }
1187 }
1188
1189 # if relative path could not be made, return full file URL
1190 return "file://[file normalize $file]"
1191}
1192
40093367 1193# Auxiliary procedure to save log to file
1194proc _log_html {file log {title {}}} {
1195 # create missing directories as needed
1196 catch {file mkdir [file dirname $file]}
1197
1198 # try to open a file
1199 if [catch {set fd [open $file w]} res] {
1200 error "Error saving log file $file: $res"
1201 }
1202
1203 # print header
22db40eb 1204 puts $fd "<html><head><title>$title</title></head><body><h1>$title</h1>"
40093367 1205
ffc26091 1206 # add images if present; these should have either PNG, GIF, or JPG extension,
1207 # and start with name of the test script, with optional suffix separated
1208 # by underscore or dash
40093367 1209 set imgbasename [file rootname [file tail $file]]
ffc26091 1210 foreach img [lsort [glob -nocomplain -directory [file dirname $file] -tails \
1211 ${imgbasename}.gif ${imgbasename}.png ${imgbasename}.jpg \
1212 ${imgbasename}_*.gif ${imgbasename}_*.png ${imgbasename}_*.jpg \
1213 ${imgbasename}-*.gif ${imgbasename}-*.png ${imgbasename}-*.jpg]] {
3b21f8ae 1214 puts $fd "<p>[file tail $img]<br><img src=\"$img\"/><p>"
40093367 1215 }
1216
b725d7c5 1217 # print log body, trying to add HTML links to script files on lines like
1218 # "Executing <filename>..."
40093367 1219 puts $fd "<pre>"
b725d7c5 1220 foreach line [split $log "\n"] {
1221 if { [regexp {Executing[ \t]+([a-zA-Z0-9._/:-]+[^.])} $line res script] &&
1222 [file exists $script] } {
22db40eb 1223 set line [regsub $script $line "<a href=\"[_make_url $file $script]\">$script</a>"]
b725d7c5 1224 }
1225 puts $fd $line
1226 }
40093367 1227 puts $fd "</pre></body></html>"
1228
1229 close $fd
1230 return
1231}
1232
1233# Auxiliary method to make text with HTML highlighting according to status
1234proc _html_color {status} {
1235 # choose a color for the cell according to result
1236 if { $status == "OK" } {
1237 return lightgreen
1238 } elseif { [regexp -nocase {^FAIL} $status] } {
1239 return red
1240 } elseif { [regexp -nocase {^BAD} $status] } {
1241 return yellow
1242 } elseif { [regexp -nocase {^IMP} $status] } {
1243 return orange
1244 } elseif { [regexp -nocase {^SKIP} $status] } {
1245 return gray
1246 } elseif { [regexp -nocase {^IGNOR} $status] } {
1247 return gray
1248 } else {
1249 puts "Warning: no color defined for status $status, using red as if FAILED"
1250 return red
1251 }
1252}
1253
1254# Format text line in HTML to be colored according to the status
1255proc _html_highlight {status line} {
1256 return "<table><tr><td bgcolor=\"[_html_color $status]\">$line</td></tr></table>"
1257}
1258
1259# Internal procedure to generate HTML page presenting log of the tests
1260# execution in tabular form, with links to reports on individual cases
1261proc _log_html_summary {logdir log totals regressions improvements total_time} {
1262 global _test_case_regexp
1263
9753e6de 1264 # create missing directories as needed
1265 file mkdir $logdir
40093367 1266
1267 # try to open a file and start HTML
1268 if [catch {set fd [open $logdir/summary.html w]} res] {
1269 error "Error creating log file: $res"
1270 }
1271
1272 # write HRML header, including command to refresh log if still in progress
1273 puts $fd "<html><head>"
1274 puts $fd "<title>Tests summary</title>"
1275 if { $total_time == "" } {
1276 puts $fd "<meta http-equiv=\"refresh\" content=\"10\">"
1277 }
1278 puts $fd "<meta http-equiv=\"pragma\" content=\"NO-CACHE\">"
1279 puts $fd "</head><body>"
1280
1281 # put summary
1282 set legend(OK) "Test passed OK"
1283 set legend(FAILED) "Test failed (regression)"
1284 set legend(BAD) "Known problem"
1285 set legend(IMPROVEMENT) "Possible improvement (expected problem not detected)"
1286 set legend(SKIPPED) "Test skipped due to lack of data file"
1287 puts $fd "<h1>Summary</h1><table>"
1288 foreach nbstat $totals {
1289 set status [lindex $nbstat 1]
1290 if { [info exists legend($status)] } {
1291 set comment $legend($status)
1292 } else {
1293 set comment "User-defined status"
1294 }
1295 puts $fd "<tr><td align=\"right\">[lindex $nbstat 0]</td><td bgcolor=\"[_html_color $status]\">$status</td><td>$comment</td></tr>"
1296 }
1297 puts $fd "</table>"
1298
1299 # time stamp and elapsed time info
1300 if { $total_time != "" } {
8a262fa1 1301 puts $fd "<p>Generated on [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S}] on [info hostname]\n<p>"
1302 puts $fd [join [split $total_time "\n"] "<p>"]
40093367 1303 } else {
1304 puts $fd "<p>NOTE: This is intermediate summary; the tests are still running! This page will refresh automatically until tests are finished."
1305 }
1306
1307 # print regressions and improvements
1308 foreach featured [list $regressions $improvements] {
1309 if { [llength $featured] <= 1 } { continue }
1310 set status [string trim [lindex $featured 0] { :}]
1311 puts $fd "<h2>$status</h2>"
1312 puts $fd "<table>"
1313 set groupgrid ""
1314 foreach test [lrange $featured 1 end] {
1315 if { ! [regexp {^(.*)\s+([\w.]+)$} $test res gg name] } {
1316 set gg UNKNOWN
1317 set name "Error building short list; check details"
1318 }
1319 if { $gg != $groupgrid } {
1320 if { $groupgrid != "" } { puts $fd "</tr>" }
1321 set groupgrid $gg
1322 puts $fd "<tr><td>$gg</td>"
1323 }
1324 puts $fd "<td bgcolor=\"[_html_color $status]\"><a href=\"[regsub -all { } $gg /]/${name}.html\">$name</a></td>"
1325 }
1326 if { $groupgrid != "" } { puts $fd "</tr>" }
1327 puts $fd "</table>"
1328 }
1329
9753e6de 1330 # put detailed log with TOC
1331 puts $fd "<hr><h1>Details</h1>"
1332 puts $fd "<div style=\"float:right; padding: 10px; border-style: solid; border-color: blue; border-width: 2px;\">"
40093367 1333
1334 # process log line-by-line
1335 set group {}
1336 set letter {}
9753e6de 1337 set body {}
1338 foreach line [lsort -dictionary $log] {
40093367 1339 # check that the line is case report in the form "CASE group grid name: result (explanation)"
1340 if { ! [regexp $_test_case_regexp $line res grp grd casename result message] } {
1341 continue
1342 }
1343
1344 # start new group
1345 if { $grp != $group } {
9753e6de 1346 if { $letter != "" } { lappend body "</tr></table>" }
40093367 1347 set letter {}
1348 set group $grp
1349 set grid {}
9753e6de 1350 puts $fd "<a href=\"#$group\">$group</a><br>"
1351 lappend body "<h2><a name=\"$group\">Group $group</a></h2>"
40093367 1352 }
1353
1354 # start new grid
1355 if { $grd != $grid } {
9753e6de 1356 if { $letter != "" } { lappend body "</tr></table>" }
40093367 1357 set letter {}
1358 set grid $grd
9753e6de 1359 puts $fd "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#$group-$grid\">$grid</a><br>"
1360 lappend body "<h2><a name=\"$group-$grid\">Grid $group $grid</a></h2>"
40093367 1361 }
1362
1363 # check if test case name is <letter><digit>;
1364 # if not, set alnum to period "." to recognize non-standard test name
9753e6de 1365 if { ! [regexp {\A([A-Za-z]{1,2})([0-9]{1,2})\Z} $casename res alnum number] &&
1366 ! [regexp {\A([A-Za-z0-9]+)_([0-9]+)\Z} $casename res alnum number] } {
1367 set alnum $casename
40093367 1368 }
1369
1370 # start new row when letter changes or for non-standard names
1371 if { $alnum != $letter || $alnum == "." } {
1372 if { $letter != "" } {
9753e6de 1373 lappend body "</tr><tr>"
40093367 1374 } else {
9753e6de 1375 lappend body "<table><tr>"
40093367 1376 }
1377 set letter $alnum
1378 }
1379
9753e6de 1380 lappend body "<td bgcolor=\"[_html_color $result]\"><a href=\"$group/$grid/${casename}.html\">$casename</a></td>"
40093367 1381 }
9753e6de 1382 puts $fd "</div>\n[join $body "\n"]</tr></table>"
40093367 1383
1384 # add remaining lines of log as plain text
1385 puts $fd "<h2>Plain text messages</h2>\n<pre>"
9753e6de 1386 foreach line $log {
40093367 1387 if { ! [regexp $_test_case_regexp $line] } {
1388 puts $fd "$line"
1389 }
1390 }
1391 puts $fd "</pre>"
1392
1393 # close file and exit
1394 puts $fd "</body>"
1395 close $fd
1396 return
1397}
1398
1399# Procedure to dump summary logs of tests
1400proc _log_summarize {logdir log {total_time {}}} {
1401
1402 # sort log records alphabetically to have the same behavior on Linux and Windows
1403 # (also needed if tests are run in parallel)
9753e6de 1404 set loglist [lsort -dictionary $log]
40093367 1405
1406 # classify test cases by status
1407 foreach line $loglist {
1408 if { [regexp {^CASE ([^:]*): ([[:alnum:]]+).*$} $line res caseid status] } {
1409 lappend stat($status) $caseid
1410 }
1411 }
1412 set totals {}
1413 set improvements {Improvements:}
1414 set regressions {Failed:}
1415 if { [info exists stat] } {
1416 foreach status [lsort [array names stat]] {
1417 lappend totals [list [llength $stat($status)] $status]
1418
1419 # separately count improvements (status starting with IMP) and regressions (all except IMP, OK, BAD, and SKIP)
1420 if { [regexp -nocase {^IMP} $status] } {
1421 eval lappend improvements $stat($status)
1422 } elseif { $status != "OK" && ! [regexp -nocase {^BAD} $status] && ! [regexp -nocase {^SKIP} $status] } {
1423 eval lappend regressions $stat($status)
1424 }
1425 }
1426 }
1427
1428 # if time is specified, add totals
1429 if { $total_time != "" } {
1430 if { [llength $improvements] > 1 } {
1431 _log_and_puts log [join $improvements "\n "]
1432 }
1433 if { [llength $regressions] > 1 } {
1434 _log_and_puts log [join $regressions "\n "]
1435 }
1436 if { [llength $improvements] == 1 && [llength $regressions] == 1 } {
1437 _log_and_puts log "No regressions"
1438 }
1439 _log_and_puts log "Total cases: [join $totals {, }]"
1440 _log_and_puts log $total_time
1441 }
1442
1443 # save log to files
1444 if { $logdir != "" } {
1445 _log_html_summary $logdir $log $totals $regressions $improvements $total_time
9753e6de 1446 _log_save $logdir/tests.log [join $log "\n"] "Tests summary"
40093367 1447 }
1448
1449 return
1450}
1451
1452# Internal procedure to generate XML log in JUnit style, for further
1453# consumption by Jenkins or similar systems.
1454#
1455# The output is intended to conform to XML schema supported by Jenkins found at
1456# https://svn.jenkins-ci.org/trunk/hudson/dtkit/dtkit-format/dtkit-junit-model/src/main/resources/com/thalesgroup/dtkit/junit/model/xsd/junit-4.xsd
1457#
1458# The mapping of the fields is inspired by annotated schema of Apache Ant JUnit XML format found at
1459# http://windyroad.org/dl/Open%20Source/JUnit.xsd
1460proc _log_xml_summary {logdir filename log include_cout} {
1461 global _test_case_regexp
1462
1463 catch {file mkdir [file dirname $filename]}
1464
1465 # try to open a file and start XML
1466 if [catch {set fd [open $filename w]} res] {
1467 error "Error creating XML summary file $filename: $res"
1468 }
1469 puts $fd "<?xml version='1.0' encoding='utf-8'?>"
1470 puts $fd "<testsuites>"
1471
1472 # prototype for command to generate test suite tag
1473 set time_and_host "timestamp=\"[clock format [clock seconds] -format {%Y-%m-%dT%H:%M:%S}]\" hostname=\"[info hostname]\""
1474 set cmd_testsuite {puts $fd "<testsuite name=\"$group $grid\" tests=\"$nbtests\" failures=\"$nbfail\" errors=\"$nberr\" time=\"$time\" skipped=\"$nbskip\" $time_and_host>\n$testcases\n</testsuite>\n"}
1475
1476 # sort log and process it line-by-line
1477 set group {}
9753e6de 1478 foreach line [lsort -dictionary $log] {
40093367 1479 # check that the line is case report in the form "CASE group grid name: result (explanation)"
1480 if { ! [regexp $_test_case_regexp $line res grp grd casename result message] } {
1481 continue
1482 }
1483 set message [string trim $message " \t\r\n()"]
1484
1485 # start new testsuite for each grid
1486 if { $grp != $group || $grd != $grid } {
1487
1488 # write previous test suite
1489 if [info exists testcases] { eval $cmd_testsuite }
1490
1491 set testcases {}
1492 set nbtests 0
1493 set nberr 0
1494 set nbfail 0
1495 set nbskip 0
1496 set time 0.
1497
1498 set group $grp
1499 set grid $grd
1500 }
1501
1502 incr nbtests
1503
1504 # parse test log and get its CPU time
1505 set testout {}
1506 set add_cpu {}
1507 if { [catch {set fdlog [open $logdir/$group/$grid/${casename}.log r]} ret] } {
1508 puts "Error: cannot open $logdir/$group/$grid/${casename}.log: $ret"
1509 } else {
1510 while { [gets $fdlog logline] >= 0 } {
1511 if { $include_cout } {
1512 set testout "$testout$logline\n"
1513 }
1514 if [regexp -nocase {TOTAL CPU TIME:\s*([\d.]+)\s*sec} $logline res cpu] {
1515 set add_cpu " time=\"$cpu\""
1516 set time [expr $time + $cpu]
1517 }
1518 }
1519 close $fdlog
1520 }
1521 if { ! $include_cout } {
1522 set testout "$line\n"
1523 }
1524
1525 # record test case with its output and status
1526 # Mapping is: SKIPPED, BAD, and OK to OK, all other to failure
1527 set testcases "$testcases\n <testcase name=\"$casename\"$add_cpu status=\"$result\">\n"
1528 set testcases "$testcases\n <system-out>\n$testout </system-out>"
1529 if { $result != "OK" } {
1530 if { [regexp -nocase {^SKIP} $result] } {
1531 incr nberr
1532 set testcases "$testcases\n <error name=\"$result\" message=\"$message\"/>"
1533 } elseif { [regexp -nocase {^BAD} $result] } {
1534 incr nbskip
1535 set testcases "$testcases\n <skipped>$message</skipped>"
1536 } else {
1537 incr nbfail
1538 set testcases "$testcases\n <failure name=\"$result\" message=\"$message\"/>"
1539 }
1540 }
1541 set testcases "$testcases\n </testcase>"
1542 }
1543
1544 # write last test suite
1545 if [info exists testcases] { eval $cmd_testsuite }
1546
1547 # the end
1548 puts $fd "</testsuites>"
1549 close $fd
1550 return
1551}
1552
1553# define custom platform name
1554proc _tests_platform_def {} {
1555 global env tcl_platform
1556
1557 if [info exists env(os_type)] { return }
1558
1559 set env(os_type) $tcl_platform(platform)
1560
1561 # use detailed mapping for various versions of Lunix
1562 # (note that mapping is rather non-uniform, for historical reasons)
d9e8bb08 1563 if { $tcl_platform(os) == "Linux" && ! [catch {exec cat /etc/issue} issue] } {
40093367 1564 if { [regexp {Mandriva[ \tA-Za-z]+([0-9]+)} $issue res num] } {
1565 set env(os_type) Mandriva$num
1566 } elseif { [regexp {Red Hat[ \tA-Za-z]+([0-9]+)} $issue res num] } {
1567 set env(os_type) RedHat$num
1568 } elseif { [regexp {Debian[ \tA-Za-z/]+([0-9]+)[.]([0-9]+)} $issue res num subnum] } {
1569 set env(os_type) Debian$num$subnum
1570 } elseif { [regexp {CentOS[ \tA-Za-z]+([0-9]+)[.]([0-9]+)} $issue res num subnum] } {
1571 set env(os_type) CentOS$num$subnum
1572 } elseif { [regexp {Scientific[ \tA-Za-z]+([0-9]+)[.]([0-9]+)} $issue res num subnum] } {
1573 set env(os_type) SL$num$subnum
1574 } elseif { [regexp {Fedora Core[ \tA-Za-z]+([0-9]+)} $issue res num] } {
1575 set env(os_type) FedoraCore$num
1576 }
1577 if { [exec uname -m] == "x86_64" } {
1578 set env(os_type) "$env(os_type)-64"
1579 }
d9e8bb08 1580 } elseif { $tcl_platform(os) == "Darwin" } {
1581 set env(os_type) MacOS
40093367 1582 }
1583}
1584_tests_platform_def
1585
1586# Auxiliary procedure to split path specification (usually defined by
1587# environment variable) into list of directories or files
1588proc _split_path {pathspec} {
1589 global tcl_platform
1590
1591 # first replace all \ (which might occur on Windows) by /
1592 regsub -all "\\\\" $pathspec "/" pathspec
1593
1594 # split path by platform-specific separator
1595 return [split $pathspec [_path_separator]]
1596}
1597
1598# Auxiliary procedure to define platform-specific separator for directories in
1599# path specification
1600proc _path_separator {} {
1601 global tcl_platform
1602
1603 # split path by platform-specific separator
1604 if { $tcl_platform(platform) == "windows" } {
1605 return ";"
1606 } else {
1607 return ":"
1608 }
1609}
1610
cc6a292d 1611# Procedure to make a diff and common of two lists
1612proc _list_diff {list1 list2 _in1 _in2 _common} {
1613 upvar $_in1 in1
1614 upvar $_in2 in2
1615 upvar $_common common
1616
1617 set in1 {}
1618 set in2 {}
1619 set common {}
1620 foreach item $list1 {
1621 if { [lsearch -exact $list2 $item] >= 0 } {
1622 lappend common $item
1623 } else {
1624 lappend in1 $item
1625 }
1626 }
1627 foreach item $list2 {
1628 if { [lsearch -exact $common $item] < 0 } {
1629 lappend in2 $item
1630 }
1631 }
1632 return
1633}
1634
1635# procedure to load a file to Tcl string
1636proc _read_file {filename} {
1637 set fd [open $filename r]
1638 set result [read -nonewline $fd]
1639 close $fd
1640 return $result
1641}
1642
22db40eb 1643# procedure to construct name for the mage diff file
1644proc _diff_img_name {dir1 dir2 casepath imgfile} {
1645 return [file join $dir1 $casepath "diff-[file tail $dir2]-$imgfile"]
1646}
1647
cc6a292d 1648# Procedure to compare results of two runs of test cases
1649proc _test_diff {dir1 dir2 basename status verbose _logvar {_statvar ""}} {
1650 upvar $_logvar log
1651
22db40eb 1652 # make sure to load diffimage command
1653 uplevel pload VISUALIZATION
1654
cc6a292d 1655 # prepare variable (array) for collecting statistics
1656 if { "$_statvar" != "" } {
1657 upvar $_statvar stat
1658 } else {
1659 set stat(cpu1) 0
1660 set stat(cpu2) 0
22db40eb 1661 set stat(mem1) 0
1662 set stat(mem2) 0
cc6a292d 1663 set log {}
1664 }
1665
1666 # first check subdirectories
1667 set path1 [file join $dir1 $basename]
1668 set path2 [file join $dir2 $basename]
1669 set list1 [glob -directory $path1 -types d -tails -nocomplain *]
1670 set list2 [glob -directory $path2 -types d -tails -nocomplain *]
1671 if { [llength $list1] >0 || [llength $list2] > 0 } {
1672 _list_diff $list1 $list2 in1 in2 common
1673 if { "$verbose" > 1 } {
1674 if { [llength $in1] > 0 } { _log_and_puts log "Only in $path1: $in1" }
1675 if { [llength $in2] > 0 } { _log_and_puts log "Only in $path2: $in2" }
1676 }
1677 foreach subdir $common {
1678 if { "$verbose" > 2 } {
1679 _log_and_puts log "Checking [file join $basename $subdir]"
1680 }
1681 _test_diff $dir1 $dir2 [file join $basename $subdir] $status $verbose log stat
1682 }
1683 } else {
1684 # check log files (only if directory has no subdirs)
1685 set list1 [glob -directory $path1 -types f -tails -nocomplain *.log]
1686 set list2 [glob -directory $path2 -types f -tails -nocomplain *.log]
1687 _list_diff $list1 $list2 in1 in2 common
1688 if { "$verbose" > 1 } {
1689 if { [llength $in1] > 0 } { _log_and_puts log "Only in $path1: $in1" }
1690 if { [llength $in2] > 0 } { _log_and_puts log "Only in $path2: $in2" }
1691 }
1692 foreach logfile $common {
1693 # load two logs
1694 set log1 [_read_file [file join $dir1 $basename $logfile]]
1695 set log2 [_read_file [file join $dir2 $basename $logfile]]
22db40eb 1696 set casename [file rootname $logfile]
cc6a292d 1697
1698 # check execution statuses
1699 set status1 UNDEFINED
1700 set status2 UNDEFINED
1701 if { ! [regexp {CASE [^:]*:\s*([\w]+)} $log1 res1 status1] ||
1702 ! [regexp {CASE [^:]*:\s*([\w]+)} $log2 res2 status2] ||
1703 "$status1" != "$status2" } {
22db40eb 1704 _log_and_puts log "STATUS [split $basename /] $casename: $status1 / $status2"
cc6a292d 1705
1706 # if test statuses are different, further comparison makes
1707 # no sense unless explicitly requested
1708 if { "$status" != "all" } {
1709 continue
1710 }
1711 }
1712 if { "$status" == "ok" && "$status1" != "OK" } {
1713 continue
1714 }
1715
1716 # check CPU times
1717 set cpu1 UNDEFINED
1718 set cpu2 UNDEFINED
1719 if { [regexp {TOTAL CPU TIME:\s*([\d.]+)} $log1 res1 cpu1] &&
1720 [regexp {TOTAL CPU TIME:\s*([\d.]+)} $log2 res1 cpu2] } {
1721 set stat(cpu1) [expr $stat(cpu1) + $cpu1]
1722 set stat(cpu2) [expr $stat(cpu2) + $cpu2]
1723
1724 # compare CPU times with 10% precision (but not less 0.5 sec)
1725 if { [expr abs ($cpu1 - $cpu2) > 0.5 + 0.05 * abs ($cpu1 + $cpu2)] } {
22db40eb 1726 _log_and_puts log "CPU [split $basename /] $casename: $cpu1 / $cpu2"
cc6a292d 1727 }
1728 }
22db40eb 1729
1730 # check memory delta
1731 set mem1 UNDEFINED
1732 set mem2 UNDEFINED
1733 if { [regexp {MEMORY DELTA:\s*([\d.]+)} $log1 res1 mem1] &&
1734 [regexp {MEMORY DELTA:\s*([\d.]+)} $log2 res1 mem2] } {
1735 set stat(mem1) [expr $stat(mem1) + $mem1]
1736 set stat(mem2) [expr $stat(mem2) + $mem2]
1737
1738 # compare memory usage with 10% precision (but not less 16 KiB)
1739 if { [expr abs ($mem1 - $mem2) > 16 + 0.05 * abs ($mem1 + $mem2)] } {
1740 _log_and_puts log "MEMORY [split $basename /] $casename: $mem1 / $mem2"
1741 }
1742 }
1743
1744 # check images
1745 set imglist1 [glob -directory $path1 -types f -tails -nocomplain $casename*.{png,gif}]
1746 set imglist2 [glob -directory $path2 -types f -tails -nocomplain $casename*.{png,gif}]
1747 _list_diff $imglist1 $imglist2 imgin1 imgin2 imgcommon
1748 if { "$verbose" > 1 } {
1749 if { [llength $imgin1] > 0 } { _log_and_puts log "Only in $path1: $imgin1" }
1750 if { [llength $imgin2] > 0 } { _log_and_puts log "Only in $path2: $imgin2" }
1751 }
1752 foreach imgfile $imgcommon {
1753# if { $verbose > 1 } { _log_and_puts log "Checking [split basename /] $casename: $imgfile" }
1754 set diffile [_diff_img_name $dir1 $dir2 $basename $imgfile]
1755 if { [catch {diffimage [file join $dir1 $basename $imgfile] \
1756 [file join $dir2 $basename $imgfile] \
1757 0 0 0 $diffile} diff] } {
1758 _log_and_puts log "IMAGE [split $basename /] $casename: $imgfile cannot be compared"
1759 file delete -force $diffile ;# clean possible previous result of diffimage
1760 } elseif { $diff != 0 } {
1761 _log_and_puts log "IMAGE [split $basename /] $casename: $imgfile differs"
1762 } else {
1763 file delete -force $diffile ;# clean useless artifact of diffimage
1764 }
1765 }
cc6a292d 1766 }
1767 }
1768
1769 if { "$_statvar" == "" } {
22db40eb 1770 _log_and_puts log "Total MEMORY difference: $stat(mem1) / $stat(mem2)"
cc6a292d 1771 _log_and_puts log "Total CPU difference: $stat(cpu1) / $stat(cpu2)"
1772 }
1773}
b725d7c5 1774
22db40eb 1775# Auxiliary procedure to save log of results comparison to file
1776proc _log_html_diff {file log dir1 dir2} {
1777 # create missing directories as needed
1778 catch {file mkdir [file dirname $file]}
1779
1780 # try to open a file
1781 if [catch {set fd [open $file w]} res] {
1782 error "Error saving log file $file: $res"
1783 }
1784
1785 # print header
1786 puts $fd "<html><head><title>Diff $dir1 vs. $dir2</title></head><body>"
1787 puts $fd "<h1>Comparison of test results: $dir1 vs. $dir2</h1>"
1788
1789 # print log body, trying to add HTML links to script files on lines like
1790 # "Executing <filename>..."
1791 puts $fd "<pre>"
1792 set logpath [file split [file normalize $file]]
9753e6de 1793 foreach line $log {
22db40eb 1794 puts $fd $line
1795
1796 if { [regexp {IMAGE[ \t]+([^:]+):[ \t]+([A-Za-z0-9_.-]+)} $line res case img] } {
1797 if { [catch {eval file join "" [lrange $case 0 end-1]} gridpath] } {
1798 # note: special handler for the case if test grid directoried are compared directly
1799 set gridpath ""
1800 }
1801 set img1 "<img src=\"[_make_url $file [file join $dir1 $gridpath $img]]\">"
1802 set img2 "<img src=\"[_make_url $file [file join $dir2 $gridpath $img]]\">"
1803
1804 set difffile [_diff_img_name $dir1 $dir2 $gridpath $img]
1805 if { [file exists $difffile] } {
1806 set imgd "<img src=\"[_make_url $file $difffile]\">"
1807 } else {
1808 set imgd "N/A"
1809 }
1810
1811 puts $fd "<table><tr><th>[file tail $dir1]</th><th>[file tail $dir2]</th><th>Different pixels</th></tr>"
1812 puts $fd "<tr><td>$img1</td><td>$img2</td><td>$imgd</td></tr></table>"
1813 }
1814 }
1815 puts $fd "</pre></body></html>"
1816
1817 close $fd
1818 return
1819}
1820
b725d7c5 1821# get number of CPUs on the system
1822proc _get_nb_cpus {} {
1823 global tcl_platform env
1824
1825 if { "$tcl_platform(platform)" == "windows" } {
1826 # on Windows, take the value of the environment variable
1827 if { [info exists env(NUMBER_OF_PROCESSORS)] &&
1828 ! [catch {expr $env(NUMBER_OF_PROCESSORS) > 0} res] && $res >= 0 } {
1829 return $env(NUMBER_OF_PROCESSORS)
1830 }
1831 } elseif { "$tcl_platform(os)" == "Linux" } {
1832 # on Linux, take number of logical processors listed in /proc/cpuinfo
1833 if { [catch {open "/proc/cpuinfo" r} fd] } {
1834 return 0 ;# should never happen, but...
1835 }
1836 set nb 0
1837 while { [gets $fd line] >= 0 } {
1838 if { [regexp {^processor[ \t]*:} $line] } {
1839 incr nb
1840 }
1841 }
1842 close $fd
1843 return $nb
1844 } elseif { "$tcl_platform(os)" == "Darwin" } {
1845 # on MacOS X, call sysctl command
1846 if { ! [catch {exec sysctl hw.ncpu} ret] &&
1847 [regexp {^hw[.]ncpu[ \t]*:[ \t]*([0-9]+)} $ret res nb] } {
1848 return $nb
1849 }
1850 }
1851
1852 # if cannot get good value, return 0 as default
1853 return 0
1854}
351bbcba 1855
1856# check two files for difference
1857proc _diff_files {file1 file2} {
1858 set fd1 [open $file1 "r"]
1859 set fd2 [open $file2 "r"]
1860
1861 set differ f
1862 while {! $differ} {
1863 set nb1 [gets $fd1 line1]
1864 set nb2 [gets $fd2 line2]
1865 if { $nb1 != $nb2 } { set differ t; break }
1866 if { $nb1 < 0 } { break }
1867 if { [string compare $line1 $line2] } {
1868 set differ t
1869 }
1870 }
1871
1872 close $fd1
1873 close $fd2
1874
1875 return $differ
1876}
1877
1878# Check if file is in DOS encoding.
1879# This check is done by presence of \r\n combination at the end of the first
1880# line (i.e. prior to any other \n symbol).
1881# Note that presence of non-ascii symbols typically used for recognition
1882# of binary files is not suitable since some IGES and STEP files contain
1883# non-ascii symbols.
1884# Special check is added for PNG files which contain \r\n in the beginning.
1885proc _check_dos_encoding {file} {
1886 set fd [open $file rb]
1887 set isdos f
1888 if { [gets $fd line] && [regexp {.*\r$} $line] &&
1889 ! [regexp {^.PNG} $line] } {
1890 set isdos t
1891 }
1892 close $fd
1893 return $isdos
1894}
1895
1896# procedure to recognize format of a data file by its first symbols (for OCCT
1897# BREP and geometry DRAW formats, IGES, and STEP) and extension (all others)
1898proc _check_file_format {file} {
1899 set fd [open $file rb]
1900 set line [read $fd 1024]
1901 close $fd
1902
1903 set warn f
1904 set ext [file extension $file]
1905 set format unknown
1906 if { [regexp {^DBRep_DrawableShape} $line] } {
1907 set format BREP
1908 if { "$ext" != ".brep" && "$ext" != ".rle" &&
1909 "$ext" != ".draw" && "$ext" != "" } {
1910 set warn t
1911 }
1912 } elseif { [regexp {^DrawTrSurf_} $line] } {
1913 set format DRAW
1914 if { "$ext" != ".rle" &&
1915 "$ext" != ".draw" && "$ext" != "" } {
1916 set warn t
1917 }
1918 } elseif { [regexp {^[ \t]*ISO-10303-21} $line] } {
1919 set format STEP
1920 if { "$ext" != ".step" && "$ext" != ".stp" } {
1921 set warn t
1922 }
1923 } elseif { [regexp {^.\{72\}S[0 ]\{6\}1} $line] } {
1924 set format IGES
1925 if { "$ext" != ".iges" && "$ext" != ".igs" } {
1926 set warn t
1927 }
1928 } elseif { "$ext" == ".igs" } {
1929 set format IGES
1930 } elseif { "$ext" == ".stp" } {
1931 set format STEP
1932 } else {
1933 set format [string toupper [string range $ext 1 end]]
1934 }
1935
1936 if { $warn } {
1937 puts "$file: Warning: extension ($ext) does not match format ($format)"
1938 }
1939
1940 return $format
1941}
1942
1943# procedure to load file knowing its format
1944proc load_data_file {file format shape} {
1945 switch $format {
1946 BREP { uplevel restore $file $shape }
808b3f9f 1947 DRAW { uplevel restore $file $shape }
351bbcba 1948 IGES { pload XSDRAW; uplevel igesbrep $file $shape * }
1949 STEP { pload XSDRAW; uplevel stepread $file __a *; uplevel renamevar __a_1 $shape }
1950 STL { pload XSDRAW; uplevel readstl $shape $file }
1951 default { error "Cannot read $format file $file" }
1952 }
1953}
60874ff8 1954
1955# procedure to get name of temporary directory,
1956# ensuring it is existing and writeable
1957proc _get_temp_dir {} {
723754e2 1958 global env tcl_platform
60874ff8 1959
1960 # check typical environment variables
1961 foreach var {TempDir Temp Tmp} {
1962 # check different case
1963 foreach name [list [string toupper $var] $var [string tolower $var]] {
1964 if { [info exists env($name)] && [file isdirectory $env($name)] &&
1965 [file writable $env($name)] } {
1966 return [regsub -all {\\} $env($name) /]
1967 }
1968 }
1969 }
1970
1971 # check platform-specific locations
1972 set fallback tmp
1973 if { "$tcl_platform(platform)" == "windows" } {
1974 set paths "c:/TEMP c:/TMP /TEMP /TMP"
1975 if { [info exists env(HOMEDRIVE)] && [info exists env(HOMEPATH)] } {
1976 set fallback [regsub -all {\\} "$env(HOMEDRIVE)$(HOMEPATH)/tmp" /]
1977 }
1978 } else {
1979 set paths "/tmp /var/tmp /usr/tmp"
1980 if { [info exists env(HOME)] } {
1981 set fallback "$env(HOME)/tmp"
1982 }
1983 }
1984 foreach dir $paths {
723754e2 1985 if { [file isdirectory $dir] && [file writable $dir] } {
60874ff8 1986 return $dir
1987 }
1988 }
1989
1990 # fallback case: use subdir /tmp of home or current dir
1991 file mkdir $fallback
1992 return $fallback
1993}
9753e6de 1994
1995# extract of code from testgrid command used to process jobs running in
1996# parallel until number of jobs in the queue becomes equal or less than
1997# specified value
1998proc _testgrid_process_jobs {worker {nb_ok 0}} {
1999 # bind local vars to variables of the caller procedure
2000 upvar log log
2001 upvar logdir logdir
2002 upvar job_def job_def
2003 upvar nbpooled nbpooled
2004 upvar userbreak userbreak
2005 upvar refresh refresh
2006 upvar refresh_timer refresh_timer
2007
2008 catch {tpool::resume $worker}
2009 while { ! $userbreak && $nbpooled > $nb_ok } {
2010 foreach job [tpool::wait $worker [array names job_def]] {
2011 eval _log_test_case \[tpool::get $worker $job\] $job_def($job) log
2012 unset job_def($job)
2013 incr nbpooled -1
2014 }
2015
2016 # check for user break
2017 if { "[info commands dbreak]" == "dbreak" && [catch dbreak] } {
2018 set userbreak 1
2019 }
2020
2021 # update summary log with requested period
2022 if { $logdir != "" && $refresh > 0 && [clock seconds] > $refresh_timer + $refresh } {
2023 _log_summarize $logdir $log
2024 set refresh_timer [clock seconds]
2025 }
2026 }
2027 catch {tpool::suspend $worker}
2028}
ccadc126 2029
2030help checkcolor {
2031 Check pixel color.
2032 Use: checkcolor x y red green blue
2033 x y - pixel coordinates
2034 red green blue - expected pixel color (values from 0 to 1)
2035 Function check color with tolerance (5x5 area)
2036}
2037# Procedure to check color using command vreadpixel with tolerance
2038proc checkcolor { coord_x coord_y rd_get gr_get bl_get } {
2039 puts "Coordinate x = $coord_x"
2040 puts "Coordinate y = $coord_y"
2041 puts "RED color of RGB is $rd_get"
2042 puts "GREEN color of RGB is $gr_get"
2043 puts "BLUE color of RGB is $bl_get"
2044
2045 if { $coord_x <= 1 || $coord_y <= 1 } {
2046 puts "Error : minimal coordinate is x = 2, y = 2. But we have x = $coord_x y = $coord_y"
2047 return -1
2048 }
2049
2050 set color ""
2051 catch { [set color "[vreadpixel ${coord_x} ${coord_y} rgb]"] }
2052 if {"$color" == ""} {
2053 puts "Error : Pixel coordinates (${position_x}; ${position_y}) are out of view"
2054 }
2055 set rd [lindex $color 0]
2056 set gr [lindex $color 1]
2057 set bl [lindex $color 2]
2058 set rd_int [expr int($rd * 1.e+05)]
2059 set gr_int [expr int($gr * 1.e+05)]
2060 set bl_int [expr int($bl * 1.e+05)]
2061 set rd_ch [expr int($rd_get * 1.e+05)]
2062 set gr_ch [expr int($gr_get * 1.e+05)]
2063 set bl_ch [expr int($bl_get * 1.e+05)]
2064
2065 if { $rd_ch != 0 } {
2066 set tol_rd [expr abs($rd_ch - $rd_int)/$rd_ch]
2067 } else {
2068 set tol_rd $rd_int
2069 }
2070 if { $gr_ch != 0 } {
2071 set tol_gr [expr abs($gr_ch - $gr_int)/$gr_ch]
2072 } else {
2073 set tol_gr $gr_int
2074 }
2075 if { $bl_ch != 0 } {
2076 set tol_bl [expr abs($bl_ch - $bl_int)/$bl_ch]
2077 } else {
2078 set tol_bl $bl_int
2079 }
2080
2081 set status 0
2082 if { $tol_rd > 0.2 } {
2083 puts "Warning : RED light of additive color model RGB is invalid"
2084 set status 1
2085 }
2086 if { $tol_gr > 0.2 } {
2087 puts "Warning : GREEN light of additive color model RGB is invalid"
2088 set status 1
2089 }
2090 if { $tol_bl > 0.2 } {
2091 puts "Warning : BLUE light of additive color model RGB is invalid"
2092 set status 1
2093 }
2094
2095 if { $status != 0 } {
2096 puts "Warning : Colors of default coordinate are not equal"
2097 }
2098
2099 global stat
2100 if { $tol_rd > 0.2 || $tol_gr > 0.2 || $tol_bl > 0.2 } {
2101 set info [_checkpoint $coord_x $coord_y $rd_ch $gr_ch $bl_ch]
2102 set stat [lindex $info end]
2103 if { ${stat} != 1 } {
2104 puts "Error : Colors are not equal in default coordinate and in the near coordinates too"
2105 return $stat
2106 } else {
2107 puts "Point with valid color was found"
2108 return $stat
2109 }
2110 } else {
2111 set stat 1
2112 }
2113}
2114
2115# Procedure to check color in the point near default coordinate
2116proc _checkpoint {coord_x coord_y rd_ch gr_ch bl_ch} {
2117 set x_start [expr ${coord_x} - 2]
2118 set y_start [expr ${coord_y} - 2]
2119 set mistake 0
2120 set i 0
2121 while { $mistake != 1 && $i <= 5 } {
2122 set j 0
2123 while { $mistake != 1 && $j <= 5 } {
2124 set position_x [expr ${x_start} + $j]
2125 set position_y [expr ${y_start} + $i]
2126 puts $position_x
2127 puts $position_y
2128
2129 set color ""
2130 catch { [set color "[vreadpixel ${position_x} ${position_y} rgb]"] }
2131 if {"$color" == ""} {
2132 puts "Warning : Pixel coordinates (${position_x}; ${position_y}) are out of view"
2133 incr j
2134 continue
2135 }
2136 set rd [lindex $color 0]
2137 set gr [lindex $color 1]
2138 set bl [lindex $color 2]
2139 set rd_int [expr int($rd * 1.e+05)]
2140 set gr_int [expr int($gr * 1.e+05)]
2141 set bl_int [expr int($bl * 1.e+05)]
2142
2143 if { $rd_ch != 0 } {
2144 set tol_rd [expr abs($rd_ch - $rd_int)/$rd_ch]
2145 } else {
2146 set tol_rd $rd_int
2147 }
2148 if { $gr_ch != 0 } {
2149 set tol_gr [expr abs($gr_ch - $gr_int)/$gr_ch]
2150 } else {
2151 set tol_gr $gr_int
2152 }
2153 if { $bl_ch != 0 } {
2154 set tol_bl [expr abs($bl_ch - $bl_int)/$bl_ch]
2155 } else {
2156 set tol_bl $bl_int
2157 }
2158
2159 if { $tol_rd > 0.2 || $tol_gr > 0.2 || $tol_bl > 0.2 } {
2160 puts "Warning : Point with true color was not found near default coordinates"
2161 set mistake 0
2162 } else {
2163 set mistake 1
2164 }
2165 incr j
2166 }
2167 incr i
2168 }
2169 return $mistake
2170}