1 # $Id: test_scanf.rb 19657 2008-10-01 13:46:53Z mame $
12 # Comment out either of these lines to skip those tests.
14 class TestStringScanf < Test::Unit::TestCase;end
15 class TestIOScanf < Test::Unit::TestCase;end
23 [ "%2[a]", "nbc", []],
24 [ "%*d %*3d %*s", "123 +456 abc", [] ],
25 [ "%d%c", "123 x", [ 123, " " ] ],
26 [ "%d%c", "123x", [ 123, "x" ] ],
27 [ "%d %c", "123x", [ 123, "x" ] ],
28 [ "%d %c", "123 x", [ 123, "x" ] ],
34 # ]; end; def nothing; [
40 [ "%d%[abc]", "eabc", [] ],
41 [ "%d\n%[abc]", "\neabc", [] ],
42 [ "%d%[^abc]", "ghiabc", [ ] ],
43 [ "%d%[abc]", "abc", [] ],
44 [ "%*d %*3d %*s", "123 +456 abc", [] ],
46 [ "%d%s", "blah 123 string", [] ],
47 [ "%[\n]", "abc\n", [] ],
48 [ "%[\n]", "abc\n", [] ],
49 [ "%[\n]", "abc\n", [] ],
52 [ "%f", "z3.2534", [] ],
54 [ "", "abc 123", [] ],
55 [ '%[^\\w]%c', "a...1", [] ],
58 [ "%3x", "0xz", [0] ],
61 [ "%3i", "097", [0] ],
62 [ "%3i", "0xz", [0] ],
63 [ "%1i", "3", [ 3 ] ],
64 [ "%2i", "07", [ 7 ] ],
65 [ "%2i", "0a", [ 0 ] ],
68 [ "%3c", "abc", [ "abc" ] ],
69 [ "%3c", "a\nb", [ "a\nb" ] ],
70 [ "%3c", "a\nbcd", [ "a\nb" ] ],
71 [ "%c\n\n", "x\n\n", [ "x" ] ],
72 [ "%c\n\n", "x\n\n", [ "x" ] ],
73 [ "%c", "\n", [ "\n" ] ],
74 [ "%c", "x\n", [ "x" ] ],
75 [ "%2c", " 123", [" 1"] ],
76 [ " %c", " x", ["x"] ],
77 [ "%c", " x", [" "] ],
78 [ "%c", "123", ["1"] ],
79 [ "%2c", "123", ["12"] ],
80 [ "%5c", "a\nb\n\n", [ "a\nb\n\n" ] ],
81 [ "%6c", "a\nb\n\nx", [ "a\nb\n\nx" ] ],
82 [ "%5c", "ab\ncd", [ "ab\ncd" ] ],
83 [ "%5c", "a\nb\n\n", [ "a\nb\n\n" ] ],
86 [ "%3o", "0xz", [0] ],
89 [ "%d", "\n123", [ 123 ] ],
90 [ "%d", "\n\n123", [ 123 ] ],
91 [ "%d", "\n123", [ 123 ] ],
98 # various mixed specifiers
100 [ "%[^\\w]%c", "...1", [ "...", "1"] ],
101 [ '%[^\\w]%c', "...1", [ "...", "1"] ],
102 [ "%[abc\n]%d", "a\n\nb\n\nc 123", [ "a\n\nb\n\nc", 123 ] ],
103 [ "%[abc\n]%d", "a\n\nb\n\nc \t 123", [ "a\n\nb\n\nc", 123 ] ],
104 [ "%[abc\t]%d", "a\t\tb\t\tc 123", [ "a\t\tb\t\tc", 123 ] ],
105 [ "%d%3[abc\n]", "123a\nbeaab", [ 123, "a\nb" ] ],
106 [ "%d%20c", "42 is the key", [ 42, " is the key" ] ],
107 [ "%d %20c", "42 is the key", [ 42, "is the key" ] ],
108 [ "%d%3[^abc\n]%d", "123de\nf123", [ 123, "de" ] ],
109 [ "%d %4c", "3abc", [ 3, "abc" ] ],
110 [ "%f%d\n%[abc]", "1\neabc", [1.0] ],
111 [ "%d%3[abc]", "123aaab", [ 123, "aaa" ] ],
112 [ "%d%3[abc]", "123 aaab", [ 123 ] ],
113 [ "%d%3[abc]", "123aeaab", [ 123, "a" ] ],
114 [ "%d%[^abc]", "123defabc", [123, "def" ] ],
115 [ "%d%3[^abc]", "123defdef", [ 123, "def" ] ],
116 [ "%d%3[^abc] ", "123defdef ", [ 123, "def" ] ],
117 [ "%d%3[^abc]ghi", "123defghi", [ 123, "def" ] ],
118 [ "%d%3[^abc]", "123defdef", [ 123, "def" ] ],
119 [ "%d%3[^abc]", "123adefdef", [ 123 ] ],
120 [ "%d%3[^abc]", "123deafdef", [ 123, "de" ] ],
121 [ "%d%3[^abc\n]", "123de\nf", [ 123, "de" ] ],
122 [ "%s%c%c%s", "abc\n\ndef", ["abc", "\n","\n", "def" ] ],
123 [ "%c%d", "\n\n123", [ "\n",123 ] ],
124 [ "%s%c%d", "abc\n123", [ "abc", "\n", 123 ] ],
125 [ "%s%c%d", "abc\n\n123", [ "abc", "\n", 123 ] ],
126 [ "%c%d", "\t\n123", [ "\t",123 ] ],
127 [ "%s%c%d", "abc\t\n123", [ "abc", "\t", 123 ] ],
128 [ "%3c%d", "abc123", [ "abc", 123 ] ],
129 [ "%3c\n%d", "abc123", [ "abc", 123 ] ],
130 [ "%3c\n%d", "abc 123", [ "abc", 123 ] ],
131 [ "%3c %d", "abc123", [ "abc", 123 ] ],
132 [ "%3c\t%d", "abc \n 123", [ "abc", 123 ] ],
133 [ "%3c\t%d", "abc \n 123 ", [ "abc", 123 ] ],
134 [ "%3c%d", "a\nb123", [ "a\nb", 123 ] ],
135 [ "%f%3c", "1.2x\ny", [ 1.2, "x\ny"] ],
136 [ "%d\n%d\n%d", "123 456 789", [ 123,456,789 ] ],
137 [ "%d\n%i%2d%x\n%d", "123 0718932", [ 123, 071, 89, 0x32] ],
138 [ "%c\n%c", "x y", [ "x", "y" ] ],
139 [ "%c\t%c", "x y", [ "x", "y" ] ],
140 [ "%s\n%s", "x y", [ "x", "y" ] ],
141 [ "%s%s\n", "x y", [ "x", "y" ] ],
142 [ "%c\n\n%c", "x\n\ny", [ "x", "y" ] ],
143 [ "%s%d%d", "abc\n123\n456", [ "abc", 123, 456 ] ],
144 [ "%3s%c%3c%d", "1.2x\n\ny123", [ "1.2", "x", "\n\ny", 123 ] ],
145 [ "%f%3c", "1.2x\ny", [ 1.2, "x\ny"] ],
146 [ "%c\n%c", "x\n\ny", [ "x", "y" ] ],
147 [ "%c\n\n%c", "x\n\ny", [ "x", "y" ] ],
148 [ "%c %c", "x\n\ny", [ "x", "y" ] ],
149 [ "%s\n\n%c", "x\n\ny", [ "x", "y" ] ],
150 [ "%s\n\n%s", "x\n\ny", [ "x", "y" ] ],
151 [ "%d\n\n%d", "23\n\n45", [ 23, 45 ] ],
152 [ "%d\n%d", "23\n\n45", [ 23, 45 ] ],
153 [ "%c\n\n%c", "x y", [ "x", "y" ] ],
154 [ "%c%c", "x\n\ny", [ "x", "\n" ] ],
155 [ "%c\n%c", "x y", [ "x", "y" ] ],
156 [ "%c\t%c", "x y", [ "x", "y" ] ],
157 [ "%s\n%s", "x y", [ "x", "y" ] ],
158 [ "%s%s\n", "x y", [ "x", "y" ] ],
159 [ "%c%c", "x\n", [ "x", "\n" ] ],
160 [ "%d%c%c%d", "345 678", [ 345, " ", " ", 678] ],
161 [ "%d %c%s", "123 x hello", [123, "x", "hello"] ],
162 [ "%d%2c", "654 123", [654," 1"] ],
163 [ "%5c%s", "a\nb\n\nxyz", [ "a\nb\n\n","xyz" ] ],
164 [ "%s%[ xyz]%d", "hello x 32", ["hello", " x ", 32] ],
165 [ "%5s%8[a-z]%d", "helloblahblah 32", ["hello", "blahblah", 32] ],
166 [ '%s%[abcde\\s]%d', "hello badea 32", ["hello", " badea ", 32] ],
167 [ '%d%[\\s]%c', "123 \n\t X", [ 123," \n\t ", "X"] ],
168 [ "%4s%2c%c", "1.2x\n\ny", [ "1.2x", "\n\n","y"] ],
169 [ "%f%c %3c%d", "1.2x\n\ny123", [ 1.2, "x", "y12", 3 ] ],
170 [ "%s%5c", "abc ab\ncd", [ "abc", " ab\nc" ] ],
171 [ "%5c%f", "ab\ncd1.2", [ "ab\ncd",1.2 ] ],
172 [ "%5c%c", "ab\ncd1", [ "ab\ncd","1" ] ],
173 [ "%f%c%2c%d", "1.2x\ny123", [ 1.2, "x", "\ny", 123 ] ],
174 [ "%f%c%3c", "1.2x\ny123", [ 1.2, "x", "\ny1"] ],
175 [ "%s\n%s", "blah\n\nand\nmore stuff", [ "blah", "and" ] ],
176 [ "%o%d%x", "21912a3", [ "21".oct, 912, "a3".hex ] ],
177 [ "%3o%4d%3x", "21912a3", [ "21".oct, 912, "a3".hex ] ],
178 [ "%3o%4d%5x", "2191240xa3", [ "21".oct, 9124, "a3".hex ] ],
179 [ "%3d%3x", "12abc", [12, "abc".hex] ],
180 [ "%s%i%d", "hello +0xdef 123", [ "hello", "def".hex, 123] ],
181 [ "%s%i%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
182 [ "%s%i%i%i%i", "hello 012 -012 100 1", [ "hello", 10, -10, 100, 1 ] ],
183 [ "%s%i%i%i%i", "hello 012 0x12 100 1", [ "hello", 10, 18, 100, 1 ] ],
184 [ "%s%5i%3i%4i", "hello 0x123 123 0123", [ "hello", "0x123".hex, 123,"0123".oct] ],
185 [ "%s%3i%4i", "hello 1230123", [ "hello", 123,"0123".oct] ],
186 [ "%s%3i", "hello 1230", [ "hello", 123] ],
187 [ "%s%5x%d", "hello 0xdef 123", [ "hello", "def".hex, 123] ],
188 [ "%s%6x%d", "hello +0xdef 123", [ "hello", "def".hex, 123] ],
189 [ "%s%6x%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
190 [ "%s%6x%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
191 [ "%s%4x%d", "hello -def 123", [ "hello", -"def".hex, 123] ],
192 [ "%s%3x%d", "hello def 123", [ "hello", "def".hex, 123] ],
193 [ "%s%x%d", "hello -def 123", [ "hello", -"def".hex, 123] ],
194 [ "%s%x%d", "hello -0xdef 123", [ "hello", -"def".hex, 123] ],
195 [ "%s%x%d", "hello 0xdef 123", [ "hello", "def".hex, 123] ],
196 [ "%s%d%x%s", "hello 123 abc def", [ "hello", 123, "abc".hex, "def"] ],
197 [ "%s%d%o%d", "hello 012 012 100", [ "hello", 12, 10, 100 ] ],
198 [ "%s%d%o%d", "hello 012 -012 100", [ "hello", 12, -10, 100 ] ],
199 [ "%s%o%x%d", "hello 012 0x12 100", [ "hello", 10, 18, 100 ] ],
200 [ "%s%d%o%d", "hello 012 +01288", [ "hello", 12, 10, 88 ] ],
201 [ "%f %d %s", "12.3e23 45 string", ["12.3e23".to_f, 45, "string"] ],
202 [ "%f %d %s", "12.3e+23 45 string", ["12.3e23".to_f, 45, "string"] ],
203 [ "%f %d %s", "12.3e-23 45 string", ["12.3e-23".to_f, 45, "string"] ],
204 [ "%f %d %s", "12.3e23 45 string", ["12.3e23".to_f, 45, "string"] ],
205 [ "%f %d %s", "-12.3e-23 45 string", ["-12.3e-23".to_f, 45, "string"] ],
206 [ "%f %d %s", "12.e23 45 string", ["12.e23".to_f, 45, "string"] ],
207 [ "%5f %d %s", "1.2e23 string", ["1.2e2".to_f, 3, "string"] ],
208 [ "%5f%d %s", "1.2e23 string", ["1.2e2".to_f, 3, "string"] ],
209 [ "%5f%d %d %s", "1.2e23 45 string", ["1.2e2".to_f, 3, 45, "string"] ],
210 [ "%6f %d %d %s", "+1.2e23 45 string", ["1.2e2".to_f, 3, 45, "string"] ],
211 [ "%d %d", "123 \n 345", [123, 345] ],
212 [ "%d %*d", "123 \n 345", [123] ],
213 [ "%d %3d789", "123 +45789", [123, 45] ],
214 [ "%d %3d%d", "123 +456789", [123, 45, 6789] ],
215 [ "%d %3dabc", "123 456abc", [123, 456] ],
216 [ "%d %s", "123abc", [123, "abc"] ],
217 [ "%d%s %s", "123 abc def", [123, "abc", "def"] ],
218 [ "%s%s", "abc123 def", ["abc123", "def"] ],
219 [ "%s%s %s", "123 abc def", ["123", "abc", "def"] ],
220 [ "%s%%%s", "abc % def", ["abc", "def"] ],
221 [ "%d %3d %s", "+123 456abc", [123, 456, "abc"] ],
222 [ "%d %3d %s", "123 456abc", [123, 456, "abc"] ],
223 [ "%d %3d %s", "123 +456 abc", [123, 45, "6"] ],
224 [ "%d %3d %s", "-123-456abc", [-123, -45, "6abc"] ],
225 [ "%dabc%d", "123abc345", [123, 345] ],
226 [ "%d%5s%d", "123 abcde12", [123, "abcde", 12] ],
227 [ "%5d%5s%5d", "12345abcde67890", [12345, "abcde", 67890] ],
228 [ "%5d%*5s%5d", "12345abcde67890", [12345, 67890] ],
229 [ " 12345%5s%5d", "12345abcde67890", [ "abcde", 67890] ],
230 [ "%5dabcde%5d", "12345abcde67890", [ 12345, 67890] ],
231 [ "%s%%%*s", "abc % def", ["abc"] ],
232 [ "%*6s %d", "string 123", [123] ],
233 [ "%d %*3d %s", "-123-456abc", [-123, "6abc"] ],
234 [ "%d%s", "123", [123] ],
235 [ "%s%d", "abc", ["abc"] ],
236 [ "%f%x", "3.2e45x", ["3.2e45x".to_f] ],
237 [ "%s%d", "abc", ["abc"] ],
238 [ "%*5f%d %d %s", "1.2e23 45 string", [3, 45, "string"] ],
239 [ "%5f%*d %d %s", "1.2e23 45 string", ["1.2e2".to_f, 45, "string"] ],
240 [ "%*5f%*d %*d %s", "1.2e23 45 string", ["string"] ],
241 [ "%f %*d %s", "12.e23 45 string", ["12.e23".to_f, "string"] ],
242 [ "%5f %d %s", "1.2e23 string", ["1.2e2".to_f, 3, "string"] ],
243 [ "%s %f %s %d %x%c%c%c%c",
244 "float: 1.2e23 dec/hex: 135a23 abc",
245 ["float:", "1.2e23".to_f, "dec/hex:", 135, "a23".hex, " ", "a", "b", "c" ] ],
248 [ "%s\n", "blah\n\n\n", [ "blah" ] ],
251 [ "%[a\nb]", "a\nb", [ "a\nb" ] ],
252 [ "%[abc]", "acb", [ "acb" ] ],
253 [ "%[abc\n]", "a\nb", [ "a\nb" ] ],
254 [ "%[^abc]", "defabc", [ "def" ] ],
255 [ "%[-abc]", "abc-cba", [ "abc-cba" ] ],
256 [ "%[\n]", "\n", [ "\n" ] ],
257 [ "%[\n]", "\nabc", [ "\n" ] ],
258 [ "%[\n\t]", "\t\n", [ "\t\n" ] ],
259 [ "%[a-f]", "abczef", [ "abc" ] ],
260 [ "%d%3[abc]", "123 aaab", [ 123 ] ],
261 [ "%d%3[^abc]", "123adefdef", [ 123 ] ],
262 [ "%d%3[[:lower:]] %f", "123ade1.2", [ 123,"ade",1.2 ] ],
263 [ "%d%3[[:lower:]] %f", "123ad1.2", [ 123,"ad",1.2 ] ],
264 [ "%d%3[[:lower:]] %f", "123 ad1.2", [ 123 ] ],
265 [ "%d%[[:lower:]]", "123abcdef1.2", [ 123, "abcdef" ] ],
266 [ "%[[:lower:]]%d", "abcdef123", [ "abcdef", 123 ] ],
267 [ "%[[:digit:]]%[[:alpha:]]", "123abcdef", [ "123", "abcdef" ] ],
268 [ "%[[:digit:]]%d", "123 123", [ "123", 123 ] ],
269 [ "%[[:upper:]]", "ABCdefGHI", [ "ABC" ] ],
272 [ "%2f", "x", [0.0] ], # width-floats match anything (by design)
273 [ "%f", "1.23e45", [1.23e+45] ],
274 [ "%f", "3.25ee", [3.25] ],
275 [ "%f", "3..25", [3.0] ],
276 [ "%f", "+3.25", [3.25] ],
277 [ "%f", "+3.25e2", [325.0] ],
278 [ "%f", "3.z", [3.0] ],
280 # Testing embedded matches including literal '[' behavior
281 [",%d,%f", ",10,1.1", [10,1.1] ],
282 [" ,%d,%f", " ,10,1.1", [10,1.1] ],
283 ["[%d,%f", "[10,1.1", [10,1.1] ],
284 [" [%d,%f", " [10,1.1", [10,1.1] ],
290 class TestStringScanf
295 self.tests.each do |test|
296 define_method("test_#{i}") do ||
297 assert_equal(test[2], test[1].scanf(test[0]))
307 tmpfilename = "#{Dir.tmpdir}/iotest.dat.#{$$}"
310 self.tests.each do |test|
311 define_method("test_#{i}") do ||
312 File.open(tmpfilename, "w") {|fh| fh.print test[1]}
313 File.open(tmpfilename, "r") { |fh|
314 assert_equal(test[2], fh.scanf(test[0]))
316 File.delete(tmpfilename)