1use serde::Serialize;
10
11#[derive(Clone, Copy, PartialEq, Eq, Debug)]
13pub enum Rel {
14 Less,
15 Equal,
16 Greater,
17 Unordered,
18}
19
20#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
21#[serde(rename_all = "snake_case")]
22pub enum CmpOp {
23 Lt,
24 Le,
25 Gt,
26 Ge,
27 Eq,
28 Ne,
29}
30
31impl CmpOp {
32 pub const ALL: [CmpOp; 6] = [
33 CmpOp::Lt,
34 CmpOp::Le,
35 CmpOp::Gt,
36 CmpOp::Ge,
37 CmpOp::Eq,
38 CmpOp::Ne,
39 ];
40
41 pub fn code(self) -> u8 {
42 match self {
43 CmpOp::Lt => 0,
44 CmpOp::Le => 1,
45 CmpOp::Gt => 2,
46 CmpOp::Ge => 3,
47 CmpOp::Eq => 4,
48 CmpOp::Ne => 5,
49 }
50 }
51
52 pub fn from_code(code: u8) -> Option<CmpOp> {
53 CmpOp::ALL.into_iter().find(|op| op.code() == code)
54 }
55
56 pub fn symbol(self) -> &'static str {
57 match self {
58 CmpOp::Lt => "<",
59 CmpOp::Le => "<=",
60 CmpOp::Gt => ">",
61 CmpOp::Ge => ">=",
62 CmpOp::Eq => "==",
63 CmpOp::Ne => "!=",
64 }
65 }
66
67 pub fn is_ordering(self) -> bool {
68 !matches!(self, CmpOp::Eq | CmpOp::Ne)
69 }
70
71 pub fn eval_rel(self, rel: Rel) -> bool {
72 match (self, rel) {
73 (CmpOp::Ne, Rel::Unordered) => true,
74 (_, Rel::Unordered) => false,
75 (CmpOp::Lt, r) => r == Rel::Less,
76 (CmpOp::Le, r) => r != Rel::Greater,
77 (CmpOp::Gt, r) => r == Rel::Greater,
78 (CmpOp::Ge, r) => r != Rel::Less,
79 (CmpOp::Eq, r) => r == Rel::Equal,
80 (CmpOp::Ne, r) => r != Rel::Equal,
81 }
82 }
83
84 pub fn eval_d(self, d: i128) -> bool {
86 self.eval_rel(match d {
87 d if d < 0 => Rel::Less,
88 0 => Rel::Equal,
89 _ => Rel::Greater,
90 })
91 }
92
93 pub fn mistaken_for(self) -> &'static [CmpOp] {
95 match self {
96 CmpOp::Lt => &[CmpOp::Eq, CmpOp::Gt],
97 CmpOp::Gt => &[CmpOp::Eq, CmpOp::Lt],
98 _ => &[],
99 }
100 }
101
102 pub fn negated(self) -> CmpOp {
103 match self {
104 CmpOp::Lt => CmpOp::Ge,
105 CmpOp::Le => CmpOp::Gt,
106 CmpOp::Gt => CmpOp::Le,
107 CmpOp::Ge => CmpOp::Lt,
108 CmpOp::Eq => CmpOp::Ne,
109 CmpOp::Ne => CmpOp::Eq,
110 }
111 }
112
113 pub fn flip_eq(self) -> Option<CmpOp> {
115 match self {
116 CmpOp::Lt => Some(CmpOp::Le),
117 CmpOp::Le => Some(CmpOp::Lt),
118 CmpOp::Gt => Some(CmpOp::Ge),
119 CmpOp::Ge => Some(CmpOp::Gt),
120 CmpOp::Eq | CmpOp::Ne => None,
121 }
122 }
123
124 fn threshold(self) -> Option<(bool, i128)> {
127 match self {
128 CmpOp::Ge => Some((true, 0)),
129 CmpOp::Gt => Some((true, 1)),
130 CmpOp::Le => Some((false, 0)),
131 CmpOp::Lt => Some((false, -1)),
132 CmpOp::Eq | CmpOp::Ne => None,
133 }
134 }
135}
136
137#[derive(Clone, Copy, PartialEq, Debug, Default)]
140pub struct Obs {
141 pub lt: bool,
142 pub eq: bool,
143 pub gt: bool,
144 pub un: bool,
145 pub unvalued: bool,
147 pub max_neg: Option<i128>,
149 pub min_pos: Option<i128>,
151 pub unfloat: bool,
153 pub f_max_neg: Option<f64>,
155 pub f_min_pos: Option<f64>,
156}
157
158impl Obs {
159 pub fn reached(&self) -> bool {
160 self.lt || self.eq || self.gt || self.un
161 }
162
163 pub fn valued(&self) -> bool {
164 self.reached() && !self.unvalued
165 }
166
167 pub fn merge(&mut self, other: &Obs) {
168 self.lt |= other.lt;
169 self.eq |= other.eq;
170 self.gt |= other.gt;
171 self.un |= other.un;
172 self.unvalued |= other.unvalued;
173 self.max_neg = match (self.max_neg, other.max_neg) {
174 (Some(a), Some(b)) => Some(a.max(b)),
175 (a, b) => a.or(b),
176 };
177 self.min_pos = match (self.min_pos, other.min_pos) {
178 (Some(a), Some(b)) => Some(a.min(b)),
179 (a, b) => a.or(b),
180 };
181 self.unfloat |= other.unfloat;
182 self.f_max_neg = match (self.f_max_neg, other.f_max_neg) {
183 (Some(a), Some(b)) => Some(a.max(b)),
184 (a, b) => a.or(b),
185 };
186 self.f_min_pos = match (self.f_min_pos, other.f_min_pos) {
187 (Some(a), Some(b)) => Some(a.min(b)),
188 (a, b) => a.or(b),
189 };
190 }
191
192 pub fn of_d(d: i128) -> Obs {
194 Obs {
195 lt: d < 0,
196 eq: d == 0,
197 gt: d > 0,
198 max_neg: (d < 0).then_some(d),
199 min_pos: (d > 0).then_some(d),
200 unfloat: true,
201 ..Obs::default()
202 }
203 }
204
205 pub fn of_rel(rel: Rel) -> Obs {
207 Obs {
208 lt: rel == Rel::Less,
209 eq: rel == Rel::Equal,
210 gt: rel == Rel::Greater,
211 un: rel == Rel::Unordered,
212 unvalued: true,
213 unfloat: true,
214 ..Obs::default()
215 }
216 }
217
218 pub fn of_f(d: f64) -> Obs {
220 Obs {
221 lt: d < 0.0,
222 eq: d == 0.0,
223 gt: d > 0.0,
224 unvalued: true,
225 f_max_neg: (d < 0.0).then_some(d),
226 f_min_pos: (d > 0.0).then_some(d),
227 ..Obs::default()
228 }
229 }
230
231 pub fn float_gap(&self) -> Option<(f64, f64)> {
234 if self.unfloat || self.un {
236 return None;
237 }
238 Some((self.f_max_neg?, self.f_min_pos?))
239 }
240
241 fn rels(&self) -> impl Iterator<Item = Rel> {
242 [
243 (self.lt, Rel::Less),
244 (self.eq, Rel::Equal),
245 (self.gt, Rel::Greater),
246 (self.un, Rel::Unordered),
247 ]
248 .into_iter()
249 .filter_map(|(seen, rel)| seen.then_some(rel))
250 }
251
252 fn saw(&self, d: i128) -> bool {
254 match d {
255 0 => self.eq,
256 -1 => self.max_neg == Some(-1),
257 1 => self.min_pos == Some(1),
258 _ => unreachable!("only the values next to zero can be answered from the summary"),
259 }
260 }
261
262 fn max_le(&self, x: i128) -> Option<i128> {
264 debug_assert!(x == -1 || x == 0);
265 if x == 0 && self.eq {
266 Some(0)
267 } else {
268 self.max_neg
269 }
270 }
271
272 fn min_ge(&self, x: i128) -> Option<i128> {
274 debug_assert!(x == 0 || x == 1);
275 if x == 0 && self.eq {
276 Some(0)
277 } else {
278 self.min_pos
279 }
280 }
281
282 fn saw_any_in(&self, lo: i128, hi: i128) -> bool {
285 if lo > hi {
286 return false;
287 }
288 debug_assert!(
289 lo <= 1 && hi >= -1,
290 "interval {lo}..={hi} is not anchored at zero"
291 );
292 let zero = self.eq && lo <= 0 && 0 <= hi;
293 let neg = lo <= -1 && self.max_neg.is_some_and(|m| m >= lo);
294 let pos = hi >= 1 && self.min_pos.is_some_and(|p| p <= hi);
295 zero || neg || pos
296 }
297}
298
299#[derive(Clone, Copy, PartialEq, Debug, Serialize)]
300#[serde(rename_all = "snake_case")]
301pub enum Mutation {
302 Const(bool),
304 Negate,
306 Op(CmpOp),
308 Shift(i128),
310 ShiftF(f64),
312 Delete,
314 LogicSwap,
316 LogicDropRhs,
318 LogicDropLhs,
320 ArithSwap,
322 ArithSwap2,
325 DropNot,
327 RangeEnd,
329 MinMax,
331 OtherEnd,
333}
334
335impl Mutation {
336 pub fn encode(&self) -> String {
338 match self {
339 Mutation::Const(c) => format!("c{}", *c as u8),
340 Mutation::Negate => "neg".to_string(),
341 Mutation::Op(op) => format!("op{}", op.code()),
342 Mutation::Shift(d) => format!("sh{d}"),
343 Mutation::ShiftF(d) => format!("shf{d:?}"),
344 Mutation::Delete => "del".to_string(),
345 Mutation::LogicSwap => "lsw".to_string(),
346 Mutation::LogicDropRhs => "ldr".to_string(),
347 Mutation::LogicDropLhs => "ldl".to_string(),
348 Mutation::ArithSwap => "asw".to_string(),
349 Mutation::ArithSwap2 => "as2".to_string(),
350 Mutation::DropNot => "unn".to_string(),
351 Mutation::RangeEnd => "rng".to_string(),
352 Mutation::MinMax => "mmx".to_string(),
353 Mutation::OtherEnd => "end".to_string(),
354 }
355 }
356}
357
358pub fn eval_mutated_d(op: CmpOp, m: Mutation, d: i128) -> bool {
360 match m {
361 Mutation::Const(c) => c,
362 Mutation::Negate => !op.eval_d(d),
363 Mutation::Op(op2) => op2.eval_d(d),
364 Mutation::Shift(s) => op.eval_d(d - s),
365 _ => op.eval_d(d),
366 }
367}
368
369pub fn infects(op: CmpOp, m: Mutation, obs: &Obs) -> bool {
372 match m {
373 Mutation::Const(c) => obs.rels().any(|r| op.eval_rel(r) != c),
374 Mutation::Op(op2) => obs.rels().any(|r| op.eval_rel(r) != op2.eval_rel(r)),
375 Mutation::Shift(s) => {
376 let Some((lo, hi)) = shift_diff(op, s) else {
377 return false;
378 };
379 if !obs.valued() {
380 return obs.reached();
382 }
383 obs.saw_any_in(lo, hi)
384 }
385 Mutation::ShiftF(s) => match obs.float_gap() {
387 Some((lo, hi)) => !(lo < s && s < hi) || obs.eq,
388 None => obs.reached(),
389 },
390 _ => obs.reached(),
392 }
393}
394
395fn shift_diff(op: CmpOp, s: i128) -> Option<(i128, i128)> {
397 let (up, t0) = op.threshold()?;
398 let t = t0 + s;
399 let (a, b) = (t0.min(t), t0.max(t));
400 Some(if up { (a, b - 1) } else { (a + 1, b) })
403}
404
405#[derive(Clone, PartialEq, Debug, Serialize)]
408#[serde(rename_all = "snake_case", tag = "kind")]
409pub enum InputGap {
410 Unreached,
412 OneSided { always: bool },
414 Boundary {
417 min_shift: i128,
418 max_shift: i128,
419 missing: Vec<i128>,
420 #[serde(skip_serializing_if = "no_shift")]
424 same: (i128, i128),
425 },
426 EqualNeverObserved {
430 flipped: CmpOp,
431 float_gap: Option<(f64, f64)>,
432 },
433}
434
435fn no_shift(range: &(i128, i128)) -> bool {
436 *range == (0, 0)
437}
438
439#[derive(Clone, PartialEq, Debug, Default)]
440pub struct CmpPlan {
441 pub cannot_differ: bool,
444 pub gap: Option<InputGap>,
445 pub run: Vec<Mutation>,
448}
449
450impl InputGap {
451 pub fn representative(&self) -> Option<Mutation> {
453 match self {
454 InputGap::Unreached => None,
455 InputGap::OneSided { always } => Some(Mutation::Const(*always)),
456 InputGap::Boundary {
457 min_shift,
458 max_shift,
459 same,
460 ..
461 } => {
462 if *max_shift > same.1 {
464 Some(Mutation::Shift(same.1 + 1))
465 } else if *min_shift < same.0 {
466 Some(Mutation::Shift(same.0 - 1))
467 } else {
468 None
469 }
470 }
471 InputGap::EqualNeverObserved { flipped, .. } => Some(Mutation::Op(*flipped)),
472 }
473 }
474
475 pub fn survivors(&self) -> Vec<Mutation> {
477 match self {
478 InputGap::Boundary {
479 min_shift,
480 max_shift,
481 same,
482 ..
483 } => (1..=3)
484 .flat_map(|n| [same.1 + n, same.0 - n])
485 .filter(|s| min_shift <= s && s <= max_shift)
486 .take(3)
487 .map(Mutation::Shift)
488 .collect(),
489 InputGap::EqualNeverObserved { flipped, float_gap } => {
490 let mut all = vec![Mutation::Op(*flipped)];
491 if let Some((lo, hi)) = float_gap {
492 all.extend([Mutation::ShiftF(hi / 2.0), Mutation::ShiftF(lo / 2.0)]);
493 }
494 all
495 }
496 other => other.representative().into_iter().collect(),
497 }
498 }
499}
500
501pub fn plan_cmp(op: CmpOp, obs: &Obs, domain: Option<(i128, i128)>) -> CmpPlan {
505 let mut plan = plan_boundary(op, obs, domain);
506 let answers =
514 |other: CmpOp| -> Vec<bool> { obs.rels().map(|rel| other.eval_rel(rel)).collect() };
515 for other in op.mistaken_for() {
516 if answers(*other) != answers(op) {
517 plan.run.push(Mutation::Op(*other));
518 }
519 }
520 if matches!(op, CmpOp::Le | CmpOp::Ge) && !obs.valued() && answers(op.negated()) != answers(op)
526 {
527 plan.run.push(Mutation::Op(op.negated()));
528 }
529 plan
530}
531
532fn plan_boundary(op: CmpOp, obs: &Obs, domain: Option<(i128, i128)>) -> CmpPlan {
533 if !obs.reached() {
534 return CmpPlan {
535 cannot_differ: false,
536 gap: Some(InputGap::Unreached),
537 run: vec![],
538 };
539 }
540 let saw_true = obs.rels().any(|r| op.eval_rel(r));
541 let saw_false = obs.rels().any(|r| !op.eval_rel(r));
542 if !(saw_true && saw_false) {
543 let always = saw_true;
544 let other_possible = match domain {
546 Some((lo, hi)) if obs.valued() => [-1, 0, 1]
549 .into_iter()
550 .any(|d| lo <= d && d <= hi && op.eval_d(d) != always),
551 _ => true,
552 };
553 return CmpPlan {
554 cannot_differ: !other_possible,
555 gap: other_possible.then_some(InputGap::OneSided { always }),
556 run: vec![Mutation::Negate],
557 };
558 }
559
560 let Some((up, t0)) = op.threshold() else {
561 return CmpPlan {
563 cannot_differ: false,
564 gap: None,
565 run: vec![Mutation::Negate],
566 };
567 };
568
569 if !obs.valued() {
570 let flipped = op
571 .flip_eq()
572 .expect("ordering operators have a flipped form");
573 return if obs.eq {
574 CmpPlan {
575 cannot_differ: false,
576 gap: None,
577 run: vec![Mutation::Op(flipped), Mutation::Negate],
578 }
579 } else {
580 CmpPlan {
581 cannot_differ: false,
582 gap: Some(InputGap::EqualNeverObserved {
583 flipped,
584 float_gap: obs.float_gap(),
585 }),
586 run: vec![Mutation::Negate],
587 }
588 };
589 }
590
591 let (below, above, w_minus, w_plus) = if up {
594 (obs.max_le(t0 - 1), obs.min_ge(t0), t0 - 1, t0)
595 } else {
596 (obs.max_le(t0), obs.min_ge(t0 + 1), t0, t0 + 1)
597 };
598 let below = below.expect("both outcomes were observed");
599 let above = above.expect("both outcomes were observed");
600 let (min_shift, max_shift) = if up {
601 (below + 1 - t0, above - t0)
602 } else {
603 (below - t0, above - 1 - t0)
604 };
605
606 let mut missing = vec![];
609 let mut run = vec![];
610 for (w, shift) in [(w_minus, -1), (w_plus, 1)] {
611 if obs.saw(w) {
612 run.push(Mutation::Shift(shift));
613 } else {
614 missing.push(w);
615 }
616 }
617 run.push(Mutation::Negate);
618 let gap = (!missing.is_empty()).then_some(InputGap::Boundary {
619 min_shift,
620 max_shift,
621 missing,
622 same: (0, 0),
623 });
624 CmpPlan {
625 cannot_differ: false,
626 gap,
627 run,
628 }
629}
630
631#[derive(Clone, Copy, PartialEq, Eq, Debug)]
634pub enum Reach {
635 Multiples(i128),
637 Bits(i128),
639 Below(i128),
641}
642
643impl Reach {
644 pub fn holds(self, value: i128) -> bool {
645 match self {
646 Reach::Multiples(step) => value % step == 0,
647 Reach::Bits(mask) => value >= 0 && value & !mask == 0,
648 Reach::Below(limit) => -limit < value && value < limit,
649 }
650 }
651
652 pub fn of_shift(bits: u32, (low, high): (i128, i128)) -> Option<Reach> {
657 let values = high.checked_sub(low)?.checked_add(1)?;
658 if values < 2 || values & (values - 1) != 0 {
659 return None;
660 }
661 let effective = bits % values.trailing_zeros();
662 (effective > 0).then(|| Reach::Multiples(1 << effective))
663 }
664
665 pub fn within(self, low: i128, high: i128) -> Option<(i128, i128)> {
668 let least = if self.holds(low) {
669 low
670 } else {
671 self.next_above(low)?
672 };
673 let greatest = if self.holds(high) {
674 high
675 } else {
676 self.next_below(high)?
677 };
678 (least <= greatest).then_some((least, greatest))
679 }
680
681 pub fn interval(self) -> Option<(i128, i128)> {
683 match self {
684 Reach::Multiples(_) => None,
685 Reach::Bits(mask) => Some((0, mask)),
686 Reach::Below(limit) => Some((1 - limit, limit - 1)),
687 }
688 }
689
690 fn next_above(self, value: i128) -> Option<i128> {
692 match self {
693 Reach::Multiples(step) => value.div_euclid(step).checked_add(1)?.checked_mul(step),
694 Reach::Bits(_) if value < 0 => Some(0),
695 Reach::Bits(mask) => {
696 let over = value.checked_add(1)?;
697 let outside = over & !mask;
698 if outside == 0 {
699 return Some(over);
700 }
701 let top = 127 - outside.leading_zeros();
705 let low = 1_i128.checked_shl(top + 1)?.checked_sub(1)?;
706 let next = (over | !mask | low).wrapping_add(1) & mask;
707 (next > value).then_some(next)
708 }
709 Reach::Below(limit) => {
710 let next = value.checked_add(1)?.max(1 - limit);
711 (next < limit).then_some(next)
712 }
713 }
714 }
715
716 fn next_below(self, value: i128) -> Option<i128> {
718 match self {
719 Reach::Multiples(step) => value.checked_sub(1)?.div_euclid(step).checked_mul(step),
720 Reach::Bits(_) if value <= 0 => None,
721 Reach::Bits(mask) => {
722 let under = value - 1;
723 let outside = under & !mask;
724 Some(if outside == 0 {
725 under
726 } else {
727 let top = 127 - outside.leading_zeros();
729 let low = (1_i128 << top) - 1;
730 (under & !low & !(1_i128 << top)) | (mask & low)
731 })
732 }
733 Reach::Below(limit) => {
734 let next = value.checked_sub(1)?.min(limit - 1);
735 (next > -limit).then_some(next)
736 }
737 }
738 }
739}
740
741pub fn within_reach(
746 gap: InputGap,
747 op: CmpOp,
748 obs: &Obs,
749 reach: Reach,
750 literal: i128,
751 literal_is_rhs: bool,
752) -> Option<InputGap> {
753 let InputGap::Boundary {
754 min_shift,
755 max_shift,
756 missing,
757 same,
758 } = gap
759 else {
760 return Some(gap);
761 };
762 let Some((up, t0)) = op.threshold() else {
763 return Some(InputGap::Boundary {
764 min_shift,
765 max_shift,
766 missing,
767 same,
768 });
769 };
770 let (below, above, w_minus, w_plus) = if up {
773 (obs.max_le(t0 - 1), obs.min_ge(t0), t0 - 1, t0)
774 } else {
775 (obs.max_le(t0), obs.min_ge(t0 + 1), t0, t0 + 1)
776 };
777 let (below, above) = (below?, above?);
778 let value = |d: i128| {
780 if literal_is_rhs {
781 literal.checked_add(d)
782 } else {
783 literal.checked_sub(d)
784 }
785 };
786 let difference = |v: i128| {
787 if literal_is_rhs {
788 v.checked_sub(literal)
789 } else {
790 literal.checked_sub(v)
791 }
792 };
793 let nearest = |start: i128, bound: i128| -> Option<i128> {
796 if (start < bound) != (start <= bound) || start == bound {
797 return None;
798 }
799 let (from, to) = (value(start)?, value(bound)?);
800 let found = if reach.holds(from) {
801 from
802 } else if to > from {
803 reach.next_above(from)?
804 } else {
805 reach.next_below(from)?
806 };
807 let inside = if to > from { found < to } else { found > to };
808 inside.then(|| difference(found)).flatten()
809 };
810 let down = nearest(w_minus, below);
811 let up_side = nearest(w_plus, above);
812 let missing: Vec<i128> = [(w_minus, down), (w_plus, up_side)]
813 .into_iter()
814 .filter(|(w, _)| missing.contains(w))
815 .filter_map(|(_, nearest)| nearest)
816 .collect();
817 if missing.is_empty() {
818 return None;
819 }
820 let (first_under, first_over) = (down.unwrap_or(below), up_side.unwrap_or(above));
824 let same = if up {
825 (first_under - t0 + 1, first_over - t0)
826 } else {
827 (first_under - t0, first_over - t0 - 1)
828 };
829 Some(InputGap::Boundary {
830 min_shift,
831 max_shift,
832 missing,
833 same: (same.0.max(min_shift).min(0), same.1.min(max_shift).max(0)),
834 })
835}
836
837#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
839pub struct LogicObs {
840 pub short: bool,
842 pub rhs_true: bool,
843 pub rhs_false: bool,
844 pub rhs_started: bool,
847}
848
849impl LogicObs {
850 pub fn from_bits(bits: u8) -> LogicObs {
851 LogicObs {
852 short: bits & 1 != 0,
853 rhs_true: bits & 2 != 0,
854 rhs_false: bits & 4 != 0,
855 rhs_started: bits & 8 != 0,
856 }
857 }
858
859 pub fn merge(&mut self, other: &LogicObs) {
860 self.short |= other.short;
861 self.rhs_true |= other.rhs_true;
862 self.rhs_false |= other.rhs_false;
863 self.rhs_started |= other.rhs_started;
864 }
865
866 pub fn reached(&self) -> bool {
867 self.short || self.rhs_true || self.rhs_false || self.rhs_started
868 }
869
870 pub fn constant_value(&self, and: bool) -> Option<bool> {
872 let saw_true = self.rhs_true || (self.short && !and);
874 let saw_false = self.rhs_false || (self.short && and);
875 match (saw_true, saw_false) {
876 (true, false) => Some(true),
877 (false, true) => Some(false),
878 _ => None,
879 }
880 }
881
882 fn rhs_decided(&self, and: bool) -> bool {
884 if and { self.rhs_false } else { self.rhs_true }
885 }
886}
887
888#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
889#[serde(rename_all = "snake_case")]
890pub enum LogicGap {
891 Unreached,
892 RhsNeverDecides,
894 LhsNeverDecides,
896}
897
898#[derive(Clone, PartialEq, Debug, Default)]
899pub struct LogicPlan {
900 pub gaps: Vec<LogicGap>,
901 pub run: Vec<Mutation>,
902}
903
904pub fn logic_infects(and: bool, m: Mutation, obs: &LogicObs) -> bool {
908 match m {
909 Mutation::LogicDropRhs => obs.rhs_decided(and),
910 Mutation::LogicDropLhs => obs.short,
911 _ => obs.reached(),
912 }
913}
914
915pub fn plan_logic(and: bool, obs: &LogicObs) -> LogicPlan {
916 if !obs.reached() {
917 return LogicPlan {
918 gaps: vec![LogicGap::Unreached],
919 run: vec![],
920 };
921 }
922 let mut plan = LogicPlan::default();
923 for (m, gap) in [
924 (Mutation::LogicDropRhs, LogicGap::RhsNeverDecides),
925 (Mutation::LogicDropLhs, LogicGap::LhsNeverDecides),
926 ] {
927 if logic_infects(and, m, obs) {
928 plan.run.push(m);
929 } else {
930 plan.gaps.push(gap);
931 }
932 }
933 plan.run.push(Mutation::LogicSwap);
934 plan
935}
936
937#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
939pub struct ArithObs {
940 pub same: bool,
942 pub differs: bool,
944 pub opaque: bool,
946 pub same2: bool,
948 pub differs2: bool,
949}
950
951impl ArithObs {
952 pub fn from_bits(bits: u8) -> ArithObs {
953 ArithObs {
954 same: bits & 1 != 0,
955 differs: bits & 2 != 0,
956 opaque: bits & 4 != 0,
957 same2: bits & 8 != 0,
958 differs2: bits & 16 != 0,
959 }
960 }
961
962 pub fn merge(&mut self, other: &ArithObs) {
963 self.same |= other.same;
964 self.differs |= other.differs;
965 self.opaque |= other.opaque;
966 self.same2 |= other.same2;
967 self.differs2 |= other.differs2;
968 }
969
970 pub fn second(&self) -> ArithObs {
972 ArithObs {
973 same: self.same2,
974 differs: self.differs2,
975 opaque: self.opaque,
976 same2: false,
977 differs2: false,
978 }
979 }
980
981 pub fn infected_by(&self, m: Mutation) -> bool {
983 if m == Mutation::ArithSwap2 {
984 self.differs2
985 } else {
986 self.differs
987 }
988 }
989}
990
991#[derive(Clone, Copy, PartialEq, Eq, Debug)]
992pub enum ArithPlan {
993 Unreached,
994 Opaque,
996 NeverDiffers,
998 Run,
1000}
1001
1002pub fn plan_arith(obs: &ArithObs) -> ArithPlan {
1003 if obs.differs {
1004 ArithPlan::Run
1005 } else if obs.same {
1006 ArithPlan::NeverDiffers
1007 } else if obs.opaque {
1008 ArithPlan::Opaque
1009 } else {
1010 ArithPlan::Unreached
1011 }
1012}
1013
1014#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
1016pub struct ReturnObs {
1017 pub saw_true: bool,
1018 pub saw_false: bool,
1019}
1020
1021impl ReturnObs {
1022 pub fn from_bits(bits: u8) -> ReturnObs {
1023 ReturnObs {
1024 saw_true: bits & 1 != 0,
1025 saw_false: bits & 2 != 0,
1026 }
1027 }
1028
1029 pub fn merge(&mut self, other: &ReturnObs) {
1030 self.saw_true |= other.saw_true;
1031 self.saw_false |= other.saw_false;
1032 }
1033
1034 pub fn reached(&self) -> bool {
1035 self.saw_true || self.saw_false
1036 }
1037
1038 pub fn infected_by_const(&self, value: bool) -> bool {
1040 if value { self.saw_false } else { self.saw_true }
1041 }
1042}
1043
1044#[derive(Clone, PartialEq, Debug, Default)]
1045pub struct ReturnPlan {
1046 pub unreached: bool,
1047 pub always: Option<bool>,
1049 pub run: Vec<Mutation>,
1050}
1051
1052pub fn plan_option_return(obs: &ReturnObs) -> ReturnPlan {
1055 match (obs.saw_true, obs.saw_false) {
1056 (false, false) => ReturnPlan {
1057 unreached: true,
1058 ..ReturnPlan::default()
1059 },
1060 (false, true) => ReturnPlan {
1062 unreached: false,
1063 always: Some(false),
1064 run: vec![],
1065 },
1066 (true, _) => ReturnPlan {
1067 unreached: false,
1068 always: None,
1069 run: vec![Mutation::Const(false)],
1070 },
1071 }
1072}
1073
1074pub fn plan_return(obs: &ReturnObs) -> ReturnPlan {
1075 match (obs.saw_true, obs.saw_false) {
1076 (false, false) => ReturnPlan {
1077 unreached: true,
1078 ..ReturnPlan::default()
1079 },
1080 (true, true) => ReturnPlan {
1081 run: vec![Mutation::Const(true), Mutation::Const(false)],
1082 ..ReturnPlan::default()
1083 },
1084 (always, _) => ReturnPlan {
1085 unreached: false,
1086 always: Some(always),
1087 run: vec![Mutation::Const(!always)],
1088 },
1089 }
1090}
1091
1092#[cfg(test)]
1093mod tests {
1094 use super::*;
1095
1096 const REACHES: [Reach; 5] = [
1097 Reach::Multiples(4),
1098 Reach::Multiples(8),
1099 Reach::Bits(0b1010),
1100 Reach::Bits(0b0110),
1101 Reach::Below(5),
1102 ];
1103
1104 #[test]
1105 fn the_next_value_an_expression_can_be() {
1106 for reach in REACHES {
1107 for value in -40..=40_i128 {
1108 let above = (value + 1..=80).find(|v| reach.holds(*v));
1109 let below = (-80..value).rev().find(|v| reach.holds(*v));
1110 assert_eq!(reach.next_above(value), above, "{reach:?} above {value}");
1111 assert_eq!(reach.next_below(value), below, "{reach:?} below {value}");
1112 }
1113 }
1114 assert_eq!(Reach::Multiples(8).next_above(i128::MAX), None);
1116 assert_eq!(Reach::Multiples(8).next_below(i128::MIN), None);
1117 assert_eq!(Reach::Below(5).next_above(i128::MAX), None);
1118 assert_eq!(Reach::Below(5).next_below(i128::MIN), None);
1119 assert_eq!(Reach::Bits(i128::MAX).next_above(i128::MAX), None);
1120 assert!(Reach::Bits(0xF0).holds(0x30) && !Reach::Bits(0xF0).holds(0x31));
1121 assert!(!Reach::Bits(0xF0).holds(-16));
1122 }
1123
1124 #[test]
1128 fn a_gap_is_kept_only_for_values_the_expression_can_be() {
1129 let mut dropped = 0;
1130 let mut kept = 0;
1131 for reach in REACHES {
1132 let can_be: Vec<i128> = (-24..=24).filter(|v| reach.holds(*v)).collect();
1133 for op in [CmpOp::Lt, CmpOp::Le, CmpOp::Gt, CmpOp::Ge] {
1134 for literal in -6..=14_i128 {
1135 for literal_is_rhs in [true, false] {
1136 let d_of = |v: i128| {
1137 if literal_is_rhs {
1138 v - literal
1139 } else {
1140 literal - v
1141 }
1142 };
1143 for &low in &can_be {
1144 for &high in can_be.iter().filter(|v| **v > low) {
1145 let mut obs = Obs::of_d(d_of(low));
1146 obs.merge(&Obs::of_d(d_of(high)));
1147 let Some(gap @ InputGap::Boundary { .. }) =
1148 plan_boundary(op, &obs, None).gap
1149 else {
1150 continue;
1151 };
1152 let InputGap::Boundary {
1153 min_shift,
1154 max_shift,
1155 ..
1156 } = gap.clone()
1157 else {
1158 unreachable!()
1159 };
1160 let differs = |shift: i128, v: i128| {
1163 op.eval_d(d_of(v)) != op.eval_d(d_of(v) - shift)
1164 };
1165 let told_apart =
1166 |shift: i128| can_be.iter().any(|v| differs(shift, *v));
1167 let case = format!(
1168 "{reach:?} {op:?} literal {literal} rhs {literal_is_rhs}, saw {low} and {high}"
1169 );
1170 match within_reach(gap, op, &obs, reach, literal, literal_is_rhs) {
1171 None => {
1172 for shift in min_shift..=max_shift {
1174 assert!(!told_apart(shift), "{case}: shift {shift}");
1175 }
1176 dropped += 1;
1177 }
1178 Some(InputGap::Boundary {
1179 min_shift: least,
1180 max_shift: most,
1181 missing,
1182 same,
1183 }) => {
1184 assert!(!missing.is_empty(), "{case}");
1185 assert_eq!((least, most), (min_shift, max_shift), "{case}");
1186 for d in &missing {
1187 let v = if literal_is_rhs {
1188 literal + d
1189 } else {
1190 literal - d
1191 };
1192 assert!(reach.holds(v), "{case}: {v}");
1193 assert!(low < v && v < high, "{case}: {v}");
1194 }
1195 assert!(same.0 <= 0 && 0 <= same.1, "{case}: {same:?}");
1198 for shift in least..=most {
1199 assert_eq!(
1200 same.0 <= shift && shift <= same.1,
1201 !told_apart(shift),
1202 "{case}: shift {shift}, same {same:?}"
1203 );
1204 }
1205 let gap = InputGap::Boundary {
1208 min_shift: least,
1209 max_shift: most,
1210 missing: missing.clone(),
1211 same,
1212 };
1213 for mutation in gap.survivors() {
1214 let Mutation::Shift(shift) = mutation else {
1215 panic!("{case}: {mutation:?}")
1216 };
1217 assert!(told_apart(shift), "{case}: shift {shift}");
1218 }
1219 assert!(gap.representative().is_some(), "{case}");
1220 kept += 1;
1221 }
1222 Some(other) => panic!("{case}: {other:?}"),
1223 }
1224 }
1225 }
1226 }
1227 }
1228 }
1229 }
1230 assert!(
1231 dropped > 100 && kept > 1000,
1232 "{dropped} dropped, {kept} kept"
1233 );
1234 }
1235
1236 #[test]
1237 fn an_outcome_no_value_gives_is_not_asked_for() {
1238 for (reach, literal, op, differs) in [
1240 (Reach::Below(10), 10, CmpOp::Lt, false),
1241 (Reach::Below(10), 9, CmpOp::Lt, true),
1242 (Reach::Below(10), -10, CmpOp::Gt, false),
1243 (Reach::Bits(0xF0), 0xF0, CmpOp::Gt, false),
1244 (Reach::Bits(0xF0), 0xF0, CmpOp::Ge, true),
1245 (Reach::Bits(0xF0), 0, CmpOp::Lt, false),
1246 ] {
1247 let (lo, hi) = reach.interval().unwrap();
1248 assert!((lo..=hi).all(|v| !reach.holds(v) || (lo <= v && v <= hi)));
1249 assert!(reach.holds(lo) && reach.holds(hi), "{reach:?}");
1250 assert!(!reach.holds(lo - 1) && !reach.holds(hi + 1), "{reach:?}");
1251 let can_differ = (lo..=hi)
1252 .filter(|v| reach.holds(*v))
1253 .map(|v| op.eval_d(v - literal))
1254 .collect::<std::collections::BTreeSet<_>>()
1255 .len()
1256 == 2;
1257 assert_eq!(can_differ, differs, "{reach:?} {op:?} {literal}");
1258 let seen = (lo..=hi).find(|v| reach.holds(*v)).unwrap();
1260 let plan = plan_cmp(
1261 op,
1262 &Obs::of_d(seen - literal),
1263 Some((lo - literal, hi - literal)),
1264 );
1265 assert_eq!(plan.cannot_differ, !differs, "{reach:?} {op:?} {literal}");
1266 assert_eq!(plan.gap.is_some(), differs, "{reach:?} {op:?} {literal}");
1267 }
1268 assert_eq!(Reach::Multiples(8).interval(), None);
1269 }
1270
1271 #[test]
1272 fn a_shift_is_a_multiple_by_the_bits_that_are_shifted_in_fact() {
1273 let (byte, signed_byte) = ((0, 255), (-128, 127));
1274 for (bits, range, expected) in [
1275 (3, byte, Some(Reach::Multiples(8))),
1276 (7, byte, Some(Reach::Multiples(128))),
1277 (8, byte, None),
1279 (9, byte, Some(Reach::Multiples(2))),
1280 (16, byte, None),
1281 (9, signed_byte, Some(Reach::Multiples(2))),
1282 (33, (0, u32::MAX as i128), Some(Reach::Multiples(2))),
1283 (3, (0, u32::MAX as i128), Some(Reach::Multiples(8))),
1284 (3, (0, 9), None),
1286 (3, (0, 0), None),
1287 (3, (5, 4), None),
1288 (3, (i128::MIN, i128::MAX), None),
1289 ] {
1290 assert_eq!(Reach::of_shift(bits, range), expected, "{bits} {range:?}");
1291 }
1292 for bits in 1..=20u32 {
1294 let reach = Reach::of_shift(bits, byte);
1295 for n in 0..=255u8 {
1296 let shifted = i128::from(n.wrapping_shl(bits));
1297 assert!(
1298 reach.is_none_or(|reach| reach.holds(shifted)),
1299 "{n} << {bits}"
1300 );
1301 }
1302 }
1303 }
1304
1305 #[test]
1306 fn the_ends_of_what_an_expression_can_be_in_its_type() {
1307 for reach in REACHES {
1308 for (low, high) in [(0, 255), (-128, 127), (0, 0), (3, 3), (5, 9)] {
1309 let all: Vec<i128> = (low..=high).filter(|v| reach.holds(*v)).collect();
1310 assert_eq!(
1311 reach.within(low, high),
1312 all.first().copied().zip(all.last().copied()),
1313 "{reach:?} in {low}..={high}"
1314 );
1315 }
1316 }
1317 assert_eq!(Reach::Multiples(2).within(0, 255), Some((0, 254)));
1319 }
1320
1321 #[test]
1322 fn gaps_of_another_kind_are_not_touched() {
1323 let obs = Obs::of_d(3);
1324 for gap in [
1325 InputGap::Unreached,
1326 InputGap::OneSided { always: true },
1327 InputGap::EqualNeverObserved {
1328 flipped: CmpOp::Le,
1329 float_gap: None,
1330 },
1331 ] {
1332 assert_eq!(
1333 within_reach(gap.clone(), CmpOp::Lt, &obs, Reach::Multiples(8), 20, true),
1334 Some(gap)
1335 );
1336 }
1337 let mut obs = Obs::of_d(16 - 20);
1339 obs.merge(&Obs::of_d(24 - 20));
1340 let gap = plan_boundary(CmpOp::Gt, &obs, None).gap.unwrap();
1341 assert_eq!(
1342 within_reach(gap, CmpOp::Gt, &obs, Reach::Multiples(8), 20, true),
1343 None
1344 );
1345 let mut obs = Obs::of_d(8 - 20);
1347 obs.merge(&Obs::of_d(40 - 20));
1348 let gap = plan_boundary(CmpOp::Gt, &obs, None).gap.unwrap();
1349 assert_eq!(
1350 within_reach(gap, CmpOp::Gt, &obs, Reach::Multiples(8), 20, true),
1351 Some(InputGap::Boundary {
1352 min_shift: -12,
1353 max_shift: 19,
1354 missing: vec![-4, 4],
1355 same: (-4, 3),
1357 })
1358 );
1359 }
1360
1361 fn obs_of(ds: &[i128]) -> Obs {
1362 let mut obs = Obs::default();
1363 for d in ds {
1364 obs.merge(&Obs::of_d(*d));
1365 }
1366 obs
1367 }
1368
1369 fn rel_of(d: i128) -> Rel {
1370 match d {
1371 d if d < 0 => Rel::Less,
1372 0 => Rel::Equal,
1373 _ => Rel::Greater,
1374 }
1375 }
1376
1377 fn brute_infects(op: CmpOp, m: Mutation, ds: &[i128]) -> bool {
1379 ds.iter()
1380 .any(|d| op.eval_d(*d) != eval_mutated_d(op, m, *d))
1381 }
1382
1383 #[test]
1384 fn is_eligible_example_from_the_readme() {
1385 let plan = plan_cmp(CmpOp::Ge, &obs_of(&[-1, 2]), None);
1387 assert_eq!(
1388 plan.gap,
1389 Some(InputGap::Boundary {
1390 min_shift: 0,
1391 max_shift: 2,
1392 missing: vec![0],
1393 same: (0, 0),
1394 })
1395 );
1396 assert_eq!(plan.run, vec![Mutation::Shift(-1), Mutation::Negate]);
1398 assert_eq!(plan.gap.unwrap().representative(), Some(Mutation::Shift(1)));
1399 }
1400
1401 #[test]
1406 fn the_operators_a_comparison_is_mistaken_for() {
1407 let rels = [Rel::Less, Rel::Equal, Rel::Greater, Rel::Unordered];
1408 for op in [CmpOp::Lt, CmpOp::Gt] {
1409 for seen in 1u8..16 {
1410 let mut obs = Obs::default();
1411 let seen: Vec<Rel> = (0..4)
1412 .filter(|i| seen & (1 << i) != 0)
1413 .map(|i| rels[i])
1414 .collect();
1415 for rel in &seen {
1416 obs.merge(&Obs::of_rel(*rel));
1417 }
1418 let plan = plan_cmp(op, &obs, None);
1419 for other in op.mistaken_for() {
1420 let like_the_original = seen
1421 .iter()
1422 .all(|rel| other.eval_rel(*rel) == op.eval_rel(*rel));
1423 let what = format!("{op:?} as {other:?} with {seen:?}: {plan:?}");
1424 let run = plan
1425 .run
1426 .iter()
1427 .filter(|m| **m == Mutation::Op(*other))
1428 .count();
1429 if like_the_original {
1430 assert!(plan.gap.is_some(), "{what}");
1431 assert_eq!(run, 0, "{what}");
1432 } else {
1433 assert_eq!(run, 1, "{what}");
1434 }
1435 }
1436 }
1437 }
1438 for op in [CmpOp::Le, CmpOp::Ge, CmpOp::Eq, CmpOp::Ne] {
1440 assert!(op.mistaken_for().is_empty());
1441 }
1442 let seen = |rels: &[Rel]| {
1447 let mut obs = Obs::default();
1448 for rel in rels {
1449 obs.merge(&Obs::of_rel(*rel));
1450 }
1451 obs
1452 };
1453 for (op, reverse) in [(CmpOp::Ge, CmpOp::Lt), (CmpOp::Le, CmpOp::Gt)] {
1454 let run = |rels: &[Rel]| plan_cmp(op, &seen(rels), None).run;
1455 let reversed = |rels: &[Rel]| run(rels).contains(&Mutation::Op(reverse));
1456 assert!(reversed(&[Rel::Unordered, Rel::Less]), "{op:?}");
1457 assert!(reversed(&[Rel::Less, Rel::Greater]), "{op:?}");
1458 assert!(reversed(&[Rel::Less, Rel::Equal, Rel::Greater]), "{op:?}");
1459 assert_eq!(run(&[Rel::Unordered]), vec![Mutation::Negate], "{op:?}");
1463 for ds in [&[-1i128, 1][..], &[-2, 0, 3], &[0, 1]] {
1465 let plan = plan_cmp(op, &obs_of(ds), None);
1466 assert!(!plan.run.contains(&Mutation::Op(reverse)), "{op:?} {ds:?}");
1467 }
1468 for d in -3..=3 {
1469 assert_eq!(eval_mutated_d(op, Mutation::Negate, d), reverse.eval_d(d));
1470 }
1471 }
1472 let run = plan_cmp(CmpOp::Eq, &seen(&[Rel::Unordered, Rel::Equal]), None).run;
1474 assert_eq!(run, vec![Mutation::Negate]);
1475 let plan = plan_cmp(CmpOp::Lt, &Obs::of_rel(Rel::Greater), None);
1478 assert_eq!(plan.run, vec![Mutation::Negate, Mutation::Op(CmpOp::Gt)]);
1479 }
1480
1481 #[test]
1482 fn pinned_boundary_has_no_input_gap() {
1483 for op in [CmpOp::Lt, CmpOp::Le, CmpOp::Gt, CmpOp::Ge] {
1484 let plan = plan_cmp(op, &obs_of(&[-1, 0, 1]), None);
1485 assert_eq!(plan.gap, None, "{op:?}");
1486 let mut run = vec![Mutation::Shift(-1), Mutation::Shift(1), Mutation::Negate];
1488 run.extend(op.mistaken_for().iter().map(|other| Mutation::Op(*other)));
1489 assert_eq!(plan.run, run, "{op:?}");
1490 }
1491 }
1492
1493 #[test]
1494 fn unreached_site() {
1495 let plan = plan_cmp(CmpOp::Lt, &Obs::default(), None);
1496 assert_eq!(
1497 plan,
1498 CmpPlan {
1499 cannot_differ: false,
1500 gap: Some(InputGap::Unreached),
1501 run: vec![]
1502 }
1503 );
1504 }
1505
1506 #[test]
1507 fn one_sided_condition() {
1508 let plan = plan_cmp(CmpOp::Gt, &obs_of(&[3, 5]), None);
1509 assert_eq!(plan.gap, Some(InputGap::OneSided { always: true }));
1510 assert_eq!(
1512 plan.run,
1513 vec![
1514 Mutation::Negate,
1515 Mutation::Op(CmpOp::Eq),
1516 Mutation::Op(CmpOp::Lt)
1517 ]
1518 );
1519 }
1520
1521 #[test]
1522 fn unsigned_compared_with_zero_is_not_reported() {
1523 let plan = plan_cmp(CmpOp::Ge, &obs_of(&[0, 4]), Some((0, 255)));
1525 assert_eq!(plan.gap, None);
1526 assert!(plan.cannot_differ);
1527 assert!(!plan_cmp(CmpOp::Ge, &obs_of(&[0, 4]), Some((-1, 255))).cannot_differ);
1528 assert!(!plan_cmp(CmpOp::Ge, &obs_of(&[-1, 4]), Some((-1, 255))).cannot_differ);
1529 let plan = plan_cmp(CmpOp::Lt, &obs_of(&[0, 4]), Some((0, 255)));
1531 assert_eq!(plan.gap, None);
1532 let plan = plan_cmp(CmpOp::Gt, &obs_of(&[0, 1]), Some((0, 255)));
1535 assert_eq!(plan.gap, None);
1536 assert_eq!(
1538 plan.run,
1539 vec![
1540 Mutation::Shift(-1),
1541 Mutation::Shift(1),
1542 Mutation::Negate,
1543 Mutation::Op(CmpOp::Eq),
1544 Mutation::Op(CmpOp::Lt)
1545 ]
1546 );
1547 let plan = plan_cmp(CmpOp::Le, &obs_of(&[0, 1]), Some((0, 255)));
1550 assert_eq!(plan.gap, None);
1551 }
1552
1553 #[test]
1554 fn one_sided_condition_at_the_edge_of_the_type() {
1555 let plan = plan_cmp(CmpOp::Ge, &obs_of(&[0, 4]), Some((-1, 10)));
1557 assert_eq!(plan.gap, Some(InputGap::OneSided { always: true }));
1558 let plan = plan_cmp(CmpOp::Le, &obs_of(&[-4, 0]), Some((-10, 1)));
1560 assert_eq!(plan.gap, Some(InputGap::OneSided { always: true }));
1561 let plan = plan_cmp(CmpOp::Le, &obs_of(&[-4, 0]), Some((-10, 0)));
1562 assert_eq!(plan.gap, None);
1563 let plan = plan_cmp(CmpOp::Ge, &Obs::of_rel(Rel::Greater), Some((0, 10)));
1565 assert_eq!(plan.gap, Some(InputGap::OneSided { always: true }));
1566 }
1567
1568 #[test]
1569 fn representative_is_the_nearest_survivor() {
1570 let boundary = |min_shift, max_shift| InputGap::Boundary {
1571 min_shift,
1572 max_shift,
1573 missing: vec![0],
1574 same: (0, 0),
1575 };
1576 assert_eq!(boundary(-3, 1).representative(), Some(Mutation::Shift(1)));
1577 assert_eq!(boundary(-1, 0).representative(), Some(Mutation::Shift(-1)));
1578 assert_eq!(boundary(0, 0).representative(), None);
1579 assert_eq!(InputGap::Unreached.representative(), None);
1580 assert_eq!(
1581 InputGap::OneSided { always: false }.representative(),
1582 Some(Mutation::Const(false))
1583 );
1584 assert_eq!(
1585 InputGap::EqualNeverObserved {
1586 flipped: CmpOp::Ge,
1587 float_gap: None
1588 }
1589 .representative(),
1590 Some(Mutation::Op(CmpOp::Ge))
1591 );
1592 }
1593
1594 fn eval_logic(and: bool, m: Option<Mutation>, a: bool, b: bool) -> (bool, bool) {
1597 let decided = |and: bool| if and { !a } else { a };
1598 match m {
1599 None => {
1600 if decided(and) {
1601 (a, false)
1602 } else {
1603 (b, true)
1604 }
1605 }
1606 Some(Mutation::LogicSwap) => {
1607 if decided(!and) {
1608 (a, false)
1609 } else {
1610 (b, true)
1611 }
1612 }
1613 Some(Mutation::LogicDropRhs) => (a, !decided(and)),
1615 Some(Mutation::LogicDropLhs) => (b, true),
1616 other => panic!("{other:?}"),
1617 }
1618 }
1619
1620 #[test]
1621 fn logic_plans_match_a_reference_evaluator() {
1622 let cases = [(false, false), (false, true), (true, false), (true, true)];
1623 for and in [true, false] {
1624 for mask in 0u32..16 {
1625 let seen: Vec<(bool, bool)> = cases
1626 .iter()
1627 .enumerate()
1628 .filter_map(|(i, c)| (mask & (1 << i) != 0).then_some(*c))
1629 .collect();
1630 let mut obs = LogicObs::default();
1631 for (a, b) in &seen {
1632 let (_, evaluated) = eval_logic(and, None, *a, *b);
1633 obs.short |= !evaluated;
1634 obs.rhs_true |= evaluated && *b;
1635 obs.rhs_false |= evaluated && !*b;
1636 }
1637 let ctx = format!("and={and} seen={seen:?}");
1638 let differs = |m: Mutation| {
1639 seen.iter().any(|(a, b)| {
1640 eval_logic(and, None, *a, *b) != eval_logic(and, Some(m), *a, *b)
1641 })
1642 };
1643 let plan = plan_logic(and, &obs);
1644 if seen.is_empty() {
1645 assert_eq!(
1646 plan,
1647 LogicPlan {
1648 gaps: vec![LogicGap::Unreached],
1649 run: vec![]
1650 }
1651 );
1652 continue;
1653 }
1654 for (m, gap) in [
1655 (Mutation::LogicDropRhs, LogicGap::RhsNeverDecides),
1656 (Mutation::LogicDropLhs, LogicGap::LhsNeverDecides),
1657 ] {
1658 assert_eq!(plan.gaps.contains(&gap), !differs(m), "{gap:?} {ctx}");
1660 assert_eq!(plan.run.contains(&m), differs(m), "{m:?} {ctx}");
1661 assert_eq!(logic_infects(and, m, &obs), differs(m), "{m:?} {ctx}");
1662 }
1663 assert!(plan.run.contains(&Mutation::LogicSwap), "{ctx}");
1665 assert!(logic_infects(and, Mutation::LogicSwap, &obs), "{ctx}");
1666 }
1667 }
1668 let started = LogicObs::from_bits(8);
1672 assert!(started.rhs_started && started.reached());
1673 for and in [true, false] {
1674 let plan = plan_logic(and, &started);
1675 assert_eq!(plan.run, vec![Mutation::LogicSwap]);
1676 assert_eq!(
1677 plan.gaps,
1678 vec![LogicGap::RhsNeverDecides, LogicGap::LhsNeverDecides]
1679 );
1680 assert!(logic_infects(and, Mutation::LogicSwap, &started));
1681 assert!(!logic_infects(and, Mutation::LogicDropRhs, &started));
1682 assert!(!logic_infects(and, Mutation::LogicDropLhs, &started));
1683 }
1684 let mut merged = LogicObs::from_bits(2);
1685 merged.merge(&started);
1686 assert_eq!(merged, LogicObs::from_bits(10));
1687 assert_eq!(
1688 LogicObs::from_bits(5),
1689 LogicObs {
1690 short: true,
1691 rhs_true: false,
1692 rhs_false: true,
1693 rhs_started: false,
1694 }
1695 );
1696 assert_eq!(
1697 LogicObs::from_bits(2),
1698 LogicObs {
1699 short: false,
1700 rhs_true: true,
1701 rhs_false: false,
1702 rhs_started: false,
1703 }
1704 );
1705 }
1706
1707 #[test]
1708 fn return_plans() {
1709 assert_eq!(
1710 plan_return(&ReturnObs::from_bits(0)),
1711 ReturnPlan {
1712 unreached: true,
1713 always: None,
1714 run: vec![]
1715 }
1716 );
1717 assert_eq!(
1718 plan_return(&ReturnObs::from_bits(1)),
1719 ReturnPlan {
1720 unreached: false,
1721 always: Some(true),
1722 run: vec![Mutation::Const(false)]
1723 }
1724 );
1725 assert_eq!(
1726 plan_return(&ReturnObs::from_bits(2)),
1727 ReturnPlan {
1728 unreached: false,
1729 always: Some(false),
1730 run: vec![Mutation::Const(true)]
1731 }
1732 );
1733 assert_eq!(
1734 plan_return(&ReturnObs::from_bits(3)),
1735 ReturnPlan {
1736 unreached: false,
1737 always: None,
1738 run: vec![Mutation::Const(true), Mutation::Const(false)]
1739 }
1740 );
1741 for bits in 0u8..4 {
1743 let obs = ReturnObs::from_bits(bits);
1744 assert_eq!(obs.infected_by_const(true), obs.saw_false);
1745 assert_eq!(obs.infected_by_const(false), obs.saw_true);
1746 assert_eq!(obs.reached(), bits != 0);
1747 let plan = plan_return(&obs);
1748 for m in &plan.run {
1749 let Mutation::Const(value) = m else { panic!() };
1750 assert!(
1751 obs.infected_by_const(*value),
1752 "a run mutant must change some test"
1753 );
1754 }
1755 if let Some(always) = plan.always {
1756 assert!(
1757 !obs.infected_by_const(always),
1758 "the survivor changes no test"
1759 );
1760 }
1761 }
1762 let mut merged = ReturnObs::from_bits(1);
1763 merged.merge(&ReturnObs::from_bits(2));
1764 assert_eq!(
1765 merged,
1766 ReturnObs {
1767 saw_true: true,
1768 saw_false: true
1769 }
1770 );
1771 }
1772
1773 #[test]
1774 fn logic_constant_value() {
1775 let cases = [(false, false), (false, true), (true, false), (true, true)];
1776 for and in [true, false] {
1777 for mask in 0u32..16 {
1778 let seen: Vec<(bool, bool)> = cases
1779 .iter()
1780 .enumerate()
1781 .filter_map(|(i, c)| (mask & (1 << i) != 0).then_some(*c))
1782 .collect();
1783 let mut obs = LogicObs::default();
1784 let mut values = std::collections::BTreeSet::new();
1785 for (a, b) in &seen {
1786 let (value, evaluated) = eval_logic(and, None, *a, *b);
1787 values.insert(value);
1788 obs.short |= !evaluated;
1789 obs.rhs_true |= evaluated && *b;
1790 obs.rhs_false |= evaluated && !*b;
1791 }
1792 let expected = if values.len() == 1 {
1793 values.first().copied()
1794 } else {
1795 None
1796 };
1797 assert_eq!(obs.constant_value(and), expected, "and={and} seen={seen:?}");
1798 }
1799 }
1800 }
1801
1802 #[test]
1803 fn option_return_plans() {
1804 let plan = |bits| plan_option_return(&ReturnObs::from_bits(bits));
1805 assert_eq!(
1806 plan(0),
1807 ReturnPlan {
1808 unreached: true,
1809 always: None,
1810 run: vec![]
1811 }
1812 );
1813 assert_eq!(
1815 plan(2),
1816 ReturnPlan {
1817 unreached: false,
1818 always: Some(false),
1819 run: vec![]
1820 }
1821 );
1822 for bits in [1, 3] {
1824 assert_eq!(
1825 plan(bits),
1826 ReturnPlan {
1827 unreached: false,
1828 always: None,
1829 run: vec![Mutation::Const(false)]
1830 }
1831 );
1832 assert!(ReturnObs::from_bits(bits).infected_by_const(false));
1833 }
1834 assert!(!ReturnObs::from_bits(2).infected_by_const(false));
1835 }
1836
1837 #[test]
1838 fn arithmetic_plans() {
1839 for bits in 0u8..8 {
1840 let obs = ArithObs::from_bits(bits);
1841 let expected = if bits & 2 != 0 {
1842 ArithPlan::Run
1843 } else if bits & 1 != 0 {
1844 ArithPlan::NeverDiffers
1845 } else if bits & 4 != 0 {
1846 ArithPlan::Opaque
1847 } else {
1848 ArithPlan::Unreached
1849 };
1850 assert_eq!(plan_arith(&obs), expected, "bits={bits}");
1851 }
1852 let mut merged = ArithObs::from_bits(1);
1853 merged.merge(&ArithObs::from_bits(4));
1854 assert_eq!(
1855 merged,
1856 ArithObs {
1857 same: true,
1858 differs: false,
1859 opaque: true,
1860 ..Default::default()
1861 }
1862 );
1863 }
1864
1865 #[test]
1866 fn float_comparison_without_equal_operands() {
1867 let mut obs = Obs::of_f(-20.5);
1869 obs.merge(&Obs::of_f(9.5));
1870 let plan = plan_cmp(CmpOp::Gt, &obs, None);
1871 let gap = plan.gap.expect("a gap");
1872 assert_eq!(
1873 gap,
1874 InputGap::EqualNeverObserved {
1875 flipped: CmpOp::Ge,
1876 float_gap: Some((-20.5, 9.5))
1877 }
1878 );
1879 assert_eq!(
1880 gap.survivors(),
1881 vec![
1882 Mutation::Op(CmpOp::Ge),
1883 Mutation::ShiftF(4.75),
1884 Mutation::ShiftF(-10.25)
1885 ]
1886 );
1887 for op in [CmpOp::Lt, CmpOp::Le, CmpOp::Gt, CmpOp::Ge] {
1890 assert!(!infects(op, Mutation::ShiftF(4.75), &obs));
1891 assert!(!infects(op, Mutation::ShiftF(-10.25), &obs));
1892 assert!(infects(op, Mutation::ShiftF(9.5), &obs));
1893 assert!(infects(op, Mutation::ShiftF(-20.5), &obs));
1894 }
1895 let mut with_eq = obs;
1896 with_eq.merge(&Obs::of_f(0.0));
1897 assert!(infects(CmpOp::Gt, Mutation::ShiftF(4.75), &with_eq));
1898 assert_eq!(plan_cmp(CmpOp::Gt, &with_eq, None).gap, None);
1899 let mut mixed = obs;
1901 mixed.merge(&Obs::of_rel(Rel::Less));
1902 assert_eq!(mixed.float_gap(), None);
1903 assert!(infects(CmpOp::Gt, Mutation::ShiftF(4.75), &mixed));
1904 assert_eq!(Obs::of_d(3).float_gap(), None);
1905 }
1906
1907 #[test]
1908 fn survivors_are_listed_nearest_first() {
1909 let boundary = |min_shift, max_shift| InputGap::Boundary {
1910 min_shift,
1911 max_shift,
1912 missing: vec![0],
1913 same: (0, 0),
1914 };
1915 assert_eq!(
1916 boundary(-9, 9).survivors(),
1917 vec![Mutation::Shift(1), Mutation::Shift(-1), Mutation::Shift(2)]
1918 );
1919 assert_eq!(boundary(-1, 0).survivors(), vec![Mutation::Shift(-1)]);
1920 assert_eq!(
1921 boundary(0, 2).survivors(),
1922 vec![Mutation::Shift(1), Mutation::Shift(2)]
1923 );
1924 assert_eq!(
1925 InputGap::OneSided { always: true }.survivors(),
1926 vec![Mutation::Const(true)]
1927 );
1928 assert_eq!(InputGap::Unreached.survivors(), vec![]);
1929 }
1930
1931 #[test]
1932 fn single_observation_summaries() {
1933 let bits = |o: Obs| (o.lt, o.eq, o.gt, o.un);
1934 assert_eq!(bits(Obs::of_f(-0.5)), (true, false, false, false));
1935 assert_eq!(bits(Obs::of_f(0.0)), (false, true, false, false));
1936 assert_eq!(bits(Obs::of_f(0.5)), (false, false, true, false));
1937 assert_eq!(
1938 (Obs::of_f(-0.5).f_max_neg, Obs::of_f(-0.5).f_min_pos),
1939 (Some(-0.5), None)
1940 );
1941 assert_eq!(
1942 (Obs::of_f(0.5).f_max_neg, Obs::of_f(0.5).f_min_pos),
1943 (None, Some(0.5))
1944 );
1945 assert_eq!(
1946 (Obs::of_f(0.0).f_max_neg, Obs::of_f(0.0).f_min_pos),
1947 (None, None)
1948 );
1949 assert!(Obs::of_f(1.0).unvalued && !Obs::of_f(1.0).unfloat);
1950 assert!(Obs::of_d(1).unfloat && !Obs::of_d(1).unvalued);
1951
1952 assert!(Obs::of_rel(Rel::Unordered).reached());
1954 assert!(!Obs::default().reached());
1955 assert!(!Obs::default().valued());
1957 assert!(Obs::of_d(0).valued());
1958 assert!(!Obs::of_rel(Rel::Equal).valued());
1959
1960 let mut nan = Obs::of_f(-1.0);
1962 nan.merge(&Obs::of_f(1.0));
1963 assert_eq!(nan.float_gap(), Some((-1.0, 1.0)));
1964 nan.un = true;
1965 assert_eq!(nan.float_gap(), None);
1966 assert_eq!(Obs::default().float_gap(), None);
1967 assert_eq!(Obs::of_f(-1.0).float_gap(), None, "one side only");
1968 }
1969
1970 #[test]
1971 fn interval_queries_look_at_the_right_side_of_zero() {
1972 assert!(!obs_of(&[2]).saw_any_in(-3, -1));
1974 assert!(!obs_of(&[-2]).saw_any_in(1, 3));
1975 assert!(obs_of(&[-2]).saw_any_in(-3, -1));
1976 assert!(obs_of(&[2]).saw_any_in(1, 3));
1977 assert!(!obs_of(&[-4]).saw_any_in(-3, 0));
1978 assert!(!obs_of(&[4]).saw_any_in(0, 3));
1979 let mut odd = obs_of(&[3]);
1981 odd.max_neg = Some(5);
1982 assert!(!odd.saw_any_in(0, 2));
1983 let mut odd = obs_of(&[-3]);
1984 odd.min_pos = Some(-5);
1985 assert!(!odd.saw_any_in(-2, 0));
1986 }
1987
1988 #[test]
1989 fn empty_interval_has_no_observations() {
1990 assert!(!obs_of(&[-1, 0, 1]).saw_any_in(1, 0));
1991 assert!(obs_of(&[0]).saw_any_in(0, 0));
1992 }
1993
1994 #[test]
1995 fn unvalued_ordering_without_equal_operands() {
1996 let mut obs = Obs::of_rel(Rel::Less);
1997 obs.merge(&Obs::of_rel(Rel::Greater));
1998 let plan = plan_cmp(CmpOp::Lt, &obs, None);
1999 assert_eq!(
2000 plan.gap,
2001 Some(InputGap::EqualNeverObserved {
2002 flipped: CmpOp::Le,
2003 float_gap: None
2004 })
2005 );
2006 assert_eq!(
2008 plan.run,
2009 vec![
2010 Mutation::Negate,
2011 Mutation::Op(CmpOp::Eq),
2012 Mutation::Op(CmpOp::Gt)
2013 ]
2014 );
2015
2016 obs.merge(&Obs::of_rel(Rel::Equal));
2017 let plan = plan_cmp(CmpOp::Lt, &obs, None);
2018 assert_eq!(plan.gap, None);
2019 assert_eq!(
2020 plan.run,
2021 vec![
2022 Mutation::Op(CmpOp::Le),
2023 Mutation::Negate,
2024 Mutation::Op(CmpOp::Eq),
2025 Mutation::Op(CmpOp::Gt)
2026 ]
2027 );
2028 }
2029
2030 #[test]
2031 fn equality_with_both_outcomes() {
2032 let plan = plan_cmp(CmpOp::Eq, &obs_of(&[0, 7]), None);
2033 assert_eq!(
2034 plan,
2035 CmpPlan {
2036 cannot_differ: false,
2037 gap: None,
2038 run: vec![Mutation::Negate]
2039 }
2040 );
2041 }
2042
2043 #[test]
2044 fn unordered_counts_as_not_equal() {
2045 let obs = Obs::of_rel(Rel::Unordered);
2046 assert!(infects(CmpOp::Ne, Mutation::Const(false), &obs));
2047 assert!(!infects(CmpOp::Lt, Mutation::Op(CmpOp::Ge), &obs));
2048 assert!(infects(CmpOp::Lt, Mutation::Op(CmpOp::Ne), &obs));
2049 }
2050
2051 #[test]
2052 fn merge_is_a_semilattice() {
2053 let samples: Vec<Obs> = [-3, -1, 0, 1, 4]
2054 .into_iter()
2055 .map(Obs::of_d)
2056 .chain([
2057 Obs::of_rel(Rel::Unordered),
2058 Obs::of_rel(Rel::Less),
2059 Obs::default(),
2060 ])
2061 .collect();
2062 for a in &samples {
2063 let mut aa = *a;
2064 aa.merge(a);
2065 assert_eq!(aa, *a, "idempotent");
2066 for b in &samples {
2067 let (mut ab, mut ba) = (*a, *b);
2068 ab.merge(b);
2069 ba.merge(a);
2070 assert_eq!(ab, ba, "commutative");
2071 for c in &samples {
2072 let mut ab_c = ab;
2073 ab_c.merge(c);
2074 let mut bc = *b;
2075 bc.merge(c);
2076 let mut a_bc = *a;
2077 a_bc.merge(&bc);
2078 assert_eq!(ab_c, a_bc, "associative");
2079 }
2080 }
2081 }
2082 }
2083
2084 #[test]
2085 fn mutation_wire_format() {
2086 assert_eq!(Mutation::Const(true).encode(), "c1");
2087 assert_eq!(Mutation::Const(false).encode(), "c0");
2088 assert_eq!(Mutation::Negate.encode(), "neg");
2089 assert_eq!(Mutation::Op(CmpOp::Ge).encode(), "op3");
2090 assert_eq!(Mutation::Shift(-2).encode(), "sh-2");
2091 assert_eq!(Mutation::Delete.encode(), "del");
2092 assert_eq!(Mutation::ShiftF(4.75).encode(), "shf4.75");
2093 assert_eq!(Mutation::ShiftF(-10.0).encode(), "shf-10.0");
2094 assert_eq!(Mutation::LogicSwap.encode(), "lsw");
2095 assert_eq!(Mutation::LogicDropRhs.encode(), "ldr");
2096 assert_eq!(Mutation::LogicDropLhs.encode(), "ldl");
2097 assert_eq!(Mutation::ArithSwap.encode(), "asw");
2098 assert_eq!(Mutation::DropNot.encode(), "unn");
2099 assert_eq!(Mutation::RangeEnd.encode(), "rng");
2100 assert_eq!(Mutation::MinMax.encode(), "mmx");
2101 assert_eq!(Mutation::OtherEnd.encode(), "end");
2102 }
2103
2104 fn candidate_mutations(op: CmpOp) -> Vec<Mutation> {
2105 let mut ms = vec![
2106 Mutation::Const(true),
2107 Mutation::Const(false),
2108 Mutation::Negate,
2109 ];
2110 ms.extend(CmpOp::ALL.into_iter().map(Mutation::Op));
2111 if op.is_ordering() {
2112 ms.extend((-5..=5).filter(|s| *s != 0).map(Mutation::Shift));
2113 }
2114 ms
2115 }
2116
2117 fn check_plan(op: CmpOp, ds: &[i128], domain: Option<(i128, i128)>) {
2119 let obs = obs_of(ds);
2120 let ctx = format!("{op:?} ds={ds:?} domain={domain:?}");
2121 let (dom_lo, dom_hi) = domain.unwrap_or((-50, 50));
2122 let differs_somewhere =
2123 |m: Mutation| (dom_lo..=dom_hi).any(|d| op.eval_d(d) != eval_mutated_d(op, m, d));
2124
2125 for m in candidate_mutations(op) {
2126 assert_eq!(
2127 infects(op, m, &obs),
2128 brute_infects(op, m, ds),
2129 "infects {m:?} {ctx}"
2130 );
2131 }
2132
2133 let plan = plan_cmp(op, &obs, domain);
2134 for m in &plan.run {
2135 assert!(
2136 brute_infects(op, *m, ds),
2137 "run {m:?} must be infected: {ctx}"
2138 );
2139 assert!(differs_somewhere(*m), "run {m:?} is equivalent: {ctx}");
2140 }
2141 match &plan.gap {
2142 None => {}
2143 Some(InputGap::Unreached) => assert!(ds.is_empty(), "{ctx}"),
2144 Some(InputGap::OneSided { always }) => {
2145 assert!(ds.iter().all(|d| op.eval_d(*d) == *always), "{ctx}");
2146 assert!(differs_somewhere(Mutation::Const(*always)), "{ctx}");
2147 }
2148 Some(InputGap::Boundary {
2149 min_shift,
2150 max_shift,
2151 missing,
2152 same: _,
2153 }) => {
2154 assert!(*min_shift <= 0 && 0 <= *max_shift, "{ctx}");
2155 for s in (*min_shift..=*max_shift).filter(|s| *s != 0) {
2156 let m = Mutation::Shift(s);
2157 assert!(
2158 !brute_infects(op, m, ds),
2159 "survivor {m:?} is infected: {ctx}"
2160 );
2161 assert!(differs_somewhere(m), "survivor {m:?} is equivalent: {ctx}");
2162 }
2163 if domain.is_none() {
2164 for s in [min_shift - 1, max_shift + 1] {
2166 assert!(
2167 brute_infects(op, Mutation::Shift(s), ds),
2168 "range not maximal: {ctx}"
2169 );
2170 }
2171 }
2172 assert!(!missing.is_empty(), "{ctx}");
2173 for w in missing {
2174 assert!(!ds.contains(w), "missing value {w} was observed: {ctx}");
2175 assert!(
2176 dom_lo <= *w && *w <= dom_hi,
2177 "missing value {w} out of domain: {ctx}"
2178 );
2179 let s = if op.eval_d(*w) != eval_mutated_d(op, Mutation::Shift(1), *w) {
2180 1
2181 } else {
2182 -1
2183 };
2184 assert!(
2185 op.eval_d(*w) != eval_mutated_d(op, Mutation::Shift(s), *w),
2186 "missing value {w} separates nothing: {ctx}"
2187 );
2188 assert!(*min_shift <= s && s <= *max_shift, "{ctx}");
2189 }
2190 }
2191 Some(InputGap::EqualNeverObserved { .. }) => panic!("valued site: {ctx}"),
2192 }
2193 if let Some(m) = plan.gap.as_ref().and_then(InputGap::representative) {
2194 assert!(
2195 !brute_infects(op, m, ds),
2196 "representative {m:?} is infected: {ctx}"
2197 );
2198 }
2199
2200 if op.is_ordering() && ds.iter().any(|d| op.eval_d(*d)) && ds.iter().any(|d| !op.eval_d(*d))
2202 {
2203 for s in [-1, 1] {
2204 let m = Mutation::Shift(s);
2205 let survivor = matches!(&plan.gap, Some(InputGap::Boundary { min_shift, max_shift, .. })
2206 if *min_shift <= s && s <= *max_shift);
2207 let ran = plan.run.contains(&m);
2208 assert!(
2209 survivor ^ ran || !differs_somewhere(m),
2210 "Shift({s}) unaccounted for: survivor={survivor} ran={ran} {ctx}"
2211 );
2212 }
2213 }
2214 }
2215
2216 #[test]
2217 fn exhaustive_small_observation_sets() {
2218 let universe: Vec<i128> = (-3..=3).collect();
2219 for op in CmpOp::ALL {
2220 for mask in 0u32..(1 << universe.len()) {
2221 let ds: Vec<i128> = universe
2222 .iter()
2223 .enumerate()
2224 .filter_map(|(i, d)| (mask & (1 << i) != 0).then_some(*d))
2225 .collect();
2226 check_plan(op, &ds, None);
2227 for (lo, hi) in [(0, 10), (-10, 0), (-2, 2), (1, 9), (-9, -1)] {
2229 if ds.iter().all(|d| lo <= *d && *d <= hi) {
2230 check_plan(op, &ds, Some((lo, hi)));
2231 }
2232 }
2233 }
2234 }
2235 }
2236
2237 #[test]
2239 fn pict_table() {
2240 let table = include_str!("../tests/data/planner.pict.tsv");
2241 let mut rows = 0;
2242 for line in table.lines().skip(1) {
2243 let cols: Vec<&str> = line.split('\t').collect();
2244 let [op, below, equal, above, valued, literal, domain] = cols[..] else {
2245 panic!("bad row: {line}");
2246 };
2247 let op = match op {
2248 "lt" => CmpOp::Lt,
2249 "le" => CmpOp::Le,
2250 "gt" => CmpOp::Gt,
2251 "ge" => CmpOp::Ge,
2252 "eq" => CmpOp::Eq,
2253 "ne" => CmpOp::Ne,
2254 other => panic!("op {other}"),
2255 };
2256 let mut ds: Vec<i128> = vec![];
2257 match below {
2258 "none" => {}
2259 "adjacent" => ds.extend([-1, -4]),
2260 "far" => ds.extend([-3, -6]),
2261 other => panic!("below {other}"),
2262 }
2263 if equal == "yes" {
2264 ds.push(0);
2265 }
2266 match above {
2267 "none" => {}
2268 "adjacent" => ds.extend([1, 5]),
2269 "far" => ds.extend([3, 7]),
2270 other => panic!("above {other}"),
2271 }
2272 rows += 1;
2273 if valued == "no" {
2274 let mut obs = Obs::default();
2275 for d in &ds {
2276 obs.merge(&Obs::of_rel(rel_of(*d)));
2277 }
2278 check_unvalued(op, &obs, &ds, line);
2279 continue;
2280 }
2281 let domain = match (literal, domain) {
2284 ("none", _) => None,
2285 (_, "wide") => Some((-40, 40)),
2286 ("rhs", "tight_low") | ("lhs", "tight_high") => Some((0, 40)),
2287 ("rhs", "tight_high") | ("lhs", "tight_low") => Some((-40, 0)),
2288 other => panic!("domain {other:?}"),
2289 };
2290 check_plan(op, &ds, domain);
2291 }
2292 assert_eq!(rows, table.lines().count() - 1);
2293 assert!(rows > 50, "the PICT table looks truncated: {rows} rows");
2294 }
2295
2296 fn check_unvalued(op: CmpOp, obs: &Obs, ds: &[i128], ctx: &str) {
2297 let plan = plan_cmp(op, obs, None);
2298 for m in [
2299 Mutation::Const(true),
2300 Mutation::Const(false),
2301 Mutation::Negate,
2302 ]
2303 .into_iter()
2304 .chain(CmpOp::ALL.into_iter().map(Mutation::Op))
2305 {
2306 assert_eq!(
2307 infects(op, m, obs),
2308 brute_infects(op, m, ds),
2309 "infects {m:?} {ctx}"
2310 );
2311 }
2312 for m in &plan.run {
2313 assert!(
2314 brute_infects(op, *m, ds),
2315 "run {m:?} must be infected: {ctx}"
2316 );
2317 }
2318 if let Some(m) = plan.gap.as_ref().and_then(InputGap::representative) {
2319 assert!(
2320 !brute_infects(op, m, ds),
2321 "representative {m:?} is infected: {ctx}"
2322 );
2323 }
2324 match &plan.gap {
2325 Some(InputGap::Boundary { .. }) => panic!("unvalued site cannot shift: {ctx}"),
2326 Some(InputGap::EqualNeverObserved { flipped, .. }) => {
2327 assert!(!ds.contains(&0), "{ctx}");
2328 assert_eq!(op.flip_eq(), Some(*flipped), "{ctx}");
2329 }
2330 Some(InputGap::Unreached) => assert!(ds.is_empty(), "{ctx}"),
2331 _ => {}
2332 }
2333 if op.is_ordering() && obs.reached() {
2335 assert!(infects(op, Mutation::Shift(1), obs), "{ctx}");
2336 }
2337 }
2338}