aboutsummaryrefslogtreecommitdiff
path: root/tests/loop.test
blob: c6144e0281a0d2211dcf8a822c4d255fba79279e (plain)
1
2
3
4
5
6
7
8
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
150
151
152
153
154
155
156
157
158
159
160
source [file dirname [info script]]/testing.tcl

# Check "loop" and its use of continue and break.
needs cmd loop

catch {unset a i}
test loop-1.1 {loop tests} {
    set a {}
    loop i 1 6 {
        set a [concat $a $i]
    }
    set a
} {1 2 3 4 5}

test loop-1.2 {loop tests} {
    set a {}
    loop i 1 6 {
        if $i==4 continue
        set a [concat $a $i]
    }
    set a
} {1 2 3 5}

test loop-1.3 {loop tests} {
    set a {}
    loop i 1 6 {
        if $i==4 break
        set a [concat $a $i]
    }
    set a
} {1 2 3}

test loop-1.5 {loop errors} {
    catch {loop 1 2 3} msg
} {1}

test loop-1.6 {loop errors} {
    catch {loop 1 2 3 4 5} msg
} {1}

test loop-1.7 {loop tests} {
    set a {xyz}
    loop i 1 6 {
    }
    set a
} xyz

test loop-1.8 {error in loop} {
    set rc [catch {
        set a {}
        loop i 1 6 {
            lappend a $i
            if {$i == 3} {
                error "stop"
            }
        }
    }]
    list $a $rc
} {{1 2 3} 1}

test loop-1.9 {loop incr} {
    set a {}
    loop i 0 6 2 {
        lappend a $i
    }
    set a
} {0 2 4}

test loop-1.10 {no exec infinite loop} {
    set a {}
    loop i 0 6 -1 {
        lappend a $i
        break
    }
    set a
} {}

test loop-1.11 {no start} {
    set a {}
    loop i  5 {
        lappend a $i
    }
    set a
} {0 1 2 3 4}

test loop-2.1 {loop shimmering tests} {
    loop i 1 6 {
    }
    set i
} 6

test loop-2.2 {loop shimmering tests} {
    # Setting the variable inside the loop doesn't
    # affect the loop or the final variable value
    loop i 1 6 {
        set i blah
    }
    set i
} 6

test loop-2.3 {loop shimmering tests} {
    set a {}
    loop i 1 6 {
        lappend a $i
        set i blah
        lappend a $i
    }
    set a
} {1 blah 2 blah 3 blah 4 blah 5 blah}

test loop-2.4 {loop shimmering tests} {
    set i xyz
    loop i 1 6 {
    }
    set i
} 6

test loop-2.5 {loop shimmering tests} {
    # Ensure that the string rep of $i is updated
    set i {1 3}
    loop i(1) 1 6 {
    }
    set i
} {1 6}

test loop-2.6 {modify loop var} {
    unset -nocomplain i
    catch {
        loop i(1) 1 6 {
            # this makes it impossible to set the loop var
            set i blah
        }
    }
} 1

test loop-2.7 {unset loop var} {
    unset -nocomplain i
    loop i 1 6 {
        # var will simply get recreated on each loop
        unset i
    }
    set i
} 6

test loop-2.8 {modify loop var} {
    unset -nocomplain i
    set a {}
    loop i 1 6 {
        lappend a $i
        incr i
    }
    set a
} {1 2 3 4 5}

testreport
break



testreport