reorganize testing

This commit is contained in:
Simon Gardling
2022-05-11 15:19:22 -04:00
parent d9100c64cc
commit 7109151b0f
17 changed files with 585 additions and 596 deletions

View File

@@ -202,7 +202,7 @@ pub fn generate_hint<'a>(input: &str) -> &'a Hint<'a> {
}
}
fn get_last_term(chars: &[char]) -> String {
pub fn get_last_term(chars: &[char]) -> String {
assert!(!chars.is_empty());
unsafe {
@@ -253,143 +253,3 @@ impl<'a> Hint<'a> {
}
include!(concat!(env!("OUT_DIR"), "/codegen.rs"));
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::*;
/// Tests to make sure hints are properly outputed based on input
#[test]
fn hints() {
let values = HashMap::from([
("", Hint::Single("x^2")),
("si", Hint::Many(&["n(", "nh(", "gnum("])),
("log", Hint::Many(&["2(", "10("])),
("cos", Hint::Many(&["(", "h("])),
("sin(", Hint::Single(")")),
("sqrt", Hint::Single("(")),
("ln(x)", Hint::None),
("ln(x)cos", Hint::Many(&["(", "h("])),
("ln(x)*cos", Hint::Many(&["(", "h("])),
("sin(cos", Hint::Many(&["(", "h("])),
]);
for (key, value) in values {
println!("{} + {:?}", key, value);
assert_eq!(super::generate_hint(key), &value);
}
}
#[test]
fn hint_to_string() {
let values = HashMap::from([
("x^2", Hint::Single("x^2")),
(
r#"["n(", "nh(", "gnum("]"#,
Hint::Many(&["n(", "nh(", "gnum("]),
),
(r#"["n("]"#, Hint::Many(&["n("])),
("None", Hint::None),
]);
for (key, value) in values {
assert_eq!(value.to_string(), key);
}
}
#[test]
fn invalid_function() {
SUPPORTED_FUNCTIONS
.iter()
.map(|func1| {
SUPPORTED_FUNCTIONS
.iter()
.map(|func2| func1.to_string() + func2)
.collect::<Vec<String>>()
})
.flatten()
.filter(|func| !SUPPORTED_FUNCTIONS.contains(&func.as_str()))
.for_each(|key| {
let split = super::split_function(&key);
if split.len() != 1 {
panic!("failed: {} (len: {}, split: {:?})", key, split.len(), split);
}
let generated_hint = super::generate_hint(&key);
if generated_hint.is_none() {
println!("success: {}", key);
} else {
panic!("failed: {} (Hint: '{}')", key, generated_hint.to_string());
}
});
}
#[test]
fn split_function() {
let values = HashMap::from([
("cos(x)", vec!["cos(x)"]),
("cos(", vec!["cos("]),
("cos(x)sin(x)", vec!["cos(x)", "sin(x)"]),
("aaaaaaaaaaa", vec!["aaaaaaaaaaa"]),
("emax(x)", vec!["e", "max(x)"]),
("x", vec!["x"]),
("xxx", vec!["x", "x", "x"]),
("sin(cos(x)x)", vec!["sin(cos(x)", "x)"]),
("sin(x)*cos(x)", vec!["sin(x)", "cos(x)"]),
("x*x", vec!["x", "x"]),
("10*10", vec!["10", "10"]),
]);
for (key, value) in values {
assert_eq!(super::split_function(key), value);
}
}
#[test]
fn hint_tests() {
{
let hint = Hint::None;
assert!(hint.is_none());
assert!(!hint.is_some());
assert!(!hint.is_single());
}
{
let hint = Hint::Single(&"");
assert!(!hint.is_none());
assert!(hint.is_some());
assert!(hint.is_single());
}
{
let hint = Hint::Many(&[""]);
assert!(!hint.is_none());
assert!(hint.is_some());
assert!(!hint.is_single());
}
}
#[test]
fn get_last_term() {
let values = HashMap::from([
("cos(x)", "x)"),
("cos(", "cos("),
("aaaaaaaaaaa", "aaaaaaaaaaa"),
("x", "x"),
("xxx", "x"),
("x*x", "x"),
("10*10", "10"),
("sin(cos", "cos"),
]);
for (key, value) in values {
assert_eq!(
super::get_last_term(key.chars().collect::<Vec<char>>().as_slice()),
value
);
}
}
}