OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [tests/] [defs] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# This file contains support code for the Tcl test suite.  It is
2
# normally sourced by the individual files in the test suite before
3
# they run their tests.  This improved approach to testing was designed
4
# and initially implemented by Mary Ann May-Pumphrey of Sun Microsystems.
5
#
6
# Copyright (c) 1990-1994 The Regents of the University of California.
7
# Copyright (c) 1994-1996 Sun Microsystems, Inc.
8
#
9
# See the file "license.terms" for information on usage and redistribution
10
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
#
12
# RCS: @(#) $Id: defs,v 1.1.1.1 2002-01-16 10:25:35 markom Exp $
13
 
14
if ![info exists srcdir] {
15
    set srcdir .
16
}
17
 
18
if ![info exists VERBOSE] {
19
    set VERBOSE 0
20
}
21
if ![info exists TESTS] {
22
    set TESTS {}
23
}
24
 
25
# If tests are being run as root, issue a warning message and set a
26
# variable to prevent some tests from running at all.
27
 
28
set user {}
29
if {$tcl_platform(platform) == "unix"} {
30
    catch {set user [exec whoami]}
31
    if {$user == ""} {
32
        catch {regexp {^[^(]*\(([^)]*)\)} [exec id] dummy user}
33
    }
34
    if {$user == ""} {set user root}
35
    if {$user == "root"} {
36
        puts stdout "Warning: you're executing as root.  I'll have to"
37
        puts stdout "skip some of the tests, since they'll fail as root."
38
        set testConfig(root) 1
39
    }
40
}
41
 
42
# Some of the tests don't work on some system configurations due to
43
# differences in word length, file system configuration, etc.  In order
44
# to prevent false alarms, these tests are generally only run in the
45
# master development directory for Tcl.  The presence of a file
46
# "doAllTests" in this directory is used to indicate that the non-portable
47
# tests should be run.
48
 
49
# If there is no "memory" command (because memory debugging isn't
50
# enabled), generate a dummy command that does nothing.
51
 
52
if {[info commands memory] == ""} {
53
    proc memory args {}
54
}
55
 
56
# Check configuration information that will determine which tests
57
# to run.  To do this, create an array testConfig.  Each element
58
# has a 0 or 1 value, and the following elements are defined:
59
#       unixOnly -      1 means this is a UNIX platform, so it's OK
60
#                       to run tests that only work under UNIX.
61
#       macOnly -       1 means this is a Mac platform, so it's OK
62
#                       to run tests that only work on Macs.
63
#       pcOnly -        1 means this is a PC platform, so it's OK to
64
#                       run tests that only work on PCs.
65
#       unixOrPc -      1 means this is a UNIX or PC platform.
66
#       macOrPc -       1 means this is a Mac or PC platform.
67
#       macOrUnix -     1 means this is a Mac or UNIX platform.
68
#       nonPortable -   1 means this the tests are being running in
69
#                       the master Tcl/Tk development environment;
70
#                       Some tests are inherently non-portable because
71
#                       they depend on things like word length, file system
72
#                       configuration, window manager, etc.  These tests
73
#                       are only run in the main Tcl development directory
74
#                       where the configuration is well known.  The presence
75
#                       of the file "doAllTests" in this directory indicates
76
#                       that it is safe to run non-portable tests.
77
#       knownBug -      The test is known to fail and the bug is not yet
78
#                       fixed. The test will be run only if the file
79
#                       "doBuggyTests" exists (intended for Tcl dev. group
80
#                       internal use only).
81
#       tempNotPc -     The inverse of pcOnly.  This flag is used to
82
#                       temporarily disable a test.
83
#       tempNotMac -    The inverse of macOnly.  This flag is used to
84
#                       temporarily disable a test.
85
#       nonBlockFiles - 1 means this platform supports setting files into
86
#                       nonblocking mode.
87
#       asyncPipeClose- 1 means this platform supports async flush and
88
#                       async close on a pipe.
89
#       unixExecs     - 1 means this machine has commands such as 'cat',
90
#                       'echo' etc available.
91
#       notIfCompiled - 1 means this that it is safe to run tests that
92
#                       might fail if the bytecode compiler is used. This
93
#                       element is set 1 if the file "doAllTests" exists in
94
#                       this directory. Normally, this element is 0 so that
95
#                       tests that fail with the bytecode compiler are
96
#                       skipped. As of 11/2/96 these are the history tests
97
#                       since they depend on accurate source location
98
#                       information.
99
 
100
catch {unset testConfig}
101
if {$tcl_platform(platform) == "unix"} {
102
    set testConfig(unixOnly) 1
103
    set testConfig(tempNotPc) 1
104
    set testConfig(tempNotMac) 1
105
} else {
106
    set testConfig(unixOnly) 0
107
}
108
if {$tcl_platform(platform) == "macintosh"} {
109
    set testConfig(tempNotPc) 1
110
    set testConfig(macOnly) 1
111
} else {
112
    set testConfig(macOnly) 0
113
}
114
if {$tcl_platform(platform) == "windows"} {
115
    set testConfig(tempNotMac) 1
116
    set testConfig(pcOnly) 1
117
} else {
118
    set testConfig(pcOnly) 0
119
}
120
set testConfig(unixOrPc) [expr $testConfig(unixOnly) || $testConfig(pcOnly)]
121
set testConfig(macOrPc) [expr $testConfig(macOnly) || $testConfig(pcOnly)]
122
set testConfig(macOrUnix) [expr $testConfig(macOnly) || $testConfig(unixOnly)]
123
set testConfig(nonPortable)     [expr [file exists doAllTests] || [file exists doAllTe]]
124
set testConfig(knownBug) [expr [file exists doBuggyTests] || [file exists doBuggyT]]
125
set testConfig(notIfCompiled) [file exists doAllCompilerTests]
126
 
127
set testConfig(unix)    $testConfig(unixOnly)
128
set testConfig(mac)     $testConfig(macOnly)
129
set testConfig(pc)      $testConfig(pcOnly)
130
 
131
set testConfig(nt)      [expr {$tcl_platform(os) == "Windows NT"}]
132
set testConfig(95)      [expr {$tcl_platform(os) == "Windows 95"}]
133
set testConfig(win32s)  [expr {$tcl_platform(os) == "Win32s"}]
134
 
135
# The following config switches are used to mark tests that crash on
136
# certain platforms, so that they can be reactivated again when the
137
# underlying problem is fixed.
138
 
139
set testConfig(pcCrash) $testConfig(macOrUnix)
140
set testConfig(macCrash) $testConfig(unixOrPc)
141
set testConfig(unixCrash) $testConfig(macOrPc)
142
 
143
if {[catch {set f [open $srcdir/defs r]}]} {
144
    set testConfig(nonBlockFiles) 1
145
} else {
146
    if {[expr [catch {fconfigure $f -blocking off}]] == 0} {
147
        set testConfig(nonBlockFiles) 1
148
    } else {
149
        set testConfig(nonBlockFiles) 0
150
    }
151
    close $f
152
}
153
 
154
trace variable testConfig r safeFetch
155
 
156
proc safeFetch {n1 n2 op} {
157
    global testConfig
158
 
159
    if {($n2 != {}) && ([info exists testConfig($n2)] == 0)} {
160
        set testConfig($n2) 0
161
    }
162
}
163
 
164
# Test for SCO Unix - cannot run async flushing tests because a potential
165
# problem with select is apparently interfering. (Mark Diekhans).
166
 
167
if {$tcl_platform(platform) == "unix"} {
168
    if {[catch {exec uname -X | fgrep {Release = 3.2v}}] == 0} {
169
        set testConfig(asyncPipeClose) 0
170
    } else {
171
        set testConfig(asyncPipeClose) 1
172
    }
173
} else {
174
    set testConfig(asyncPipeClose) 1
175
}
176
 
177
# Test to see if we have a broken version of sprintf with respect to the
178
# "e" format of floating-point numbers.
179
 
180
set testConfig(eformat) 1
181
if {[string compare "[format %g 5e-5]" "5e-05"] != 0} {
182
    set testConfig(eformat) 0
183
    puts "(will skip tests that depend on the \"e\" format of floating-point numbers)"
184
}
185
# Test to see if execed commands such as cat, echo, rm and so forth are
186
# present on this machine.
187
 
188
set testConfig(unixExecs) 1
189
if {$tcl_platform(platform) == "macintosh"} {
190
    set testConfig(unixExecs) 0
191
}
192
if {($testConfig(unixExecs) == 1) && ($tcl_platform(platform) == "windows")} {
193
    if {[catch {exec cat $srcdir/defs}] == 1} {
194
        set testConfig(unixExecs) 0
195
    }
196
    if {($testConfig(unixExecs) == 1) && ([catch {exec echo hello}] == 1)} {
197
        set testConfig(unixExecs) 0
198
    }
199
    if {($testConfig(unixExecs) == 1) && \
200
                ([catch {exec sh -c echo hello}] == 1)} {
201
        set testConfig(unixExecs) 0
202
    }
203
    if {($testConfig(unixExecs) == 1) && ([catch {exec wc $srcdir/defs}] == 1)} {
204
        set testConfig(unixExecs) 0
205
    }
206
    if {$testConfig(unixExecs) == 1} {
207
        exec echo hello > removeMe
208
        if {[catch {exec rm removeMe}] == 1} {
209
            set testConfig(unixExecs) 0
210
        }
211
    }
212
    if {($testConfig(unixExecs) == 1) && ([catch {exec sleep 1}] == 1)} {
213
        set testConfig(unixExecs) 0
214
    }
215
    if {($testConfig(unixExecs) == 1) && \
216
                ([catch {exec fgrep unixExecs $srcdir/defs}] == 1)} {
217
        set testConfig(unixExecs) 0
218
    }
219
    if {($testConfig(unixExecs) == 1) && ([catch {exec ps}] == 1)} {
220
        set testConfig(unixExecs) 0
221
    }
222
    if {($testConfig(unixExecs) == 1) && \
223
                ([catch {exec echo abc > removeMe}] == 0) && \
224
                ([catch {exec chmod 644 removeMe}] == 1) && \
225
                ([catch {exec rm removeMe}] == 0)} {
226
        set testConfig(unixExecs) 0
227
    } else {
228
        catch {exec rm -f removeMe}
229
    }
230
    if {($testConfig(unixExecs) == 1) && \
231
                ([catch {exec mkdir removeMe}] == 1)} {
232
        set testConfig(unixExecs) 0
233
    } else {
234
        catch {exec rm -r removeMe}
235
    }
236
    if {$testConfig(unixExecs) == 0} {
237
        puts stdout "Warning: Unix-style executables are not available, so"
238
        puts stdout "some tests will be skipped."
239
    }
240
}
241
 
242
proc print_verbose {name description constraints script code answer} {
243
    puts stdout "\n"
244
    if {[string length $constraints]} {
245
        puts stdout "==== $name $description\t--- ($constraints) ---"
246
    } else {
247
        puts stdout "==== $name $description"
248
    }
249
    puts stdout "==== Contents of test case:"
250
    puts stdout "$script"
251
    if {$code != 0} {
252
        if {$code == 1} {
253
            puts stdout "==== Test generated error:"
254
            puts stdout $answer
255
        } elseif {$code == 2} {
256
            puts stdout "==== Test generated return exception;  result was:"
257
            puts stdout $answer
258
        } elseif {$code == 3} {
259
            puts stdout "==== Test generated break exception"
260
        } elseif {$code == 4} {
261
            puts stdout "==== Test generated continue exception"
262
        } else {
263
            puts stdout "==== Test generated exception $code;  message was:"
264
            puts stdout $answer
265
        }
266
    } else {
267
        puts stdout "==== Result was:"
268
        puts stdout "$answer"
269
    }
270
}
271
 
272
# test --
273
# This procedure runs a test and prints an error message if the
274
# test fails.  If VERBOSE has been set, it also prints a message
275
# even if the test succeeds.  The test will be skipped if it
276
# doesn't match the TESTS variable, or if one of the elements
277
# of "constraints" turns out not to be true.
278
#
279
# Arguments:
280
# name -                Name of test, in the form foo-1.2.
281
# description -         Short textual description of the test, to
282
#                       help humans understand what it does.
283
# constraints -         A list of one or more keywords, each of
284
#                       which must be the name of an element in
285
#                       the array "testConfig".  If any of these
286
#                       elements is zero, the test is skipped.
287
#                       This argument may be omitted.
288
# script -              Script to run to carry out the test.  It must
289
#                       return a result that can be checked for
290
#                       correctness.
291
# answer -              Expected result from script.
292
 
293
proc test {name description script answer args} {
294
    global VERBOSE TESTS testConfig
295
    if {[string compare $TESTS ""] != 0} then {
296
        set ok 0
297
        foreach test $TESTS {
298
            if [string match $test $name] then {
299
                set ok 1
300
                break
301
            }
302
        }
303
        if !$ok then return
304
    }
305
    set i [llength $args]
306
    if {$i == 0} {
307
        set constraints {}
308
    } elseif {$i == 1} {
309
        # "constraints" argument exists;  shuffle arguments down, then
310
        # make sure that the constraints are satisfied.
311
 
312
        set constraints $script
313
        set script $answer
314
        set answer [lindex $args 0]
315
        set doTest 0
316
        if {[string match {*[$\[]*} $constraints] != 0} {
317
            # full expression, e.g. {$foo > [info tclversion]}
318
 
319
            catch {set doTest [uplevel #0 expr [list $constraints]]} msg
320
        } elseif {[regexp {[^.a-zA-Z0-9 ]+} $constraints] != 0} {
321
            # something like {a || b} should be turned into
322
            # $testConfig(a) || $testConfig(b).
323
 
324
            regsub -all {[.a-zA-Z0-9]+} $constraints {$testConfig(&)} c
325
            catch {set doTest [eval expr $c]}
326
        } else {
327
            # just simple constraints such as {unixOnly fonts}.
328
 
329
            set doTest 1
330
            foreach constraint $constraints {
331
                if {![info exists testConfig($constraint)]
332
                        || !$testConfig($constraint)} {
333
                    set doTest 0
334
                    break
335
                }
336
            }
337
        }
338
        if {$doTest == 0} {
339
            if $VERBOSE then {
340
                puts stdout "++++ $name SKIPPED: $constraints"
341
            }
342
            return
343
        }
344
    } else {
345
        error "wrong # args: must be \"test name description ?constraints? script answer\""
346
    }
347
    memory tag $name
348
    set code [catch {uplevel $script} result]
349
    if {$code != 0} {
350
        print_verbose $name $description $constraints $script \
351
                $code $result
352
    } elseif {[string compare $result $answer] == 0} then {
353
        if $VERBOSE then {
354
            if {$VERBOSE > 0} {
355
                print_verbose $name $description $constraints $script \
356
                    $code $result
357
            }
358
            if {$VERBOSE != -2} {
359
                puts stdout "++++ $name PASSED"
360
            }
361
        }
362
    } else {
363
        print_verbose $name $description $constraints $script \
364
                $code $result
365
        puts stdout "---- Result should have been:"
366
        puts stdout "$answer"
367
        puts stdout "---- $name FAILED"
368
    }
369
}
370
 
371
proc dotests {file args} {
372
    global TESTS
373
    set savedTests $TESTS
374
    set TESTS $args
375
    source $file
376
    set TESTS $savedTests
377
}
378
 
379
proc normalizeMsg {msg} {
380
    regsub "\n$" [string tolower $msg] "" msg
381
    regsub -all "\n\n" $msg "\n" msg
382
    regsub -all "\n\}" $msg "\}" msg
383
    return $msg
384
}
385
 
386
proc makeFile {contents name} {
387
    set fd [open $name w]
388
    fconfigure $fd -translation lf
389
    if {[string index $contents [expr [string length $contents] - 1]] == "\n"} {
390
        puts -nonewline $fd $contents
391
    } else {
392
        puts $fd $contents
393
    }
394
    close $fd
395
}
396
 
397
proc removeFile {name} {
398
    file delete $name
399
}
400
 
401
proc makeDirectory {name} {
402
    file mkdir $name
403
}
404
 
405
proc removeDirectory {name} {
406
    file delete -force $name
407
}
408
 
409
proc viewFile {name} {
410
    global tcl_platform testConfig
411
    if {($tcl_platform(platform) == "macintosh") || \
412
                ($testConfig(unixExecs) == 0)} {
413
        set f [open $name]
414
        set data [read -nonewline $f]
415
        close $f
416
        return $data
417
    } else {
418
        exec cat $name
419
    }
420
}
421
 
422
# Locate tcltest executable
423
 
424
set tcltest [info nameofexecutable]
425
 
426
if {$tcltest == "{}"} {
427
    set tcltest {}
428
    puts "Unable to find tcltest executable, multiple process tests will fail."
429
}
430
 
431
if {$tcl_platform(os) != "Win32s"} {
432
    # Don't even try running another copy of tcltest under win32s, or you
433
    # get an error dialog about multiple instances.
434
 
435
    catch {
436
        file delete -force tmp
437
        set f [open tmp w]
438
        puts $f {
439
            exit
440
        }
441
        close $f
442
        set f [open "|[list $tcltest tmp]" r]
443
        close $f
444
        set testConfig(stdio) 1
445
    }
446
    catch {file delete -force tmp}
447
}
448
 
449
if {($tcl_platform(platform) == "windows") && ($testConfig(stdio) == 0)} {
450
    puts "(will skip tests that redirect stdio of exec'd 32-bit applications)"
451
}
452
 
453
catch {socket} msg
454
set testConfig(socket) [expr {$msg != "sockets are not available on this system"}]
455
 
456
if {$testConfig(socket) == 0} {
457
    puts "(will skip tests that use sockets)"
458
}
459
 
460
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.