about summary refs log tree commit diff
path: root/library/compiler-builtins/libm-test/src/generate.rs
blob: da080d23fa79ce49aba75a9f4dcd9115f22eef1c (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
//! Different generators that can create random or systematic bit patterns.

pub mod case_list;
pub mod edge_cases;
pub mod random;
pub mod spaced;

/// A wrapper to turn any iterator into an `ExactSizeIterator`. Asserts the final result to ensure
/// the provided size was correct.
#[derive(Debug)]
pub struct KnownSize<I> {
    total: u64,
    current: u64,
    iter: I,
}

impl<I> KnownSize<I> {
    pub fn new(iter: I, total: u64) -> Self {
        Self {
            total,
            current: 0,
            iter,
        }
    }
}

impl<I: Iterator> Iterator for KnownSize<I> {
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        let next = self.iter.next();
        if next.is_some() {
            self.current += 1;
            return next;
        }

        assert_eq!(
            self.current, self.total,
            "total items did not match expected"
        );
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = usize::try_from(self.total - self.current).unwrap();
        (remaining, Some(remaining))
    }
}

impl<I: Iterator> ExactSizeIterator for KnownSize<I> {}