vfs/
test_utils.rs

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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Utilities used by tests in both file and directory modules.

pub mod assertions;
pub mod node;
pub mod run;

pub use run::{run_client, run_server_client, test_client, test_server_client, TestController};

/// Returns a list of flag combinations to test. Returns a vector of the aggregate of
/// every constant flag and every combination of variable flags. For example, calling
/// build_flag_combinations(100, 011) would return [100, 110, 101, 111] (in binary),
/// whereas build_flag_combinations(0, 011) would return [000, 001, 010, 011].
pub fn build_flag_combinations<T: bitflags::Flags + Copy>(
    constant_flags: T,
    variable_flags: T,
) -> Vec<T> {
    let mut vec = vec![constant_flags];
    for flag in variable_flags.iter() {
        for i in 0..vec.len() {
            vec.push(vec[i].union(flag));
        }
    }
    vec
}

#[cfg(test)]
mod tests {
    use super::build_flag_combinations;

    bitflags::bitflags! {
        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct TestFlags: u32 {
            const A = 1<<0;
            const B = 1<<1;
            const C = 1<<2;
        }
    }

    #[test]
    fn test_build_flag_combinations() {
        let constant_flags = TestFlags::C;
        let variable_flags = TestFlags::A | TestFlags::B;
        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
        let expected_result = [
            TestFlags::C,
            TestFlags::A | TestFlags::C,
            TestFlags::B | TestFlags::C,
            TestFlags::A | TestFlags::B | TestFlags::C,
        ];
        assert_eq!(generated_combinations, expected_result);
    }

    #[test]
    fn test_build_flag_combinations_with_empty_constant_flags() {
        let constant_flags = TestFlags::empty();
        let variable_flags = TestFlags::A | TestFlags::B;
        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
        let expected_result =
            [TestFlags::empty(), TestFlags::A, TestFlags::B, TestFlags::A | TestFlags::B];
        assert_eq!(generated_combinations, expected_result);
    }

    #[test]
    fn test_build_flag_combinations_with_empty_variable_flags() {
        let constant_flags = TestFlags::A | TestFlags::B;
        let variable_flags = TestFlags::empty();
        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
        let expected_result = [constant_flags];
        assert_eq!(generated_combinations, expected_result);
    }

    #[test]
    fn test_build_flag_combinations_with_empty_flags() {
        let constant_flags = TestFlags::empty();
        let variable_flags = TestFlags::empty();
        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
        let expected_result = [constant_flags];
        assert_eq!(generated_combinations, expected_result);
    }
}