aboutsummaryrefslogtreecommitdiff
path: root/src/tests/contains.rs
blob: 12428ddcb09c7612936f0575c212999c599fa8ee (plain)
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
use super::*;

use crate::Flags;

#[test]
fn cases() {
    case(
        TestFlags::empty(),
        &[
            (TestFlags::empty(), true),
            (TestFlags::A, false),
            (TestFlags::B, false),
            (TestFlags::C, false),
            (TestFlags::from_bits_retain(1 << 3), false),
        ],
        TestFlags::contains,
    );

    case(
        TestFlags::A,
        &[
            (TestFlags::empty(), true),
            (TestFlags::A, true),
            (TestFlags::B, false),
            (TestFlags::C, false),
            (TestFlags::ABC, false),
            (TestFlags::from_bits_retain(1 << 3), false),
            (TestFlags::from_bits_retain(1 | (1 << 3)), false),
        ],
        TestFlags::contains,
    );

    case(
        TestFlags::ABC,
        &[
            (TestFlags::empty(), true),
            (TestFlags::A, true),
            (TestFlags::B, true),
            (TestFlags::C, true),
            (TestFlags::ABC, true),
            (TestFlags::from_bits_retain(1 << 3), false),
        ],
        TestFlags::contains,
    );

    case(
        TestFlags::from_bits_retain(1 << 3),
        &[
            (TestFlags::empty(), true),
            (TestFlags::A, false),
            (TestFlags::B, false),
            (TestFlags::C, false),
            (TestFlags::from_bits_retain(1 << 3), true),
        ],
        TestFlags::contains,
    );

    case(
        TestZero::ZERO,
        &[(TestZero::ZERO, true)],
        TestZero::contains,
    );

    case(
        TestOverlapping::AB,
        &[
            (TestOverlapping::AB, true),
            (TestOverlapping::BC, false),
            (TestOverlapping::from_bits_retain(1 << 1), true),
        ],
        TestOverlapping::contains,
    );

    case(
        TestExternal::all(),
        &[
            (TestExternal::A, true),
            (TestExternal::B, true),
            (TestExternal::C, true),
            (TestExternal::from_bits_retain(1 << 5 | 1 << 7), true),
        ],
        TestExternal::contains,
    );
}

#[track_caller]
fn case<T: Flags + std::fmt::Debug + Copy>(
    value: T,
    inputs: &[(T, bool)],
    mut inherent: impl FnMut(&T, T) -> bool,
) {
    for (input, expected) in inputs {
        assert_eq!(
            *expected,
            inherent(&value, *input),
            "{:?}.contains({:?})",
            value,
            input
        );
        assert_eq!(
            *expected,
            Flags::contains(&value, *input),
            "Flags::contains({:?}, {:?})",
            value,
            input
        );
    }
}