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
/// This macro calculates the levenshtein distance between 2 strings. /// See: https://crates.io/crates/levenshtein /// /// # Arguments /// /// * `control: &String` - The string to compare against. This would be the real data from the data sample.</br> /// * `experiment: &String` - The string to compare. This would be the generated data for which you want to find the distance.</br> /// /// #Example /// /// ``` /// # #[macro_use] extern crate test_data_generation; extern crate levenshtein; /// # fn main() { /// // let kitten = String::from("kitten"); /// // let sitting = String::from("sitting"); /// // assert_eq!(levenshtein_distance!(&kitten, &sitting), 3 as usize); /// # } /// #[macro_export] macro_rules! levenshtein_distance { ( $c:ident, $e:ident ) => { { use levenshtein; levenshtein::levenshtein($c, $e) } } } /// This macro generates a random number between 0 and 100. /// Returns a f64. /// /// # Example /// /// ``` /// # #[macro_use] extern crate test_data_generation; extern crate rand; /// # fn main() { /// let rnd: f64 = random_percentage!(); /// println!("Your random number is {}", rnd); /// # } /// ``` #[macro_export] macro_rules! random_percentage { ( $( $x:expr ),* ) => { { use rand::{thread_rng, Rng}; let mut rng = thread_rng(); rng.gen_range::<f64>(0 as f64,100 as f64) } }; } /// This macro generates a random number for a given range. /// Returns a u32. /// /// # Arguments /// /// * `a: u32` - The lowest number of the range to use for the random number.</br> /// * `b: u32` - The highest number of the range to use for the random number.</br> /// /// # Example /// /// ``` /// # #[macro_use] extern crate test_data_generation; extern crate rand; /// # fn main() { /// //let rnd: u32 = random_between!(0, 100); /// //println!("Your random number is {}", rnd); /// # } /// ``` #[macro_export] macro_rules! random_between { ($a:ident, $b:ident) => { { use rand::{thread_rng, Rng}; let mut rng = thread_rng(); let nbr = rng.gen_range::<u32>($a as u32, $b as u32); nbr } }; } /// This function calculates the percent difference between 2 strings. /// /// # Arguments /// /// * `control: &String` - The string to compare against. This would be the real data from the data sample.</br> /// * `experiment: &String` - The string to compare. This would be the generated data for which you want to find the percent difference.</br> /// /// #Example /// /// ``` /// # #[macro_use] extern crate test_data_generation; extern crate levenshtein; /// # fn main() { /// // let kitten = String::from("kitten"); /// // let sitting = String::from("sitting"); /// // assert_eq!(realistic_test!(&kitten, &sitting), 76.92307692307692 as f64); /// # } /// #[macro_export] macro_rules! realistic_test { ( $c:ident, $e:ident ) => { { let ld: f64 = levenshtein_distance!($c, $e) as f64; let total: f64 = $c.len() as f64 + $e.len() as f64; let diff: f64 = total - ld; (1 as f64 - ((total - diff)/total)) * 100 as f64 } } }