use std::collections::HashSet;
use super::*;
fn make_counts(n1: &[&str], n2: &[&str], big_n1: usize, big_n2: usize) -> TokenCounts {
TokenCounts {
distinct_operators: n1.iter().map(|s| s.to_string()).collect(),
distinct_operands: n2.iter().map(|s| s.to_string()).collect(),
total_operators: big_n1,
total_operands: big_n2,
}
}
#[test]
fn empty_returns_none() {
let counts = TokenCounts {
distinct_operators: HashSet::new(),
distinct_operands: HashSet::new(),
total_operators: 0,
total_operands: 0,
};
assert!(compute(&counts).is_none());
}
#[test]
fn basic_computation() {
let counts = make_counts(&["if", "+"], &["x", "y", "1"], 5, 7);
let m = compute(&counts).unwrap();
assert_eq!(m.distinct_operators, 2);
assert_eq!(m.distinct_operands, 3);
assert_eq!(m.total_operators, 5);
assert_eq!(m.total_operands, 7);
assert_eq!(m.vocabulary, 5); assert_eq!(m.length, 12);
assert!((m.volume - 27.863).abs() < 0.1);
assert!((m.difficulty - 2.333).abs() < 0.01);
assert!((m.effort - 65.01).abs() < 0.1);
assert!((m.bugs - 0.00929).abs() < 0.001);
assert!((m.time - 3.612).abs() < 0.1);
}
#[test]
fn zero_operands_returns_none() {
let counts = make_counts(&["if"], &[], 3, 0);
assert!(compute(&counts).is_none());
}
#[test]
fn zero_operators_returns_none() {
let counts = make_counts(&[], &["x", "y"], 0, 5);
assert!(compute(&counts).is_none());
}
#[test]
fn single_operator_single_operand() {
let counts = make_counts(&["="], &["x"], 1, 1);
let m = compute(&counts).unwrap();
assert_eq!(m.vocabulary, 2);
assert_eq!(m.length, 2);
assert!((m.volume - 2.0).abs() < 0.001);
}