-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
tests.rs
681 lines (630 loc) · 28.5 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! This crate hosts a selection of "unit tests" for components of the `InstrumentCoverage` MIR
//! pass.
//!
//! ```shell
//! ./x.py test --keep-stage 1 compiler/rustc_mir --test-args '--show-output coverage'
//! ```
//!
//! The tests construct a few "mock" objects, as needed, to support the `InstrumentCoverage`
//! functions and algorithms. Mocked objects include instances of `mir::Body`; including
//! `Terminator`s of various `kind`s, and `Span` objects. Some functions used by or used on
//! real, runtime versions of these mocked-up objects have constraints (such as cross-thread
//! limitations) and deep dependencies on other elements of the full Rust compiler (which is
//! *not* constructed or mocked for these tests).
//!
//! Of particular note, attempting to simply print elements of the `mir::Body` with default
//! `Debug` formatting can fail because some `Debug` format implementations require the
//! `TyCtxt`, obtained via a static global variable that is *not* set for these tests.
//! Initializing the global type context is prohibitively complex for the scope and scale of these
//! tests (essentially requiring initializing the entire compiler).
//!
//! Also note, some basic features of `Span` also rely on the `Span`s own "session globals", which
//! are unrelated to the `TyCtxt` global. Without initializing the `Span` session globals, some
//! basic, coverage-specific features would be impossible to test, but thankfully initializing these
//! globals is comparatively simpler. The easiest way is to wrap the test in a closure argument
//! to: `rustc_span::create_default_session_globals_then(|| { test_here(); })`.
use super::counters;
use super::graph::{self, BasicCoverageBlock};
use coverage_test_macros::let_bcb;
use itertools::Itertools;
use rustc_data_structures::graph::WithNumNodes;
use rustc_data_structures::graph::WithSuccessors;
use rustc_index::{Idx, IndexVec};
use rustc_middle::mir::*;
use rustc_middle::ty;
use rustc_span::{self, BytePos, Pos, Span, DUMMY_SP};
// All `TEMP_BLOCK` targets should be replaced before calling `to_body() -> mir::Body`.
const TEMP_BLOCK: BasicBlock = BasicBlock::MAX;
struct MockBlocks<'tcx> {
blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
dummy_place: Place<'tcx>,
next_local: usize,
}
impl<'tcx> MockBlocks<'tcx> {
fn new() -> Self {
Self {
blocks: IndexVec::new(),
dummy_place: Place { local: RETURN_PLACE, projection: ty::List::empty() },
next_local: 0,
}
}
fn new_temp(&mut self) -> Local {
let index = self.next_local;
self.next_local += 1;
Local::new(index)
}
fn push(&mut self, kind: TerminatorKind<'tcx>) -> BasicBlock {
let next_lo = if let Some(last) = self.blocks.last_index() {
self.blocks[last].terminator().source_info.span.hi()
} else {
BytePos(1)
};
let next_hi = next_lo + BytePos(1);
self.blocks.push(BasicBlockData {
statements: vec![],
terminator: Some(Terminator {
source_info: SourceInfo::outermost(Span::with_root_ctxt(next_lo, next_hi)),
kind,
}),
is_cleanup: false,
})
}
fn link(&mut self, from_block: BasicBlock, to_block: BasicBlock) {
match self.blocks[from_block].terminator_mut().kind {
TerminatorKind::Assert { ref mut target, .. }
| TerminatorKind::Call { target: Some(ref mut target), .. }
| TerminatorKind::Drop { ref mut target, .. }
| TerminatorKind::FalseEdge { real_target: ref mut target, .. }
| TerminatorKind::FalseUnwind { real_target: ref mut target, .. }
| TerminatorKind::Goto { ref mut target }
| TerminatorKind::InlineAsm { destination: Some(ref mut target), .. }
| TerminatorKind::Yield { resume: ref mut target, .. } => *target = to_block,
ref invalid => bug!("Invalid from_block: {:?}", invalid),
}
}
fn add_block_from(
&mut self,
some_from_block: Option<BasicBlock>,
to_kind: TerminatorKind<'tcx>,
) -> BasicBlock {
let new_block = self.push(to_kind);
if let Some(from_block) = some_from_block {
self.link(from_block, new_block);
}
new_block
}
fn set_branch(&mut self, switchint: BasicBlock, branch_index: usize, to_block: BasicBlock) {
match self.blocks[switchint].terminator_mut().kind {
TerminatorKind::SwitchInt { ref mut targets, .. } => {
let mut branches = targets.iter().collect::<Vec<_>>();
let otherwise = if branch_index == branches.len() {
to_block
} else {
let old_otherwise = targets.otherwise();
if branch_index > branches.len() {
branches.push((branches.len() as u128, old_otherwise));
while branches.len() < branch_index {
branches.push((branches.len() as u128, TEMP_BLOCK));
}
to_block
} else {
branches[branch_index] = (branch_index as u128, to_block);
old_otherwise
}
};
*targets = SwitchTargets::new(branches.into_iter(), otherwise);
}
ref invalid => bug!("Invalid BasicBlock kind or no to_block: {:?}", invalid),
}
}
fn call(&mut self, some_from_block: Option<BasicBlock>) -> BasicBlock {
self.add_block_from(
some_from_block,
TerminatorKind::Call {
func: Operand::Copy(self.dummy_place.clone()),
args: vec![],
destination: self.dummy_place.clone(),
target: Some(TEMP_BLOCK),
unwind: UnwindAction::Continue,
call_source: CallSource::Misc,
fn_span: DUMMY_SP,
},
)
}
fn goto(&mut self, some_from_block: Option<BasicBlock>) -> BasicBlock {
self.add_block_from(some_from_block, TerminatorKind::Goto { target: TEMP_BLOCK })
}
fn switchint(&mut self, some_from_block: Option<BasicBlock>) -> BasicBlock {
let switchint_kind = TerminatorKind::SwitchInt {
discr: Operand::Move(Place::from(self.new_temp())),
targets: SwitchTargets::static_if(0, TEMP_BLOCK, TEMP_BLOCK),
};
self.add_block_from(some_from_block, switchint_kind)
}
fn return_(&mut self, some_from_block: Option<BasicBlock>) -> BasicBlock {
self.add_block_from(some_from_block, TerminatorKind::Return)
}
fn to_body(self) -> Body<'tcx> {
Body::new_cfg_only(self.blocks)
}
}
fn debug_basic_blocks(mir_body: &Body<'_>) -> String {
format!(
"{:?}",
mir_body
.basic_blocks
.iter_enumerated()
.map(|(bb, data)| {
let term = &data.terminator();
let kind = &term.kind;
let span = term.source_info.span;
let sp = format!("(span:{},{})", span.lo().to_u32(), span.hi().to_u32());
match kind {
TerminatorKind::Assert { target, .. }
| TerminatorKind::Call { target: Some(target), .. }
| TerminatorKind::Drop { target, .. }
| TerminatorKind::FalseEdge { real_target: target, .. }
| TerminatorKind::FalseUnwind { real_target: target, .. }
| TerminatorKind::Goto { target }
| TerminatorKind::InlineAsm { destination: Some(target), .. }
| TerminatorKind::Yield { resume: target, .. } => {
format!("{}{:?}:{} -> {:?}", sp, bb, kind.name(), target)
}
TerminatorKind::SwitchInt { targets, .. } => {
format!("{}{:?}:{} -> {:?}", sp, bb, kind.name(), targets)
}
_ => format!("{}{:?}:{}", sp, bb, kind.name()),
}
})
.collect::<Vec<_>>()
)
}
static PRINT_GRAPHS: bool = false;
fn print_mir_graphviz(name: &str, mir_body: &Body<'_>) {
if PRINT_GRAPHS {
println!(
"digraph {} {{\n{}\n}}",
name,
mir_body
.basic_blocks
.iter_enumerated()
.map(|(bb, data)| {
format!(
" {:?} [label=\"{:?}: {}\"];\n{}",
bb,
bb,
data.terminator().kind.name(),
mir_body
.basic_blocks
.successors(bb)
.map(|successor| { format!(" {:?} -> {:?};", bb, successor) })
.join("\n")
)
})
.join("\n")
);
}
}
fn print_coverage_graphviz(
name: &str,
mir_body: &Body<'_>,
basic_coverage_blocks: &graph::CoverageGraph,
) {
if PRINT_GRAPHS {
println!(
"digraph {} {{\n{}\n}}",
name,
basic_coverage_blocks
.iter_enumerated()
.map(|(bcb, bcb_data)| {
format!(
" {:?} [label=\"{:?}: {}\"];\n{}",
bcb,
bcb,
mir_body[bcb_data.last_bb()].terminator().kind.name(),
basic_coverage_blocks
.successors(bcb)
.map(|successor| { format!(" {:?} -> {:?};", bcb, successor) })
.join("\n")
)
})
.join("\n")
);
}
}
/// Create a mock `Body` with a simple flow.
fn goto_switchint<'a>() -> Body<'a> {
let mut blocks = MockBlocks::new();
let start = blocks.call(None);
let goto = blocks.goto(Some(start));
let switchint = blocks.switchint(Some(goto));
let then_call = blocks.call(None);
let else_call = blocks.call(None);
blocks.set_branch(switchint, 0, then_call);
blocks.set_branch(switchint, 1, else_call);
blocks.return_(Some(then_call));
blocks.return_(Some(else_call));
let mir_body = blocks.to_body();
print_mir_graphviz("mir_goto_switchint", &mir_body);
/* Graphviz character plots created using: `graph-easy --as=boxart`:
┌────────────────┐
│ bb0: Call │
└────────────────┘
│
│
▼
┌────────────────┐
│ bb1: Goto │
└────────────────┘
│
│
▼
┌─────────────┐ ┌────────────────┐
│ bb4: Call │ ◀── │ bb2: SwitchInt │
└─────────────┘ └────────────────┘
│ │
│ │
▼ ▼
┌─────────────┐ ┌────────────────┐
│ bb6: Return │ │ bb3: Call │
└─────────────┘ └────────────────┘
│
│
▼
┌────────────────┐
│ bb5: Return │
└────────────────┘
*/
mir_body
}
macro_rules! assert_successors {
($basic_coverage_blocks:ident, $i:ident, [$($successor:ident),*]) => {
let mut successors = $basic_coverage_blocks.successors[$i].clone();
successors.sort_unstable();
assert_eq!(successors, vec![$($successor),*]);
}
}
#[test]
fn test_covgraph_goto_switchint() {
let mir_body = goto_switchint();
if false {
eprintln!("basic_blocks = {}", debug_basic_blocks(&mir_body));
}
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
print_coverage_graphviz("covgraph_goto_switchint ", &mir_body, &basic_coverage_blocks);
/*
┌──────────────┐ ┌─────────────────┐
│ bcb2: Return │ ◀── │ bcb0: SwitchInt │
└──────────────┘ └─────────────────┘
│
│
▼
┌─────────────────┐
│ bcb1: Return │
└─────────────────┘
*/
assert_eq!(
basic_coverage_blocks.num_nodes(),
3,
"basic_coverage_blocks: {:?}",
basic_coverage_blocks.iter_enumerated().collect::<Vec<_>>()
);
let_bcb!(0);
let_bcb!(1);
let_bcb!(2);
assert_successors!(basic_coverage_blocks, bcb0, [bcb1, bcb2]);
assert_successors!(basic_coverage_blocks, bcb1, []);
assert_successors!(basic_coverage_blocks, bcb2, []);
}
/// Create a mock `Body` with a loop.
fn switchint_then_loop_else_return<'a>() -> Body<'a> {
let mut blocks = MockBlocks::new();
let start = blocks.call(None);
let switchint = blocks.switchint(Some(start));
let then_call = blocks.call(None);
blocks.set_branch(switchint, 0, then_call);
let backedge_goto = blocks.goto(Some(then_call));
blocks.link(backedge_goto, switchint);
let else_return = blocks.return_(None);
blocks.set_branch(switchint, 1, else_return);
let mir_body = blocks.to_body();
print_mir_graphviz("mir_switchint_then_loop_else_return", &mir_body);
/*
┌────────────────┐
│ bb0: Call │
└────────────────┘
│
│
▼
┌─────────────┐ ┌────────────────┐
│ bb4: Return │ ◀── │ bb1: SwitchInt │ ◀┐
└─────────────┘ └────────────────┘ │
│ │
│ │
▼ │
┌────────────────┐ │
│ bb2: Call │ │
└────────────────┘ │
│ │
│ │
▼ │
┌────────────────┐ │
│ bb3: Goto │ ─┘
└────────────────┘
*/
mir_body
}
#[test]
fn test_covgraph_switchint_then_loop_else_return() {
let mir_body = switchint_then_loop_else_return();
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
print_coverage_graphviz(
"covgraph_switchint_then_loop_else_return",
&mir_body,
&basic_coverage_blocks,
);
/*
┌─────────────────┐
│ bcb0: Call │
└─────────────────┘
│
│
▼
┌────────────┐ ┌─────────────────┐
│ bcb3: Goto │ ◀── │ bcb1: SwitchInt │ ◀┐
└────────────┘ └─────────────────┘ │
│ │ │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ bcb2: Return │ │
│ └─────────────────┘ │
│ │
└─────────────────────────────────────┘
*/
assert_eq!(
basic_coverage_blocks.num_nodes(),
4,
"basic_coverage_blocks: {:?}",
basic_coverage_blocks.iter_enumerated().collect::<Vec<_>>()
);
let_bcb!(0);
let_bcb!(1);
let_bcb!(2);
let_bcb!(3);
assert_successors!(basic_coverage_blocks, bcb0, [bcb1]);
assert_successors!(basic_coverage_blocks, bcb1, [bcb2, bcb3]);
assert_successors!(basic_coverage_blocks, bcb2, []);
assert_successors!(basic_coverage_blocks, bcb3, [bcb1]);
}
/// Create a mock `Body` with nested loops.
fn switchint_loop_then_inner_loop_else_break<'a>() -> Body<'a> {
let mut blocks = MockBlocks::new();
let start = blocks.call(None);
let switchint = blocks.switchint(Some(start));
let then_call = blocks.call(None);
blocks.set_branch(switchint, 0, then_call);
let else_return = blocks.return_(None);
blocks.set_branch(switchint, 1, else_return);
let inner_start = blocks.call(Some(then_call));
let inner_switchint = blocks.switchint(Some(inner_start));
let inner_then_call = blocks.call(None);
blocks.set_branch(inner_switchint, 0, inner_then_call);
let inner_backedge_goto = blocks.goto(Some(inner_then_call));
blocks.link(inner_backedge_goto, inner_switchint);
let inner_else_break_goto = blocks.goto(None);
blocks.set_branch(inner_switchint, 1, inner_else_break_goto);
let backedge_goto = blocks.goto(Some(inner_else_break_goto));
blocks.link(backedge_goto, switchint);
let mir_body = blocks.to_body();
print_mir_graphviz("mir_switchint_loop_then_inner_loop_else_break", &mir_body);
/*
┌────────────────┐
│ bb0: Call │
└────────────────┘
│
│
▼
┌─────────────┐ ┌────────────────┐
│ bb3: Return │ ◀── │ bb1: SwitchInt │ ◀─────┐
└─────────────┘ └────────────────┘ │
│ │
│ │
▼ │
┌────────────────┐ │
│ bb2: Call │ │
└────────────────┘ │
│ │
│ │
▼ │
┌────────────────┐ │
│ bb4: Call │ │
└────────────────┘ │
│ │
│ │
▼ │
┌─────────────┐ ┌────────────────┐ │
│ bb8: Goto │ ◀── │ bb5: SwitchInt │ ◀┐ │
└─────────────┘ └────────────────┘ │ │
│ │ │ │
│ │ │ │
▼ ▼ │ │
┌─────────────┐ ┌────────────────┐ │ │
│ bb9: Goto │ ─┐ │ bb6: Call │ │ │
└─────────────┘ │ └────────────────┘ │ │
│ │ │ │
│ │ │ │
│ ▼ │ │
│ ┌────────────────┐ │ │
│ │ bb7: Goto │ ─┘ │
│ └────────────────┘ │
│ │
└───────────────────────────┘
*/
mir_body
}
#[test]
fn test_covgraph_switchint_loop_then_inner_loop_else_break() {
let mir_body = switchint_loop_then_inner_loop_else_break();
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
print_coverage_graphviz(
"covgraph_switchint_loop_then_inner_loop_else_break",
&mir_body,
&basic_coverage_blocks,
);
/*
┌─────────────────┐
│ bcb0: Call │
└─────────────────┘
│
│
▼
┌──────────────┐ ┌─────────────────┐
│ bcb2: Return │ ◀── │ bcb1: SwitchInt │ ◀┐
└──────────────┘ └─────────────────┘ │
│ │
│ │
▼ │
┌─────────────────┐ │
│ bcb3: Call │ │
└─────────────────┘ │
│ │
│ │
▼ │
┌──────────────┐ ┌─────────────────┐ │
│ bcb6: Goto │ ◀── │ bcb4: SwitchInt │ ◀┼────┐
└──────────────┘ └─────────────────┘ │ │
│ │ │ │
│ │ │ │
│ ▼ │ │
│ ┌─────────────────┐ │ │
│ │ bcb5: Goto │ ─┘ │
│ └─────────────────┘ │
│ │
└────────────────────────────────────────────┘
*/
assert_eq!(
basic_coverage_blocks.num_nodes(),
7,
"basic_coverage_blocks: {:?}",
basic_coverage_blocks.iter_enumerated().collect::<Vec<_>>()
);
let_bcb!(0);
let_bcb!(1);
let_bcb!(2);
let_bcb!(3);
let_bcb!(4);
let_bcb!(5);
let_bcb!(6);
assert_successors!(basic_coverage_blocks, bcb0, [bcb1]);
assert_successors!(basic_coverage_blocks, bcb1, [bcb2, bcb3]);
assert_successors!(basic_coverage_blocks, bcb2, []);
assert_successors!(basic_coverage_blocks, bcb3, [bcb4]);
assert_successors!(basic_coverage_blocks, bcb4, [bcb5, bcb6]);
assert_successors!(basic_coverage_blocks, bcb5, [bcb1]);
assert_successors!(basic_coverage_blocks, bcb6, [bcb4]);
}
#[test]
fn test_find_loop_backedges_none() {
let mir_body = goto_switchint();
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
if false {
eprintln!(
"basic_coverage_blocks = {:?}",
basic_coverage_blocks.iter_enumerated().collect::<Vec<_>>()
);
eprintln!("successors = {:?}", basic_coverage_blocks.successors);
}
let backedges = graph::find_loop_backedges(&basic_coverage_blocks);
assert_eq!(
backedges.iter_enumerated().map(|(_bcb, backedges)| backedges.len()).sum::<usize>(),
0,
"backedges: {:?}",
backedges
);
}
#[test]
fn test_find_loop_backedges_one() {
let mir_body = switchint_then_loop_else_return();
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
let backedges = graph::find_loop_backedges(&basic_coverage_blocks);
assert_eq!(
backedges.iter_enumerated().map(|(_bcb, backedges)| backedges.len()).sum::<usize>(),
1,
"backedges: {:?}",
backedges
);
let_bcb!(1);
let_bcb!(3);
assert_eq!(backedges[bcb1], vec![bcb3]);
}
#[test]
fn test_find_loop_backedges_two() {
let mir_body = switchint_loop_then_inner_loop_else_break();
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
let backedges = graph::find_loop_backedges(&basic_coverage_blocks);
assert_eq!(
backedges.iter_enumerated().map(|(_bcb, backedges)| backedges.len()).sum::<usize>(),
2,
"backedges: {:?}",
backedges
);
let_bcb!(1);
let_bcb!(4);
let_bcb!(5);
let_bcb!(6);
assert_eq!(backedges[bcb1], vec![bcb5]);
assert_eq!(backedges[bcb4], vec![bcb6]);
}
#[test]
fn test_traverse_coverage_with_loops() {
let mir_body = switchint_loop_then_inner_loop_else_break();
let basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
let mut traversed_in_order = Vec::new();
let mut traversal = graph::TraverseCoverageGraphWithLoops::new(&basic_coverage_blocks);
while let Some(bcb) = traversal.next() {
traversed_in_order.push(bcb);
}
let_bcb!(6);
// bcb0 is visited first. Then bcb1 starts the first loop, and all remaining nodes, *except*
// bcb6 are inside the first loop.
assert_eq!(
*traversed_in_order.last().expect("should have elements"),
bcb6,
"bcb6 should not be visited until all nodes inside the first loop have been visited"
);
}
#[test]
fn test_make_bcb_counters() {
rustc_span::create_default_session_globals_then(|| {
let mir_body = goto_switchint();
let mut basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
// Historically this test would use `spans` internals to set up fake
// coverage spans for BCBs 1 and 2. Now we skip that step and just tell
// BCB counter construction that those BCBs have spans.
let bcb_has_coverage_spans = |bcb: BasicCoverageBlock| (1..=2).contains(&bcb.as_usize());
let mut coverage_counters = counters::CoverageCounters::new(&basic_coverage_blocks);
coverage_counters
.make_bcb_counters(&mut basic_coverage_blocks, bcb_has_coverage_spans)
.expect("should be Ok");
assert_eq!(coverage_counters.num_expressions(), 0);
let_bcb!(1);
assert_eq!(
0, // bcb1 has a `Counter` with id = 0
match coverage_counters.bcb_counter(bcb1).expect("should have a counter") {
counters::BcbCounter::Counter { id, .. } => id,
_ => panic!("expected a Counter"),
}
.as_u32()
);
let_bcb!(2);
assert_eq!(
1, // bcb2 has a `Counter` with id = 1
match coverage_counters.bcb_counter(bcb2).expect("should have a counter") {
counters::BcbCounter::Counter { id, .. } => id,
_ => panic!("expected a Counter"),
}
.as_u32()
);
});
}