Debian Codegraph @

src/obey.rs

raw symbols SCIP
1 #![allow(dead_code)]
2 use core::fmt::Debug;
3 use std::collections::BTreeSet;
4
5 /// A support trait for testing any kind of collection.
6 ///
7 /// Almost anything can be viewed as a collection. E.g. an integer can be viewed as a collection of bit offsets at which it has
8 /// a boolean value.
9 pub trait TestSamples<K, V> {
10 /// produces "interesting" sample points to test a property for.
11 fn samples(&self, res: &mut BTreeSet<K>);
12
13 /// gets the value of the collection at position k
14 fn at(&self, k: K) -> V;
15 }
16
17 pub fn unary_element_test<C, K, V>(a: &C, r: C, op: impl Fn(V) -> V) -> bool
18 where
19 C: TestSamples<K, V> + Debug + Clone,
20 K: Ord + Clone + Debug,
21 V: Eq + Debug,
22 {
23 let mut s: BTreeSet<K> = BTreeSet::new();
24 a.samples(&mut s);
25 r.samples(&mut s);
26 s.into_iter().all(|key| {
27 let value = a.at(key.clone());
28 let actual = op(value);
29 let expected = r.at(key.clone());
30 if expected != actual {
31 println!(
32 "expected!=actual at: {:?}. {:?}!={:?}",
33 key, expected, actual
34 );
35 println!("a: {:?}", a);
36 println!("r: {:?}", r);
37 false
38 } else {
39 true
40 }
41 })
42 }
43
44 pub fn binary_element_test<C, K, V>(a: &C, b: &C, r: C, op: impl Fn(V, V) -> V) -> bool
45 where
46 C: TestSamples<K, V> + Debug,
47 K: Ord + Clone + Debug,
48 V: Eq + Debug,
49 {
50 let mut s: BTreeSet<K> = BTreeSet::new();
51 a.samples(&mut s);
52 b.samples(&mut s);
53 r.samples(&mut s);
54 s.into_iter().all(|key| {
55 let a_value = a.at(key.clone());
56 let b_value = b.at(key.clone());
57 let actual = op(a_value, b_value);
58 let expected = r.at(key.clone());
59 if expected != actual {
60 println!(
61 "expected!=actual at: {:?}. {:?}!={:?}",
62 key, expected, actual
63 );
64 println!("a: {:?}", a);
65 println!("b: {:?}", b);
66 println!("r: {:?}", r);
67 false
68 } else {
69 true
70 }
71 })
72 }
73
74 pub fn binary_property_test<C, K, V>(a: &C, b: &C, r: bool, op: impl Fn(V, V) -> bool) -> bool
75 where
76 C: TestSamples<K, V> + Debug,
77 K: Ord + Clone + Debug,
78 V: Eq + Debug,
79 {
80 let mut s: BTreeSet<K> = BTreeSet::new();
81 a.samples(&mut s);
82 b.samples(&mut s);
83 if r {
84 s.iter().cloned().all(|e| op(a.at(e.clone()), b.at(e)))
85 } else {
86 s.iter().cloned().any(|e| !op(a.at(e.clone()), b.at(e)))
87 }
88 }