proptest/test_runner/failure_persistence/
map.rs

1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use crate::std_facade::{fmt, BTreeMap, BTreeSet, Box, Vec};
11use core::any::Any;
12
13use crate::test_runner::failure_persistence::FailurePersistence;
14use crate::test_runner::failure_persistence::PersistedSeed;
15
16/// Failure persistence option that loads and saves seeds in memory
17/// on the heap. This may be useful when accumulating test failures
18/// across multiple `TestRunner` instances for external reporting
19/// or batched persistence.
20#[derive(Clone, Debug, Default, PartialEq)]
21pub struct MapFailurePersistence {
22    /// Backing map, keyed by source_file.
23    pub map: BTreeMap<&'static str, BTreeSet<PersistedSeed>>,
24}
25
26impl FailurePersistence for MapFailurePersistence {
27    fn load_persisted_failures2(
28        &self,
29        source_file: Option<&'static str>,
30    ) -> Vec<PersistedSeed> {
31        source_file
32            .and_then(|source| self.map.get(source))
33            .map(|seeds| seeds.iter().cloned().collect::<Vec<_>>())
34            .unwrap_or_default()
35    }
36
37    fn save_persisted_failure2(
38        &mut self,
39        source_file: Option<&'static str>,
40        seed: PersistedSeed,
41        _shrunken_value: &dyn fmt::Debug,
42    ) {
43        let s = match source_file {
44            Some(sf) => sf,
45            None => return,
46        };
47        let set = self.map.entry(s).or_insert_with(BTreeSet::new);
48        set.insert(seed);
49    }
50
51    fn box_clone(&self) -> Box<dyn FailurePersistence> {
52        Box::new(self.clone())
53    }
54
55    fn eq(&self, other: &dyn FailurePersistence) -> bool {
56        other
57            .as_any()
58            .downcast_ref::<Self>()
59            .map_or(false, |x| x == self)
60    }
61
62    fn as_any(&self) -> &dyn Any {
63        self
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use crate::test_runner::failure_persistence::tests::*;
71
72    #[test]
73    fn initial_map_is_empty() {
74        assert!(MapFailurePersistence::default()
75            .load_persisted_failures2(HI_PATH)
76            .is_empty())
77    }
78
79    #[test]
80    fn seeds_recoverable() {
81        let mut p = MapFailurePersistence::default();
82        p.save_persisted_failure2(HI_PATH, INC_SEED, &"");
83        let restored = p.load_persisted_failures2(HI_PATH);
84        assert_eq!(1, restored.len());
85        assert_eq!(INC_SEED, *restored.first().unwrap());
86
87        assert!(p.load_persisted_failures2(None).is_empty());
88        assert!(p.load_persisted_failures2(UNREL_PATH).is_empty());
89    }
90
91    #[test]
92    fn seeds_deduplicated() {
93        let mut p = MapFailurePersistence::default();
94        p.save_persisted_failure2(HI_PATH, INC_SEED, &"");
95        p.save_persisted_failure2(HI_PATH, INC_SEED, &"");
96        let restored = p.load_persisted_failures2(HI_PATH);
97        assert_eq!(1, restored.len());
98    }
99}