test_data_generation/macros.rs
1/// This macro calculates the levenshtein distance between 2 strings.
2/// See: https://crates.io/crates/levenshtein
3///
4/// # Arguments
5///
6/// * `control: &String` - The string to compare against. This would be the real data from the data sample.</br>
7/// * `experiment: &String` - The string to compare. This would be the generated data for which you want to find the distance.</br>
8///
9/// #Example
10///
11/// ```rust
12/// # #[macro_use] extern crate test_data_generation; extern crate levenshtein;
13/// # fn main() {
14/// assert_eq!(levenshtein_distance!("kitten", "sitting"), 3 as usize);
15/// # }
16///
17#[macro_export]
18macro_rules! levenshtein_distance {
19 ( $c:expr, $e:expr ) => {{
20 use levenshtein;
21
22 levenshtein::levenshtein($c, $e)
23 }};
24}
25
26/// This macro generates a random number between 0 and 100.
27/// Returns a f64.
28///
29/// # Example
30///
31/// ```rust
32/// # #[macro_use] extern crate test_data_generation; extern crate rand;
33/// # fn main() {
34/// let rnd: f64 = random_percentage!();
35/// println!("Your random number is {}", rnd);
36/// # }
37/// ```
38#[macro_export]
39macro_rules! random_percentage {
40 ( $( $x:expr ),* ) => {{
41 use rand::{thread_rng, Rng};
42
43 let mut rng = thread_rng();
44 let nbr: f64 = rng.gen_range(0_f64..100_f64);
45
46 nbr
47 }};
48}
49
50/// This macro generates a random number for a given range.
51/// Returns a u32.
52///
53/// # Arguments
54///
55/// * `a: u32` - The lowest number of the range to use for the random number.</br>
56/// * `b: u32` - The highest number of the range to use for the random number.</br>
57///
58/// # Example
59///
60/// ```rust
61/// # #[macro_use] extern crate test_data_generation; extern crate rand;
62/// # fn main() {
63/// let rnd: u32 = random_between!(0, 100);
64/// println!("Your random number is {}", rnd);
65/// # }
66/// ```
67#[macro_export]
68macro_rules! random_between {
69 ($a:expr, $b:expr) => {{
70 use rand::{thread_rng, Rng};
71
72 let mut rng = thread_rng();
73 let nbr: u32 = rng.gen_range($a as u32..$b as u32);
74
75 nbr
76 }};
77}
78
79/// This function calculates the percent difference between 2 strings.
80///
81/// # Arguments
82///
83/// * `control: &String` - The string to compare against. This would be the real data from the data sample.</br>
84/// * `experiment: &String` - The string to compare. This would be the generated data for which you want to find the percent difference.</br>
85///
86/// # Example
87///
88/// ```rust
89/// # #[macro_use] extern crate test_data_generation; extern crate levenshtein;
90///
91/// # fn main() {
92/// assert_eq!(realistic_test!("kitten", "sitting"), 76.92307692307692 as f64);
93/// # }
94///
95#[macro_export]
96macro_rules! realistic_test {
97 ( $c:expr, $e:expr ) => {{
98 let ld: f64 = levenshtein_distance!($c, $e) as f64;
99 let total: f64 = $c.len() as f64 + $e.len() as f64;
100 let diff: f64 = total - ld;
101 (1 as f64 - ((total - diff) / total)) * 100 as f64
102 }};
103}