bitbelay_tests/chi_squared.rs
1//! Chi-squared tests.
2
3use std::hash::BuildHasher;
4
5use bitbelay_report::section;
6
7use crate::r#trait::Test as _;
8
9pub mod goodness_of_fit;
10
11/// A type of chi-squared test.
12#[derive(Debug)]
13pub enum Test<'a, H: BuildHasher> {
14 /// Goodness of fit test.
15 GoodnessOfFit(goodness_of_fit::Test<'a, H>),
16}
17
18impl<'a, H: BuildHasher> Test<'a, H> {
19 /// Gets a reference to a [`goodness_of_fit::Test`] wrapped in [`Some`] if
20 /// the [`Test`] is a [`Test::GoodnessOfFit`]. Else, returns [`None`].
21 ///
22 /// # Examples
23 ///
24 /// ```
25 /// use std::hash::RandomState;
26 /// use std::num::NonZeroUsize;
27 ///
28 /// use bitbelay_providers::ascii::AlphanumericProvider;
29 /// use bitbelay_tests::chi_squared::goodness_of_fit;
30 /// use bitbelay_tests::chi_squared::Test;
31 ///
32 /// let provider = Box::new(AlphanumericProvider::new(10));
33 ///
34 /// let hasher = RandomState::new();
35 /// let mut test = Test::GoodnessOfFit(goodness_of_fit::Test::new(
36 /// &hasher,
37 /// provider,
38 /// NonZeroUsize::try_from(256).unwrap(),
39 /// 0.05,
40 /// ));
41 ///
42 /// assert!(matches!(test.as_goodness_of_fit_test(), Some(_)));
43 ///
44 /// # Ok::<(), Box<dyn std::error::Error>>(())
45 /// ```
46 pub fn as_goodness_of_fit_test(&self) -> Option<&goodness_of_fit::Test<'a, H>> {
47 match self {
48 Test::GoodnessOfFit(test) => Some(test),
49 }
50 }
51
52 /// Consumes the [`Test`] and returns a [`goodness_of_fit::Test`] wrapped in
53 /// [`Some`] if the [`Test`] is a [`Test::GoodnessOfFit`]. Else, returns
54 /// [`None`].
55 ///
56 /// # Examples
57 ///
58 /// ```
59 /// use std::hash::RandomState;
60 /// use std::num::NonZeroUsize;
61 ///
62 /// use bitbelay_providers::ascii::AlphanumericProvider;
63 /// use bitbelay_tests::chi_squared::goodness_of_fit;
64 /// use bitbelay_tests::chi_squared::Test;
65 ///
66 /// let provider = Box::new(AlphanumericProvider::new(10));
67 ///
68 /// let hasher = RandomState::new();
69 /// let mut test = Test::GoodnessOfFit(goodness_of_fit::Test::new(
70 /// &hasher,
71 /// provider,
72 /// NonZeroUsize::try_from(256).unwrap(),
73 /// 0.05,
74 /// ));
75 ///
76 /// assert!(matches!(test.into_goodness_of_fit_test(), Some(_)));
77 ///
78 /// # Ok::<(), Box<dyn std::error::Error>>(())
79 /// ```
80 pub fn into_goodness_of_fit_test(self) -> Option<goodness_of_fit::Test<'a, H>> {
81 match self {
82 Test::GoodnessOfFit(test) => Some(test),
83 }
84 }
85
86 /// Generates a report section for the [`Test`].
87 ///
88 /// # Examples
89 ///
90 /// ```
91 /// use std::hash::RandomState;
92 /// use std::num::NonZeroUsize;
93 ///
94 /// use bitbelay_providers::ascii::AlphanumericProvider;
95 /// use bitbelay_tests::chi_squared::goodness_of_fit;
96 /// use bitbelay_tests::chi_squared::Test;
97 ///
98 /// let provider = Box::new(AlphanumericProvider::new(10));
99 ///
100 /// let hasher = RandomState::new();
101 /// let mut test = Test::GoodnessOfFit(goodness_of_fit::Test::new(
102 /// &hasher,
103 /// provider,
104 /// NonZeroUsize::try_from(256).unwrap(),
105 /// 0.05,
106 /// ));
107 ///
108 /// let section = test.report_section();
109 /// // Do something with `section`.
110 ///
111 /// # Ok::<(), Box<dyn std::error::Error>>(())
112 /// ```
113 pub fn report_section(&self) -> section::Test {
114 match self {
115 Test::GoodnessOfFit(test) => test.report_section(),
116 }
117 }
118}