vfs/
test_utils.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Utilities used by tests in both file and directory modules.
6
7#[cfg(test)]
8pub(crate) mod assertions;
9
10/// Returns a list of flag combinations to test. Returns a vector of the aggregate of
11/// every constant flag and every combination of variable flags. For example, calling
12/// build_flag_combinations(100, 011) would return [100, 110, 101, 111] (in binary),
13/// whereas build_flag_combinations(0, 011) would return [000, 001, 010, 011].
14pub fn build_flag_combinations<T: bitflags::Flags + Copy>(
15    constant_flags: T,
16    variable_flags: T,
17) -> Vec<T> {
18    let mut vec = vec![constant_flags];
19    for flag in variable_flags.iter() {
20        for i in 0..vec.len() {
21            vec.push(vec[i].union(flag));
22        }
23    }
24    vec
25}
26
27#[cfg(test)]
28mod tests {
29    use super::build_flag_combinations;
30
31    bitflags::bitflags! {
32        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
33        pub struct TestFlags: u32 {
34            const A = 1<<0;
35            const B = 1<<1;
36            const C = 1<<2;
37        }
38    }
39
40    #[test]
41    fn test_build_flag_combinations() {
42        let constant_flags = TestFlags::C;
43        let variable_flags = TestFlags::A | TestFlags::B;
44        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
45        let expected_result = [
46            TestFlags::C,
47            TestFlags::A | TestFlags::C,
48            TestFlags::B | TestFlags::C,
49            TestFlags::A | TestFlags::B | TestFlags::C,
50        ];
51        assert_eq!(generated_combinations, expected_result);
52    }
53
54    #[test]
55    fn test_build_flag_combinations_with_empty_constant_flags() {
56        let constant_flags = TestFlags::empty();
57        let variable_flags = TestFlags::A | TestFlags::B;
58        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
59        let expected_result =
60            [TestFlags::empty(), TestFlags::A, TestFlags::B, TestFlags::A | TestFlags::B];
61        assert_eq!(generated_combinations, expected_result);
62    }
63
64    #[test]
65    fn test_build_flag_combinations_with_empty_variable_flags() {
66        let constant_flags = TestFlags::A | TestFlags::B;
67        let variable_flags = TestFlags::empty();
68        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
69        let expected_result = [constant_flags];
70        assert_eq!(generated_combinations, expected_result);
71    }
72
73    #[test]
74    fn test_build_flag_combinations_with_empty_flags() {
75        let constant_flags = TestFlags::empty();
76        let variable_flags = TestFlags::empty();
77        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
78        let expected_result = [constant_flags];
79        assert_eq!(generated_combinations, expected_result);
80    }
81}