hfs 1.0.0

Hereditarily finite sets.
Documentation
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
//! General library tests.
//!
//! We maintain a [`Suite`] of generic sets for which we test our operations. Unfortunately, due to
//! how various tests' results have to be hardcoded, it's a bit difficult to add new sets to it.

#![cfg(test)]

use crate::prelude::*;

/// Creates analogous tests for [`Set`] and [`Mset`].
macro_rules! test {
    ($($name: ident),*) => {
        $(
            concat_idents::concat_idents!(fn_name = mset, $name {
                #[test]
                #[doc = concat!("Test [`Mset::", stringify!($name), "`].")]
                fn fn_name() {
                    Mset::$name();
                }
            });

            concat_idents::concat_idents!(fn_name = set, $name {
                #[test]
                #[doc = concat!("Test [`Set::", stringify!($name), "`].")]
                fn fn_name() {
                    Set::$name();
                }
            });
        )*
    };
}

/// Asserts that two booleans are equal. If they aren't, a string equal to either a whitespace or `"
/// not "` is passed to the formatting string, depending on the expected result.
///
/// ```
/// # let (expect, cmp) = (true, true);
/// // If `expect` and `!cmp`, this fails with "cmp is not true".
/// // If `!expect` and `cmp`, this fails with "cmp is true".
/// assert_cmp!(expect, cmp, "cmp is{}true");
/// ```
macro_rules! assert_beq {
    ($expect: expr, $cmp: expr, $msg: literal) => {
        let expect = $expect;
        if expect != $cmp {
            let not = if expect { " not " } else { " " };
            panic!($msg, not);
        }
    };
}

/// Our suite of sets to perform tests on.
trait Suite: SetTrait {
    /// A multitude of general sets for general-purpose testing.
    ///
    /// Both the list and each constituent set should be normalized, i.e. in decreasing
    /// lexicographic order.
    const SUITE: &'static [&'static str];

    /// Hardcoded indices of pairs of sets containing each other.
    const MEM: &'static [(usize, usize)];

    /// Parses string or panics.
    fn parse(str: &str) -> Self {
        str.parse().expect("could not parse string")
    }

    /// Normalize string for a set.
    #[must_use]
    fn normalize(str: &str) -> String {
        Self::parse(str).to_string()
    }

    /// Verify round-trip conversion between a set and a string.
    fn roundtrip(&self, str: &str) {
        assert_eq!(
            self,
            &Self::parse(str),
            "roundtrip fail: {str} not mapped to {self}"
        );
        assert_eq!(
            self.to_string(),
            str,
            "roundtrip fail: {self} not mapped to {str}"
        );
    }

    /// Our [`SUITE`](Suite::SUITE) as `(usize, &str, Self)` tuples.
    fn suite() -> impl Iterator<Item = (usize, &'static str, Self)> {
        Self::SUITE
            .iter()
            .enumerate()
            .map(|(i, &str)| (i, str, Self::parse(str)))
    }

    /// Test that our [`SUITE`](Suite::SUITE) is well-formatted.
    fn _suite() {
        for j in 1..Self::SUITE.len() {
            let i = j - 1;
            let fst = &Self::SUITE[i];
            let snd = &Self::SUITE[j];
            assert!(
                fst > snd,
                "suite fail at {i}, {j}: {fst} not greater than {snd}",
            );
        }

        for (i, str) in Self::SUITE.iter().enumerate() {
            assert_eq!(
                str,
                &Self::normalize(str),
                "suite fail at {i}: set {str} is not normalized"
            );
        }
    }

    /// Test [`SetTrait::empty`].
    fn _empty() {
        Self::empty().roundtrip("{}");
    }

    /// Test [`SetTrait::singleton`].
    fn _singleton() {
        for (_, str, set) in Self::suite() {
            set.singleton().roundtrip(&format!("{{{str}}}"));
        }
    }

    /// Test [`SetTrait::pair`].
    fn _pair() {
        for (i, str_1, set_1) in Self::suite() {
            for (_, str_2, set_2) in Self::suite().skip(i + 1) {
                set_1
                    .clone()
                    .pair(set_2.clone())
                    .roundtrip(&format!("{{{str_1}, {str_2}}}"));
            }
        }
    }

    /// Test [`PartialEq::eq`].
    fn _eq() {
        for (i, _, set_1) in Self::suite() {
            for (j, _, set_2) in Self::suite() {
                assert_beq!(
                    i == j,
                    set_1 == set_2,
                    "set equality fail at {i}, {j}: {set_1}{}equal to {set_2}"
                );

                assert_beq!(
                    i == j,
                    set_1.partial_cmp(&set_2) == Some(Ordering::Equal),
                    "set equality fail at {i}, {j}: {set_1}{}equal to {set_2}"
                );
            }
        }
    }

    /// Test [`SetTrait::subset`].
    fn _subset() {
        for (i, _, set_1) in Self::suite() {
            for (j, _, set_2) in Self::suite() {
                let subset = set_1.iter().all(|s| set_1.count(s) <= set_2.count(s));

                assert_beq!(
                    subset,
                    set_1 <= set_2,
                    "set equality fail at {i}, {j}: {set_1}{}a subset of {set_2}"
                );

                assert_beq!(
                    subset,
                    set_1.partial_cmp(&set_2).is_some_and(Ordering::is_le),
                    "set equality fail at {i}, {j}: {set_1}{}a subset of {set_2}"
                );
            }
        }
    }

    /// Test [`SetTrait::contains`].
    fn _contains() {
        for (i, _, set_1) in Self::suite() {
            for (j, _, set_2) in Self::suite() {
                let exp = Self::MEM.contains(&(i, j));
                assert_beq!(
                    exp,
                    set_2.contains(&set_1),
                    "set membership fail at {i}, {j}: {set_1}{}a member of {set_2}"
                );
            }
        }
    }

    /// Test [`SetTrait::nat`].
    fn _nat() {
        let mut outputs = Vec::<String>::new();
        for n in 0..5 {
            // Build naturals manually.
            let mut str = String::from('{');
            let mut iter = outputs.iter();
            if let Some(fst) = iter.next() {
                str.push_str(fst);
            }
            for set in iter {
                str.push_str(", ");
                str.push_str(set);
            }
            str.push('}');

            Self::nat(n).roundtrip(&str);
            outputs.push(str);
        }
    }

    /// Test [`SetTrait::sum`].
    fn _sum();

    /// Test [`SetTrait::union`].
    fn _union() {
        for (i, _, set_1) in Self::suite() {
            for (j, _, set_2) in Self::suite() {
                let union = set_1.clone().union(set_2.clone());
                for set in [&set_1, &set_2] {
                    assert!(
                        set.subset(&union),
                        "union fail at {i}, {j}: {set} not a subset of {union}"
                    );
                }
            }
        }
    }

    /// Test [`SetTrait::inter`].
    fn _inter() {
        for (i, _, set_1) in Self::suite() {
            for (j, _, set_2) in Self::suite() {
                let inter = set_1.clone().inter(set_2.clone());
                for set in [&set_1, &set_2] {
                    assert!(
                        inter.subset(set),
                        "intersection fail at {i}, {j}: {inter} not a subset of {set}"
                    );
                }
            }
        }
    }

    /// Test [`SetTrait::powerset`].
    fn _powerset() {
        for (i, _, set) in Self::suite() {
            for subset in Self::powerset(set.clone()) {
                assert!(
                    subset.subset(&set),
                    "powerset fail at {i}: {subset} not a subset of {set}"
                );
            }
        }
    }

    /// Test [`SetTrait::choose`].
    fn _choose() {
        for (i, _, set) in Self::suite() {
            if !set.is_empty() {
                let choose_1 = set.choose().expect("could not choose set");
                assert!(
                    set.contains(choose_1),
                    "choice fail at {i}: {choose_1} not an element of {set}"
                );

                let choose_2 = set.choose_uniq().expect("could not choose set");
                assert!(
                    set.contains(choose_2),
                    "unique choice fail at {i}: {choose_2} not an element of {set}"
                );
            }
        }
    }

    /// Test [`SetTrait::disjoint`].
    fn _disjoint() {
        for (i, _, set_1) in Self::suite() {
            for (j, _, set_2) in Self::suite() {
                assert_beq!(
                    set_1.clone().inter(set_2.clone()).is_empty(),
                    set_1.disjoint(&set_2),
                    "disjoint fail at {i}, {j}: {set_1} is{}disjoint with {set_2}"
                );
            }
        }
    }

    /// Test [`SetTrait::disjoint_set`].
    fn _disjoint_set() {
        for (i, _, set) in Self::suite() {
            let expect = set
                .clone()
                .big_inter()
                .map(|set| set.is_empty())
                .unwrap_or_default();

            assert_beq!(
                expect,
                set.disjoint_set(),
                "disjoint fail at {i}: {set} is{}disjoint"
            );
        }
    }

    /// Test [`SetTrait::disjoint_pairwise_set`].
    fn _disjoint_pairwise_set() {
        for (i, _, set) in Self::suite() {
            let expect =
                set.iter().map(|c| c.card()).sum::<usize>() == set.clone().big_union().card();

            assert_beq!(
                expect,
                set.disjoint_pairwise_set(),
                "disjoint fail at {i}: {set} is{}pairwise disjoint"
            );
        }
    }
}

impl Suite for Mset {
    const SUITE: &'static [&'static str] = &[
        "{}",
        "{{}}",
        "{{}, {}}",
        "{{}, {{}}}",
        "{{}, {{}}, {{}, {{}}}}",
        "{{{}, {}}, {{}, {}}}",
        "{{{{{}}}}}",
    ];

    #[rustfmt::skip]
    const MEM: &'static [(usize, usize)] = &[
        (0, 1), (0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (2, 5), (3, 4)
    ];

    fn _sum() {
        // Remove initial parentheses.
        let suite: Vec<_> = Self::suite()
            .map(|(_, str, set)| (&str[1..(str.len() - 1)], set))
            .collect();

        for (str_1, set_1) in suite.iter() {
            for (str_2, set_2) in suite.iter() {
                set_1
                    .clone()
                    .sum(set_2.clone())
                    .roundtrip(&Self::normalize(&format!("{{{str_1}, {str_2}}}")));
            }
        }
    }
}

impl Suite for Set {
    const SUITE: &'static [&'static str] = &[
        "{}",
        "{{}}",
        "{{}, {{}}}",
        "{{}, {{}}, {{}, {{}}}}",
        "{{}, {{}}, {{{}}}}",
        "{{{}, {{}}}, {{}, {{{}}}}}",
        "{{{{{}}}}}",
    ];

    #[rustfmt::skip]
    const MEM: &'static [(usize, usize)] = &[
        (0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 5)
    ];

    fn _sum() {
        Self::_union();
    }
}

// Run all the suite functions for [`Mset`] and [`Set`].
#[rustfmt::skip]
test!(
    _suite, _empty, _singleton, _pair, _eq, _subset, _contains, _nat, _sum, _union, _inter,
    _powerset, _choose, _disjoint, _disjoint_set, _disjoint_pairwise_set
);

/// Test [`Mset::is_set`].
#[test]
fn mset_is_set() {
    /// Whether each multiset in the suite is a set.
    const IS_SET: &[bool] = &[true, true, false, true, true, false, true];

    for (i, _, set) in Mset::suite() {
        assert_beq!(
            IS_SET[i],
            set.is_set(),
            "is_set fail at {i}: multiset is{}a set"
        );
    }
}

/// Test that each [`Set`] in its suite is a set.
#[test]
fn set_is_set() {
    for (i, _, set) in Set::suite() {
        assert!(
            set.mset().is_set(),
            "is_set fail at {i}: multiset is not a set"
        );
    }
}

/// Test [`Mset::flatten`].
#[test]
fn mset_flatten() {
    /// Flattened suite.
    const FLATTEN: &'static [&'static str] = &[
        "{}",
        "{{}}",
        "{{}}",
        "{{}, {{}}}",
        "{{}, {{}}, {{}, {{}}}}",
        "{{{}}}",
        "{{{{{}}}}}",
    ];

    for (i, _, set) in Mset::suite() {
        set.flatten().roundtrip(FLATTEN[i])
    }
}

/// Test [`Set::kpair`].
#[test]
fn set_kpair() {
    for (i, _, set_1) in Set::suite() {
        for (j, _, set_2) in Set::suite() {
            let pair = Set::kpair(set_1.clone(), set_2.clone());
            assert_eq!(
                pair.ksplit().expect("could not split pair").pair(),
                (&set_1, &set_2),
                "kpair fail at {i}, {j}: pair not split correctly"
            );

            assert_eq!(
                pair.into_ksplit()
                    .expect("could not split pair")
                    .into_pair(),
                (set_1.clone(), set_2),
                "kpair fail at {i}, {j}: pair not split correctly"
            );
        }
    }
}