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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::{borrow::Cow, sync::Arc};

#[derive(Copy, Clone, Debug)]
pub(crate) enum TestKind {
    Test,
    Bench,
}

/// Description about a test.
#[derive(Debug, Clone)]
pub struct TestDesc(Arc<TestDescInner>);

#[derive(Debug)]
struct TestDescInner {
    name: Cow<'static, str>,
    kind: TestKind,
    ignored: bool,
}

impl AsRef<Self> for TestDesc {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl TestDesc {
    pub(crate) fn kind(&self) -> &TestKind {
        &self.0.kind
    }

    /// Return the name of test.
    #[inline]
    pub fn name(&self) -> &str {
        &*self.0.name
    }

    /// Return whether the test is a benchmark or not.
    #[inline]
    pub fn is_bench(&self) -> bool {
        match self.0.kind {
            TestKind::Bench => true,
            _ => false,
        }
    }

    /// Return whether the test should be ignored or not.
    #[inline]
    pub fn ignored(&self) -> bool {
        self.0.ignored
    }
}

/// Data that describes a single test.
pub struct Test<D> {
    desc: TestDesc,
    data: D,
}

impl<D> Test<D> {
    /// Create a single test.
    pub fn test(name: impl Into<Cow<'static, str>>, data: D) -> Self {
        Self::new(name.into(), TestKind::Test, data)
    }

    /// Create a single benchmark test.
    pub fn bench(name: impl Into<Cow<'static, str>>, data: D) -> Self {
        Self::new(name.into(), TestKind::Bench, data)
    }

    fn new(name: Cow<'static, str>, kind: TestKind, data: D) -> Self {
        Self {
            desc: TestDesc(Arc::new(TestDescInner {
                name,
                kind,
                ignored: false,
            })),
            data,
        }
    }

    /// Mark that this test should be ignored.
    pub fn ignore(mut self, value: bool) -> Self {
        Arc::get_mut(&mut self.desc.0).unwrap().ignored = value;
        self
    }

    pub(crate) fn desc(&self) -> &TestDesc {
        &self.desc
    }

    pub(crate) fn deconstruct(self) -> (TestDesc, D) {
        (self.desc, self.data)
    }
}

/// The outcome of performing a test.
#[derive(Debug)]
pub struct Outcome {
    kind: OutcomeKind,
    err_msg: Option<Arc<Cow<'static, str>>>,
}

impl Outcome {
    #[inline]
    fn new(kind: OutcomeKind) -> Self {
        Self {
            kind,
            err_msg: None,
        }
    }

    /// Create an `Outcome` representing that the test passed.
    #[inline]
    pub fn passed() -> Self {
        Self::new(OutcomeKind::Passed)
    }

    /// Create an `Outcome` representing that the test or benchmark failed.
    pub fn failed() -> Self {
        Self::new(OutcomeKind::Failed)
    }

    /// Create an `Outcome` representing that the benchmark test was successfully run.
    pub fn measured(average: u64, variance: u64) -> Self {
        Self::new(OutcomeKind::Measured { average, variance })
    }

    /// Specify the error message.
    pub fn error_message(self, err_msg: impl Into<Cow<'static, str>>) -> Self {
        Self {
            err_msg: Some(Arc::new(err_msg.into())),
            ..self
        }
    }

    pub(crate) fn kind(&self) -> &OutcomeKind {
        &self.kind
    }

    pub(crate) fn err_msg(&self) -> Option<Arc<Cow<'static, str>>> {
        self.err_msg.clone()
    }
}

#[derive(Debug, Copy, Clone)]
pub(crate) enum OutcomeKind {
    Passed,
    Failed,
    Measured { average: u64, variance: u64 },
}