VMInspector.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. #include "config.h"
  26. #include <interpreter/VMInspector.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. using namespace JSC;
  30. // There's not much we can test for the VMInspector::printf() case except to
  31. // make sure it does not crash. Unfortunately, we also don't want all the
  32. // test strings crowding out stdout. So, we forego the printf tests.
  33. // NOTE that the most interesting part of VMInspector::printf() is the
  34. // formatting functionality, and it is are already being tested by the
  35. // fprintf() and sprintf() cases.
  36. // The VMInspector::fprintf() test works by printing the string to a temp file,
  37. // and then reading the file content back into a buffer, which we, in turn,
  38. // compare against the expected string.
  39. TEST(JSC, VMInspectorFprintf)
  40. {
  41. #if ENABLE(VMINSPECTOR)
  42. char actual[1024];
  43. char expected[1024];
  44. const char* format;
  45. const char* expectedLiteral;
  46. FILE* file;
  47. const char* filename = "/tmp/VMInspectorFprintfTest.txt";
  48. size_t size;
  49. #define OPEN_FILE(file) \
  50. do { \
  51. file = fopen(filename, "w"); \
  52. } while (false)
  53. #define READ_AND_CLOSE_FILE(file, actual) \
  54. do { \
  55. fclose(file); \
  56. file = fopen(filename, "r"); \
  57. fseek(file, 0, SEEK_END); \
  58. size = ftell(file); \
  59. rewind(file); \
  60. fread(actual, 1, size, file); \
  61. actual[size] = '\0'; \
  62. fclose(file); \
  63. } while (false)
  64. // Testing standard default format specifiers:
  65. // Note: should work just like sprintf. So, we just compare against that.
  66. memset(actual, 'z', sizeof(actual));
  67. // The compiler warning flags are configured to expect a literal string for
  68. // ::sprintf below. So, use a #define for this one case to keep the
  69. // compiler happy.
  70. #undef LITERAL_FORMAT
  71. #define LITERAL_FORMAT "'%%%%' ==> '%%'\n"
  72. OPEN_FILE(file);
  73. VMInspector::fprintf(file, LITERAL_FORMAT);
  74. READ_AND_CLOSE_FILE(file, actual);
  75. ::sprintf(expected, LITERAL_FORMAT);
  76. #undef LITERAL_FORMAT
  77. ASSERT_EQ(strcmp(actual, expected), 0);
  78. memset(actual, 'z', sizeof(actual));
  79. format = "'%%c', 'x' ==> '%c'\n";
  80. OPEN_FILE(file);
  81. VMInspector::fprintf(file, format, 'x');
  82. READ_AND_CLOSE_FILE(file, actual);
  83. ::sprintf(expected, format, 'x');
  84. ASSERT_EQ(strcmp(actual, expected), 0);
  85. memset(actual, 'z', sizeof(actual));
  86. format = "'%%*c', 8, 'x' ==> '%*c'\n";
  87. OPEN_FILE(file);
  88. VMInspector::fprintf(file, format, 8, 'x');
  89. READ_AND_CLOSE_FILE(file, actual);
  90. ::sprintf(expected, format, 8, 'x');
  91. ASSERT_EQ(strcmp(actual, expected), 0);
  92. memset(actual, 'z', sizeof(actual));
  93. format = "'%%s', \"hello world\" ==> '%s'\n";
  94. OPEN_FILE(file);
  95. VMInspector::fprintf(file, format, "hello world");
  96. READ_AND_CLOSE_FILE(file, actual);
  97. ::sprintf(expected, format, "hello world");
  98. ASSERT_EQ(strcmp(actual, expected), 0);
  99. memset(actual, 'z', sizeof(actual));
  100. format = "'%%*s', 8, \"hello world\" ==> '%*s'\n";
  101. OPEN_FILE(file);
  102. VMInspector::fprintf(file, format, 8, "hello world");
  103. READ_AND_CLOSE_FILE(file, actual);
  104. ::sprintf(expected, format, 8, "hello world");
  105. ASSERT_EQ(strcmp(actual, expected), 0);
  106. memset(actual, 'z', sizeof(actual));
  107. format = "'%%*s', 8, \"hello\" ==> '%*s'\n";
  108. OPEN_FILE(file);
  109. VMInspector::fprintf(file, format, 8, "hello");
  110. READ_AND_CLOSE_FILE(file, actual);
  111. ::sprintf(expected, format, 8, "hello");
  112. ASSERT_EQ(strcmp(actual, expected), 0);
  113. memset(actual, 'z', sizeof(actual));
  114. format = "'%%d', 987654321 ==> '%d'\n";
  115. OPEN_FILE(file);
  116. VMInspector::fprintf(file, format, 987654321);
  117. READ_AND_CLOSE_FILE(file, actual);
  118. ::sprintf(expected, format, 987654321);
  119. ASSERT_EQ(strcmp(actual, expected), 0);
  120. memset(actual, 'z', sizeof(actual));
  121. format = "'%%u', 4276543210u ==> '%u'\n";
  122. OPEN_FILE(file);
  123. VMInspector::fprintf(file, format, 4276543210u);
  124. READ_AND_CLOSE_FILE(file, actual);
  125. ::sprintf(expected, format, 4276543210u);
  126. ASSERT_EQ(strcmp(actual, expected), 0);
  127. memset(actual, 'z', sizeof(actual));
  128. format = "'%%u', 0xffffffff ==> '%u'\n";
  129. OPEN_FILE(file);
  130. VMInspector::fprintf(file, format, 0xffffffff);
  131. READ_AND_CLOSE_FILE(file, actual);
  132. ::sprintf(expected, format, 0xffffffff);
  133. ASSERT_EQ(strcmp(actual, expected), 0);
  134. memset(actual, 'z', sizeof(actual));
  135. format = "'%%x', 0xffffffff ==> '%x'\n";
  136. OPEN_FILE(file);
  137. VMInspector::fprintf(file, format, 0xffffffff);
  138. READ_AND_CLOSE_FILE(file, actual);
  139. ::sprintf(expected, format, 0xffffffff);
  140. ASSERT_EQ(strcmp(actual, expected), 0);
  141. memset(actual, 'z', sizeof(actual));
  142. format = "'%%p', (void*)0xabcdbabe ==> '%p'\n";
  143. OPEN_FILE(file);
  144. VMInspector::fprintf(file, format, (void*)0xabcdbabe);
  145. READ_AND_CLOSE_FILE(file, actual);
  146. ::sprintf(expected, format, (void*)0xabcdbabe);
  147. ASSERT_EQ(strcmp(actual, expected), 0);
  148. memset(actual, 'z', sizeof(actual));
  149. format = "'%%lu', 1234567890987654321ul ==> '%lu'\n";
  150. OPEN_FILE(file);
  151. VMInspector::fprintf(file, format, 1234567890987654321ul);
  152. READ_AND_CLOSE_FILE(file, actual);
  153. ::sprintf(expected, format, 1234567890987654321ul);
  154. ASSERT_EQ(strcmp(actual, expected), 0);
  155. memset(actual, 'z', sizeof(actual));
  156. format = "'%%f', 1234.567 ==> '%f'\n";
  157. OPEN_FILE(file);
  158. VMInspector::fprintf(file, format, 1234.567);
  159. READ_AND_CLOSE_FILE(file, actual);
  160. ::sprintf(expected, format, 1234.567);
  161. ASSERT_EQ(strcmp(actual, expected), 0);
  162. memset(actual, 'z', sizeof(actual));
  163. format = "'%%.2f', 1234.567 ==> '%.2f'\n";
  164. OPEN_FILE(file);
  165. VMInspector::fprintf(file, format, 1234.567);
  166. READ_AND_CLOSE_FILE(file, actual);
  167. ::sprintf(expected, format, 1234.567);
  168. ASSERT_EQ(strcmp(actual, expected), 0);
  169. memset(actual, 'z', sizeof(actual));
  170. format = "'%%10.2f', 1234.567 ==> '%10.2f'\n";
  171. OPEN_FILE(file);
  172. VMInspector::fprintf(file, format, 1234.567);
  173. READ_AND_CLOSE_FILE(file, actual);
  174. ::sprintf(expected, format, 1234.567);
  175. ASSERT_EQ(strcmp(actual, expected), 0);
  176. memset(actual, 'z', sizeof(actual));
  177. format = "'%%010.2f', 1234.567 ==> '%010.2f'\n";
  178. OPEN_FILE(file);
  179. VMInspector::fprintf(file, format, 1234.567);
  180. READ_AND_CLOSE_FILE(file, actual);
  181. ::sprintf(expected, format, 1234.567);
  182. ASSERT_EQ(strcmp(actual, expected), 0);
  183. // Bad / weird formats:
  184. memset(actual, 'z', sizeof(actual));
  185. format = "'%%5.4', 987654321 ==> '%5.4'\n";
  186. OPEN_FILE(file);
  187. VMInspector::fprintf(file, format, 987654321);
  188. READ_AND_CLOSE_FILE(file, actual);
  189. expectedLiteral = "'%5.4', 987654321 ==> 'ERROR @ \"%5.4' \"\n";
  190. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  191. memset(actual, 'z', sizeof(actual));
  192. format = "'%%5.4' '%%d', 987654321, 4 ==> '%5.4' '%d'\n";
  193. OPEN_FILE(file);
  194. VMInspector::fprintf(file, format, 987654321, 4);
  195. READ_AND_CLOSE_FILE(file, actual);
  196. ::sprintf(expected, format, 987654321, 4);
  197. ASSERT_EQ(strcmp(actual, expected), 0);
  198. memset(actual, 'z', sizeof(actual));
  199. format = "'%%w' '%%d', 987654321, 6 ==> '%w' '%d'\n";
  200. OPEN_FILE(file);
  201. VMInspector::fprintf(file, format, 987654321, 6);
  202. READ_AND_CLOSE_FILE(file, actual);
  203. ::sprintf(expected, format, 987654321, 6);
  204. ASSERT_EQ(strcmp(actual, expected), 0);
  205. // Testing the %b extension:
  206. memset(actual, 'z', sizeof(actual));
  207. OPEN_FILE(file);
  208. VMInspector::fprintf(file, "'%%b', 0 ==> '%b'\n", 0);
  209. READ_AND_CLOSE_FILE(file, actual);
  210. ASSERT_EQ(strcmp(actual, "'%b', 0 ==> 'FALSE'\n"), 0);
  211. memset(actual, 'z', sizeof(actual));
  212. OPEN_FILE(file);
  213. VMInspector::fprintf(file, "'%%b', 1 ==> '%b'\n", 1);
  214. READ_AND_CLOSE_FILE(file, actual);
  215. ASSERT_EQ(strcmp(actual, "'%b', 1 ==> 'TRUE'\n"), 0);
  216. memset(actual, 'z', sizeof(actual));
  217. OPEN_FILE(file);
  218. VMInspector::fprintf(file, "'%%b', -123456789 ==> '%b'\n", -123456789);
  219. READ_AND_CLOSE_FILE(file, actual);
  220. ASSERT_EQ(strcmp(actual, "'%b', -123456789 ==> 'TRUE'\n"), 0);
  221. memset(actual, 'z', sizeof(actual));
  222. OPEN_FILE(file);
  223. VMInspector::fprintf(file, "'%%b', 123456789 ==> '%b'\n", 123456789);
  224. READ_AND_CLOSE_FILE(file, actual);
  225. ASSERT_EQ(strcmp(actual, "'%b', 123456789 ==> 'TRUE'\n"), 0);
  226. // Testing the %J<x> extensions:
  227. String str1("Test WTF String");
  228. String str2("");
  229. memset(actual, 'z', sizeof(actual));
  230. OPEN_FILE(file);
  231. VMInspector::fprintf(file, "'%%Js' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n",
  232. &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY");
  233. READ_AND_CLOSE_FILE(file, actual);
  234. expectedLiteral = "'%Js' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'Test WTF String' is NOT EMPTY\n";
  235. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  236. memset(actual, 'z', sizeof(actual));
  237. OPEN_FILE(file);
  238. VMInspector::fprintf(file, "'%%Js' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n",
  239. &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY");
  240. READ_AND_CLOSE_FILE(file, actual);
  241. expectedLiteral = "'%Js' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '' is EMPTY\n";
  242. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  243. memset(actual, 'z', sizeof(actual));
  244. OPEN_FILE(file);
  245. VMInspector::fprintf(file, "'%%J+s' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n",
  246. &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY");
  247. READ_AND_CLOSE_FILE(file, actual);
  248. expectedLiteral = "'%J+s' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'WTF::String \"Test WTF String\"' is NOT EMPTY\n";
  249. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  250. memset(actual, 'z', sizeof(actual));
  251. OPEN_FILE(file);
  252. VMInspector::fprintf(file, "'%%J+s' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n",
  253. &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY");
  254. READ_AND_CLOSE_FILE(file, actual);
  255. expectedLiteral = "'%J+s' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'WTF::String \"\"' is EMPTY\n";
  256. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  257. #undef OPEN_FILE
  258. #undef READ_AND_CLOSE_FILE
  259. #endif
  260. }
  261. TEST(JSC, VMInspectorSprintf)
  262. {
  263. #if ENABLE(VMINSPECTOR)
  264. char actual[1024];
  265. char expected[1024];
  266. const char* format;
  267. const char* expectedLiteral;
  268. // Testing standard default format specifiers:
  269. // Note: should work just like sprintf. So, we just compare against that.
  270. memset(actual, 'z', sizeof(actual));
  271. // The compiler warning flags are configured to expect a literal string for
  272. // ::sprintf below. So, use a #define for this one case to keep the
  273. // compiler happy.
  274. #undef LITERAL_FORMAT
  275. #define LITERAL_FORMAT "'%%%%' ==> '%%'\n"
  276. VMInspector::sprintf(actual, LITERAL_FORMAT);
  277. ::sprintf(expected, LITERAL_FORMAT);
  278. #undef LITERAL_FORMAT
  279. ASSERT_EQ(strcmp(actual, expected), 0);
  280. memset(actual, 'z', sizeof(actual));
  281. format = "'%%c', 'x' ==> '%c'\n";
  282. VMInspector::sprintf(actual, format, 'x');
  283. ::sprintf(expected, format, 'x');
  284. ASSERT_EQ(strcmp(actual, expected), 0);
  285. memset(actual, 'z', sizeof(actual));
  286. format = "'%%*c', 8, 'x' ==> '%*c'\n";
  287. VMInspector::sprintf(actual, format, 8, 'x');
  288. ::sprintf(expected, format, 8, 'x');
  289. ASSERT_EQ(strcmp(actual, expected), 0);
  290. memset(actual, 'z', sizeof(actual));
  291. format = "'%%s', \"hello world\" ==> '%s'\n";
  292. VMInspector::sprintf(actual, format, "hello world");
  293. ::sprintf(expected, format, "hello world");
  294. ASSERT_EQ(strcmp(actual, expected), 0);
  295. memset(actual, 'z', sizeof(actual));
  296. format = "'%%*s', 8, \"hello world\" ==> '%*s'\n";
  297. VMInspector::sprintf(actual, format, 8, "hello world");
  298. ::sprintf(expected, format, 8, "hello world");
  299. ASSERT_EQ(strcmp(actual, expected), 0);
  300. memset(actual, 'z', sizeof(actual));
  301. format = "'%%*s', 8, \"hello\" ==> '%*s'\n";
  302. VMInspector::sprintf(actual, format, 8, "hello");
  303. ::sprintf(expected, format, 8, "hello");
  304. ASSERT_EQ(strcmp(actual, expected), 0);
  305. memset(actual, 'z', sizeof(actual));
  306. format = "'%%d', 987654321 ==> '%d'\n";
  307. VMInspector::sprintf(actual, format, 987654321);
  308. ::sprintf(expected, format, 987654321);
  309. ASSERT_EQ(strcmp(actual, expected), 0);
  310. memset(actual, 'z', sizeof(actual));
  311. format = "'%%u', 4276543210u ==> '%u'\n";
  312. VMInspector::sprintf(actual, format, 4276543210u);
  313. ::sprintf(expected, format, 4276543210u);
  314. ASSERT_EQ(strcmp(actual, expected), 0);
  315. memset(actual, 'z', sizeof(actual));
  316. format = "'%%u', 0xffffffff ==> '%u'\n";
  317. VMInspector::sprintf(actual, format, 0xffffffff);
  318. ::sprintf(expected, format, 0xffffffff);
  319. ASSERT_EQ(strcmp(actual, expected), 0);
  320. memset(actual, 'z', sizeof(actual));
  321. format = "'%%x', 0xffffffff ==> '%x'\n";
  322. VMInspector::sprintf(actual, format, 0xffffffff);
  323. ::sprintf(expected, format, 0xffffffff);
  324. ASSERT_EQ(strcmp(actual, expected), 0);
  325. memset(actual, 'z', sizeof(actual));
  326. format = "'%%p', (void*)0xabcdbabe ==> '%p'\n";
  327. VMInspector::sprintf(actual, format, (void*)0xabcdbabe);
  328. ::sprintf(expected, format, (void*)0xabcdbabe);
  329. ASSERT_EQ(strcmp(actual, expected), 0);
  330. memset(actual, 'z', sizeof(actual));
  331. format = "'%%lu', 1234567890987654321ul ==> '%lu'\n";
  332. VMInspector::sprintf(actual, format, 1234567890987654321ul);
  333. ::sprintf(expected, format, 1234567890987654321ul);
  334. ASSERT_EQ(strcmp(actual, expected), 0);
  335. memset(actual, 'z', sizeof(actual));
  336. format = "'%%f', 1234.567 ==> '%f'\n";
  337. VMInspector::sprintf(actual, format, 1234.567);
  338. ::sprintf(expected, format, 1234.567);
  339. ASSERT_EQ(strcmp(actual, expected), 0);
  340. memset(actual, 'z', sizeof(actual));
  341. format = "'%%.2f', 1234.567 ==> '%.2f'\n";
  342. VMInspector::sprintf(actual, format, 1234.567);
  343. ::sprintf(expected, format, 1234.567);
  344. ASSERT_EQ(strcmp(actual, expected), 0);
  345. memset(actual, 'z', sizeof(actual));
  346. format = "'%%10.2f', 1234.567 ==> '%10.2f'\n";
  347. VMInspector::sprintf(actual, format, 1234.567);
  348. ::sprintf(expected, format, 1234.567);
  349. ASSERT_EQ(strcmp(actual, expected), 0);
  350. memset(actual, 'z', sizeof(actual));
  351. format = "'%%010.2f', 1234.567 ==> '%010.2f'\n";
  352. VMInspector::sprintf(actual, format, 1234.567);
  353. ::sprintf(expected, format, 1234.567);
  354. ASSERT_EQ(strcmp(actual, expected), 0);
  355. // Bad / weird formats:
  356. memset(actual, 'z', sizeof(actual));
  357. format = "'%%5.4', 987654321 ==> '%5.4'\n";
  358. VMInspector::sprintf(actual, format, 987654321);
  359. expectedLiteral = "'%5.4', 987654321 ==> 'ERROR @ \"%5.4' \"\n";
  360. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  361. memset(actual, 'z', sizeof(actual));
  362. format = "'%%5.4' '%%d', 987654321, 4 ==> '%5.4' '%d'\n";
  363. VMInspector::sprintf(actual, format, 987654321, 4);
  364. ::sprintf(expected, format, 987654321, 4);
  365. ASSERT_EQ(strcmp(actual, expected), 0);
  366. memset(actual, 'z', sizeof(actual));
  367. format = "'%%w' '%%d', 987654321, 6 ==> '%w' '%d'\n";
  368. VMInspector::sprintf(actual, format, 987654321, 6);
  369. ::sprintf(expected, format, 987654321, 6);
  370. ASSERT_EQ(strcmp(actual, expected), 0);
  371. // Testing the %b extension:
  372. memset(actual, 'z', sizeof(actual));
  373. VMInspector::sprintf(actual, "'%%b', 0 ==> '%b'\n", 0);
  374. ASSERT_EQ(strcmp(actual, "'%b', 0 ==> 'FALSE'\n"), 0);
  375. memset(actual, 'z', sizeof(actual));
  376. VMInspector::sprintf(actual, "'%%b', 1 ==> '%b'\n", 1);
  377. ASSERT_EQ(strcmp(actual, "'%b', 1 ==> 'TRUE'\n"), 0);
  378. memset(actual, 'z', sizeof(actual));
  379. VMInspector::sprintf(actual, "'%%b', -123456789 ==> '%b'\n", -123456789);
  380. ASSERT_EQ(strcmp(actual, "'%b', -123456789 ==> 'TRUE'\n"), 0);
  381. memset(actual, 'z', sizeof(actual));
  382. VMInspector::sprintf(actual, "'%%b', 123456789 ==> '%b'\n", 123456789);
  383. ASSERT_EQ(strcmp(actual, "'%b', 123456789 ==> 'TRUE'\n"), 0);
  384. // Testing the %J<x> extensions:
  385. String str1("Test WTF String");
  386. String str2("");
  387. memset(actual, 'z', sizeof(actual));
  388. VMInspector::sprintf(actual, "'%%Js' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n",
  389. &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY");
  390. expectedLiteral = "'%Js' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'Test WTF String' is NOT EMPTY\n";
  391. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  392. memset(actual, 'z', sizeof(actual));
  393. VMInspector::sprintf(actual, "'%%Js' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n",
  394. &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY");
  395. expectedLiteral = "'%Js' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '' is EMPTY\n";
  396. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  397. memset(actual, 'z', sizeof(actual));
  398. VMInspector::sprintf(actual, "'%%J+s' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n",
  399. &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY");
  400. expectedLiteral = "'%J+s' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'WTF::String \"Test WTF String\"' is NOT EMPTY\n";
  401. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  402. memset(actual, 'z', sizeof(actual));
  403. VMInspector::sprintf(actual, "'%%J+s' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n",
  404. &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY");
  405. expectedLiteral = "'%J+s' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'WTF::String \"\"' is EMPTY\n";
  406. ASSERT_EQ(strcmp(actual, expectedLiteral), 0);
  407. #endif
  408. }
  409. TEST(JSC, VMInspectorSnprintf)
  410. {
  411. #if ENABLE(VMINSPECTOR)
  412. char actual[1024];
  413. char expected[1024];
  414. const char* format;
  415. const char* expectedLiteral;
  416. size_t size = 1;
  417. while (size <= 100) {
  418. // Testing standard default format specifiers:
  419. // Note: should work just like snprintf. So, we just compare against that.
  420. memset(actual, 'z', sizeof(actual));
  421. // The compiler warning flags are configured to expect a literal string for
  422. // ::snprintf below. So, use a #define for this one case to keep the
  423. // compiler happy.
  424. #undef LITERAL_FORMAT
  425. #define LITERAL_FORMAT "'%%%%' ==> '%%'\n"
  426. VMInspector::snprintf(actual, size, LITERAL_FORMAT);
  427. ::snprintf(expected, size, LITERAL_FORMAT);
  428. #undef LITERAL_FORMAT
  429. ASSERT_EQ(strcmp(actual, expected), 0);
  430. memset(actual, 'z', sizeof(actual));
  431. format = "'%%c', 'x' ==> '%c'\n";
  432. VMInspector::snprintf(actual, size, format, 'x');
  433. ::snprintf(expected, size, format, 'x');
  434. ASSERT_EQ(strcmp(actual, expected), 0);
  435. memset(actual, 'z', sizeof(actual));
  436. format = "'%%*c', 8, 'x' ==> '%*c'\n";
  437. VMInspector::snprintf(actual, size, format, 8, 'x');
  438. ::snprintf(expected, size, format, 8, 'x');
  439. ASSERT_EQ(strcmp(actual, expected), 0);
  440. memset(actual, 'z', sizeof(actual));
  441. format = "'%%s', \"hello world\" ==> '%s'\n";
  442. VMInspector::snprintf(actual, size, format, "hello world");
  443. ::snprintf(expected, size, format, "hello world");
  444. ASSERT_EQ(strcmp(actual, expected), 0);
  445. memset(actual, 'z', sizeof(actual));
  446. format = "'%%*s', 8, \"hello world\" ==> '%*s'\n";
  447. VMInspector::snprintf(actual, size, format, 8, "hello world");
  448. ::snprintf(expected, size, format, 8, "hello world");
  449. ASSERT_EQ(strcmp(actual, expected), 0);
  450. memset(actual, 'z', sizeof(actual));
  451. format = "'%%*s', 8, \"hello\" ==> '%*s'\n";
  452. VMInspector::snprintf(actual, size, format, 8, "hello");
  453. ::snprintf(expected, size, format, 8, "hello");
  454. ASSERT_EQ(strcmp(actual, expected), 0);
  455. memset(actual, 'z', sizeof(actual));
  456. format = "'%%d', 987654321 ==> '%d'\n";
  457. VMInspector::snprintf(actual, size, format, 987654321);
  458. ::snprintf(expected, size, format, 987654321);
  459. ASSERT_EQ(strcmp(actual, expected), 0);
  460. memset(actual, 'z', sizeof(actual));
  461. format = "'%%u', 4276543210u ==> '%u'\n";
  462. VMInspector::snprintf(actual, size, format, 4276543210u);
  463. ::snprintf(expected, size, format, 4276543210u);
  464. ASSERT_EQ(strcmp(actual, expected), 0);
  465. memset(actual, 'z', sizeof(actual));
  466. format = "'%%u', 0xffffffff ==> '%u'\n";
  467. VMInspector::snprintf(actual, size, format, 0xffffffff);
  468. ::snprintf(expected, size, format, 0xffffffff);
  469. ASSERT_EQ(strcmp(actual, expected), 0);
  470. memset(actual, 'z', sizeof(actual));
  471. format = "'%%x', 0xffffffff ==> '%x'\n";
  472. VMInspector::snprintf(actual, size, format, 0xffffffff);
  473. ::snprintf(expected, size, format, 0xffffffff);
  474. ASSERT_EQ(strcmp(actual, expected), 0);
  475. memset(actual, 'z', sizeof(actual));
  476. format = "'%%p', (void*)0xabcdbabe ==> '%p'\n";
  477. VMInspector::snprintf(actual, size, format, (void*)0xabcdbabe);
  478. ::snprintf(expected, size, format, (void*)0xabcdbabe);
  479. ASSERT_EQ(strcmp(actual, expected), 0);
  480. memset(actual, 'z', sizeof(actual));
  481. format = "'%%lu', 1234567890987654321ul ==> '%lu'\n";
  482. VMInspector::snprintf(actual, size, format, 1234567890987654321ul);
  483. ::snprintf(expected, size, format, 1234567890987654321ul);
  484. ASSERT_EQ(strcmp(actual, expected), 0);
  485. memset(actual, 'z', sizeof(actual));
  486. format = "'%%f', 1234.567 ==> '%f'\n";
  487. VMInspector::snprintf(actual, size, format, 1234.567);
  488. ::snprintf(expected, size, format, 1234.567);
  489. ASSERT_EQ(strcmp(actual, expected), 0);
  490. memset(actual, 'z', sizeof(actual));
  491. format = "'%%.2f', 1234.567 ==> '%.2f'\n";
  492. VMInspector::snprintf(actual, size, format, 1234.567);
  493. ::snprintf(expected, size, format, 1234.567);
  494. ASSERT_EQ(strcmp(actual, expected), 0);
  495. memset(actual, 'z', sizeof(actual));
  496. format = "'%%10.2f', 1234.567 ==> '%10.2f'\n";
  497. VMInspector::snprintf(actual, size, format, 1234.567);
  498. ::snprintf(expected, size, format, 1234.567);
  499. ASSERT_EQ(strcmp(actual, expected), 0);
  500. memset(actual, 'z', sizeof(actual));
  501. format = "'%%010.2f', 1234.567 ==> '%010.2f'\n";
  502. VMInspector::snprintf(actual, size, format, 1234.567);
  503. ::snprintf(expected, size, format, 1234.567);
  504. ASSERT_EQ(strcmp(actual, expected), 0);
  505. // Bad / weird formats:
  506. memset(actual, 'z', sizeof(actual));
  507. format = "'%%5.4', 987654321 ==> '%5.4'\n";
  508. VMInspector::snprintf(actual, size, format, 987654321);
  509. expectedLiteral = "'%5.4', 987654321 ==> 'ERROR @ \"%5.4' \"\n";
  510. ::snprintf(expected, size, "%s", expectedLiteral);
  511. ASSERT_EQ(strcmp(actual, expected), 0);
  512. memset(actual, 'z', sizeof(actual));
  513. format = "'%%5.4' '%%d', 987654321, 4 ==> '%5.4' '%d'\n";
  514. VMInspector::snprintf(actual, size, format, 987654321, 4);
  515. ::snprintf(expected, size, format, 987654321, 4);
  516. ASSERT_EQ(strcmp(actual, expected), 0);
  517. memset(actual, 'z', sizeof(actual));
  518. format = "'%%w' '%%d', 987654321, 6 ==> '%w' '%d'\n";
  519. VMInspector::snprintf(actual, size, format, 987654321, 6);
  520. ::snprintf(expected, size, format, 987654321, 6);
  521. ASSERT_EQ(strcmp(actual, expected), 0);
  522. // Testing the %b extension:
  523. memset(actual, 'z', sizeof(actual));
  524. VMInspector::snprintf(actual, size, "'%%b', 0 ==> '%b'\n", 0);
  525. expectedLiteral = "'%b', 0 ==> 'FALSE'\n";
  526. ::snprintf(expected, size, "%s", expectedLiteral);
  527. ASSERT_EQ(strcmp(actual, expected), 0);
  528. memset(actual, 'z', sizeof(actual));
  529. VMInspector::snprintf(actual, size, "'%%b', 1 ==> '%b'\n", 1);
  530. expectedLiteral = "'%b', 1 ==> 'TRUE'\n";
  531. ::snprintf(expected, size, "%s", expectedLiteral);
  532. ASSERT_EQ(strcmp(actual, expected), 0);
  533. memset(actual, 'z', sizeof(actual));
  534. VMInspector::snprintf(actual, size, "'%%b', -123456789 ==> '%b'\n", -123456789);
  535. expectedLiteral = "'%b', -123456789 ==> 'TRUE'\n";
  536. ::snprintf(expected, size, "%s", expectedLiteral);
  537. ASSERT_EQ(strcmp(actual, expected), 0);
  538. memset(actual, 'z', sizeof(actual));
  539. VMInspector::snprintf(actual, size, "'%%b', 123456789 ==> '%b'\n", 123456789);
  540. expectedLiteral = "'%b', 123456789 ==> 'TRUE'\n";
  541. ::snprintf(expected, size, "%s", expectedLiteral);
  542. ASSERT_EQ(strcmp(actual, expected), 0);
  543. // Testing the %J<x> extensions:
  544. String str1("Test WTF String");
  545. String str2("");
  546. memset(actual, 'z', sizeof(actual));
  547. VMInspector::snprintf(actual, size, "'%%Js' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n",
  548. &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY");
  549. expectedLiteral = "'%Js' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'Test WTF String' is NOT EMPTY\n";
  550. ::snprintf(expected, size, "%s", expectedLiteral);
  551. ASSERT_EQ(strcmp(actual, expected), 0);
  552. memset(actual, 'z', sizeof(actual));
  553. VMInspector::snprintf(actual, size, "'%%Js' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%Js' is %s\n",
  554. &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY");
  555. expectedLiteral = "'%Js' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '' is EMPTY\n";
  556. ::snprintf(expected, size, "%s", expectedLiteral);
  557. ASSERT_EQ(strcmp(actual, expected), 0);
  558. memset(actual, 'z', sizeof(actual));
  559. VMInspector::snprintf(actual, size, "'%%J+s' is %%s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n",
  560. &str1, str1.isEmpty() ? "EMPTY" : "NOT EMPTY");
  561. expectedLiteral = "'%J+s' is %s, &str1, str1.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'WTF::String \"Test WTF String\"' is NOT EMPTY\n";
  562. ::snprintf(expected, size, "%s", expectedLiteral);
  563. ASSERT_EQ(strcmp(actual, expected), 0);
  564. memset(actual, 'z', sizeof(actual));
  565. VMInspector::snprintf(actual, size, "'%%J+s' is %%s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> '%J+s' is %s\n",
  566. &str2, str2.isEmpty() ? "EMPTY" : "NOT EMPTY");
  567. expectedLiteral = "'%J+s' is %s, &str2, str2.isEmpty()?\"EMPTY\":\"NOT EMPTY\" ==> 'WTF::String \"\"' is EMPTY\n";
  568. ::snprintf(expected, size, "%s", expectedLiteral);
  569. ASSERT_EQ(strcmp(actual, expected), 0);
  570. // Test lower sizes more densely, and then space out to larger sizes.
  571. // We're doing this because the lower sizes might be interesting, but
  572. // for expediency, we don't want to test at this fine grain resolution
  573. // for all possible sizes. Hence, we accelerate the rate once we're
  574. // pass the interesting small sizes.
  575. if (size <= 5)
  576. size++;
  577. else
  578. size += 4;
  579. }
  580. #endif
  581. }