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

@@ -11,7 +11,7 @@ fn compare_len_reverse_alpha(a: &String, b: &String) -> Ordering {
/// Generates hashmap (well really a vector of tuple of strings that are then turned into a hashmap by phf)
#[allow(dead_code)]
fn compile_hashmap(data: Vec<String>) -> Vec<(String, String)> {
pub fn compile_hashmap(data: Vec<String>) -> Vec<(String, String)> {
let mut seen = HashSet::new();
let tuple_list_1: Vec<(String, String)> = data
@@ -70,33 +70,3 @@ fn all_possible_splits(
})
.collect::<Vec<(String, String)>>()
}
#[cfg(test)]
mod tests {
use super::*;
/// Tests to make sure hashmap generation works as expected
#[test]
fn hashmap_gen_test() {
let data = vec!["time", "text", "test"];
let expect = vec![
("t", r#"Hint::Many(&["ime(", "ext(", "est("])"#),
("ti", r#"Hint::Single("me(")"#),
("tim", r#"Hint::Single("e(")"#),
("time", r#"Hint::Single("(")"#),
("te", r#"Hint::Many(&["xt(", "st("])"#),
("tex", r#"Hint::Single("t(")"#),
("text", r#"Hint::Single("(")"#),
("tes", r#"Hint::Single("t(")"#),
("test", r#"Hint::Single("(")"#),
];
assert_eq!(
compile_hashmap(data.iter().map(|e| e.to_string()).collect()),
expect
.iter()
.map(|(a, b)| (a.to_string(), b.to_string()))
.collect::<Vec<(String, String)>>()
);
}
}

View File

@@ -4,5 +4,14 @@
#![feature(const_mut_refs)]
mod autocomplete_helper;
pub mod parsing;
pub mod suggestions;
mod parsing;
mod suggestions;
pub use crate::{
autocomplete_helper::compile_hashmap,
parsing::{process_func_str, BackingFunction},
suggestions::{
generate_hint, get_last_term, split_function, split_function_chars, Hint, HINT_EMPTY,
SUPPORTED_FUNCTIONS,
},
};

View File

@@ -193,104 +193,3 @@ pub fn process_func_str(function_in: &str) -> String {
crate::suggestions::split_function(&function_in).join("*")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::suggestions::SUPPORTED_FUNCTIONS;
use std::collections::HashMap;
/// Returns if function with string `func_str` is valid after processing through [`process_func_str`]
fn func_is_valid(func_str: &str) -> bool {
BackingFunction::new(&process_func_str(func_str)).is_ok()
}
/// Used for testing: passes function to [`process_func_str`] before running [`test_func`]. if `expect_valid` == `true`, it expects no errors to be created.
fn test_func_helper(func_str: &str, expect_valid: bool) {
let is_valid = func_is_valid(func_str);
let string = format!(
"function: {} (expected: {}, got: {})",
func_str, expect_valid, is_valid
);
if is_valid == expect_valid {
println!("{}", string);
} else {
panic!("{}", string);
}
}
/// Tests to make sure functions that are expected to succeed, succeed.
#[test]
fn test_expected() {
let values = HashMap::from([
("", true),
("x^2", true),
("2x", true),
("E^x", true),
("log10(x)", true),
("xxxxx", true),
("sin(x)", true),
("xsin(x)", true),
("sin(x)cos(x)", true),
("x/0", true),
("(x+1)(x-3)", true),
("cos(xsin(x)x)", true),
("(2x+1)x", true),
("(2x+1)pi", true),
("pi(2x+1)", true),
("pipipipipipix", true),
("e^sin(x)", true),
("E^sin(x)", true),
("e^x", true),
("x**2", true),
("a", false),
("log222(x)", false),
("abcdef", false),
("log10(x", false),
("x^a", false),
("sin(cos(x)))", false),
("0/0", false),
]);
for (key, value) in values {
test_func_helper(key, value);
}
}
/// Helps with tests of [`process_func_str`]
#[cfg(test)]
fn test_process_helper(input: &str, expected: &str) {
assert_eq!(&process_func_str(input), expected);
}
/// Tests to make sure my cursed function works as intended
#[test]
fn func_process_test() {
let values = HashMap::from([
("2x", "2*x"),
(")(", ")*("),
("(2", "(2"),
("log10(x)", "log10(x)"),
("log2(x)", "log2(x)"),
("pipipipipipi", "π*π*π*π*π*π"),
("10pi", "10*π"),
("pi10", "π*10"),
("10pi10", "10*π*10"),
("emax(x)", "e*max(x)"),
("pisin(x)", "π*sin(x)"),
("e^sin(x)", "e^sin(x)"),
("x**2", "x^2"),
("(x+1)(x-3)", "(x+1)*(x-3)"),
]);
for (key, value) in values {
test_process_helper(key, value);
}
for func in SUPPORTED_FUNCTIONS.iter() {
let func_new = format!("{}(x)", func);
test_process_helper(&func_new, &func_new);
}
}
}

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
);
}
}
}