rucc_codegen/coverage.rs
1//! Which IR opcodes have somewhere to go, and which do not.
2//!
3//! Design: `spec/10-backend.md` section 10.2, under **Coverage**.
4//!
5//! Every opcode has to be lowered by something or be a hole somebody wrote down. Without this the
6//! way a hole is found is that somebody compiles a program containing one and the selector reports
7//! that it cannot lower an instruction, which is a fine diagnostic and a bad discovery mechanism:
8//! it turns a gap in the rule set into a user's problem rather than a failing build.
9//!
10//! # The three answers
11//!
12//! An opcode is lowered by a rule, or somewhere a rule cannot reach, or nowhere.
13//!
14//! The first is the ordinary answer and the one this can check by itself. [`crate::term`] says
15//! every name a rule could be written at, the table says every name one is written at, and an
16//! opcode is covered when each of its names is in both. That is what makes this a check about
17//! widths rather than about opcodes: an `add` with a rule at four widths and no rule at the fifth
18//! is not covered, and would be reported here as the missing name rather than as a covered opcode.
19//!
20//! The second is a lowering, which is not a gap. `spec/10-backend.md` names five of them and there
21//! are more now, and they are all the same kind of thing: an opcode whose lowering depends on
22//! something no pattern can see. Where a call's arguments go depends on the signature, where a
23//! local lives depends on the frame, an unconditional jump is an edge and edges live on the block,
24//! and a `memcpy` is a run of moves whose length is a constant the pattern would have to count. A
25//! rule matches one term and can say none of that. Which opcodes those are is
26//! [`crate::capability::lowering`] and is not written down here, because it was written down here
27//! and in the lowering group both and the two could disagree.
28//!
29//! The third is [`GAPS`], which is the number `spec/15-testing.md` section 15.8 says we keep. Each
30//! entry names why it is there and the issue that closes it, so that an opcode nobody has written a
31//! rule for is a decision somebody wrote down rather than a surprise.
32//!
33//! [`WIDTHS`] and [`NAMES`] are the same third answer said about something smaller than an opcode.
34//! A width on [`WIDTHS`] has no names at all, so no opcode is missing a rule at it, and a name on
35//! [`NAMES`] is one width of an opcode that lowers at its other widths. Both carry the issue that
36//! closes them for the same reason [`GAPS`] does.
37//!
38//! # What makes the lists honest
39//!
40//! An entry that stops being true fails. An opcode on either list that a rule starts covering is a
41//! stale entry and the tests below say so by name, which is the same rule the exclusion lists in
42//! the compatibility harness are kept under: a list nothing checks is a list that only grows.
43//!
44//! The direction this cannot check is an opcode moving from [`GAPS`] to a hand written lowering
45//! without [`crate::capability::HAND`] following it, because where an opcode is lowered by name is
46//! a `match` arm and there is nothing to ask about a `match` arm from here. What that costs is one
47//! line of a list going out of date; what it does not cost is a gap going unnoticed, since the
48//! opcode is still on a list and still counted.
49//!
50//! # The other question
51//!
52//! All of the above is about the rule set as it is written. [`Fired`] is about the rule set as it
53//! is used: which rules a compilation actually reached. A rule nothing reaches is proved and dead
54//! weight, or it is a construct the corpus does not contain and somebody should know which. The
55//! selector marks a rule as it fires it, the driver writes the marks out under
56//! `-Zrule-coverage=FILE`, and the harness in `tamnd/rucc-compat` unions those files over a corpus,
57//! which is what turns coverage of the rule set into a number. `spec/20-execution-testing.md`
58//! section 20.9 is the design and `tamnd/rucc#261` is the work.
59
60use core::fmt;
61use core::fmt::Write as _;
62
63use rucc_ir::Opcode;
64use rucc_target::Arch;
65
66use crate::capability::{self, pattern_heads};
67use crate::select::Table;
68use crate::term;
69
70/// An opcode nothing lowers, why it is here, and the issue that closes it.
71///
72/// This is the count `spec/15-testing.md` section 15.8 asks for. It is not zero yet and the
73/// spec says it should be, which is the honest reading of where the back end is: every one of
74/// these is a feature nobody has written, and all of them but one are opcodes the front end
75/// cannot produce either, so a program that reaches one of these is a program that reaches an
76/// unimplemented builtin first. The one is the remainder of two floats, which a program writes
77/// with an operator and which is a call to the maths library rather than an instruction.
78pub static GAPS: &[(Opcode, &str, &str)] = &[
79 (Opcode::Splat, "a vector, and no rule is written about a lane count", "tamnd/rucc#200"),
80 (
81 Opcode::TargetIntrinsic,
82 "the same, since what needs one is a vector builtin",
83 "tamnd/rucc#200",
84 ),
85 (
86 Opcode::FRem,
87 "a call to `fmod`, so a link line question as much as a lowering one",
88 "tamnd/rucc#226",
89 ),
90 (
91 Opcode::Fma,
92 "a call or one instruction, depending on what the machine is told it has",
93 "tamnd/rucc#226",
94 ),
95 (Opcode::Bitreverse, "a node nothing writes and nothing lowers", "tamnd/rucc#363"),
96 (Opcode::TailCall, "a terminator nothing writes and nothing lowers", "tamnd/rucc#365"),
97 // Memory safety. These are a gap in a different sense from the rest: nothing emits one yet
98 // either, since the passes that would are milestones S5 and after, so there is no program the
99 // back end can be handed that reaches one. The ones the safety pass lowers are on `HAND`,
100 // and the five that make a capability all left this list without anything emitting them, which
101 // is the whole of tamnd/rucc#1085's lowering half: each has a lowering waiting for the pass that
102 // will write one, because a capability had to be a value the back end could hold before any of
103 // them could be written down at all. The two region markers left the same way and for a
104 // different reason, which is that what they cost is a count rather than a lowering.
105 // What is left is the plane writes, which the runtime does for itself today because the only
106 // ranges anything asks about are the ones its own allocator handed out. A stack object needs
107 // these, since nothing in the runtime sees a frame being set up or torn down.
108 (Opcode::MetaBegin, "a write over a range of the lifetime plane", "tamnd/rucc#856"),
109 (
110 Opcode::MetaEnd,
111 "the same write, with the version bumped past every capability",
112 "tamnd/rucc#856",
113 ),
114 (
115 Opcode::MetaTransfer,
116 "the same, and the state a range is in while a device owns it, which is S2's",
117 "tamnd/rucc#856",
118 ),
119];
120
121/// A width no rule is written at, why, and the issue that closes it.
122///
123/// The other half of coverage, and the half an opcode list cannot say. An opcode is covered when
124/// every name it has is a name a rule is written at, and a width with no name has no names to
125/// check: an `add` of two `__int128`s is not a missing rule for `add`, it is a width the rule
126/// language cannot spell. So the widths are written down here for the same reason the opcodes are
127/// written down above.
128pub static WIDTHS: &[(&str, &str, &str)] = &[
129 (
130 "one bit",
131 "everything but and, or, xor, a constant, and the widening out of one",
132 "tamnd/rucc#352",
133 ),
134 (
135 "a hundred and twenty eight bits",
136 "split into two halves before selection, except a division",
137 "tamnd/rucc#351",
138 ),
139 (
140 "eighty bits",
141 "a long double is on the x87 stack and no rule is about that stack",
142 "tamnd/rucc#326",
143 ),
144 (
145 "a hundred and twenty eight bits of float",
146 "turned into a call before selection, except a conditional move and the conversions \
147 against an integer that wide",
148 "tamnd/rucc#1064",
149 ),
150 (
151 "a vector of any lane count",
152 "a rule at a width says nothing about how many lanes",
153 "tamnd/rucc#200",
154 ),
155];
156
157/// A name a rule could be written at and deliberately is not, why, and the issue that puts it
158/// back.
159///
160/// The third list, and the one that is about a name rather than about an opcode or a width. An
161/// opcode on [`GAPS`] has no lowering at any width and a width on [`WIDTHS`] has no names at all,
162/// and neither of those can say that `add` is lowered at four widths and left alone at two.
163///
164/// This list used to be all of the narrow arithmetic. C promotes the operands of an arithmetic
165/// operator to `int` before the operator is applied, so `char a, b; a + b` is an `int` addition of
166/// two sign extended chars and there is no C program that asks the back end to add two bytes.
167/// Rules were written at those names anyway, ahead of the pass that would reach them, and they sat
168/// proved and never selected: `tamnd/rucc#261` measured that and `tamnd/rucc#368` took them out.
169/// They are back, because the width narrowing pass in `tamnd/rucc#375` is that caller and it
170/// writes a byte add out of the truncation the assignment back to a `char` already was. The last
171/// to come back were the divides, which narrow when the operands are zero extensions and, for
172/// sign extensions, when the ranges rule out the most negative value over minus one.
173///
174/// So the list is empty, and it stays here for the next name somebody decides to leave out, since
175/// the report and the capability table both read it.
176pub static NAMES: &[(&str, &str, &str)] = &[];
177
178/// What a target's rules cover, and what they do not.
179#[derive(Debug)]
180pub struct Report {
181 /// The rule file this is about, so that anything said about it names a file to open.
182 pub source: &'static str,
183 /// How many opcodes the IR has.
184 pub opcodes: usize,
185 /// The opcodes every name of which a rule is written at.
186 pub by_rule: Vec<Opcode>,
187 /// How many names those are, which is one per opcode and width.
188 pub names: usize,
189 /// A name a rule could be written at and none is, which is what a missing rule looks like.
190 pub uncovered: Vec<(Opcode, &'static str)>,
191 /// A name on [`NAMES`], which is a missing rule somebody decided to be missing.
192 pub deferred: Vec<(Opcode, &'static str)>,
193 /// A name a rule is written at that nothing can ever be called, which is a dead rule.
194 pub unreachable: Vec<&'static str>,
195 /// The opcodes lowered somewhere a rule cannot reach.
196 pub elsewhere: Vec<Opcode>,
197 /// The opcodes nothing lowers.
198 pub gaps: Vec<Opcode>,
199 /// The opcodes on none of the three lists, which is what a new opcode is until somebody says
200 /// where it goes.
201 pub unaccounted: Vec<Opcode>,
202}
203
204impl fmt::Display for Report {
205 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206 write!(
207 f,
208 "rucc-codegen: {} lowers {} of the {} IR opcodes by rule at {} names, {} are lowered \
209 where no rule reaches, {} have no lowering yet and {} names are left for later",
210 self.source,
211 self.by_rule.len(),
212 self.opcodes,
213 self.names,
214 self.elsewhere.len(),
215 self.gaps.len(),
216 self.deferred.len()
217 )
218 }
219}
220
221/// What a table covers.
222///
223/// Nothing is executed and nothing is compiled. The rule set and the naming of instructions are
224/// both data, and the answer is a comparison of two lists.
225#[must_use]
226pub fn report(table: &Table) -> Report {
227 let named = term::heads();
228 let patterns = pattern_heads(table);
229
230 let mut by_rule = Vec::new();
231 let mut uncovered = Vec::new();
232 let mut deferred = Vec::new();
233 for &(opcode, name) in &named {
234 if patterns.contains(&name) {
235 by_rule.push(opcode);
236 } else if NAMES.iter().any(|&(deliberate, ..)| deliberate == name) {
237 deferred.push((opcode, name));
238 } else {
239 uncovered.push((opcode, name));
240 }
241 }
242 // An opcode is covered when every name it has is covered, so one missing width takes the
243 // whole opcode off the list however many of its other widths are there. A name on `NAMES` does
244 // not take it off, because the opcode is lowered and the entry says which widths were left for
245 // later and why: that is a narrower claim than the opcode having nowhere to go, and putting it
246 // on `GAPS` instead would say the wrong thing about an `add` that lowers perfectly well at
247 // four widths.
248 for &(opcode, _) in &uncovered {
249 by_rule.retain(|&covered| covered != opcode);
250 }
251 by_rule.sort_unstable();
252 by_rule.dedup();
253
254 let names = named.len() - uncovered.len() - deferred.len();
255 let unreachable: Vec<&'static str> = patterns
256 .iter()
257 .filter(|head| !named.iter().any(|(_, name)| name == *head))
258 .copied()
259 .collect();
260
261 let elsewhere: Vec<Opcode> =
262 Opcode::all().filter(|&opcode| capability::lowering(opcode).is_some()).collect();
263 let gaps: Vec<Opcode> = GAPS.iter().map(|&(opcode, ..)| opcode).collect();
264 let unaccounted: Vec<Opcode> = Opcode::all()
265 .filter(|opcode| {
266 !by_rule.contains(opcode)
267 && !elsewhere.contains(opcode)
268 && !gaps.contains(opcode)
269 && !capability::LIBCALLS.iter().any(|&(at, ..)| at == *opcode)
270 })
271 .collect();
272
273 Report {
274 source: table.source,
275 opcodes: Opcode::all().count(),
276 by_rule,
277 names,
278 uncovered,
279 deferred,
280 unreachable,
281 elsewhere,
282 gaps,
283 unaccounted,
284 }
285}
286
287/// The rules a target lowers by, or `None` where no back end in this crate covers it.
288///
289/// The same question [`crate::pipeline::Machine::for_target`] answers about the rest of a machine,
290/// and it is here as well because a caller that wants to write down what a run covered has a
291/// target and no machine. An architecture that gets a rule file at M6 gets an arm here at the same
292/// time, and until then it has no rules to report coverage of rather than an empty set of them.
293#[must_use]
294pub fn table(arch: Arch) -> Option<&'static Table> {
295 match arch {
296 Arch::X86_64 => Some(&crate::select::x86_64::TABLE),
297 Arch::Aarch64 | Arch::Riscv64 => None,
298 }
299}
300
301/// Which rules fired, over one function or over a whole compilation.
302///
303/// A bit per rule and nothing else. This is on the path of every instruction selected, so what it
304/// costs is paid by every compilation whether or not anybody asked for the number, and the cheapest
305/// thing that answers the question is a flag per rule set once.
306///
307/// The index of a rule is how this is kept and not how it is written down. An index moves the
308/// moment a rule is added above it, so [`Fired::listing`] names the rule file and the line instead:
309/// a line is a place somebody can open, and a report written by one build can still be read against
310/// a rule file that has grown since.
311#[derive(Debug, Clone, Default, PartialEq, Eq)]
312pub struct Fired {
313 /// One entry per rule, true once that rule has fired. It grows to fit the highest index
314 /// marked rather than being sized from a table, so nothing here has to be told which target
315 /// is being compiled for.
316 seen: Vec<bool>,
317}
318
319impl Fired {
320 /// Nothing has fired yet.
321 #[must_use]
322 pub const fn new() -> Fired {
323 Fired { seen: Vec::new() }
324 }
325
326 /// Records that the rule at this index fired.
327 pub fn mark(&mut self, rule: usize) {
328 if self.seen.len() <= rule {
329 self.seen.resize(rule + 1, false);
330 }
331 self.seen[rule] = true;
332 }
333
334 /// Whether the rule at this index fired.
335 #[must_use]
336 pub fn has(&self, rule: usize) -> bool {
337 self.seen.get(rule).copied().unwrap_or(false)
338 }
339
340 /// How many rules fired.
341 #[must_use]
342 pub fn count(&self) -> usize {
343 self.seen.iter().filter(|fired| **fired).count()
344 }
345
346 /// Takes in everything another one recorded.
347 ///
348 /// One compilation is many functions and one command line is many files, and the question is
349 /// about all of them together. Merging rather than writing a file per function is also what
350 /// keeps the answer the same however the work was scheduled.
351 pub fn merge(&mut self, other: &Fired) {
352 if self.seen.len() < other.seen.len() {
353 self.seen.resize(other.seen.len(), false);
354 }
355 for (mine, theirs) in self.seen.iter_mut().zip(&other.seen) {
356 *mine |= *theirs;
357 }
358 }
359
360 /// What `-Zrule-coverage=FILE` writes.
361 ///
362 /// One line per rule in the table, in the order the rule file writes them, each saying whether
363 /// the rule fired and naming the file and line it is written at. Every rule is listed rather
364 /// than only the ones that fired, so that one of these files says what the whole rule set was
365 /// as well as what this compilation reached: a reader unioning them over a corpus needs both
366 /// and would otherwise have to parse the rule file to get the second.
367 ///
368 /// The first line is a comment holding the count, which is the number a person wants and the
369 /// one thing here that is not worth making them add up.
370 #[must_use]
371 pub fn listing(&self, table: &Table) -> String {
372 let fired = table.rules.iter().enumerate().filter(|(index, _)| self.has(*index)).count();
373 let mut out = format!(
374 "# rucc rule coverage: {fired} of {} rules in {} fired\n",
375 table.rules.len(),
376 table.source
377 );
378 for (index, rule) in table.rules.iter().enumerate() {
379 let word = if self.has(index) { "fired" } else { "unused" };
380 let _ = writeln!(out, "{word} {}:{} {}", table.source, rule.line, rule.pattern);
381 }
382 out
383 }
384}
385
386#[cfg(test)]
387mod tests {
388 use super::*;
389 use crate::select::x86_64::TABLE;
390
391 /// The claim the whole module is for, in the direction that matters: a name an instruction
392 /// can be called by is a name a rule is written at. This is the width check as much as the
393 /// opcode check, since a name is an opcode and a width together.
394 #[test]
395 fn every_name_an_instruction_can_have_is_one_a_rule_is_written_at() {
396 let report = report(&TABLE);
397 assert!(
398 report.uncovered.is_empty(),
399 "nothing in {} lowers these, and each is an opcode at a width the rule language can \
400 spell: {:?}",
401 report.source,
402 report.uncovered
403 );
404 }
405
406 /// And the other direction, which costs nothing to ask and finds a rule that can never fire.
407 /// A pattern head no instruction is ever called by is a rule written against a name that was
408 /// renamed or misspelled, and it would sit there proved and unreachable.
409 #[test]
410 fn every_name_a_rule_is_written_at_is_one_an_instruction_can_have() {
411 let report = report(&TABLE);
412 assert!(
413 report.unreachable.is_empty(),
414 "{} has rules for these and no instruction is ever called one: {:?}",
415 report.source,
416 report.unreachable
417 );
418 }
419
420 /// Every opcode is one of the three things, so a new opcode in the IR fails this until
421 /// somebody says where it goes. That is the whole point: the answer for a new opcode should
422 /// be written down when it is added rather than discovered by a user compiling a program.
423 #[test]
424 fn every_opcode_is_lowered_or_is_a_gap_somebody_wrote_down() {
425 let report = report(&TABLE);
426 assert!(
427 report.unaccounted.is_empty(),
428 "no rule lowers these, nothing rewrites them before selection, no runtime function \
429 stands for them and `GAPS` does not say why: {:?}",
430 report.unaccounted
431 );
432 }
433
434 /// An entry that starts being covered fails, which is the rule every list in this project is
435 /// kept under. An opcode a rule now lowers is one that should be off both lists, and a list
436 /// that keeps claiming otherwise is a list nobody can read.
437 #[test]
438 fn an_entry_a_rule_now_covers_is_a_stale_entry() {
439 let report = report(&TABLE);
440 for &(opcode, where_) in capability::HAND {
441 assert!(
442 !report.by_rule.contains(&opcode),
443 "`{}` is lowered by a rule now, so the `HAND` entry saying it is lowered by \
444 {where_} is stale",
445 opcode.name()
446 );
447 }
448 for &(opcode, why, issue) in GAPS {
449 assert!(
450 !report.by_rule.contains(&opcode),
451 "`{}` is lowered by a rule now, so the `GAPS` entry saying it is {why} is stale \
452 and {issue} may be closed",
453 opcode.name()
454 );
455 assert!(
456 !report.elsewhere.contains(&opcode),
457 "`{}` is on both lists, so it is both lowered and not lowered",
458 opcode.name()
459 );
460 }
461 }
462
463 /// The same staleness rule one list down. A name a rule is written at is a name that is not
464 /// left for later, and an entry claiming otherwise is one that should have gone when the rule
465 /// arrived. The other direction is checked too: a name no instruction can ever have is a
466 /// misspelling, and it would sit here excusing nothing.
467 #[test]
468 fn a_name_a_rule_is_written_at_is_not_a_name_left_for_later() {
469 let heads = pattern_heads(&TABLE);
470 let named = term::heads();
471 for &(name, why, issue) in NAMES {
472 assert!(
473 !heads.contains(&name),
474 "`{name}` is lowered by a rule now, so the `NAMES` entry saying it is {why} is \
475 stale and {issue} may be closer than it says"
476 );
477 assert!(
478 named.iter().any(|&(_, head)| head == name),
479 "`{name}` is not a name any instruction can have, so the `NAMES` entry excuses \
480 nothing"
481 );
482 }
483 let report = report(&TABLE);
484 assert_eq!(report.deferred.len(), NAMES.len(), "{:?}", report.deferred);
485 }
486
487 /// Every gap names an issue, since a gap with no issue behind it is a gap nobody has decided
488 /// anything about, which is the thing this module exists to stop.
489 #[test]
490 fn every_gap_names_the_issue_that_closes_it() {
491 let issues = GAPS
492 .iter()
493 .map(|&(_, _, issue)| issue)
494 .chain(WIDTHS.iter().map(|&(_, _, issue)| issue))
495 .chain(NAMES.iter().map(|&(_, _, issue)| issue));
496 for issue in issues {
497 let number = issue
498 .strip_prefix("tamnd/rucc#")
499 .unwrap_or_else(|| panic!("{issue} is not an issue in this project's tracker"));
500 assert!(number.parse::<u32>().is_ok(), "{issue} does not name an issue number");
501 }
502 }
503
504 /// The count, which `spec/15-testing.md` section 15.8 says we keep about ourselves. CI runs
505 /// this test with the output shown, so the number lands in a log next to the rule proof
506 /// rather than in a file somebody has to go and read.
507 #[test]
508 fn the_count_is_reported() {
509 let report = report(&TABLE);
510 println!("{report}");
511 for &(opcode, why, issue) in GAPS {
512 println!("rucc-codegen: no lowering for `{}`, which is {why}: {issue}", opcode.name());
513 }
514 for &(width, why, issue) in WIDTHS {
515 println!("rucc-codegen: no rule at {width}, which is {why}: {issue}");
516 }
517 for &(name, why, issue) in NAMES {
518 println!("rucc-codegen: no rule at `{name}`, which is {why}: {issue}");
519 }
520 assert_eq!(report.gaps.len(), GAPS.len());
521 }
522
523 /// What the root of the trie is, which is the assumption [`pattern_heads`] rests on. If the
524 /// rule compiler ever built the trie some other way this would say so, rather than the
525 /// coverage numbers quietly becoming a report about an empty list.
526 #[test]
527 fn the_root_of_the_trie_is_the_head_of_every_pattern() {
528 let heads = pattern_heads(&TABLE);
529 assert!(!heads.is_empty(), "the table has rules and the root of the trie tests nothing");
530 for rule in TABLE.rules {
531 let head = rule
532 .pattern
533 .strip_prefix('(')
534 .and_then(|rest| rest.split([' ', ')']).next())
535 .expect("a pattern is an application");
536 assert!(
537 heads.contains(&head),
538 "line {}: {} is a pattern whose head the root of the trie does not test",
539 rule.line,
540 rule.pattern
541 );
542 }
543 }
544
545 /// The one target with a rule file, and the two that get one at M6. A machine that can be
546 /// compiled for has rules to report the coverage of, and one that cannot has none rather than
547 /// an empty set of them, which are different answers and would read the same as a number.
548 #[test]
549 fn a_target_with_a_back_end_is_a_target_with_a_rule_set() {
550 let x86 = table(Arch::X86_64).expect("x86-64 is what this crate lowers for");
551 assert_eq!(x86.source, TABLE.source);
552 assert!(!x86.rules.is_empty());
553 assert!(table(Arch::Aarch64).is_none(), "there is no aarch64 rule file yet");
554 assert!(table(Arch::Riscv64).is_none(), "there is no riscv64 rule file yet");
555 }
556
557 /// What a rule is called outside this process. The index is not it: a rule added at the top of
558 /// the file moves every index below it, and a report from last week would then be a report
559 /// about the wrong rules. The file and the line do not move that way and are somewhere to look.
560 #[test]
561 fn a_rule_is_written_down_as_the_place_it_is_written_at() {
562 let mut fired = Fired::new();
563 fired.mark(0);
564 let listing = fired.listing(&TABLE);
565 let first =
566 format!("fired {}:{} {}", TABLE.source, TABLE.rules[0].line, TABLE.rules[0].pattern);
567 assert!(listing.contains(&first), "{listing}");
568 assert!(listing.lines().next().is_some_and(|line| line.starts_with('#')), "{listing}");
569 }
570
571 /// Every rule is listed and not only the ones that fired, which is what lets one of these files
572 /// be read on its own. A reader that only got the rules that fired would have to parse the rule
573 /// file to find out what the rest of them were.
574 #[test]
575 fn one_file_says_what_the_whole_rule_set_is() {
576 let listing = Fired::new().listing(&TABLE);
577 let lines: Vec<&str> = listing.lines().collect();
578 assert_eq!(lines.len(), TABLE.rules.len() + 1, "one line per rule and one for the count");
579 assert_eq!(
580 lines.iter().filter(|line| line.starts_with("unused ")).count(),
581 TABLE.rules.len()
582 );
583 assert!(lines[0].contains(&format!("0 of {} rules", TABLE.rules.len())), "{}", lines[0]);
584 }
585
586 /// A compilation is many functions and a command line is many files, and the question is about
587 /// all of them at once. Merging is also what keeps the answer the same however the work was
588 /// scheduled, which is the rule `spec/03-architecture.md` section 3.7 holds everything to.
589 #[test]
590 fn what_two_runs_reached_is_what_either_of_them_reached() {
591 let mut one = Fired::new();
592 one.mark(3);
593 one.mark(3);
594 assert_eq!(one.count(), 1, "a rule that fires twice is one rule");
595 let mut two = Fired::new();
596 two.mark(0);
597 two.mark(9);
598 one.merge(&two);
599 assert_eq!(one.count(), 3);
600 assert!(one.has(0) && one.has(3) && one.has(9));
601 assert!(!one.has(1));
602
603 // The merge is symmetric, since neither order of two files is the right one.
604 let mut back = Fired::new();
605 back.mark(0);
606 back.mark(9);
607 let mut three = Fired::new();
608 three.mark(3);
609 back.merge(&three);
610 assert_eq!(back, one);
611 }
612}