testing overhall

This commit is contained in:
Simon Gardling 2022-03-22 10:07:02 -04:00
parent d1d48f2838
commit ac89f3eb19
2 changed files with 137 additions and 74 deletions

View File

@ -19,3 +19,4 @@
12. fix integral display 12. fix integral display
13. Improve loading indicator 13. Improve loading indicator
14. Hash asset tarball and compare hash on runtime 14. Hash asset tarball and compare hash on runtime
15. add comments in `parsing::process_func_str` for it to be better understandable

View File

@ -95,50 +95,71 @@ pub fn process_func_str(function_in: &str) -> String {
' ' ' '
}; };
let c_is_number = NUMBERS.contains(&c);
let c_is_letter = LETTERS.contains(&c);
let c_is_variable = VALID_VARIABLES.contains(&c);
let prev_char_is_variable = VALID_VARIABLES.contains(&prev_char);
let prev_char_is_number = NUMBERS.contains(&prev_char);
// makes special case for log with base of a 1-2 digit number
if (prev_prev_prev_char == 'l') if (prev_prev_prev_char == 'l')
&& (prev_prev_char == 'o') && (prev_prev_char == 'o')
&& (prev_char == 'g') && (prev_char == 'g')
&& (NUMBERS.contains(&c)) && (c_is_number)
{ {
prev_chars.push(c); prev_chars.push(c);
output_string += &c.to_string(); output_string += &c.to_string();
continue; continue;
} }
let c_letters_var = LETTERS.contains(&c) | VALID_VARIABLES.contains(&c); let c_letters_var = c_is_letter | c_is_variable;
let prev_letters_var = VALID_VARIABLES.contains(&prev_char) | LETTERS.contains(&prev_char); let prev_letters_var = prev_char_is_variable | LETTERS.contains(&prev_char);
if prev_char == ')' { if prev_char == ')' {
if (c == '(') | NUMBERS.contains(&c) | c_letters_var { // cases like `)x` like `(2x+3)x`
if c_letters_var {
add_asterisk = true; add_asterisk = true;
} }
// cases like `(x+1)2`
if c_is_number {
add_asterisk = true;
}
// cases such as `)(` like `(x+1)(x-3)`
if c == '(' {
add_asterisk = true
}
} else if c == '(' { } else if c == '(' {
if (VALID_VARIABLES.contains(&prev_char) if (prev_char_is_variable | (')' == prev_char) | prev_char_is_number)
| (')' == prev_char)
| NUMBERS.contains(&prev_char))
&& !LETTERS.contains(&prev_prev_char) && !LETTERS.contains(&prev_prev_char)
{ {
add_asterisk = true; add_asterisk = true;
} }
} else if NUMBERS.contains(&prev_char) { } else if prev_char_is_number {
if (c == '(') | c_letters_var { if (c == '(') | c_letters_var {
add_asterisk = true; add_asterisk = true;
} }
} else if LETTERS.contains(&c) { } else if c_is_letter {
if NUMBERS.contains(&prev_char) if prev_char_is_number
| (VALID_VARIABLES.contains(&prev_char) && VALID_VARIABLES.contains(&c)) | (prev_char_is_variable && c_is_variable)
| prev_char_is_variable
{ {
add_asterisk = true; add_asterisk = true;
} }
} else if (NUMBERS.contains(&c) | c_letters_var) && prev_letters_var { } else if (c_is_number | c_letters_var) && prev_letters_var {
add_asterisk = true; add_asterisk = true;
} }
// if add_asterisk is true, add the asterisk
if add_asterisk { if add_asterisk {
output_string += "*"; output_string += "*";
} }
// puch current char to vector of previously parsed chars
prev_chars.push(c); prev_chars.push(c);
// push current char to `output_string` (which is eventually returned)
output_string += &c.to_string(); output_string += &c.to_string();
} }
@ -182,31 +203,71 @@ pub fn test_func(function_string: &str) -> Option<String> {
} }
} }
/// Used for testing: passes function to `add_asterisks` before running
/// `test_func`
#[cfg(test)] #[cfg(test)]
fn test_func_helper(function_string: &str) -> bool { mod test {
test_func(&process_func_str(function_string)).is_some() use super::*;
/// returns if function with string `func_str` is valid after processing
/// through `process_func_str`
fn func_is_valid(func_str: &str) -> bool { test_func(&process_func_str(func_str)).is_none() }
/// Used for testing: passes function to `add_asterisks` 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);
println!(
"function: {} (expected: {}, got: {})",
func_str, expect_valid, is_valid
);
assert!(is_valid == expect_valid);
} }
/// Tests to make sure functions are parsed correctly /// Tests to make sure functions that are expected to succeed, succeed.
#[test] #[test]
fn test_func_test() { fn test_expected_func_successes() {
// These shouldn't fail let functions = vec![
assert!(!test_func_helper("x^2")); "x^2",
assert!(!test_func_helper("2x")); "2x",
// assert!(test_func_helper("e^x")); // need to fix!!! PR to exmex "E^x",
assert!(!test_func_helper("E^x")); "log10(x)",
assert!(!test_func_helper("log10(x)")); "xxxxx", // test variables side-by-side
assert!(!test_func_helper("xxxxx")); "sin(x)",
"xsin(x)",
"sin(x)cos(x)",
"x/0", // always returns NaN
"(x+1)(x-3)", // tests 2 parentheses in `)(` pattern
"(2x+1)x",
"(2x+1)pi",
"pi(2x+1)",
// "pipipipipipi", // need to fix
];
// Expect these to fail for func_str in functions.iter().cloned() {
assert!(test_func_helper("a")); // variable `a` is invalid test_func_helper(func_str, true);
assert!(test_func_helper("l^2")); // variable `l` is invalid }
assert!(test_func_helper("log222(x)")); // there is no such thing as `log222`
assert!(test_func_helper("abcdef")); // invalid variables
} }
/// Tests to make sure functions that are expected to fail, fail.
#[test]
fn test_expected_func_failures() {
let functions = vec![
"a", // Invalid variable
"l^2", // Invalid variable
"log222(x)", // Invalid function
"abcdef", // Invalid variables
"log10(x", // unclosed bracket
"x^a", // Invalid variable
"sin(cos(x)))", // extra bracket
"((())",
"0/0",
];
for func_str in functions.iter().cloned() {
test_func_helper(func_str, false);
}
}
/// Helps with tests of `process_func_str` /// Helps with tests of `process_func_str`
#[cfg(test)] #[cfg(test)]
fn test_process_helper(input: &str, expected: &str) { fn test_process_helper(input: &str, expected: &str) {
@ -246,3 +307,4 @@ fn func_process_test() {
// test_process_helper("emax(x)", "e*max(x)"); // test_process_helper("emax(x)", "e*max(x)");
// test_process_helper("pisin(x)", "pi*sin(x)"); // test_process_helper("pisin(x)", "pi*sin(x)");
} }
}