PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/js/src/jit-test/tests/coverage/simple.js

https://bitbucket.org/vionika/spin.android
JavaScript | 578 lines | 342 code | 38 blank | 198 comment | 46 complexity | 523c6a8c16c0c997de74a4a326929daa MD5 | raw file
Possible License(s): JSON, 0BSD, AGPL-1.0, BSD-2-Clause, GPL-3.0, LGPL-2.1, LGPL-3.0, CC0-1.0, AGPL-3.0, MPL-2.0, Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, Unlicense
  1. // |jit-test| --code-coverage;
  2. // Currently the Jit integration has a few issues, let's keep this test
  3. // case deterministic.
  4. //
  5. // - Baseline OSR increments the loop header twice.
  6. // - Ion is not updating any counter yet.
  7. //
  8. if (getJitCompilerOptions()["ion.warmup.trigger"] != 30)
  9. setJitCompilerOption("ion.warmup.trigger", 30);
  10. if (getJitCompilerOptions()["baseline.warmup.trigger"] != 10)
  11. setJitCompilerOption("baseline.warmup.trigger", 10);
  12. /*
  13. * These test cases are annotated with the output produced by LCOV [1]. Comment
  14. * starting with //<key> without any spaces are used as a reference for the code
  15. * coverage output. Any "$" in these line comments are replaced by the current
  16. * line number, and any "%" are replaced with the current function name (defined
  17. * by the FN key).
  18. *
  19. * [1] http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php
  20. */
  21. function checkLcov(fun) {
  22. var keys = [ "TN", "SF", "FN", "FNDA", "FNF", "FNH", "BRDA", "BRF", "BRH", "DA", "LF", "LH" ];
  23. function startsWithKey(s) {
  24. for (k of keys) {
  25. if (s.startsWith(k))
  26. return true;
  27. }
  28. return false;
  29. };
  30. // Extract the body of the function, as the code to be executed.
  31. var source = fun.toSource();
  32. source = source.slice(source.indexOf('{') + 1, source.lastIndexOf('}'));
  33. // Extract comment starting with the previous keys, as a reference of the
  34. // output expected from getLcovInfo.
  35. var lcovRef = [];
  36. var currLine = 0;
  37. var currFun = "<badfunction>";
  38. for (var line of source.split('\n')) {
  39. currLine++;
  40. for (var comment of line.split("//").slice(1)) {
  41. if (!startsWithKey(comment))
  42. continue;
  43. comment = comment.trim();
  44. if (comment.startsWith("FN:"))
  45. currFun = comment.split(',')[1];
  46. comment = comment.replace('$', currLine);
  47. comment = comment.replace('%', currFun);
  48. lcovRef.push(comment);
  49. }
  50. }
  51. // Evaluate the code, and generate the Lcov result from the execution. We have
  52. // to disable lazy parsing, as we rely on the ZoneCellIter to emulate the
  53. // behaviour of the finalizer.
  54. var g = newGlobal({ disableLazyParsing: true });
  55. g.eval(source);
  56. var lcovResRaw = getLcovInfo(g);
  57. // Check that all the lines are present the result.
  58. var lcovRes = lcovResRaw.split('\n');
  59. for (ref of lcovRef) {
  60. if (lcovRes.indexOf(ref) == -1) {
  61. print("Cannot find `" + ref + "` in the following Lcov result:\n", lcovResRaw);
  62. print("In the following source:\n", source);
  63. assertEq(true, false);
  64. }
  65. }
  66. }
  67. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  68. ",".split(','); //DA:$,1
  69. //FNF:1
  70. //FNH:1
  71. //LF:1
  72. //LH:1
  73. });
  74. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  75. function f() { //FN:$,f
  76. ",".split(','); //DA:$,0
  77. }
  78. ",".split(','); //DA:$,1
  79. //FNF:2
  80. //FNH:1
  81. //LF:2
  82. //LH:1
  83. });
  84. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  85. function f() { //FN:$,f //FNDA:1,%
  86. ",".split(','); //DA:$,1
  87. }
  88. f(); //DA:$,1
  89. //FNF:2
  90. //FNH:2
  91. //LF:2
  92. //LH:2
  93. });
  94. checkLcov(function () { ','.split(','); //FN:$,top-level //FNDA:1,% //DA:$,1
  95. //FNF:1
  96. //FNH:1
  97. //LF:1
  98. //LH:1
  99. });
  100. checkLcov(function () { function f() { ','.split(','); } //FN:$,top-level //FNDA:1,% //FN:$,f //FNDA:1,f //DA:$,1
  101. f(); //DA:$,1
  102. //FNF:2
  103. //FNH:2
  104. //LF:2
  105. //LH:2
  106. });
  107. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  108. var l = ",".split(','); //DA:$,1
  109. if (l.length == 3) //DA:$,1 //BRDA:$,0,0,1 //BRDA:$,0,1,0
  110. l.push(''); //DA:$,0
  111. l.pop(); //DA:$,1
  112. //FNF:1
  113. //FNH:1
  114. //LF:4
  115. //LH:3
  116. //BRF:2
  117. //BRH:1
  118. });
  119. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  120. var l = ",".split(','); //DA:$,1
  121. if (l.length == 2) //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1
  122. l.push(''); //DA:$,1
  123. l.pop(); //DA:$,1
  124. //FNF:1
  125. //FNH:1
  126. //LF:4
  127. //LH:4
  128. //BRF:2
  129. //BRH:1
  130. });
  131. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  132. var l = ",".split(','); //DA:$,1
  133. if (l.length == 3) //DA:$,1 //BRDA:$,0,0,1 //BRDA:$,0,1,0
  134. l.push(''); //DA:$,0
  135. else
  136. l.pop(); //DA:$,1
  137. //FNF:1
  138. //FNH:1
  139. //LF:4
  140. //LH:3
  141. //BRF:2
  142. //BRH:1
  143. });
  144. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  145. var l = ",".split(','); //DA:$,1
  146. if (l.length == 2) //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1
  147. l.push(''); //DA:$,1
  148. else
  149. l.pop(); //DA:$,0
  150. //FNF:1
  151. //FNH:1
  152. //LF:4
  153. //LH:3
  154. //BRF:2
  155. //BRH:1
  156. });
  157. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  158. var l = ",".split(','); //DA:$,1
  159. if (l.length == 2) //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1
  160. l.push(''); //DA:$,1
  161. else {
  162. if (l.length == 1) //DA:$,0 //BRDA:$,1,0,- //BRDA:$,1,1,-
  163. l.pop(); //DA:$,0
  164. }
  165. //FNF:1
  166. //FNH:1
  167. //LF:5
  168. //LH:3
  169. //BRF:4
  170. //BRH:1
  171. });
  172. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  173. function f(i) { //FN:$,f //FNDA:2,%
  174. var x = 0; //DA:$,2
  175. while (i--) { // Currently OSR wrongly count the loop header twice.
  176. // So instead of DA:$,12 , we have DA:$,13 .
  177. x += i; //DA:$,10
  178. x = x / 2; //DA:$,10
  179. }
  180. return x; //DA:$,2
  181. //BRF:2
  182. //BRH:2
  183. }
  184. f(5); //DA:$,1
  185. f(5); //DA:$,1
  186. //FNF:2
  187. //FNH:2
  188. });
  189. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  190. try { //DA:$,1
  191. var l = ",".split(','); //DA:$,1
  192. if (l.length == 2) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1
  193. l.push(''); //DA:$,1
  194. throw l; //DA:$,1
  195. }
  196. l.pop(); //DA:$,0
  197. } catch (x) { //DA:$,1
  198. x.pop(); //DA:$,1
  199. }
  200. //FNF:1
  201. //FNH:1
  202. //LF:8
  203. //LH:7
  204. //BRF:2
  205. //BRH:1
  206. });
  207. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  208. var l = ",".split(','); //DA:$,1
  209. try { //DA:$,1
  210. try { //DA:$,1
  211. if (l.length == 2) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1
  212. l.push(''); //DA:$,1
  213. throw l; //DA:$,1
  214. }
  215. l.pop(); //DA:$,0
  216. } finally { //DA:$,1
  217. l.pop(); //DA:$,1
  218. }
  219. } catch (x) { //DA:$,1
  220. }
  221. //FNF:1
  222. //FNH:1
  223. //LF:10
  224. //LH:9
  225. //BRF:2
  226. //BRH:1
  227. });
  228. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  229. function f() { //FN:$,f //FNDA:1,%
  230. throw 1; //DA:$,1
  231. f(); //DA:$,0
  232. }
  233. var l = ",".split(','); //DA:$,1
  234. try { //DA:$,1
  235. f(); //DA:$,1
  236. f(); //DA:$,0
  237. } catch (x) { //DA:$,1
  238. }
  239. //FNF:2
  240. //FNH:2
  241. //LF:7
  242. //LH:5
  243. //BRF:0
  244. //BRH:0
  245. });
  246. // Test TableSwitch opcode
  247. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  248. var l = ",".split(','); //DA:$,1
  249. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0 //BRDA:$,0,4,0
  250. case 0:
  251. l.push('0'); //DA:$,0
  252. break;
  253. case 1:
  254. l.push('1'); //DA:$,0
  255. break;
  256. case 2:
  257. l.push('2'); //DA:$,1
  258. break;
  259. case 3:
  260. l.push('3'); //DA:$,0
  261. break;
  262. }
  263. l.pop(); //DA:$,1
  264. //FNF:1
  265. //FNH:1
  266. //LF:7
  267. //LH:4
  268. //BRF:5
  269. //BRH:1
  270. });
  271. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  272. var l = ",".split(','); //DA:$,1
  273. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0 //BRDA:$,0,4,0
  274. case 0:
  275. l.push('0'); //DA:$,0
  276. case 1:
  277. l.push('1'); //DA:$,0
  278. case 2:
  279. l.push('2'); //DA:$,1
  280. case 3:
  281. l.push('3'); //DA:$,1
  282. }
  283. l.pop(); //DA:$,1
  284. //FNF:1
  285. //FNH:1
  286. //LF:7
  287. //LH:5
  288. //BRF:5
  289. //BRH:1
  290. });
  291. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  292. var l = ",".split(','); //DA:$,1
  293. // Branches are ordered, and starting at 0
  294. switch (l.length) { //DA:$,1 //BRDA:$,0,0,1 //BRDA:$,0,1,0 //BRDA:$,0,2,0 //BRDA:$,0,3,0 //BRDA:$,0,4,0
  295. case 5:
  296. l.push('5'); //DA:$,0
  297. case 4:
  298. l.push('4'); //DA:$,0
  299. case 3:
  300. l.push('3'); //DA:$,0
  301. case 2:
  302. l.push('2'); //DA:$,1
  303. }
  304. l.pop(); //DA:$,1
  305. //FNF:1
  306. //FNH:1
  307. //LF:7
  308. //LH:4
  309. //BRF:5
  310. //BRH:1
  311. });
  312. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  313. var l = ",".split(','); //DA:$,1
  314. switch (l.length) { //DA:$,1 //BRDA:$,0,0,1 //BRDA:$,0,1,0 //BRDA:$,0,2,0
  315. case 2:
  316. l.push('2'); //DA:$,1
  317. case 5:
  318. l.push('5'); //DA:$,1
  319. }
  320. l.pop(); //DA:$,1
  321. //FNF:1
  322. //FNH:1
  323. //LF:5
  324. //LH:5
  325. //BRF:3
  326. //BRH:1
  327. });
  328. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  329. var l = ",".split(','); //DA:$,1
  330. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1 //BRDA:$,0,2,0
  331. case 3:
  332. l.push('1'); //DA:$,0
  333. case 5:
  334. l.push('5'); //DA:$,0
  335. }
  336. l.pop(); //DA:$,1
  337. //FNF:1
  338. //FNH:1
  339. //LF:5
  340. //LH:3
  341. //BRF:3
  342. //BRH:1
  343. });
  344. // Unfortunately the differences between switch implementations leaks in the
  345. // code coverage reports.
  346. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  347. function f(a) { //FN:$,f //FNDA:2,%
  348. return a; //DA:$,2
  349. }
  350. var l = ",".split(','); //DA:$,1
  351. switch (l.length) { //DA:$,1
  352. case f(-42): //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1
  353. l.push('1'); //DA:$,0
  354. case f(51): //DA:$,1 //BRDA:$,1,0,0 //BRDA:$,1,1,1
  355. l.push('5'); //DA:$,0
  356. }
  357. l.pop(); //DA:$,1
  358. //FNF:2
  359. //FNH:2
  360. //LF:8
  361. //LH:6
  362. //BRF:4
  363. //BRH:2
  364. });
  365. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  366. var l = ",".split(','); //DA:$,1
  367. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,1 //BRDA:$,0,2,0 //BRDA:$,0,3,0
  368. case 0:
  369. case 1:
  370. l.push('0'); //DA:$,0
  371. l.push('1'); //DA:$,0
  372. case 2:
  373. l.push('2'); //DA:$,1
  374. case 3:
  375. l.push('3'); //DA:$,1
  376. }
  377. l.pop(); //DA:$,1
  378. //FNF:1
  379. //FNH:1
  380. //LF:7
  381. //LH:5
  382. //BRF:4
  383. //BRH:1
  384. });
  385. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  386. var l = ",".split(','); //DA:$,1
  387. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0
  388. case 0:
  389. l.push('0'); //DA:$,0
  390. case 1:
  391. l.push('1'); //DA:$,0
  392. case 2:
  393. case 3:
  394. l.push('2'); //DA:$,1
  395. l.push('3'); //DA:$,1
  396. }
  397. l.pop(); //DA:$,1
  398. //FNF:1
  399. //FNH:1
  400. //LF:7
  401. //LH:5
  402. //BRF:4
  403. //BRH:1
  404. });
  405. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  406. var l = ",".split(','); //DA:$,1
  407. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0
  408. case 0:
  409. l.push('0'); //DA:$,0
  410. case 1:
  411. default:
  412. l.push('1'); //DA:$,0
  413. case 2:
  414. l.push('2'); //DA:$,1
  415. case 3:
  416. l.push('3'); //DA:$,1
  417. }
  418. l.pop(); //DA:$,1
  419. //FNF:1
  420. //FNH:1
  421. //LF:7
  422. //LH:5
  423. //BRF:4
  424. //BRH:1
  425. });
  426. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  427. var l = ",".split(','); //DA:$,1
  428. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0
  429. case 0:
  430. l.push('0'); //DA:$,0
  431. case 1:
  432. l.push('1'); //DA:$,0
  433. default:
  434. case 2:
  435. l.push('2'); //DA:$,1
  436. case 3:
  437. l.push('3'); //DA:$,1
  438. }
  439. l.pop(); //DA:$,1
  440. //FNF:1
  441. //FNH:1
  442. //LF:7
  443. //LH:5
  444. //BRF:4
  445. //BRH:1
  446. });
  447. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  448. var l = ",".split(','); //DA:$,1
  449. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0 //BRDA:$,0,4,0
  450. case 0:
  451. l.push('0'); //DA:$,0
  452. case 1:
  453. l.push('1'); //DA:$,0
  454. default:
  455. l.push('default'); //DA:$,0
  456. case 2:
  457. l.push('2'); //DA:$,1
  458. case 3:
  459. l.push('3'); //DA:$,1
  460. }
  461. l.pop(); //DA:$,1
  462. //FNF:1
  463. //FNH:1
  464. //LF:8
  465. //LH:5
  466. //BRF:5
  467. //BRH:1
  468. });
  469. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  470. var l = ",".split(','); //DA:$,1
  471. switch (l.length) { //DA:$,1 //BRDA:$,0,0,0 //BRDA:$,0,1,0 //BRDA:$,0,2,1 //BRDA:$,0,3,0
  472. case 0:
  473. l.push('0'); //DA:$,0
  474. case 1:
  475. l.push('1'); //DA:$,0
  476. default:
  477. l.push('2'); //DA:$,1
  478. case 3:
  479. l.push('3'); //DA:$,1
  480. }
  481. l.pop(); //DA:$,1
  482. //FNF:1
  483. //FNH:1
  484. //LF:7
  485. //LH:5
  486. //BRF:4
  487. //BRH:1
  488. });
  489. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  490. var l = ','.split(','); //DA:$,1
  491. if (l.length === 45) { //DA:$,1 //BRDA:$,0,0,1 //BRDA:$,0,1,0
  492. switch (l[0]) { //DA:$,0 //BRDA:$,1,0,- //BRDA:$,1,1,-
  493. case ',':
  494. l.push('0'); //DA:$,0
  495. default:
  496. l.push('1'); //DA:$,0
  497. }
  498. }
  499. l.pop(); //DA:$,1
  500. //FNF:1
  501. //FNH:1
  502. //LF:6
  503. //LH:3
  504. //BRF:4
  505. //BRH:1
  506. });
  507. // These tests are not included in ../debug/Script-getOffsetsCoverage-01.js
  508. // because we're specifically testing a feature of Lcov output that
  509. // Debugger.Script doesn't have (the aggregation of hits that are on the
  510. // same line but in different functions).
  511. {
  512. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  513. function f() { return 0; } var l = f(); //DA:$,2
  514. //FNF:2
  515. //FNH:2
  516. //LF:1
  517. //LH:1
  518. });
  519. // A single line has two functions on it, and both hit.
  520. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  521. function f() { return 0; } function g() { return 1; } //DA:$,2
  522. var v = f() + g(); //DA:$,1
  523. //FNF:3
  524. //FNH:3
  525. //LF:2
  526. //LH:2
  527. });
  528. // A line has both function code and toplevel code, and only one of them hits.
  529. checkLcov(function () { //FN:$,top-level //FNDA:1,%
  530. if (1 === 2) //DA:$,1
  531. throw "0 hits here"; function f() { return "1 hit here"; } //DA:$,1
  532. f(); //DA:$,1
  533. //FNF:2
  534. //FNH:2
  535. //LF:3
  536. //LH:3
  537. });
  538. }
  539. // If you add a test case here, do the same in
  540. // jit-test/tests/debug/Script-getOffsetsCoverage-01.js