Skip to content
Tcl Source Code Artifact [b53ee803b5]
Login

Artifact [b53ee803b5]

Artifact b53ee803b5e3c94037d5c57e440b578b610ecaf9451c95df98115e5d1b91424a:


# Commands covered: expr, raw commands (TIP 759)
#
# This file contains a collection of tests for the raw commands of TIP 759:
# commands without brackets in expressions. Sourcing this file into Tcl runs
# the tests and generates output for errors. No output means no errors were
# found.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {"::tcltest" ni [namespace children]} {
    package require tcltest 2.5
    namespace import -force ::tcltest::*
}

::tcltest::loadTestedCommands
catch [list package require -exact tcl::test [info patchlevel]]

testConstraint testexprparser [llength [info commands testexprparser]]

test exprRawCmd-1.1 {ends at an operator} {
    set L {a b c}
    expr {llength $L + 1}
} 4
test exprRawCmd-1.2 {ends at a close paren} {
    set L {a b c}
    expr {(llength $L) * 2}
} 6
test exprRawCmd-1.3 {ends at a semicolon} {
    set i 0
    expr {incr i; incr i; $i}
} 2
test exprRawCmd-1.4 {operator that begins a word} {
    set L {5 6 7}
    expr {lindex $L 1 + 10}
} 16
test exprRawCmd-1.5 {operator inside a word} {
    set L {5 6 7}
    expr {lindex $L end-1}
} 6
test exprRawCmd-1.6 {string} {
    set s abc
    expr {string length $s == 3}
} 1
test exprRawCmd-1.7 {if} {
    expr {if 1 {set x 5} else {set x 6}}
} 5
test exprRawCmd-1.8 {dict} {
    set d {a 1 b 2}
    expr {dict get $d b}
} 2
test exprRawCmd-1.9 {lmap} {
    expr {lmap v {1 2 3} {expr {$v*2}}}
} {2 4 6}
test exprRawCmd-1.10 {global in an expression script} -setup {
    unset -nocomplain G
    proc f {} {( global G; G = 4 )}
} -body {
    f
    set G
} -cleanup {
    rename f {}
} -result 4
test exprRawCmd-1.11 {other commands need brackets} -body {
    expr {lrepeat 3 x}
} -returnCodes error -match glob -result {invalid bareword "lrepeat"*}
test exprRawCmd-1.12 {name not followed by a space} -body {
    expr {incr}
} -returnCodes error -match glob -result {invalid bareword "incr"*}

# Expression scripts with raw commands

test exprRawCmd-2.1 {dot product} -setup {
    proc dot {U V} {( s = 0; foreach u $U v $V {( s = $s + $u*$v )}; $s )}
} -body {
    dot {1 2 3} {4 5 6}
} -cleanup {
    rename dot {}
} -result 32
test exprRawCmd-2.2 {polynomial} -setup {
    proc poly {coeffs x} {(
	r = 0;
	i = llength $coeffs;
	while {[incr i -1] >= 0} {( r = $r*$x + [lindex $coeffs $i] )};
	$r
    )}
} -body {
    poly {1 2 3} 2
} -cleanup {
    rename poly {}
} -result 17
test exprRawCmd-2.3 {dot product, on several lines} -setup {
    proc dot {u v} {(
	s = 0;
	foreach a $u b $v {( s = $s + $a*$b )};
	$s
    )}
} -body {
    dot {1 2} {3 4}
} -cleanup {
    rename dot {}
} -result 11

# Tcl_ParseExpr

test exprRawCmd-3.1 {Tcl_ParseExpr} -constraints testexprparser -body {
    testexprparser {llength $L + 1} -1
} -result {- {} 0 subexpr {llength $L + 1} 5 operator + 0 subexpr {llength $L } 1 rawcommand {llength $L } 0 subexpr 1 1 text 1 0 {}}

# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: