summaryrefslogtreecommitdiffstats
path: root/tests/scan.test
blob: 50bf876a1b8c8e0f5dc21a4a8b6bcc41c4f0caea (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
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
# Commands covered:  scan
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1994 The Regents of the University of California.
# Copyright (c) 1994-1997 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# SCCS: @(#) scan.test 1.26 97/08/06 08:56:08

if {[string compare test [info procs test]] == 1} then {source defs}

test scan-1.1 {integer scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "-20 1476 \n33 0" "%d %d %d %d" a b c d] $a $b $c $d
} {4 -20 1476 33 0}
test scan-1.2 {integer scanning} {
    set a {}; set b {}; set c {}
    list [scan "-45 16 7890 +10" "%2d %*d %10d %d" a b c] $a $b $c
} {3 -4 16 7890}
test scan-1.3 {integer scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "-45 16 +10 987" "%ld %d %ld %d" a b c d] $a $b $c $d
} {4 -45 16 10 987}
test scan-1.4 {integer scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "14 1ab 62 10" "%d %x %lo %x" a b c d] $a $b $c $d
} {4 14 427 50 16}
test scan-1.5 {integer scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "12345670 1234567890ab cdefg" "%o	 %o %x %lx" a b c d] \
	    $a $b $c $d
} {4 2739128 342391 561323 52719}
test scan-1.6 {integer scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "ab123-24642" "%2x %3x %3o %2o" a b c d] $a $b $c $d
} {4 171 291 -20 52}
test scan-1.7 {integer scanning} {
    set a {}; set b {}
    list [scan "1234567 234 567  " "%*3x %x %*o %4o" a b] $a $b
} {2 17767 375}
test scan-1.8 {integer scanning} {
    set a {}; set b {}
    list [scan "a	1234" "%d %d" a b] $a $b
} {0 {} {}}
test scan-1.9 {integer scanning} {
    set a {}; set b {}; set c {}; set d {};
    list [scan "12345678" "%2d %2d %2ld %2d" a b c d] $a $b $c $d
} {4 12 34 56 78}
test scan-1.10 {integer scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "1 2 " "%hd %d %d %d" a b c d] $a $b $c $d
} {2 1 2 {} {}}
#
# The behavior for scaning intergers larger than MAX_INT is
# not defined by the ANSI spec.  Some implementations wrap the
# input (-16) some return MAX_INT.
#
test scan-1.11 {integer scanning} {nonPortable} {
    set a {}; set b {};
    list [scan "4294967280 4294967280" "%u %d" a b] $a $b
} {2 4294967280 -16}

test scan-2.1 {floating-point scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "2.1 -3.0e8 .99962 a" "%f%g%e%f" a b c d] $a $b $c $d
} {3 2.1 -300000000.0 0.99962 {}}
test scan-2.2 {floating-point scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "-1.2345 +8.2 9" "%3e %3lf %f %f" a b c d] $a $b $c $d
} {4 -1.0 234.0 5.0 8.2}
test scan-2.3 {floating-point scanning} {
    set a {}; set b {}; set c {}
    list [scan "1e00004 332E-4 3e+4" "%Lf %*2e %f %f" a b c] $a $c
} {3 10000.0 30000.0}
#
# Some libc implementations consider 3.e- bad input.  The ANSI
# spec states that digits must follow the - sign.
#
test scan-2.4 {floating-point scanning} {nonPortable} {
    set a {}; set b {}; set c {}
    list [scan "1. 47.6 2.e2 3.e-" "%f %*f %f %f" a b c] $a $b $c
} {3 1.0 200.0 3.0}
test scan-2.5 {floating-point scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "4.6 99999.7 876.43e-1 118" "%f %f %f %e" a b c d] $a $b $c $d
} {4 4.6 99999.7 87.643 118.0}
test scan-2.6 {floating-point scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "1.2345 697.0e-3 124 .00005" "%f %e %f %e" a b c d] $a $b $c $d
} {4 1.2345 0.697 124.0 5e-05}
test scan-2.7 {floating-point scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "4.6abc" "%f %f %f %f" a b c d] $a $b $c $d
} {1 4.6 {} {} {}}
test scan-2.8 {floating-point scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "4.6 5.2" "%f %f %f %f" a b c d] $a $b $c $d
} {2 4.6 5.2 {} {}}

test scan-3.1 {string and character scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "abc defghijk dum " "%s %3s %20s %s" a b c d] $a $b $c $d
} {4 abc def ghijk dum}
test scan-3.2 {string and character scanning} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "a       bcdef" "%c%c%1s %s" a b c d] $a $b $c $d
} {4 97 32 b cdef}
test scan-3.3 {string and character scanning} {
    set a {}; set b {}; set c {}
    list [scan "123456 test " "%*c%*s %s %s %s" a b c] $a $b $c
} {1 test {} {}}
test scan-3.4 {string and character scanning} {
    set a {}; set b {}; set c {}; set d
    list [scan "ababcd01234  f 123450" {%4[abcd] %4[abcd] %[^abcdef] %[^0]} a b c d] $a $b $c $d
} {4 abab cd {01234  } {f 12345}}
test scan-3.5 {string and character scanning} {
    set a {}; set b {}; set c {}
    list [scan "aaaaaabc aaabcdefg  + +  XYZQR" {%*4[a] %s %*4[a]%s%*4[ +]%c} a b c] $a $b $c
} {3 aabc bcdefg 43}

test scan-4.1 {error conditions} {
    catch {scan a}
} 1
test scan-4.2 {error conditions} {
    catch {scan a} msg
    set msg
} {wrong # args: should be "scan string format ?varName varName ...?"}
test scan-4.3 {error conditions} {
    catch {scan "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21}
} 1
test scan-4.4 {error conditions} {
    catch {scan "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21} msg
    set msg
} {too many fields to scan}
test scan-4.5 {error conditions} {
    list [catch {scan a %D} msg] $msg
} {1 {bad scan conversion character "D"}}
test scan-4.6 {error conditions} {
    list [catch {scan a %O} msg] $msg
} {1 {bad scan conversion character "O"}}
test scan-4.7 {error conditions} {
    list [catch {scan a %X} msg] $msg
} {1 {bad scan conversion character "X"}}
test scan-4.8 {error conditions} {
    list [catch {scan a %F} msg] $msg
} {1 {bad scan conversion character "F"}}
test scan-4.9 {error conditions} {
    list [catch {scan a %E} msg] $msg
} {1 {bad scan conversion character "E"}}
test scan-4.10 {error conditions} {
    list [catch {scan a "%d %d" a} msg] $msg
} {1 {different numbers of variable names and field specifiers}}
test scan-4.11 {error conditions} {
    list [catch {scan a "%d %d" a b c} msg] $msg
} {1 {different numbers of variable names and field specifiers}}
test scan-4.12 {error conditions} {
    set a {}; set b {}; set c {}; set d {}
    list [expr {[scan "  a" " a %d %d %d %d" a b c d] <= 0}] $a $b $c $d
} {1 {} {} {} {}}
test scan-4.13 {error conditions} {
    set a {}; set b {}; set c {}; set d {}
    list [scan "1 2" "%d %d %d %d" a b c d] $a $b $c $d
} {2 1 2 {} {}}
test scan-4.14 {error conditions} {
    catch {unset a}
    set a(0) 44
    list [catch {scan 44 %d a} msg] $msg
} {1 {couldn't set variable "a"}}
test scan-4.15 {error conditions} {
    catch {unset a}
    set a(0) 44
    list [catch {scan 44 %c a} msg] $msg
} {1 {couldn't set variable "a"}}
test scan-4.16 {error conditions} {
    catch {unset a}
    set a(0) 44
    list [catch {scan 44 %s a} msg] $msg
} {1 {couldn't set variable "a"}}
test scan-4.17 {error conditions} {
    catch {unset a}
    set a(0) 44
    list [catch {scan 44 %f a} msg] $msg
} {1 {couldn't set variable "a"}}
test scan-4.18 {error conditions} {
    catch {unset a}
    set a(0) 44
    list [catch {scan 44 %f a} msg] $msg
} {1 {couldn't set variable "a"}}
catch {unset a}
test scan-4.19 {error conditions} {
    list [catch {scan 44 %2c a} msg] $msg
} {1 {field width may not be specified in %c conversion}}
test scan-4.20 {error conditions} {
    list [catch {scan abc {%[}} msg] $msg
} {1 {unmatched [ in format string}}

test scan-5.1 {lots of arguments} {
    scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20
} 20
test scan-5.2 {lots of arguments} {
    scan "10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200" "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d" a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20
    set a20
} 200

test scan-6.1 {miscellaneous tests} {
    set a {}
    list [scan ab16c ab%dc a] $a
} {1 16}
test scan-6.2 {miscellaneous tests} {
    set a {}
    list [scan ax16c ab%dc a] $a
} {0 {}}
test scan-6.3 {miscellaneous tests} {
    set a {}
    list [catch {scan ab%c114 ab%%c%d a} msg] $msg $a
} {0 1 114}
test scan-6.4 {miscellaneous tests} {
    set a {}
    list [catch {scan ab%c14 ab%%c%d a} msg] $msg $a
} {0 1 14}

test scan-7.1 {alignment in results array (TCL_ALIGN)} {
    scan "123 13.6" "%s %f" a b
    set b
} 13.6
test scan-7.2 {alignment in results array (TCL_ALIGN)} {
    scan "1234567 13.6" "%s %f" a b
    set b
} 13.6
test scan-7.3 {alignment in results array (TCL_ALIGN)} {
    scan "12345678901 13.6" "%s %f" a b
    set b
} 13.6
test scan-7.4 {alignment in results array (TCL_ALIGN)} {
    scan "123456789012345 13.6" "%s %f" a b
    set b
} 13.6
test scan-7.5 {alignment in results array (TCL_ALIGN)} {
    scan "1234567890123456789 13.6" "%s %f" a b
    set b
} 13.6