OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Apple Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * 1. Redistributions of source code must retain the above copyright | |
8 * notice, this list of conditions and the following disclaimer. | |
9 * 2. Redistributions in binary form must reproduce the above copyright | |
10 * notice, this list of conditions and the following disclaimer in the | |
11 * documentation and/or other materials provided with the distribution. | |
12 * | |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
23 * THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 | |
28 #include <interpreter/VMInspector.h> | |
29 #include <stdarg.h> | |
30 #include <stdio.h> | |
31 | |
32 using namespace JSC; | |
33 | |
34 // There's not much we can test for the VMInspector::printf() case except to | |
35 // make sure it does not crash. Unfortunately, we also don't want all the | |
36 // test strings crowding out stdout. So, we forego the printf tests. | |
37 // NOTE that the most interesting part of VMInspector::printf() is the | |
38 // formatting functionality, and it is are already being tested by the | |
39 // fprintf() and sprintf() cases. | |
40 | |
41 | |
42 // The VMInspector::fprintf() test works by printing the string to a temp file, | |
43 // and then reading the file content back into a buffer, which we, in turn, | |
44 // compare against the expected string. | |
45 | |
46 TEST(JSC, VMInspectorFprintf) | |
47 { | |
48 #if ENABLE(VMINSPECTOR) | |
49 char actual[1024]; | |
50 char expected[1024]; | |
51 const char* format; | |
52 const char* expectedLiteral; | |
53 FILE* file; | |
54 const char* filename = "/tmp/VMInspectorFprintfTest.txt"; | |
55 size_t size; | |
56 | |
57 #define OPEN_FILE(file) \ | |
58 do { \ | |
59 file = fopen(filename, "w"); \ | |
60 } while (false) | |
61 | |
62 #define READ_AND_CLOSE_FILE(file, actual) \ | |
63 do { \ | |
64 fclose(file); \ | |
65 file = fopen(filename, "r"); \ | |
66 fseek(file, 0, SEEK_END); \ | |
67 size = ftell(file); \ | |
68 rewind(file); \ | |
69 fread(actual, 1, size, file); \ | |
70 actual[size] = '\0'; \ | |
71 fclose(file); \ | |
72 } while (false) | |
73 | |
74 // Testing standard default format specifiers: | |
75 // Note: should work just like sprintf. So, we just compare against that. | |
76 memset(actual, 'z', sizeof(actual)); | |
77 // The compiler warning flags are configured to expect a literal string for | |
78 // ::sprintf below. So, use a #define for this one case to keep the | |
79 // compiler happy. | |
80 #undef LITERAL_FORMAT | |
81 #define LITERAL_FORMAT "'%%%%' ==> '%%'\n" | |
82 | |
83 OPEN_FILE(file); | |
84 VMInspector::fprintf(file, LITERAL_FORMAT); | |
85 READ_AND_CLOSE_FILE(file, actual); | |
86 ::sprintf(expected, LITERAL_FORMAT); | |
87 | |
88 #undef LITERAL_FORMAT | |
89 ASSERT_EQ(strcmp(actual, expected), 0); | |
90 | |
91 memset(actual, 'z', sizeof(actual)); | |
92 format = "'%%c', 'x' ==> '%c'\n"; | |
93 OPEN_FILE(file); | |
94 VMInspector::fprintf(file, format, 'x'); | |
95 READ_AND_CLOSE_FILE(file, actual); | |
96 ::sprintf(expected, format, 'x'); | |
97 ASSERT_EQ(strcmp(actual, expected), 0); | |
98 | |
99 memset(actual, 'z', sizeof(actual)); | |
100 format = "'%%*c', 8, 'x' ==> '%*c'\n"; | |
101 OPEN_FILE(file); | |
102 VMInspector::fprintf(file, format, 8, 'x'); | |
103 READ_AND_CLOSE_FILE(file, actual); | |
104 ::sprintf(expected, format, 8, 'x'); | |
105 ASSERT_EQ(strcmp(actual, expected), 0); | |
106 | |
107 memset(actual, 'z', sizeof(actual)); | |
108 format = "'%%s', \"hello world\" ==> '%s'\n"; | |
109 OPEN_FILE(file); | |
110 VMInspector::fprintf(file, format, "hello world"); | |
111 READ_AND_CLOSE_FILE(file, actual); | |
112 ::sprintf(expected, format, "hello world"); | |
113 ASSERT_EQ(strcmp(actual, expected), 0); | |
114 | |
115 memset(actual, 'z', sizeof(actual)); | |
116 format = "'%%*s', 8, \"hello world\" ==> '%*s'\n"; | |
117 OPEN_FILE(file); | |
118 VMInspector::fprintf(file, format, 8, "hello world"); | |
119 READ_AND_CLOSE_FILE(file, actual); | |
120 ::sprintf(expected, format, 8, "hello world"); | |
121 ASSERT_EQ(strcmp(actual, expected), 0); | |
122 | |
123 memset(actual, 'z', sizeof(actual)); | |
124 format = "'%%*s', 8, \"hello\" ==> '%*s'\n"; | |
125 OPEN_FILE(file); | |
126 VMInspector::fprintf(file, format, 8, "hello"); | |
127 READ_AND_CLOSE_FILE(file, actual); | |
128 ::sprintf(expected, format, 8, "hello"); | |
129 ASSERT_EQ(strcmp(actual, expected), 0); | |
130 | |
131 memset(actual, 'z', sizeof(actual)); | |
132 format = "'%%d', 987654321 ==> '%d'\n"; | |
133 OPEN_FILE(file); | |
134 VMInspector::fprintf(file, format, 987654321); | |
135 READ_AND_CLOSE_FILE(file, actual); | |
136 ::sprintf(expected, format, 987654321); | |
137 ASSERT_EQ(strcmp(actual, expected), 0); | |
138 | |
139 memset(actual, 'z', sizeof(actual)); | |
140 format = "'%%u', 4276543210u ==> '%u'\n"; | |
141 OPEN_FILE(file); | |
142 VMInspector::fprintf(file, format, 4276543210u); | |
143 READ_AND_CLOSE_FILE(file, actual); | |
144 ::sprintf(expected, format, 4276543210u); | |
145 ASSERT_EQ(strcmp(actual, expected), 0); | |
146 | |
147 memset(actual, 'z', sizeof(actual)); | |
148 format = "'%%u', 0xffffffff ==> '%u'\n"; | |
149 OPEN_FILE(file); | |
150 VMInspector::fprintf(file, format, 0xffffffff); | |
151 READ_AND_CLOSE_FILE(file, actual); | |
152 ::sprintf(expected, format, 0xffffffff); | |
153 ASSERT_EQ(strcmp(actual, expected), 0); | |
154 | |
155 memset(actual, 'z', sizeof(actual)); | |
156 format = "'%%x', 0xffffffff ==> '%x'\n"; | |
157 OPEN_FILE(file); | |
158 VMInspector::fprintf(file, format, 0xffffffff); | |
159 READ_AND_CLOSE_FILE(file, actual); | |
160 ::sprintf(expected, format, 0xffffffff); | |
161 ASSERT_EQ(strcmp(actual, expected), 0); | |
162 | |
163 memset(actual, 'z', sizeof(actual)); | |
164 format = "'%%p', (void*)0xabcdbabe ==> '%p'\n"; | |
165 OPEN_FILE(file); | |
166 VMInspector::fprintf(file, format, (void*)0xabcdbabe); | |
167 READ_AND_CLOSE_FILE(file, actual); | |
168 ::sprintf(expected, format, (void*)0xabcdbabe); | |
169 ASSERT_EQ(strcmp(actual, expected), 0); | |
170 | |
171 memset(actual, 'z', sizeof(actual)); | |
172 format = "'%%lu', 1234567890987654321ul ==> '%lu'\n"; | |
173 OPEN_FILE(file); | |
174 VMInspector::fprintf(file, format, 1234567890987654321ul); | |
175 READ_AND_CLOSE_FILE(file, actual); | |
176 ::sprintf(expected, format, 1234567890987654321ul); | |
177 ASSERT_EQ(strcmp(actual, expected), 0); | |
178 | |
179 memset(actual, 'z', sizeof(actual)); | |
180 format = "'%%f', 1234.567 ==> '%f'\n"; | |
181 OPEN_FILE(file); | |
182 VMInspector::fprintf(file, format, 1234.567); | |
183 READ_AND_CLOSE_FILE(file, actual); | |
184 ::sprintf(expected, format, 1234.567); | |
185 ASSERT_EQ(strcmp(actual, expected), 0); | |
186 | |
187 memset(actual, 'z', sizeof(actual)); | |
188 format = "'%%.2f', 1234.567 ==> '%.2f'\n"; | |
189 OPEN_FILE(file); | |
190 VMInspector::fprintf(file, format, 1234.567); | |
191 READ_AND_CLOSE_FILE(file, actual); | |
192 ::sprintf(expected, format, 1234.567); | |
193 ASSERT_EQ(strcmp(actual, expected), 0); | |
194 | |
195 memset(actual, 'z', sizeof(actual)); | |
196 format = "'%%10.2f', 1234.567 ==> '%10.2f'\n"; | |
197 OPEN_FILE(file); | |
198 VMInspector::fprintf(file, format, 1234.567); | |
199 READ_AND_CLOSE_FILE(file, actual); | |
200 ::sprintf(expected, format, 1234.567); | |
201 ASSERT_EQ(strcmp(actual, expected), 0); | |
202 | |
203 memset(actual, 'z', sizeof(actual)); | |
204 format = "'%%010.2f', 1234.567 ==> '%010.2f'\n"; | |
205 OPEN_FILE(file); | |
206 VMInspector::fprintf(file, format, 1234.567); | |
207 READ_AND_CLOSE_FILE(file, actual); | |
208 ::sprintf(expected, format, 1234.567); | |
209 ASSERT_EQ(strcmp(actual, expected), 0); | |
210 | |
211 // Bad / weird formats: | |
212 memset(actual, 'z', sizeof(actual)); | |
213 format = "'%%5.4', 987654321 ==> '%5.4'\n"; | |
214 OPEN_FILE(file); | |
215 VMInspector::fprintf(file, format, 987654321); | |
216 READ_AND_CLOSE_FILE(file, actual); | |
217 expectedLiteral = "'%5.4', 987654321 ==> 'ERROR @ \"%5.4' \"\n"; | |
218 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
219 | |
220 memset(actual, 'z', sizeof(actual)); | |
221 format = "'%%5.4' '%%d', 987654321, 4 ==> '%5.4' '%d'\n"; | |
222 OPEN_FILE(file); | |
223 VMInspector::fprintf(file, format, 987654321, 4); | |
224 READ_AND_CLOSE_FILE(file, actual); | |
225 ::sprintf(expected, format, 987654321, 4); | |
226 ASSERT_EQ(strcmp(actual, expected), 0); | |
227 | |
228 memset(actual, 'z', sizeof(actual)); | |
229 format = "'%%w' '%%d', 987654321, 6 ==> '%w' '%d'\n"; | |
230 OPEN_FILE(file); | |
231 VMInspector::fprintf(file, format, 987654321, 6); | |
232 READ_AND_CLOSE_FILE(file, actual); | |
233 ::sprintf(expected, format, 987654321, 6); | |
234 ASSERT_EQ(strcmp(actual, expected), 0); | |
235 | |
236 | |
237 // Testing the %b extension: | |
238 memset(actual, 'z', sizeof(actual)); | |
239 OPEN_FILE(file); | |
240 VMInspector::fprintf(file, "'%%b', 0 ==> '%b'\n", 0); | |
241 READ_AND_CLOSE_FILE(file, actual); | |
242 ASSERT_EQ(strcmp(actual, "'%b', 0 ==> 'FALSE'\n"), 0); | |
243 | |
244 memset(actual, 'z', sizeof(actual)); | |
245 OPEN_FILE(file); | |
246 VMInspector::fprintf(file, "'%%b', 1 ==> '%b'\n", 1); | |
247 READ_AND_CLOSE_FILE(file, actual); | |
248 ASSERT_EQ(strcmp(actual, "'%b', 1 ==> 'TRUE'\n"), 0); | |
249 | |
250 memset(actual, 'z', sizeof(actual)); | |
251 OPEN_FILE(file); | |
252 VMInspector::fprintf(file, "'%%b', -123456789 ==> '%b'\n", -123456789); | |
253 READ_AND_CLOSE_FILE(file, actual); | |
254 ASSERT_EQ(strcmp(actual, "'%b', -123456789 ==> 'TRUE'\n"), 0); | |
255 | |
256 memset(actual, 'z', sizeof(actual)); | |
257 OPEN_FILE(file); | |
258 VMInspector::fprintf(file, "'%%b', 123456789 ==> '%b'\n", 123456789); | |
259 READ_AND_CLOSE_FILE(file, actual); | |
260 ASSERT_EQ(strcmp(actual, "'%b', 123456789 ==> 'TRUE'\n"), 0); | |
261 | |
262 | |
263 // Testing the %J<x> extensions: | |
264 String str1("Test WTF String"); | |
265 String str2(""); | |
266 | |
267 memset(actual, 'z', sizeof(actual)); | |
268 OPEN_FILE(file); | |
269 VMInspector::fprintf(file, "'%%Js' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\
"NOT EMPTY\" ==> '%Js' is %s\n", | |
270 &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
271 READ_AND_CLOSE_FILE(file, actual); | |
272 expectedLiteral = "'%Js' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\
" ==> 'Test WTF String' is NOT EMPTY\n"; | |
273 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
274 | |
275 memset(actual, 'z', sizeof(actual)); | |
276 OPEN_FILE(file); | |
277 VMInspector::fprintf(file, "'%%Js' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\
"NOT EMPTY\" ==> '%Js' is %s\n", | |
278 &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
279 READ_AND_CLOSE_FILE(file, actual); | |
280 expectedLiteral = "'%Js' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\
" ==> '' is EMPTY\n"; | |
281 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
282 | |
283 memset(actual, 'z', sizeof(actual)); | |
284 OPEN_FILE(file); | |
285 VMInspector::fprintf(file, "'%%J+s' is %%s, &str1, str1.isEmpty()?\"EMPTY\":
\"NOT EMPTY\" ==> '%J+s' is %s\n", | |
286 &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
287 READ_AND_CLOSE_FILE(file, actual); | |
288 expectedLiteral = "'%J+s' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY
\" ==> 'WTF::String \"Test WTF String\"' is NOT EMPTY\n"; | |
289 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
290 | |
291 memset(actual, 'z', sizeof(actual)); | |
292 OPEN_FILE(file); | |
293 VMInspector::fprintf(file, "'%%J+s' is %%s, &str2, str2.isEmpty()?\"EMPTY\":
\"NOT EMPTY\" ==> '%J+s' is %s\n", | |
294 &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
295 READ_AND_CLOSE_FILE(file, actual); | |
296 expectedLiteral = "'%J+s' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY
\" ==> 'WTF::String \"\"' is EMPTY\n"; | |
297 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
298 | |
299 #undef OPEN_FILE | |
300 #undef READ_AND_CLOSE_FILE | |
301 | |
302 #endif | |
303 } | |
304 | |
305 | |
306 TEST(JSC, VMInspectorSprintf) | |
307 { | |
308 #if ENABLE(VMINSPECTOR) | |
309 char actual[1024]; | |
310 char expected[1024]; | |
311 const char* format; | |
312 const char* expectedLiteral; | |
313 | |
314 // Testing standard default format specifiers: | |
315 // Note: should work just like sprintf. So, we just compare against that. | |
316 memset(actual, 'z', sizeof(actual)); | |
317 // The compiler warning flags are configured to expect a literal string for | |
318 // ::sprintf below. So, use a #define for this one case to keep the | |
319 // compiler happy. | |
320 #undef LITERAL_FORMAT | |
321 #define LITERAL_FORMAT "'%%%%' ==> '%%'\n" | |
322 VMInspector::sprintf(actual, LITERAL_FORMAT); | |
323 ::sprintf(expected, LITERAL_FORMAT); | |
324 #undef LITERAL_FORMAT | |
325 ASSERT_EQ(strcmp(actual, expected), 0); | |
326 | |
327 memset(actual, 'z', sizeof(actual)); | |
328 format = "'%%c', 'x' ==> '%c'\n"; | |
329 VMInspector::sprintf(actual, format, 'x'); | |
330 ::sprintf(expected, format, 'x'); | |
331 ASSERT_EQ(strcmp(actual, expected), 0); | |
332 | |
333 memset(actual, 'z', sizeof(actual)); | |
334 format = "'%%*c', 8, 'x' ==> '%*c'\n"; | |
335 VMInspector::sprintf(actual, format, 8, 'x'); | |
336 ::sprintf(expected, format, 8, 'x'); | |
337 ASSERT_EQ(strcmp(actual, expected), 0); | |
338 | |
339 memset(actual, 'z', sizeof(actual)); | |
340 format = "'%%s', \"hello world\" ==> '%s'\n"; | |
341 VMInspector::sprintf(actual, format, "hello world"); | |
342 ::sprintf(expected, format, "hello world"); | |
343 ASSERT_EQ(strcmp(actual, expected), 0); | |
344 | |
345 memset(actual, 'z', sizeof(actual)); | |
346 format = "'%%*s', 8, \"hello world\" ==> '%*s'\n"; | |
347 VMInspector::sprintf(actual, format, 8, "hello world"); | |
348 ::sprintf(expected, format, 8, "hello world"); | |
349 ASSERT_EQ(strcmp(actual, expected), 0); | |
350 | |
351 memset(actual, 'z', sizeof(actual)); | |
352 format = "'%%*s', 8, \"hello\" ==> '%*s'\n"; | |
353 VMInspector::sprintf(actual, format, 8, "hello"); | |
354 ::sprintf(expected, format, 8, "hello"); | |
355 ASSERT_EQ(strcmp(actual, expected), 0); | |
356 | |
357 memset(actual, 'z', sizeof(actual)); | |
358 format = "'%%d', 987654321 ==> '%d'\n"; | |
359 VMInspector::sprintf(actual, format, 987654321); | |
360 ::sprintf(expected, format, 987654321); | |
361 ASSERT_EQ(strcmp(actual, expected), 0); | |
362 | |
363 memset(actual, 'z', sizeof(actual)); | |
364 format = "'%%u', 4276543210u ==> '%u'\n"; | |
365 VMInspector::sprintf(actual, format, 4276543210u); | |
366 ::sprintf(expected, format, 4276543210u); | |
367 ASSERT_EQ(strcmp(actual, expected), 0); | |
368 | |
369 memset(actual, 'z', sizeof(actual)); | |
370 format = "'%%u', 0xffffffff ==> '%u'\n"; | |
371 VMInspector::sprintf(actual, format, 0xffffffff); | |
372 ::sprintf(expected, format, 0xffffffff); | |
373 ASSERT_EQ(strcmp(actual, expected), 0); | |
374 | |
375 memset(actual, 'z', sizeof(actual)); | |
376 format = "'%%x', 0xffffffff ==> '%x'\n"; | |
377 VMInspector::sprintf(actual, format, 0xffffffff); | |
378 ::sprintf(expected, format, 0xffffffff); | |
379 ASSERT_EQ(strcmp(actual, expected), 0); | |
380 | |
381 memset(actual, 'z', sizeof(actual)); | |
382 format = "'%%p', (void*)0xabcdbabe ==> '%p'\n"; | |
383 VMInspector::sprintf(actual, format, (void*)0xabcdbabe); | |
384 ::sprintf(expected, format, (void*)0xabcdbabe); | |
385 ASSERT_EQ(strcmp(actual, expected), 0); | |
386 | |
387 memset(actual, 'z', sizeof(actual)); | |
388 format = "'%%lu', 1234567890987654321ul ==> '%lu'\n"; | |
389 VMInspector::sprintf(actual, format, 1234567890987654321ul); | |
390 ::sprintf(expected, format, 1234567890987654321ul); | |
391 ASSERT_EQ(strcmp(actual, expected), 0); | |
392 | |
393 memset(actual, 'z', sizeof(actual)); | |
394 format = "'%%f', 1234.567 ==> '%f'\n"; | |
395 VMInspector::sprintf(actual, format, 1234.567); | |
396 ::sprintf(expected, format, 1234.567); | |
397 ASSERT_EQ(strcmp(actual, expected), 0); | |
398 | |
399 memset(actual, 'z', sizeof(actual)); | |
400 format = "'%%.2f', 1234.567 ==> '%.2f'\n"; | |
401 VMInspector::sprintf(actual, format, 1234.567); | |
402 ::sprintf(expected, format, 1234.567); | |
403 ASSERT_EQ(strcmp(actual, expected), 0); | |
404 | |
405 memset(actual, 'z', sizeof(actual)); | |
406 format = "'%%10.2f', 1234.567 ==> '%10.2f'\n"; | |
407 VMInspector::sprintf(actual, format, 1234.567); | |
408 ::sprintf(expected, format, 1234.567); | |
409 ASSERT_EQ(strcmp(actual, expected), 0); | |
410 | |
411 memset(actual, 'z', sizeof(actual)); | |
412 format = "'%%010.2f', 1234.567 ==> '%010.2f'\n"; | |
413 VMInspector::sprintf(actual, format, 1234.567); | |
414 ::sprintf(expected, format, 1234.567); | |
415 ASSERT_EQ(strcmp(actual, expected), 0); | |
416 | |
417 // Bad / weird formats: | |
418 memset(actual, 'z', sizeof(actual)); | |
419 format = "'%%5.4', 987654321 ==> '%5.4'\n"; | |
420 VMInspector::sprintf(actual, format, 987654321); | |
421 expectedLiteral = "'%5.4', 987654321 ==> 'ERROR @ \"%5.4' \"\n"; | |
422 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
423 | |
424 memset(actual, 'z', sizeof(actual)); | |
425 format = "'%%5.4' '%%d', 987654321, 4 ==> '%5.4' '%d'\n"; | |
426 VMInspector::sprintf(actual, format, 987654321, 4); | |
427 ::sprintf(expected, format, 987654321, 4); | |
428 ASSERT_EQ(strcmp(actual, expected), 0); | |
429 | |
430 memset(actual, 'z', sizeof(actual)); | |
431 format = "'%%w' '%%d', 987654321, 6 ==> '%w' '%d'\n"; | |
432 VMInspector::sprintf(actual, format, 987654321, 6); | |
433 ::sprintf(expected, format, 987654321, 6); | |
434 ASSERT_EQ(strcmp(actual, expected), 0); | |
435 | |
436 | |
437 // Testing the %b extension: | |
438 memset(actual, 'z', sizeof(actual)); | |
439 VMInspector::sprintf(actual, "'%%b', 0 ==> '%b'\n", 0); | |
440 ASSERT_EQ(strcmp(actual, "'%b', 0 ==> 'FALSE'\n"), 0); | |
441 | |
442 memset(actual, 'z', sizeof(actual)); | |
443 VMInspector::sprintf(actual, "'%%b', 1 ==> '%b'\n", 1); | |
444 ASSERT_EQ(strcmp(actual, "'%b', 1 ==> 'TRUE'\n"), 0); | |
445 | |
446 memset(actual, 'z', sizeof(actual)); | |
447 VMInspector::sprintf(actual, "'%%b', -123456789 ==> '%b'\n", -123456789); | |
448 ASSERT_EQ(strcmp(actual, "'%b', -123456789 ==> 'TRUE'\n"), 0); | |
449 | |
450 memset(actual, 'z', sizeof(actual)); | |
451 VMInspector::sprintf(actual, "'%%b', 123456789 ==> '%b'\n", 123456789); | |
452 ASSERT_EQ(strcmp(actual, "'%b', 123456789 ==> 'TRUE'\n"), 0); | |
453 | |
454 | |
455 // Testing the %J<x> extensions: | |
456 String str1("Test WTF String"); | |
457 String str2(""); | |
458 | |
459 memset(actual, 'z', sizeof(actual)); | |
460 VMInspector::sprintf(actual, "'%%Js' is %%s, &str1, str1.isEmpty()?\"EMPTY\"
:\"NOT EMPTY\" ==> '%Js' is %s\n", | |
461 &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
462 expectedLiteral = "'%Js' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\
" ==> 'Test WTF String' is NOT EMPTY\n"; | |
463 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
464 | |
465 memset(actual, 'z', sizeof(actual)); | |
466 VMInspector::sprintf(actual, "'%%Js' is %%s, &str2, str2.isEmpty()?\"EMPTY\"
:\"NOT EMPTY\" ==> '%Js' is %s\n", | |
467 &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
468 expectedLiteral = "'%Js' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\
" ==> '' is EMPTY\n"; | |
469 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
470 | |
471 memset(actual, 'z', sizeof(actual)); | |
472 VMInspector::sprintf(actual, "'%%J+s' is %%s, &str1, str1.isEmpty()?\"EMPTY\
":\"NOT EMPTY\" ==> '%J+s' is %s\n", | |
473 &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
474 expectedLiteral = "'%J+s' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY
\" ==> 'WTF::String \"Test WTF String\"' is NOT EMPTY\n"; | |
475 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
476 | |
477 memset(actual, 'z', sizeof(actual)); | |
478 VMInspector::sprintf(actual, "'%%J+s' is %%s, &str2, str2.isEmpty()?\"EMPTY\
":\"NOT EMPTY\" ==> '%J+s' is %s\n", | |
479 &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
480 expectedLiteral = "'%J+s' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY
\" ==> 'WTF::String \"\"' is EMPTY\n"; | |
481 ASSERT_EQ(strcmp(actual, expectedLiteral), 0); | |
482 #endif | |
483 } | |
484 | |
485 | |
486 TEST(JSC, VMInspectorSnprintf) | |
487 { | |
488 #if ENABLE(VMINSPECTOR) | |
489 char actual[1024]; | |
490 char expected[1024]; | |
491 const char* format; | |
492 const char* expectedLiteral; | |
493 | |
494 size_t size = 1; | |
495 while (size <= 100) { | |
496 | |
497 // Testing standard default format specifiers: | |
498 // Note: should work just like snprintf. So, we just compare against tha
t. | |
499 memset(actual, 'z', sizeof(actual)); | |
500 // The compiler warning flags are configured to expect a literal string
for | |
501 // ::snprintf below. So, use a #define for this one case to keep the | |
502 // compiler happy. | |
503 #undef LITERAL_FORMAT | |
504 #define LITERAL_FORMAT "'%%%%' ==> '%%'\n" | |
505 VMInspector::snprintf(actual, size, LITERAL_FORMAT); | |
506 ::snprintf(expected, size, LITERAL_FORMAT); | |
507 #undef LITERAL_FORMAT | |
508 ASSERT_EQ(strcmp(actual, expected), 0); | |
509 | |
510 memset(actual, 'z', sizeof(actual)); | |
511 format = "'%%c', 'x' ==> '%c'\n"; | |
512 VMInspector::snprintf(actual, size, format, 'x'); | |
513 ::snprintf(expected, size, format, 'x'); | |
514 ASSERT_EQ(strcmp(actual, expected), 0); | |
515 | |
516 memset(actual, 'z', sizeof(actual)); | |
517 format = "'%%*c', 8, 'x' ==> '%*c'\n"; | |
518 VMInspector::snprintf(actual, size, format, 8, 'x'); | |
519 ::snprintf(expected, size, format, 8, 'x'); | |
520 ASSERT_EQ(strcmp(actual, expected), 0); | |
521 | |
522 memset(actual, 'z', sizeof(actual)); | |
523 format = "'%%s', \"hello world\" ==> '%s'\n"; | |
524 VMInspector::snprintf(actual, size, format, "hello world"); | |
525 ::snprintf(expected, size, format, "hello world"); | |
526 ASSERT_EQ(strcmp(actual, expected), 0); | |
527 | |
528 memset(actual, 'z', sizeof(actual)); | |
529 format = "'%%*s', 8, \"hello world\" ==> '%*s'\n"; | |
530 VMInspector::snprintf(actual, size, format, 8, "hello world"); | |
531 ::snprintf(expected, size, format, 8, "hello world"); | |
532 ASSERT_EQ(strcmp(actual, expected), 0); | |
533 | |
534 memset(actual, 'z', sizeof(actual)); | |
535 format = "'%%*s', 8, \"hello\" ==> '%*s'\n"; | |
536 VMInspector::snprintf(actual, size, format, 8, "hello"); | |
537 ::snprintf(expected, size, format, 8, "hello"); | |
538 ASSERT_EQ(strcmp(actual, expected), 0); | |
539 | |
540 memset(actual, 'z', sizeof(actual)); | |
541 format = "'%%d', 987654321 ==> '%d'\n"; | |
542 VMInspector::snprintf(actual, size, format, 987654321); | |
543 ::snprintf(expected, size, format, 987654321); | |
544 ASSERT_EQ(strcmp(actual, expected), 0); | |
545 | |
546 memset(actual, 'z', sizeof(actual)); | |
547 format = "'%%u', 4276543210u ==> '%u'\n"; | |
548 VMInspector::snprintf(actual, size, format, 4276543210u); | |
549 ::snprintf(expected, size, format, 4276543210u); | |
550 ASSERT_EQ(strcmp(actual, expected), 0); | |
551 | |
552 memset(actual, 'z', sizeof(actual)); | |
553 format = "'%%u', 0xffffffff ==> '%u'\n"; | |
554 VMInspector::snprintf(actual, size, format, 0xffffffff); | |
555 ::snprintf(expected, size, format, 0xffffffff); | |
556 ASSERT_EQ(strcmp(actual, expected), 0); | |
557 | |
558 memset(actual, 'z', sizeof(actual)); | |
559 format = "'%%x', 0xffffffff ==> '%x'\n"; | |
560 VMInspector::snprintf(actual, size, format, 0xffffffff); | |
561 ::snprintf(expected, size, format, 0xffffffff); | |
562 ASSERT_EQ(strcmp(actual, expected), 0); | |
563 | |
564 memset(actual, 'z', sizeof(actual)); | |
565 format = "'%%p', (void*)0xabcdbabe ==> '%p'\n"; | |
566 VMInspector::snprintf(actual, size, format, (void*)0xabcdbabe); | |
567 ::snprintf(expected, size, format, (void*)0xabcdbabe); | |
568 ASSERT_EQ(strcmp(actual, expected), 0); | |
569 | |
570 memset(actual, 'z', sizeof(actual)); | |
571 format = "'%%lu', 1234567890987654321ul ==> '%lu'\n"; | |
572 VMInspector::snprintf(actual, size, format, 1234567890987654321ul); | |
573 ::snprintf(expected, size, format, 1234567890987654321ul); | |
574 ASSERT_EQ(strcmp(actual, expected), 0); | |
575 | |
576 memset(actual, 'z', sizeof(actual)); | |
577 format = "'%%f', 1234.567 ==> '%f'\n"; | |
578 VMInspector::snprintf(actual, size, format, 1234.567); | |
579 ::snprintf(expected, size, format, 1234.567); | |
580 ASSERT_EQ(strcmp(actual, expected), 0); | |
581 | |
582 memset(actual, 'z', sizeof(actual)); | |
583 format = "'%%.2f', 1234.567 ==> '%.2f'\n"; | |
584 VMInspector::snprintf(actual, size, format, 1234.567); | |
585 ::snprintf(expected, size, format, 1234.567); | |
586 ASSERT_EQ(strcmp(actual, expected), 0); | |
587 | |
588 memset(actual, 'z', sizeof(actual)); | |
589 format = "'%%10.2f', 1234.567 ==> '%10.2f'\n"; | |
590 VMInspector::snprintf(actual, size, format, 1234.567); | |
591 ::snprintf(expected, size, format, 1234.567); | |
592 ASSERT_EQ(strcmp(actual, expected), 0); | |
593 | |
594 memset(actual, 'z', sizeof(actual)); | |
595 format = "'%%010.2f', 1234.567 ==> '%010.2f'\n"; | |
596 VMInspector::snprintf(actual, size, format, 1234.567); | |
597 ::snprintf(expected, size, format, 1234.567); | |
598 ASSERT_EQ(strcmp(actual, expected), 0); | |
599 | |
600 // Bad / weird formats: | |
601 memset(actual, 'z', sizeof(actual)); | |
602 format = "'%%5.4', 987654321 ==> '%5.4'\n"; | |
603 VMInspector::snprintf(actual, size, format, 987654321); | |
604 expectedLiteral = "'%5.4', 987654321 ==> 'ERROR @ \"%5.4' \"\
n"; | |
605 ::snprintf(expected, size, "%s", expectedLiteral); | |
606 ASSERT_EQ(strcmp(actual, expected), 0); | |
607 | |
608 memset(actual, 'z', sizeof(actual)); | |
609 format = "'%%5.4' '%%d', 987654321, 4 ==> '%5.4' '%d'\n"; | |
610 VMInspector::snprintf(actual, size, format, 987654321, 4); | |
611 ::snprintf(expected, size, format, 987654321, 4); | |
612 ASSERT_EQ(strcmp(actual, expected), 0); | |
613 | |
614 memset(actual, 'z', sizeof(actual)); | |
615 format = "'%%w' '%%d', 987654321, 6 ==> '%w' '%d'\n"; | |
616 VMInspector::snprintf(actual, size, format, 987654321, 6); | |
617 ::snprintf(expected, size, format, 987654321, 6); | |
618 ASSERT_EQ(strcmp(actual, expected), 0); | |
619 | |
620 | |
621 // Testing the %b extension: | |
622 memset(actual, 'z', sizeof(actual)); | |
623 VMInspector::snprintf(actual, size, "'%%b', 0 ==> '%b'\n", 0); | |
624 expectedLiteral = "'%b', 0 ==> 'FALSE'\n"; | |
625 ::snprintf(expected, size, "%s", expectedLiteral); | |
626 ASSERT_EQ(strcmp(actual, expected), 0); | |
627 | |
628 memset(actual, 'z', sizeof(actual)); | |
629 VMInspector::snprintf(actual, size, "'%%b', 1 ==> '%b'\n", 1); | |
630 expectedLiteral = "'%b', 1 ==> 'TRUE'\n"; | |
631 ::snprintf(expected, size, "%s", expectedLiteral); | |
632 ASSERT_EQ(strcmp(actual, expected), 0); | |
633 | |
634 memset(actual, 'z', sizeof(actual)); | |
635 VMInspector::snprintf(actual, size, "'%%b', -123456789 ==> '%b'\n", -123
456789); | |
636 expectedLiteral = "'%b', -123456789 ==> 'TRUE'\n"; | |
637 ::snprintf(expected, size, "%s", expectedLiteral); | |
638 ASSERT_EQ(strcmp(actual, expected), 0); | |
639 | |
640 memset(actual, 'z', sizeof(actual)); | |
641 VMInspector::snprintf(actual, size, "'%%b', 123456789 ==> '%b'\n", 1234
56789); | |
642 expectedLiteral = "'%b', 123456789 ==> 'TRUE'\n"; | |
643 ::snprintf(expected, size, "%s", expectedLiteral); | |
644 ASSERT_EQ(strcmp(actual, expected), 0); | |
645 | |
646 // Testing the %J<x> extensions: | |
647 String str1("Test WTF String"); | |
648 String str2(""); | |
649 | |
650 memset(actual, 'z', sizeof(actual)); | |
651 VMInspector::snprintf(actual, size, "'%%Js' is %%s, &str1, str1.isEmpty(
)?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n", | |
652 &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
653 expectedLiteral = "'%Js' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EM
PTY\" ==> 'Test WTF String' is NOT EMPTY\n"; | |
654 ::snprintf(expected, size, "%s", expectedLiteral); | |
655 ASSERT_EQ(strcmp(actual, expected), 0); | |
656 | |
657 memset(actual, 'z', sizeof(actual)); | |
658 VMInspector::snprintf(actual, size, "'%%Js' is %%s, &str2, str2.isEmpty(
)?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n", | |
659 &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
660 expectedLiteral = "'%Js' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EM
PTY\" ==> '' is EMPTY\n"; | |
661 ::snprintf(expected, size, "%s", expectedLiteral); | |
662 ASSERT_EQ(strcmp(actual, expected), 0); | |
663 | |
664 memset(actual, 'z', sizeof(actual)); | |
665 VMInspector::snprintf(actual, size, "'%%J+s' is %%s, &str1, str1.isEmpty
()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n", | |
666 &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
667 expectedLiteral = "'%J+s' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT E
MPTY\" ==> 'WTF::String \"Test WTF String\"' is NOT EMPTY\n"; | |
668 ::snprintf(expected, size, "%s", expectedLiteral); | |
669 ASSERT_EQ(strcmp(actual, expected), 0); | |
670 | |
671 memset(actual, 'z', sizeof(actual)); | |
672 VMInspector::snprintf(actual, size, "'%%J+s' is %%s, &str2, str2.isEmpty
()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n", | |
673 &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY"); | |
674 expectedLiteral = "'%J+s' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT E
MPTY\" ==> 'WTF::String \"\"' is EMPTY\n"; | |
675 ::snprintf(expected, size, "%s", expectedLiteral); | |
676 ASSERT_EQ(strcmp(actual, expected), 0); | |
677 | |
678 // Test lower sizes more densely, and then space out to larger sizes. | |
679 // We're doing this because the lower sizes might be interesting, but | |
680 // for expediency, we don't want to test at this fine grain resolution | |
681 // for all possible sizes. Hence, we accelerate the rate once we're | |
682 // pass the interesting small sizes. | |
683 if (size <= 5) | |
684 size++; | |
685 else | |
686 size += 4; | |
687 } | |
688 #endif | |
689 } | |
OLD | NEW |