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
7pub mod assertions;
8pub mod node;
9pub mod run;
10
11pub use run::{run_client, run_server_client, test_client, test_server_client, TestController};
12
13/// Returns a list of flag combinations to test. Returns a vector of the aggregate of
14/// every constant flag and every combination of variable flags. For example, calling
15/// build_flag_combinations(100, 011) would return [100, 110, 101, 111] (in binary),
16/// whereas build_flag_combinations(0, 011) would return [000, 001, 010, 011].
17pub fn build_flag_combinations<T: bitflags::Flags + Copy>(
18    constant_flags: T,
19    variable_flags: T,
20) -> Vec<T> {
21    let mut vec = vec![constant_flags];
22    for flag in variable_flags.iter() {
23        for i in 0..vec.len() {
24            vec.push(vec[i].union(flag));
25        }
26    }
27    vec
28}
29
30#[cfg(test)]
31mod tests {
32    use super::build_flag_combinations;
33
34    bitflags::bitflags! {
35        #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
36        pub struct TestFlags: u32 {
37            const A = 1<<0;
38            const B = 1<<1;
39            const C = 1<<2;
40        }
41    }
42
43    #[test]
44    fn test_build_flag_combinations() {
45        let constant_flags = TestFlags::C;
46        let variable_flags = TestFlags::A | TestFlags::B;
47        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
48        let expected_result = [
49            TestFlags::C,
50            TestFlags::A | TestFlags::C,
51            TestFlags::B | TestFlags::C,
52            TestFlags::A | TestFlags::B | TestFlags::C,
53        ];
54        assert_eq!(generated_combinations, expected_result);
55    }
56
57    #[test]
58    fn test_build_flag_combinations_with_empty_constant_flags() {
59        let constant_flags = TestFlags::empty();
60        let variable_flags = TestFlags::A | TestFlags::B;
61        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
62        let expected_result =
63            [TestFlags::empty(), TestFlags::A, TestFlags::B, TestFlags::A | TestFlags::B];
64        assert_eq!(generated_combinations, expected_result);
65    }
66
67    #[test]
68    fn test_build_flag_combinations_with_empty_variable_flags() {
69        let constant_flags = TestFlags::A | TestFlags::B;
70        let variable_flags = TestFlags::empty();
71        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
72        let expected_result = [constant_flags];
73        assert_eq!(generated_combinations, expected_result);
74    }
75
76    #[test]
77    fn test_build_flag_combinations_with_empty_flags() {
78        let constant_flags = TestFlags::empty();
79        let variable_flags = TestFlags::empty();
80        let generated_combinations = build_flag_combinations(constant_flags, variable_flags);
81        let expected_result = [constant_flags];
82        assert_eq!(generated_combinations, expected_result);
83    }
84}