use crate::statistical_tests::utils::convert_string;
pub fn get_ioc(text: &str) -> f64 {
let data = convert_string(text);
let cipher_symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#0123456789";
let num_symbols = cipher_symbols.len();
let mut counts = vec![0; num_symbols];
let text_len = data.len();
for &c in &data {
counts[c] += 1;
}
let mut sum = 0.0;
for count in counts {
sum += count as f64 * (count as f64 - 1.0);
}
if text_len <= 1 {
return 0.0;
}
let ic = sum / (text_len as f64 * (text_len as f64 - 1.0));
ic * 1000.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ioc_random_text() {
let text = "XQZJKVBPWMFYORGHLDSETNAIUC";
let ioc = get_ioc(text);
assert!(ioc < 1000.0);
}
}