Debian Codegraph @

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

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