Tcl Source Code

Check-in [d075bd7bcd]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:...and all the compilation and tests now work/pass
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tip-vote | tip-405-impl-td
Files: files | file ages | folders
SHA1: d075bd7bcd8fcfcd9c2b41653058d755e19db3c5
User & Date: dkf 2012-10-05 14:54:31
Context
2012-10-16
13:06
merge trunk Closed-Leaf check-in: 1196fa3726 user: dkf tags: tip-405-impl-td
2012-10-05
14:54
...and all the compilation and tests now work/pass check-in: d075bd7bcd user: dkf tags: tip-vote, tip-405-impl-td
13:05
compilation code adjusted check-in: 726ad3d776 user: dkf tags: tip-405-impl-td
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tclCompCmds.c.

848
849
850
851
852
853
854













855
856
857
858
859
860
861
...
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939










940
941
942
943
944
945
946
...
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
....
1020
1021
1022
1023
1024
1025
1026




1027
1028
1029
1030
1031
1032
1033
....
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054


1055
1056
1057
1058
1059
1060
1061
....
2275
2276
2277
2278
2279
2280
2281


2282
2283
2284
2285
2286
2287
2288
    varsTokenPtr = TokenAfter(parsePtr->tokenPtr);
    dictTokenPtr = TokenAfter(varsTokenPtr);
    bodyTokenPtr = TokenAfter(dictTokenPtr);
    if (varsTokenPtr->type != TCL_TOKEN_SIMPLE_WORD ||
	    bodyTokenPtr->type != TCL_TOKEN_SIMPLE_WORD) {
	return TCL_ERROR;
    }














    /*
     * Check we've got a pair of variables and that they are local variables.
     * Then extract their indices in the LVT.
     */

    Tcl_DStringInit(&buffer);
................................................................................
     */

    infoIndex = TclFindCompiledLocal(NULL, 0, 1, envPtr);
    if (infoIndex < 0) {
	return TCL_ERROR;
    }

    /*
     * Create temporary variable to capture return values from loop body.
     */

    if (collect == TCL_EACH_COLLECT) {
	collectVar = TclFindCompiledLocal(NULL, /*nameChars*/ 0, /*create*/ 1,
		envPtr);
	if (collectVar < 0) {
	    return TCL_ERROR;
	}
    }

    /*
     * Preparation complete; issue instructions. Note that this code issues
     * fixed-sized jumps. That simplifies things a lot!
     *
     * First up, get the dictionary and start the iteration. No catching of
     * errors at this point.
     */

    CompileWord(envPtr, dictTokenPtr, interp, 3);
    TclEmitInstInt4(	INST_DICT_FIRST, infoIndex,		envPtr);
    emptyTargetOffset = CurrentOffset(envPtr);
    TclEmitInstInt4(	INST_JUMP_TRUE4, 0,			envPtr);

    /*
     * Initialize the accumulator dictionary, if needed.
     */

    if (collect == TCL_EACH_COLLECT) {
	PushLiteral(envPtr, "", 0);
	Emit14Inst(	INST_STORE_SCALAR, collectVar,		envPtr);
	TclEmitOpcode(	INST_POP,				envPtr);
    }











    /*
     * Now we catch errors from here on so that we can finalize the search
     * started by Tcl_DictObjFirst above.
     */

    catchRange = DeclareExceptionRange(envPtr, CATCH_EXCEPTION_RANGE);
    TclEmitInstInt4(	INST_BEGIN_CATCH4, catchRange,		envPtr);
................................................................................

    SetLineInformation(3);
    CompileBody(envPtr, bodyTokenPtr, interp);
    if (collect == TCL_EACH_COLLECT) {
	Emit14Inst(	INST_LOAD_SCALAR, keyVarIndex,		envPtr);
	TclEmitInstInt4(INST_OVER, 1,				envPtr);
	TclEmitInstInt4(INST_DICT_SET, 1,			envPtr);
	TclEmitInt4(	collectVar,				envPtr);
	TclEmitOpcode(	INST_POP,				envPtr);
    }
    TclEmitOpcode(	INST_POP,				envPtr);

    /*
     * Both exception target ranges (error and loop) end here.
     */
................................................................................

    ExceptionRangeTarget(envPtr, catchRange, catchOffset);
    TclEmitOpcode(	INST_PUSH_RETURN_OPTIONS,		envPtr);
    TclEmitOpcode(	INST_PUSH_RESULT,			envPtr);
    TclEmitInstInt1(	INST_UNSET_SCALAR, 0,			envPtr);
    TclEmitInt4(	infoIndex,				envPtr);
    TclEmitOpcode(	INST_END_CATCH,				envPtr);




    TclEmitOpcode(	INST_RETURN_STK,			envPtr);

    /*
     * Otherwise we're done (the jump after the DICT_FIRST points here) and we
     * need to pop the bogus key/value pair (pushed to keep stack calculations
     * easy!) Note that we skip the END_CATCH. [Bug 1382528]
     */
................................................................................
    envPtr->currStackDepth = savedStackDepth+2;
    jumpDisplacement = CurrentOffset(envPtr) - emptyTargetOffset;
    TclUpdateInstInt4AtPc(INST_JUMP_TRUE4, jumpDisplacement,
	    envPtr->codeStart + emptyTargetOffset);
    TclEmitOpcode(	INST_POP,				envPtr);
    TclEmitOpcode(	INST_POP,				envPtr);
    TclEmitInstInt1(	INST_UNSET_SCALAR, 0,			envPtr);
    TclEmitInt4(	infoIndex,				envPtr);

    /*
     * Final stage of the command (normal case) is that we push an empty
     * object (or push the accumulator as the result object). This is done
     * last to promote peephole optimization when it's dropped immediately.
     */

    jumpDisplacement = CurrentOffset(envPtr) - endTargetOffset;
    TclUpdateInstInt4AtPc(INST_JUMP4, jumpDisplacement,
	    envPtr->codeStart + endTargetOffset);
    if (collect == TCL_EACH_COLLECT) {
	Emit14Inst(	INST_LOAD_SCALAR, collectVar,		envPtr);


    } else {
	PushLiteral(envPtr, "", 0);
    }
    return TCL_OK;
}

int
................................................................................
     * The command's result is an empty string if not collecting, or the
     * list of results from evaluating the loop body.
     */

    envPtr->currStackDepth = savedStackDepth;
    if (collect == TCL_EACH_COLLECT) {
	Emit14Inst(		INST_LOAD_SCALAR, collectVar,	envPtr);


    } else {
	PushLiteral(envPtr, "", 0);
    }
    envPtr->currStackDepth = savedStackDepth + 1;

  done:
    for (loopIndex = 0;  loopIndex < numLists;  loopIndex++) {






>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<




<
<
<
<
<
<
<
<
<
<
|








>
>
>
>
>
>
>
>
>
>







 







|







 







>
>
>
>







 







|












>
>







 







>
>







848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
...
911
912
913
914
915
916
917












918
919
920
921










922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
...
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
....
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
....
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
....
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
    varsTokenPtr = TokenAfter(parsePtr->tokenPtr);
    dictTokenPtr = TokenAfter(varsTokenPtr);
    bodyTokenPtr = TokenAfter(dictTokenPtr);
    if (varsTokenPtr->type != TCL_TOKEN_SIMPLE_WORD ||
	    bodyTokenPtr->type != TCL_TOKEN_SIMPLE_WORD) {
	return TCL_ERROR;
    }

    /*
     * Create temporary variable to capture return values from loop body when
     * we're collecting results.
     */

    if (collect == TCL_EACH_COLLECT) {
	collectVar = TclFindCompiledLocal(NULL, /*nameChars*/ 0, /*create*/ 1,
		envPtr);
	if (collectVar < 0) {
	    return TCL_ERROR;
	}
    }

    /*
     * Check we've got a pair of variables and that they are local variables.
     * Then extract their indices in the LVT.
     */

    Tcl_DStringInit(&buffer);
................................................................................
     */

    infoIndex = TclFindCompiledLocal(NULL, 0, 1, envPtr);
    if (infoIndex < 0) {
	return TCL_ERROR;
    }













    /*
     * Preparation complete; issue instructions. Note that this code issues
     * fixed-sized jumps. That simplifies things a lot!
     *










     * First up, initialize the accumulator dictionary if needed.
     */

    if (collect == TCL_EACH_COLLECT) {
	PushLiteral(envPtr, "", 0);
	Emit14Inst(	INST_STORE_SCALAR, collectVar,		envPtr);
	TclEmitOpcode(	INST_POP,				envPtr);
    }

    /*
     * Get the dictionary and start the iteration. No catching of errors at
     * this point.
     */

    CompileWord(envPtr, dictTokenPtr, interp, 3);
    TclEmitInstInt4(	INST_DICT_FIRST, infoIndex,		envPtr);
    emptyTargetOffset = CurrentOffset(envPtr);
    TclEmitInstInt4(	INST_JUMP_TRUE4, 0,			envPtr);

    /*
     * Now we catch errors from here on so that we can finalize the search
     * started by Tcl_DictObjFirst above.
     */

    catchRange = DeclareExceptionRange(envPtr, CATCH_EXCEPTION_RANGE);
    TclEmitInstInt4(	INST_BEGIN_CATCH4, catchRange,		envPtr);
................................................................................

    SetLineInformation(3);
    CompileBody(envPtr, bodyTokenPtr, interp);
    if (collect == TCL_EACH_COLLECT) {
	Emit14Inst(	INST_LOAD_SCALAR, keyVarIndex,		envPtr);
	TclEmitInstInt4(INST_OVER, 1,				envPtr);
	TclEmitInstInt4(INST_DICT_SET, 1,			envPtr);
	TclEmitInt4(		collectVar,			envPtr);
	TclEmitOpcode(	INST_POP,				envPtr);
    }
    TclEmitOpcode(	INST_POP,				envPtr);

    /*
     * Both exception target ranges (error and loop) end here.
     */
................................................................................

    ExceptionRangeTarget(envPtr, catchRange, catchOffset);
    TclEmitOpcode(	INST_PUSH_RETURN_OPTIONS,		envPtr);
    TclEmitOpcode(	INST_PUSH_RESULT,			envPtr);
    TclEmitInstInt1(	INST_UNSET_SCALAR, 0,			envPtr);
    TclEmitInt4(	infoIndex,				envPtr);
    TclEmitOpcode(	INST_END_CATCH,				envPtr);
    if (collect == TCL_EACH_COLLECT) {
	TclEmitInstInt1(INST_UNSET_SCALAR, 0,			envPtr);
	TclEmitInt4(		collectVar,			envPtr);
    }
    TclEmitOpcode(	INST_RETURN_STK,			envPtr);

    /*
     * Otherwise we're done (the jump after the DICT_FIRST points here) and we
     * need to pop the bogus key/value pair (pushed to keep stack calculations
     * easy!) Note that we skip the END_CATCH. [Bug 1382528]
     */
................................................................................
    envPtr->currStackDepth = savedStackDepth+2;
    jumpDisplacement = CurrentOffset(envPtr) - emptyTargetOffset;
    TclUpdateInstInt4AtPc(INST_JUMP_TRUE4, jumpDisplacement,
	    envPtr->codeStart + emptyTargetOffset);
    TclEmitOpcode(	INST_POP,				envPtr);
    TclEmitOpcode(	INST_POP,				envPtr);
    TclEmitInstInt1(	INST_UNSET_SCALAR, 0,			envPtr);
    TclEmitInt4(		infoIndex,			envPtr);

    /*
     * Final stage of the command (normal case) is that we push an empty
     * object (or push the accumulator as the result object). This is done
     * last to promote peephole optimization when it's dropped immediately.
     */

    jumpDisplacement = CurrentOffset(envPtr) - endTargetOffset;
    TclUpdateInstInt4AtPc(INST_JUMP4, jumpDisplacement,
	    envPtr->codeStart + endTargetOffset);
    if (collect == TCL_EACH_COLLECT) {
	Emit14Inst(	INST_LOAD_SCALAR, collectVar,		envPtr);
	TclEmitInstInt1(INST_UNSET_SCALAR, 0,			envPtr);
	TclEmitInt4(		collectVar,			envPtr);
    } else {
	PushLiteral(envPtr, "", 0);
    }
    return TCL_OK;
}

int
................................................................................
     * The command's result is an empty string if not collecting, or the
     * list of results from evaluating the loop body.
     */

    envPtr->currStackDepth = savedStackDepth;
    if (collect == TCL_EACH_COLLECT) {
	Emit14Inst(		INST_LOAD_SCALAR, collectVar,	envPtr);
	TclEmitInstInt1(INST_UNSET_SCALAR, 0,			envPtr);
	TclEmitInt4(		collectVar,			envPtr);
    } else {
	PushLiteral(envPtr, "", 0);
    }
    envPtr->currStackDepth = savedStackDepth + 1;

  done:
    for (loopIndex = 0;  loopIndex < numLists;  loopIndex++) {

Changes to generic/tclDictObj.c.

2615
2616
2617
2618
2619
2620
2621

2622
2623
2624
2625
2626
2627
2628
....
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
    /*
     * Make sure that these objects (which we need throughout the body of the
     * loop) don't vanish. Note that the dictionary internal rep is locked
     * internally so that updates, shimmering, etc are not a problem.
     */


    Tcl_IncrRefCount(storagePtr->keyVarObj);
    Tcl_IncrRefCount(storagePtr->valueVarObj);
    Tcl_IncrRefCount(storagePtr->scriptObj);

    /*
     * Stop the value from getting hit in any way by any traces on the key
     * variable.
................................................................................

    /*
     * Get the next mapping from the dictionary.
     */

    Tcl_DictObjNext(&storagePtr->search, &keyObj, &valueObj, &done);
    if (done) {
	Tcl_ResetResult(interp);
	goto done;
    }

    /*
     * Stop the value from getting hit in any way by any traces on the key
     * variable.
     */






>







 







|







2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
....
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
    /*
     * Make sure that these objects (which we need throughout the body of the
     * loop) don't vanish. Note that the dictionary internal rep is locked
     * internally so that updates, shimmering, etc are not a problem.
     */

    Tcl_IncrRefCount(storagePtr->accumulatorObj);
    Tcl_IncrRefCount(storagePtr->keyVarObj);
    Tcl_IncrRefCount(storagePtr->valueVarObj);
    Tcl_IncrRefCount(storagePtr->scriptObj);

    /*
     * Stop the value from getting hit in any way by any traces on the key
     * variable.
................................................................................

    /*
     * Get the next mapping from the dictionary.
     */

    Tcl_DictObjNext(&storagePtr->search, &keyObj, &valueObj, &done);
    if (done) {
	Tcl_SetObjResult(interp, storagePtr->accumulatorObj);
	goto done;
    }

    /*
     * Stop the value from getting hit in any way by any traces on the key
     * variable.
     */

Changes to tests/dict.test.

1539
1540
1541
1542
1543
1544
1545
1546



1547
1548
1549
1550
1551

1552
1553
1554

1555
1556
1557
1558
1559
1560
1561
....
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619


1620
1621

1622
1623
1624
1625
1626
1627
1628
1629












1630
1631
1632
1633
1634
1635
1636
....
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
....
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
} -result {must have exactly two variable names}
test dict-24.6 {dict map command: syntax} -returnCodes error -body {
    dict map {x x x} x x
} -result {must have exactly two variable names}
test dict-24.7 {dict map command: syntax} -returnCodes error -body {
    dict map "\{x" x x
} -result {unmatched open brace in list}
test dict-24.8 {dict map command} -body {



    # This test confirms that [dict keys], [dict values] and [dict map]
    # all traverse a dictionary in the same order.
    set dictv {a A b B c C}
    set values {}
    set keys [dict map {k v} $dictv {

	lappend values $v
	set k
    }]

    set result [expr {
	$keys eq [dict keys $dictv] && $values eq [dict values $dictv]
    }]
    expr {$result ? "YES" : [list "NO" $dictv $keys $values]}
} -cleanup {
    unset result keys values k v dictv
} -result YES
................................................................................
	dict map {k v} {a b} {
	    return ok,$k,$v
	    error "skipped return completely"
	}
	error "return didn't go far enough"
    }}
} ok,a,b
test dict-24.14 {dict map command: handle representation loss} -body {
    set dictVar {a b c d e f g h}
    set values {}


    set keys [dict map {k v} $dictVar {
	if {[llength $dictVar]} {

	    lappend values $v
	    return -level 0 $k
	}
    }]
    list [lsort $keys] [lsort $values]
} -cleanup {
    unset dictVar keys values k v
} -result {{a c e g} {b d f h}}












test dict-24.15 {dict map command: keys are unique and iterated over once only} -setup {
    unset -nocomplain accum
    array set accum {}
} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict map {k v} $dictVar {
	append accum($k) $v,
................................................................................
    # Bug 1379349 (dict for)
    apply {{} {
	set d [dict create a 1]		;# Dict must be unshared!
	dict map {k v} $d {
	    dict set d $k 0		;# Any modification will do
	}
    }}
} {{a 0}}
test dict-24.18 {dict map command in compilation context} {
    # Bug 1382528 (dict for)
    apply {{} {
	dict map {k v} {} {}		;# Note empty dict
	catch { error foo }		;# Note compiled [catch]
    }}
} 1
................................................................................
    }} [linenumber]}}
} 5
rename linenumber {}
test dict-24.22 {dict map results (non-compiled)} {
    dict map {k v} [dict map {k v} {a 1 b 2 c 3 d 4} { list $v $k }] {
	return -level 0 "$k,$v"
    }
} {{1 a,2 b} {3 c,4 d}}
test dict-24.23 {dict map results (compiled)} {
    apply {{} {
	dict map {k v} [dict map {k v} {a 1 b 2 c 3 d 4} { list $v $k }] {
	    return -level 0 "$k,$v"
	}
    }}
} {{1 a,2 b} {3 c,4 d}}
test dict-24.23a {dict map results (compiled)} {
    apply {{list} {
	dict map {k v} [dict map {k v} $list { list $v $k }] {
	    return -level 0 "$k,$v"
	}
    }} {a 1 b 2 c 3 d 4}
} {{1 a,2 b} {3 c,4 d}}
test dict-24.24 {dict map with huge dict (non-compiled)} {
    tcl::mathop::+ {*}[dict map {k v} [lsearch -all [lrepeat 1000000 x] x] {
	expr { $k * $v }
    }]
} 166666416666500000
test dict-24.25 {dict map with huge dict (compiled)} {
    apply {{n} {
	tcl::mathop::+ {*}[dict map {k v} [lsearch -all [lrepeat $n y] y] {
	    expr { $k * $v }
	}]
    }} 1000000
} 166666416666500000

 
# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End:






|
>
>
>



<
|
>

<
<
>







 







|
|

>
>
|

>



<
|


|
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|






|






|

|


|





|
|









1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552

1553
1554
1555


1556
1557
1558
1559
1560
1561
1562
1563
....
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629

1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
....
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
....
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
} -result {must have exactly two variable names}
test dict-24.6 {dict map command: syntax} -returnCodes error -body {
    dict map {x x x} x x
} -result {must have exactly two variable names}
test dict-24.7 {dict map command: syntax} -returnCodes error -body {
    dict map "\{x" x x
} -result {unmatched open brace in list}
test dict-24.8 {dict map command} -setup {
    set values {}
    set keys {}
} -body {
    # This test confirms that [dict keys], [dict values] and [dict map]
    # all traverse a dictionary in the same order.
    set dictv {a A b B c C}

    dict map {k v} $dictv {
	lappend keys $k
	lappend values $v


    }
    set result [expr {
	$keys eq [dict keys $dictv] && $values eq [dict values $dictv]
    }]
    expr {$result ? "YES" : [list "NO" $dictv $keys $values]}
} -cleanup {
    unset result keys values k v dictv
} -result YES
................................................................................
	dict map {k v} {a b} {
	    return ok,$k,$v
	    error "skipped return completely"
	}
	error "return didn't go far enough"
    }}
} ok,a,b
test dict-24.14 {dict map command: handle representation loss} -setup {
    set keys {}
    set values {}
} -body {
    set dictVar {a b c d e f g h}
    list [dict size [dict map {k v} $dictVar {
	if {[llength $dictVar]} {
	    lappend keys $k
	    lappend values $v
	    return -level 0 $k
	}

    }]] [lsort $keys] [lsort $values]
} -cleanup {
    unset dictVar keys values k v
} -result {4 {a c e g} {b d f h}}
test dict-24.14a {dict map command: handle representation loss} -body {
    apply {{} {
	set dictVar {a b c d e f g h}
	list [dict size [dict map {k v} $dictVar {
	    if {[llength $dictVar]} {
		lappend keys $k
		lappend values $v
		return -level 0 $k
	    }
	}]] [lsort $keys] [lsort $values]
    }}
} -result {4 {a c e g} {b d f h}}
test dict-24.15 {dict map command: keys are unique and iterated over once only} -setup {
    unset -nocomplain accum
    array set accum {}
} -body {
    set dictVar {a1 a a2 b b1 c b2 d foo bar bar foo}
    dict map {k v} $dictVar {
	append accum($k) $v,
................................................................................
    # Bug 1379349 (dict for)
    apply {{} {
	set d [dict create a 1]		;# Dict must be unshared!
	dict map {k v} $d {
	    dict set d $k 0		;# Any modification will do
	}
    }}
} {a {a 0}}
test dict-24.18 {dict map command in compilation context} {
    # Bug 1382528 (dict for)
    apply {{} {
	dict map {k v} {} {}		;# Note empty dict
	catch { error foo }		;# Note compiled [catch]
    }}
} 1
................................................................................
    }} [linenumber]}}
} 5
rename linenumber {}
test dict-24.22 {dict map results (non-compiled)} {
    dict map {k v} [dict map {k v} {a 1 b 2 c 3 d 4} { list $v $k }] {
	return -level 0 "$k,$v"
    }
} {a {a,1 a} b {b,2 b} c {c,3 c} d {d,4 d}}
test dict-24.23 {dict map results (compiled)} {
    apply {{} {
	dict map {k v} [dict map {k v} {a 1 b 2 c 3 d 4} { list $v $k }] {
	    return -level 0 "$k,$v"
	}
    }}
} {a {a,1 a} b {b,2 b} c {c,3 c} d {d,4 d}}
test dict-24.23a {dict map results (compiled)} {
    apply {{list} {
	dict map {k v} [dict map {k v} $list { list $v $k }] {
	    return -level 0 "$k,$v"
	}
    }} {a 1 b 2 c 3 d 4}
} {a {a,1 a} b {b,2 b} c {c,3 c} d {d,4 d}}
test dict-24.24 {dict map with huge dict (non-compiled)} {
    tcl::mathop::+ {*}[dict map {k v} [lsearch -all [lrepeat 100000 x] x] {
	expr { $k * $v }
    }]
} 166666666600000
test dict-24.25 {dict map with huge dict (compiled)} {
    apply {{n} {
	tcl::mathop::+ {*}[dict map {k v} [lsearch -all [lrepeat $n y] y] {
	    expr { $k * $v }
	}]
    }} 100000
} 166666666600000

 
# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End:

Changes to tests/lmap.test.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

85
86
87
88
89
90
91
92
93
94

95
96
97
98
99
100
101
102
103
104
105
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

150
151
152

153
154
155
156
157
158
159
160
161
162
163
...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
254

255

256
257

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375


376
377
378
379
380
381
382
383
384
385

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415

416
417
418
419
420
421
422


423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470


471
472
473
474
475
476
477
478
479
480
481
482
483
484


485
486
487
488
489
490

491
492
493




# Copyright (c) 2011 Trevor Davel
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

catch {unset a}
catch {unset i}
catch {unset x}

# ----- Non-compiled operation -------------------------------------------------


# Basic "lmap" operation (non-compiled)

test lmap-1.1 {basic lmap tests} {
    set a {}
    lmap i {a b c d} {
	set a [concat $a $i]
    }
} {a {a b} {a b c} {a b c d}}
test lmap-1.2 {basic lmap tests} {
    lmap i {a b {{c d} e} {123 {{x}}}} {
	set i
    }
} {a b {{c d} e} {123 {{x}}}}
test lmap-1.2a {basic lmap tests} {
    lmap i {a b {{c d} e} {123 {{x}}}} {
  return -level 0 $i
    }
} {a b {{c d} e} {123 {{x}}}}
test lmap-1.3 {basic lmap tests} {catch {lmap} msg} 1
test lmap-1.4 {basic lmap tests} {
    catch {lmap} msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-1.5 {basic lmap tests} {catch {lmap i} msg} 1
test lmap-1.6 {basic lmap tests} {
    catch {lmap i} msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-1.7 {basic lmap tests} {catch {lmap i j} msg} 1
test lmap-1.8 {basic lmap tests} {
    catch {lmap i j} msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-1.9 {basic lmap tests} {catch {lmap i j k l} msg} 1
test lmap-1.10 {basic lmap tests} {
    catch {lmap i j k l} msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-1.11 {basic lmap tests} {
    lmap i {} {
      set i
    }
} {}
test lmap-1.12 {basic lmap tests} {
    lmap i {} {
      return -level 0 x
    }
} {}
test lmap-1.13 {lmap errors} {
    list [catch {lmap {{a}{b}} {1 2 3} {}} msg] $msg
} {1 {list element in braces followed by "{b}" instead of space}}
test lmap-1.14 {lmap errors} {
    list [catch {lmap a {{1 2}3} {}} msg] $msg
} {1 {list element in braces followed by "3" instead of space}}
catch {unset a}
test lmap-1.15 {lmap errors} {
    catch {unset a}

    set a(0) 44
    list [catch {lmap a {1 2 3} {}} msg o] $msg $::errorInfo
} {1 {can't set "a": variable is array} {can't set "a": variable is array
    (setting foreach loop variable "a")
    invoked from within
"lmap a {1 2 3} {}"}}
test lmap-1.16 {lmap errors} {
    list [catch {lmap {} {} {}} msg] $msg
} {1 {foreach varlist is empty}}
catch {unset a}



# Parallel "lmap" operation (non-compiled)

test lmap-2.1 {parallel lmap tests} {
    lmap {a b} {1 2 3 4} {
	list $b $a
    }
} {{2 1} {4 3}}
test lmap-2.2 {parallel lmap tests} {
    lmap {a b} {1 2 3 4 5} {
................................................................................
} {{1111 2} 222 33 4}
test lmap-2.8 {parallel lmap tests} {
    lmap a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
	join [list $a $b $c $d $e] .
    }
} {{.1.1.1.1 2} .2.2.2. .3..3. ...4.}
test lmap-2.9 {lmap only sets vars if repeating loop} {
  namespace eval ::lmap_test {
    set rgb {65535 0 0}
    lmap {r g b} [set rgb] {}
    set ::x "r=$r, g=$g, b=$b"
  }
  namespace delete ::lmap_test
  set x
} {r=65535, g=0, b=0}
test lmap-2.10 {lmap only supports local scalar variables} {
  catch { unset a }

  lmap {a(3)} {1 2 3 4} {set {a(3)}}
} {1 2 3 4}
catch { unset a }



# "lmap" with "continue" and "break" (non-compiled)

test lmap-3.1 {continue tests} {
    lmap i {a b c d} {
	if {[string compare $i "b"] == 0} continue
	set i
    }
} {a c d}
test lmap-3.2 {continue tests} {
................................................................................
    	if {[string compare $i "b"] != 0} continue
    	set i
    }] $x
} {b 4}
test lmap-3.3 {break tests} {
    set x 0
    list [lmap i {a b c d} {
      incr x
    	if {[string compare $i "c"] == 0} break
    	set i
    }] $x
} {{a b} 3}
# Check for bug similar to #406709
test lmap-3.4 {break tests} {
	set a 1
	lmap b b {list [concat a; break]; incr a}
	incr a
} {2}


# ----- Compiled operation ------------------------------------------------------

# Basic "lmap" operation (compiled)

test lmap-4.1 {basic lmap tests} {
  apply {{} {
    set a {}
    lmap i {a b c d} {
      set a [concat $a $i]
    }
  }}
} {a {a b} {a b c} {a b c d}}
test lmap-4.2 {basic lmap tests} {
  apply {{} {
    lmap i {a b {{c d} e} {123 {{x}}}} {
      set i
    }
  }}
} {a b {{c d} e} {123 {{x}}}}
test lmap-4.2a {basic lmap tests} {
  apply {{} {
    lmap i {a b {{c d} e} {123 {{x}}}} {
      return -level 0 $i
    }
  }}
} {a b {{c d} e} {123 {{x}}}}
test lmap-4.3 {basic lmap tests} {catch { apply {{} { lmap }} } msg} 1
test lmap-4.4 {basic lmap tests} {
    catch { apply {{} { lmap }} } msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-4.5 {basic lmap tests} {catch { apply {{} { lmap i }} } msg} 1
test lmap-4.6 {basic lmap tests} {
    catch { apply {{} { lmap i }} } msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-4.7 {basic lmap tests} {catch { apply {{} { lmap i j }} } msg} 1
test lmap-4.8 {basic lmap tests} {
    catch { apply {{} { lmap i j }} } msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-4.9 {basic lmap tests} {catch { apply {{} { lmap i j k l }} } msg} 1
test lmap-4.10 {basic lmap tests} {
    catch { apply {{} { lmap i j k l }} } msg
    set msg
} {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-4.11 {basic lmap tests} {
  apply {{} { lmap i {} { set i } }}
} {}
test lmap-4.12 {basic lmap tests} {
  apply {{} { lmap i {} { return -level 0 x } }}
} {}
test lmap-4.13 {lmap errors} {
    list [catch { apply {{} { lmap {{a}{b}} {1 2 3} {} }} } msg] $msg
} {1 {list element in braces followed by "{b}" instead of space}}
test lmap-4.14 {lmap errors} {
    list [catch { apply {{} { lmap a {{1 2}3} {} }} } msg] $msg
} {1 {list element in braces followed by "3" instead of space}}
catch {unset a}

test lmap-4.15 {lmap errors} {
    apply {{} { 
      set a(0) 44
      list [catch {lmap a {1 2 3} {}} msg o] $msg $::errorInfo 
    }} 
} {1 {can't set "a": variable is array} {can't set "a": variable is array
    while executing
"lmap a {1 2 3} {}"}}
test lmap-4.16 {lmap errors} {

    list [catch { apply {{} { lmap {} {} {} }} } msg] $msg

} {1 {foreach varlist is empty}}
catch {unset a}



# Parallel "lmap" operation (compiled)

test lmap-5.1 {parallel lmap tests} {
  apply {{} {
    lmap {a b} {1 2 3 4} {
      list $b $a
    }
  }}
} {{2 1} {4 3}}
test lmap-5.2 {parallel lmap tests} {
  apply {{} {
    lmap {a b} {1 2 3 4 5} {
      list $b $a
    }
  }}
} {{2 1} {4 3} {{} 5}}
test lmap-5.3 {parallel lmap tests} {
  apply {{} {
    lmap a {1 2 3} b {4 5 6} {
      list $b $a
    }
  }}
} {{4 1} {5 2} {6 3}}
test lmap-5.4 {parallel lmap tests} {
  apply {{} {
    lmap a {1 2 3} b {4 5 6 7 8} {
	list $b $a
    }
  }}
} {{4 1} {5 2} {6 3} {7 {}} {8 {}}}
test lmap-5.5 {parallel lmap tests} {
  apply {{} {
    lmap {a b} {a b A B aa bb} c {c C cc CC} {
      list $a $b $c
    }
  }}
} {{a b c} {A B C} {aa bb cc} {{} {} CC}}
test lmap-5.6 {parallel lmap tests} {
  apply {{} {
    lmap a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} {
      list $a$b$c$d$e
    }
  }}
} {11111 22222 33333}
test lmap-5.7 {parallel lmap tests} {
  apply {{} {
    lmap a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
      set x $a$b$c$d$e
    }
  }}
} {{1111 2} 222 33 4}
test lmap-5.8 {parallel lmap tests} {
  apply {{} {
    lmap a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
      join [list $a $b $c $d $e] .
    }
  }}
} {{.1.1.1.1 2} .2.2.2. .3..3. ...4.}
test lmap-5.9 {lmap only sets vars if repeating loop} {
    apply {{} {
        set rgb {65535 0 0}
        lmap {r g b} [set rgb] {}
        return "r=$r, g=$g, b=$b"
    }}
................................................................................
} {r=65535, g=0, b=0}
test lmap-5.10 {lmap only supports local scalar variables} {
    apply {{} {
        lmap {a(3)} {1 2 3 4} {set {a(3)}}
    }}
} {1 2 3 4}


# "lmap" with "continue" and "break" (compiled)

test lmap-6.1 {continue tests} {
  apply {{} {
    lmap i {a b c d} {
      if {[string compare $i "b"] == 0} continue
      set i
    }
  }}
} {a c d}
test lmap-6.2 {continue tests} {
  apply {{} {
    list [lmap i {a b c d} {
      incr x
    	if {[string compare $i "b"] != 0} continue
    	set i
    }] $x
  }}
} {b 4}
test lmap-6.3 {break tests} {
  apply {{} {
    list [lmap i {a b c d} {
      incr x
    	if {[string compare $i "c"] == 0} break
    	set i
    }] $x
  }}
} {{a b} 3}
# Check for bug similar to #406709
test lmap-6.4 {break tests} {
    apply {{} {
	set a 1
	lmap b b {list [concat a; break]; incr a}
	incr a
    }}
} {2}



# ----- Special cases and bugs -------------------------------------------------


test lmap-7.1 {compiled lmap backward jump works correctly} {
    catch {unset x}


    array set x {0 zero 1 one 2 two 3 three}
    lsort [apply {{arrayName} {
        upvar 1 $arrayName a
        lmap member [array names a] {
            list $member [set a($member)]
        }
    }} x]
} [lsort {{0 zero} {1 one} {2 two} {3 three}}]

test lmap-7.2 {noncompiled lmap and shared variable or value list objects that are converted to another type} {

    catch {unset x}
    lmap {12.0} {a b c} {
        set x 12.0
        set x [expr $x + 1]
    }
} {13.0 13.0 13.0}

# Test for incorrect "double evaluation" semantics
test lmap-7.3 {delayed substitution of body} {
    apply {{} {
       set a 0
       lmap a [list 1 2 3] "
           set x $a
       "
       set x
    }}
} {0}

# Related to "foreach" test for [Bug 1189274]; crash on failure
test lmap-7.4 {empty list handling} {
    proc crash {} {
	rename crash {}
	set a "x y z"
	set b ""
	lmap aa $a bb $b { set x "aa = $aa bb = $bb" }
    }
    crash
} {{aa = x bb = } {aa = y bb = } {aa = z bb = }}

# Related to [Bug 1671138]; infinite loop with empty var list in bytecompiled version

test lmap-7.5 {compiled empty var list} {
    proc foo {} {
	lmap {} x {
	    error "reached body"
	}
    }
    list [catch { foo } msg] $msg


} {1 {foreach varlist is empty}}

test lmap-7.6 {lmap: related to "foreach" [Bug 1671087]} -setup {
    proc demo {} {
	set vals {1 2 3 4}
	trace add variable x write {string length $vals ;# }
	lmap {x y} $vals {format $y}
    }
} -body {
    demo
} -cleanup {
    rename demo {}
} -result {2 4}

# Huge lists must not overflow the bytecode interpreter (development bug)
test lmap-7.7 {huge list non-compiled} {
  set x [lmap a [lrepeat 1000000 x] { set b y$a }]
  list $b [llength $x] [string length $x]
} {yx 1000000 2999999}

test lmap-7.8 {huge list compiled} {
  set x [apply {{times} { lmap a [lrepeat $times x] { set b y$a }}} 1000000]
  list $b [llength $x] [string length $x]
} {yx 1000000 2999999}

test lmap-7.9 {error then dereference loop var (dev bug)} {
  catch { lmap a 0 b {1 2 3} { error x } }
  set a
} 0
test lmap-7.9a {error then dereference loop var (dev bug)} {
  catch { lmap a 0 b {1 2 3} { incr a $b; error x } }
  set a
} 1

# ----- Coroutines -------------------------------------------------------------

test lmap-8.1 {lmap non-compiled with coroutines} {
  coroutine coro apply {{} {
    set values [yield [info coroutine]]
    eval lmap i [list $values] {{ yield $i }}
  }} ;# returns 'coro'
  coro {a b c d e f} ;# -> a
  coro 1 ;# -> b
  coro 2 ;# -> c
  coro 3 ;# -> d
  coro 4 ;# -> e
  coro 5 ;# -> f
  list [coro 6] [info commands coro]


} {{1 2 3 4 5 6} {}}

test lmap-8.2 {lmap compiled with coroutines} {
  coroutine coro apply {{} {
    set values [yield [info coroutine]]
    lmap i $values { yield $i }
  }} ;# returns 'coro'
  coro {a b c d e f} ;# -> a
  coro 1 ;# -> b
  coro 2 ;# -> c
  coro 3 ;# -> d
  coro 4 ;# -> e
  coro 5 ;# -> f
  list [coro 6] [info commands coro]


} {{1 2 3 4 5 6} {}}


# cleanup
catch {unset a}
catch {unset x}

catch {rename foo {}}
::tcltest::cleanupTests
return










|
|



|
<
<
|
|

<

<













|


<
|
|
<
|
<
|
|
<
|
<
|
|
<
|
<
|
|
<
|


|




|


|
|
|
|
|
|
|
|
|
>


|
|


|
|
|
<
>

<

<







 







|
|
|
|
|
|
|

|
|
>
|
|
<
>

<

<







 







|






|
|
|


<
|


<

|
|
|
|
|
|


|
|
|
|
|


|
|
|
|
|

<
|
|
<
|
<
|
|
<
|
<
|
|
<
|
<
|
|
<
|

|


|

|
|
|
|
|
|
<
>


|
|




|
>
|
>
|
<
>

<

<

|
|
|
|
|


|
|
|
|
|


|
|
|
|
|


|
|
|
|
|


|
|
|
|
|


|
|
|
|
|


|
|
|
|
|


|
|
|
|
|







 







<

<

|
|
|
|
|
|


|
|
|
|
|
|
|


|
|
|
|
|
|
|










<
<
|
<
<
|
<
>
>







|
<
|
>
|




|
<







|


<










<
|
>
|





|
>
>
|
<











<


|
|

<

|
|

<

|
|


|
|


|
<
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
<
|
|
|
|
|
|
|
|
|
|
|
|
>
>
|
|
<

<
<
>



>
>
>
>
9
10
11
12
13
14
15
16
17
18
19
20
21


22
23
24

25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

42
43

44

45
46

47

48
49

50

51
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

83
84

85

86
87
88
89
90
91
92
...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

140
141

142

143
144
145
146
147
148
149
...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

172
173
174

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

197
198

199

200
201

202

203
204

205

206
207

208
209
210
211
212
213
214
215
216
217
218
219
220

221
222
223
224
225
226
227
228
229
230
231
232
233
234

235
236

237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
...
300
301
302
303
304
305
306

307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342


343


344

345
346
347
348
349
350
351
352
353
354

355
356
357
358
359
360
361
362

363
364
365
366
367
368
369
370
371
372

373
374
375
376
377
378
379
380
381
382

383
384
385
386
387
388
389
390
391
392
393
394

395
396
397
398
399
400
401
402
403
404
405

406
407
408
409
410

411
412
413
414

415
416
417
418
419
420
421
422
423
424

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

456


457
458
459
460
461
462
463
464
# Copyright (c) 2011 Trevor Davel
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $

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

unset -nocomplain a i x


 
# ----- Non-compiled operation -----------------------------------------------


# Basic "lmap" operation (non-compiled)

test lmap-1.1 {basic lmap tests} {
    set a {}
    lmap i {a b c d} {
	set a [concat $a $i]
    }
} {a {a b} {a b c} {a b c d}}
test lmap-1.2 {basic lmap tests} {
    lmap i {a b {{c d} e} {123 {{x}}}} {
	set i
    }
} {a b {{c d} e} {123 {{x}}}}
test lmap-1.2a {basic lmap tests} {
    lmap i {a b {{c d} e} {123 {{x}}}} {
	return -level 0 $i
    }
} {a b {{c d} e} {123 {{x}}}}

test lmap-1.4 {basic lmap tests} -returnCodes error -body {
    lmap

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}

test lmap-1.6 {basic lmap tests} -returnCodes error -body {
    lmap i

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}

test lmap-1.8 {basic lmap tests} -returnCodes error -body {
    lmap i j

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}

test lmap-1.10 {basic lmap tests} -returnCodes error -body {
    lmap i j k l

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-1.11 {basic lmap tests} {
    lmap i {} {
	set i
    }
} {}
test lmap-1.12 {basic lmap tests} {
    lmap i {} {
	return -level 0 x
    }
} {}
test lmap-1.13 {lmap errors} -returnCodes error -body {
    lmap {{a}{b}} {1 2 3} {}
} -result {list element in braces followed by "{b}" instead of space}
test lmap-1.14 {lmap errors} -returnCodes error -body {
    lmap a {{1 2}3} {}
} -result {list element in braces followed by "3" instead of space}
unset -nocomplain a
test lmap-1.15 {lmap errors} -setup {
    unset -nocomplain a
} -body {
    set a(0) 44
    list [catch {lmap a {1 2 3} {}} msg o] $msg $::errorInfo
} -result {1 {can't set "a": variable is array} {can't set "a": variable is array
    (setting lmap loop variable "a")
    invoked from within
"lmap a {1 2 3} {}"}}
test lmap-1.16 {lmap errors} -returnCodes error -body {
    lmap {} {} {}
} -result {lmap varlist is empty}

unset -nocomplain a


# Parallel "lmap" operation (non-compiled)

test lmap-2.1 {parallel lmap tests} {
    lmap {a b} {1 2 3 4} {
	list $b $a
    }
} {{2 1} {4 3}}
test lmap-2.2 {parallel lmap tests} {
    lmap {a b} {1 2 3 4 5} {
................................................................................
} {{1111 2} 222 33 4}
test lmap-2.8 {parallel lmap tests} {
    lmap a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
	join [list $a $b $c $d $e] .
    }
} {{.1.1.1.1 2} .2.2.2. .3..3. ...4.}
test lmap-2.9 {lmap only sets vars if repeating loop} {
    namespace eval ::lmap_test {
	set rgb {65535 0 0}
	lmap {r g b} [set rgb] {}
	set ::x "r=$r, g=$g, b=$b"
    }
    namespace delete ::lmap_test
    set x
} {r=65535, g=0, b=0}
test lmap-2.10 {lmap only supports local scalar variables} -setup {
    unset -nocomplain a
} -body {
    lmap {a(3)} {1 2 3 4} {set {a(3)}}
} -result {1 2 3 4}

unset -nocomplain a


# "lmap" with "continue" and "break" (non-compiled)

test lmap-3.1 {continue tests} {
    lmap i {a b c d} {
	if {[string compare $i "b"] == 0} continue
	set i
    }
} {a c d}
test lmap-3.2 {continue tests} {
................................................................................
    	if {[string compare $i "b"] != 0} continue
    	set i
    }] $x
} {b 4}
test lmap-3.3 {break tests} {
    set x 0
    list [lmap i {a b c d} {
	incr x
    	if {[string compare $i "c"] == 0} break
    	set i
    }] $x
} {{a b} 3}
# Check for bug similar to #406709
test lmap-3.4 {break tests} {
    set a 1
    lmap b b {list [concat a; break]; incr a}
    incr a
} {2}


# ----- Compiled operation ---------------------------------------------------

# Basic "lmap" operation (compiled)

test lmap-4.1 {basic lmap tests} {
    apply {{} {
	set a {}
	lmap i {a b c d} {
	    set a [concat $a $i]
	}
    }}
} {a {a b} {a b c} {a b c d}}
test lmap-4.2 {basic lmap tests} {
    apply {{} {
	lmap i {a b {{c d} e} {123 {{x}}}} {
	    set i
	}
    }}
} {a b {{c d} e} {123 {{x}}}}
test lmap-4.2a {basic lmap tests} {
    apply {{} {
	lmap i {a b {{c d} e} {123 {{x}}}} {
	    return -level 0 $i
	}
    }}
} {a b {{c d} e} {123 {{x}}}}

test lmap-4.4 {basic lmap tests} -returnCodes error -body {
    apply {{} { lmap }}

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}

test lmap-4.6 {basic lmap tests} -returnCodes error -body {
    apply {{} { lmap i }}

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}

test lmap-4.8 {basic lmap tests} -returnCodes error -body {
    apply {{} { lmap i j }}

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}

test lmap-4.10 {basic lmap tests} -returnCodes error -body {
    apply {{} { lmap i j k l }}

} -result {wrong # args: should be "lmap varList list ?varList list ...? command"}
test lmap-4.11 {basic lmap tests} {
    apply {{} { lmap i {} { set i } }}
} {}
test lmap-4.12 {basic lmap tests} {
    apply {{} { lmap i {} { return -level 0 x } }}
} {}
test lmap-4.13 {lmap errors} -returnCodes error -body {
    apply {{} { lmap {{a}{b}} {1 2 3} {} }}
} -result {list element in braces followed by "{b}" instead of space}
test lmap-4.14 {lmap errors} -returnCodes error -body {
    apply {{} { lmap a {{1 2}3} {} }}
} -result {list element in braces followed by "3" instead of space}

unset -nocomplain a
test lmap-4.15 {lmap errors} {
    apply {{} { 
	set a(0) 44
	list [catch {lmap a {1 2 3} {}} msg o] $msg $::errorInfo 
    }} 
} {1 {can't set "a": variable is array} {can't set "a": variable is array
    while executing
"lmap a {1 2 3} {}"}}
test lmap-4.16 {lmap errors} -returnCodes error -body {
    apply {{} {
	lmap {} {} {}
    }}
} -result {lmap varlist is empty}

unset -nocomplain a


# Parallel "lmap" operation (compiled)

test lmap-5.1 {parallel lmap tests} {
    apply {{} {
	lmap {a b} {1 2 3 4} {
	    list $b $a
	}
    }}
} {{2 1} {4 3}}
test lmap-5.2 {parallel lmap tests} {
    apply {{} {
	lmap {a b} {1 2 3 4 5} {
	    list $b $a
	}
    }}
} {{2 1} {4 3} {{} 5}}
test lmap-5.3 {parallel lmap tests} {
    apply {{} {
	lmap a {1 2 3} b {4 5 6} {
	    list $b $a
	}
    }}
} {{4 1} {5 2} {6 3}}
test lmap-5.4 {parallel lmap tests} {
    apply {{} {
	lmap a {1 2 3} b {4 5 6 7 8} {
	    list $b $a
	}
    }}
} {{4 1} {5 2} {6 3} {7 {}} {8 {}}}
test lmap-5.5 {parallel lmap tests} {
    apply {{} {
	lmap {a b} {a b A B aa bb} c {c C cc CC} {
	    list $a $b $c
	}
    }}
} {{a b c} {A B C} {aa bb cc} {{} {} CC}}
test lmap-5.6 {parallel lmap tests} {
    apply {{} {
	lmap a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} {
	    list $a$b$c$d$e
	}
    }}
} {11111 22222 33333}
test lmap-5.7 {parallel lmap tests} {
    apply {{} {
	lmap a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
	    set x $a$b$c$d$e
	}
    }}
} {{1111 2} 222 33 4}
test lmap-5.8 {parallel lmap tests} {
    apply {{} {
	lmap a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
	    join [list $a $b $c $d $e] .
	}
    }}
} {{.1.1.1.1 2} .2.2.2. .3..3. ...4.}
test lmap-5.9 {lmap only sets vars if repeating loop} {
    apply {{} {
        set rgb {65535 0 0}
        lmap {r g b} [set rgb] {}
        return "r=$r, g=$g, b=$b"
    }}
................................................................................
} {r=65535, g=0, b=0}
test lmap-5.10 {lmap only supports local scalar variables} {
    apply {{} {
        lmap {a(3)} {1 2 3 4} {set {a(3)}}
    }}
} {1 2 3 4}


# "lmap" with "continue" and "break" (compiled)

test lmap-6.1 {continue tests} {
    apply {{} {
	lmap i {a b c d} {
	    if {[string compare $i "b"] == 0} continue
	    set i
	}
    }}
} {a c d}
test lmap-6.2 {continue tests} {
    apply {{} {
	list [lmap i {a b c d} {
	    incr x
	    if {[string compare $i "b"] != 0} continue
	    set i
	}] $x
    }}
} {b 4}
test lmap-6.3 {break tests} {
    apply {{} {
	list [lmap i {a b c d} {
	    incr x
	    if {[string compare $i "c"] == 0} break
	    set i
	}] $x
    }}
} {{a b} 3}
# Check for bug similar to #406709
test lmap-6.4 {break tests} {
    apply {{} {
	set a 1
	lmap b b {list [concat a; break]; incr a}
	incr a
    }}
} {2}



# ----- Special cases and bugs -----------------------------------------------


test lmap-7.1 {compiled lmap backward jump works correctly} -setup {

    unset -nocomplain x
} -body {
    array set x {0 zero 1 one 2 two 3 three}
    lsort [apply {{arrayName} {
        upvar 1 $arrayName a
        lmap member [array names a] {
            list $member [set a($member)]
        }
    }} x]
} -result [lsort {{0 zero} {1 one} {2 two} {3 three}}]

test lmap-7.2 {noncompiled lmap and shared variable or value list objects that are converted to another type} -setup {
    unset -nocomplain x
} -body {
    lmap {12.0} {a b c} {
        set x 12.0
        set x [expr $x + 1]
    }
} -result {13.0 13.0 13.0}

# Test for incorrect "double evaluation" semantics
test lmap-7.3 {delayed substitution of body} {
    apply {{} {
       set a 0
       lmap a [list 1 2 3] "
           set x $a
       "
       return $x
    }}
} {0}

# Related to "foreach" test for [Bug 1189274]; crash on failure
test lmap-7.4 {empty list handling} {
    proc crash {} {
	rename crash {}
	set a "x y z"
	set b ""
	lmap aa $a bb $b { set x "aa = $aa bb = $bb" }
    }
    crash
} {{aa = x bb = } {aa = y bb = } {aa = z bb = }}

# Related to [Bug 1671138]; infinite loop with empty var list in bytecompiled
# version.
test lmap-7.5 {compiled empty var list} -returnCodes error -body {
    proc foo {} {
	lmap {} x {
	    error "reached body"
	}
    }
    foo
} -cleanup {
    catch {rename foo ""}
} -result {lmap varlist is empty}

test lmap-7.6 {lmap: related to "foreach" [Bug 1671087]} -setup {
    proc demo {} {
	set vals {1 2 3 4}
	trace add variable x write {string length $vals ;# }
	lmap {x y} $vals {format $y}
    }
} -body {
    demo
} -cleanup {
    rename demo {}
} -result {2 4}

# Huge lists must not overflow the bytecode interpreter (development bug)
test lmap-7.7 {huge list non-compiled} {
    set x [lmap a [lrepeat 1000000 x] { set b y$a }]
    list $b [llength $x] [string length $x]
} {yx 1000000 2999999}

test lmap-7.8 {huge list compiled} {
    set x [apply {{times} { lmap a [lrepeat $times x] { set b y$a }}} 1000000]
    list $b [llength $x] [string length $x]
} {yx 1000000 2999999}

test lmap-7.9 {error then dereference loop var (dev bug)} {
    catch { lmap a 0 b {1 2 3} { error x } }
    set a
} 0
test lmap-7.9a {error then dereference loop var (dev bug)} {
    catch { lmap a 0 b {1 2 3} { incr a $b; error x } }
    set a
} 1

# ----- Coroutines -----------------------------------------------------------

test lmap-8.1 {lmap non-compiled with coroutines} -body {
    coroutine coro apply {{} {
	set values [yield [info coroutine]]
	eval lmap i [list $values] {{ yield $i }}
    }} ;# returns 'coro'
    coro {a b c d e f} ;# -> a
    coro 1 ;# -> b
    coro 2 ;# -> c
    coro 3 ;# -> d
    coro 4 ;# -> e
    coro 5 ;# -> f
    list [coro 6] [info commands coro]
} -cleanup {
    catch {rename coro ""}
} -result {{1 2 3 4 5 6} {}}

test lmap-8.2 {lmap compiled with coroutines} -body {
    coroutine coro apply {{} {
	set values [yield [info coroutine]]
	lmap i $values { yield $i }
    }} ;# returns 'coro'
    coro {a b c d e f} ;# -> a
    coro 1 ;# -> b
    coro 2 ;# -> c
    coro 3 ;# -> d
    coro 4 ;# -> e
    coro 5 ;# -> f
    list [coro 6] [info commands coro]
} -cleanup {
    catch {rename coro ""}
} -result {{1 2 3 4 5 6} {}}
 

# cleanup


unset -nocomplain a x
catch {rename foo {}}
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# End: