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

135
tests/autocomplete.rs Normal file
View File

@@ -0,0 +1,135 @@
use parsing::Hint;
use ytbn_graphing_software::{AutoComplete, Movement};
enum Action<'a> {
AssertIndex(usize),
AssertString(&'a str),
AssertHint(&'a str),
SetString(&'a str),
Move(Movement),
}
fn ac_tester(actions: &[Action]) {
let mut ac = AutoComplete::default();
for action in actions.iter() {
match action {
Action::AssertIndex(target_i) => {
if &ac.i != target_i {
panic!(
"AssertIndex failed: Current: '{}' Expected: '{}'",
ac.i, target_i
)
}
}
Action::AssertString(target_string) => {
if &ac.string != target_string {
panic!(
"AssertString failed: Current: '{}' Expected: '{}'",
ac.string, target_string
)
}
}
Action::AssertHint(target_hint) => match ac.hint {
Hint::None => {
if !target_hint.is_empty() {
panic!(
"AssertHint failed on `Hint::None`: Expected: {}",
target_hint
);
}
}
Hint::Many(hints) => {
let hint = hints[ac.i];
if &hint != target_hint {
panic!(
"AssertHint failed on `Hint::Many`: Current: '{}' (index: {}) Expected: '{}'",
hint, ac.i, target_hint
)
}
}
Hint::Single(hint) => {
if hint != target_hint {
panic!(
"AssertHint failed on `Hint::Single`: Current: '{}' Expected: '{}'",
hint, target_hint
)
}
}
},
Action::SetString(target_string) => {
ac.update_string(target_string);
}
Action::Move(target_movement) => {
ac.register_movement(target_movement);
}
}
}
}
#[test]
fn single() {
ac_tester(&[
Action::SetString(""),
Action::AssertHint("x^2"),
Action::Move(Movement::Up),
Action::AssertIndex(0),
Action::AssertString(""),
Action::AssertHint("x^2"),
Action::Move(Movement::Down),
Action::AssertIndex(0),
Action::AssertString(""),
Action::AssertHint("x^2"),
Action::Move(Movement::Complete),
Action::AssertString("x^2"),
Action::AssertHint(""),
Action::AssertIndex(0),
]);
}
#[test]
fn multi() {
ac_tester(&[
Action::SetString("s"),
Action::AssertHint("in("),
Action::Move(Movement::Up),
Action::AssertIndex(3),
Action::AssertString("s"),
Action::AssertHint("ignum("),
Action::Move(Movement::Down),
Action::AssertIndex(0),
Action::AssertString("s"),
Action::AssertHint("in("),
Action::Move(Movement::Down),
Action::AssertIndex(1),
Action::AssertString("s"),
Action::AssertHint("qrt("),
Action::Move(Movement::Up),
Action::AssertIndex(0),
Action::AssertString("s"),
Action::AssertHint("in("),
Action::Move(Movement::Complete),
Action::AssertString("sin("),
Action::AssertHint(")"),
Action::AssertIndex(0),
]);
}
#[test]
fn parens() {
ac_tester(&[
Action::SetString("sin(x"),
Action::AssertHint(")"),
Action::Move(Movement::Up),
Action::AssertIndex(0),
Action::AssertString("sin(x"),
Action::AssertHint(")"),
Action::Move(Movement::Down),
Action::AssertIndex(0),
Action::AssertString("sin(x"),
Action::AssertHint(")"),
Action::Move(Movement::Complete),
Action::AssertString("sin(x)"),
Action::AssertHint(""),
Action::AssertIndex(0),
]);
}

69
tests/function.rs Normal file
View File

@@ -0,0 +1,69 @@
use ytbn_graphing_software::{AppSettings, FunctionEntry, Riemann};
fn app_settings_constructor(
sum: Riemann, integral_min_x: f64, integral_max_x: f64, pixel_width: usize, integral_num: usize,
) -> AppSettings {
AppSettings {
riemann_sum: sum,
integral_min_x,
integral_max_x,
integral_changed: true,
integral_num,
do_extrema: false,
do_roots: false,
plot_width: pixel_width,
}
}
static BACK_TARGET: [(f64, f64); 11] = [
(-1.0, 1.0),
(-0.8, 0.6400000000000001),
(-0.6, 0.36),
(-0.4, 0.16000000000000003),
(-0.19999999999999996, 0.03999999999999998),
(0.0, 0.0),
(0.19999999999999996, 0.03999999999999998),
(0.3999999999999999, 0.15999999999999992),
(0.6000000000000001, 0.3600000000000001),
(0.8, 0.6400000000000001),
(1.0, 1.0),
];
static DERIVATIVE_TARGET: [(f64, f64); 11] = [
(-1.0, -2.0),
(-0.8, -1.6),
(-0.6, -1.2),
(-0.4, -0.8),
(-0.19999999999999996, -0.3999999999999999),
(0.0, 0.0),
(0.19999999999999996, 0.3999999999999999),
(0.3999999999999999, 0.7999999999999998),
(0.6000000000000001, 1.2000000000000002),
(0.8, 1.6),
(1.0, 2.0),
];
fn do_test(sum: Riemann, area_target: f64) {
let settings = app_settings_constructor(sum, -1.0, 1.0, 10, 10);
let mut function = FunctionEntry::EMPTY;
function.update_string("x^2");
function.integral = true;
function.derivative = true;
function.tests(
settings,
BACK_TARGET.to_vec(),
DERIVATIVE_TARGET.to_vec(),
area_target,
-1.0,
1.0,
);
}
#[test]
fn function_entries() {
do_test(Riemann::Left, 0.9600000000000001);
do_test(Riemann::Middle, 0.92);
do_test(Riemann::Right, 0.8800000000000001);
}

95
tests/misc.rs Normal file
View File

@@ -0,0 +1,95 @@
/// Tests [`SteppedVector`] to ensure everything works properly (helped me find a bunch of issues)
#[test]
fn stepped_vector() {
use ytbn_graphing_software::SteppedVector;
let min: i32 = -1000;
let max: i32 = 1000;
let data: Vec<f64> = (min..=max).map(|x| x as f64).collect();
let len_data = data.len();
let stepped_vector: SteppedVector = data.into();
assert_eq!(stepped_vector.get_min(), min as f64);
assert_eq!(stepped_vector.get_max(), max as f64);
assert_eq!(stepped_vector.get_index(&(min as f64)), Some(0));
assert_eq!(stepped_vector.get_index(&(max as f64)), Some(len_data - 1));
for i in min..=max {
assert_eq!(
stepped_vector.get_index(&(i as f64)),
Some((i + min.abs()) as usize)
);
}
assert_eq!(stepped_vector.get_index(&((min - 1) as f64)), None);
assert_eq!(stepped_vector.get_index(&((max + 1) as f64)), None);
}
/// Ensures [`decimal_round`] returns correct values
#[test]
fn decimal_round() {
use ytbn_graphing_software::decimal_round;
assert_eq!(decimal_round(0.00001, 1), 0.0);
assert_eq!(decimal_round(0.00001, 2), 0.0);
assert_eq!(decimal_round(0.00001, 3), 0.0);
assert_eq!(decimal_round(0.00001, 4), 0.0);
assert_eq!(decimal_round(0.00001, 5), 0.00001);
assert_eq!(decimal_round(0.12345, 1), 0.1);
assert_eq!(decimal_round(0.12345, 2), 0.12);
assert_eq!(decimal_round(0.12345, 3), 0.123);
assert_eq!(decimal_round(0.12345, 4), 0.1235); // rounds up
assert_eq!(decimal_round(0.12345, 5), 0.12345);
assert_eq!(decimal_round(1.9, 0), 2.0);
assert_eq!(decimal_round(1.9, 1), 1.9);
}
/// Tests [`resolution_helper`] to make sure it returns expected output
#[test]
fn resolution_helper() {
use ytbn_graphing_software::resolution_helper;
assert_eq!(
resolution_helper(10, &1.0, &1.0),
vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
);
assert_eq!(
resolution_helper(5, &-2.0, &1.0),
vec![-2.0, -1.0, 0.0, 1.0, 2.0]
);
assert_eq!(resolution_helper(3, &-2.0, &1.0), vec![-2.0, -1.0, 0.0]);
}
/// Tests [`option_vec_printer`]
#[test]
fn option_vec_printer() {
use std::collections::HashMap;
use ytbn_graphing_software::option_vec_printer;
let values_strings: HashMap<Vec<Option<&str>>, &str> = HashMap::from([
(vec![None], "[None]"),
(vec![Some("text"), None], "[text, None]"),
(vec![None, None], "[None, None]"),
(vec![Some("text1"), Some("text2")], "[text1, text2]"),
]);
for (key, value) in values_strings {
assert_eq!(option_vec_printer(&key), value);
}
let values_nums = HashMap::from([
(vec![Some(10)], "[10]"),
(vec![Some(10), None], "[10, None]"),
(vec![None, Some(10)], "[None, 10]"),
(vec![Some(10), Some(100)], "[10, 100]"),
]);
for (key, value) in values_nums {
assert_eq!(option_vec_printer(&key), value);
}
}

252
tests/parsing.rs Normal file
View File

@@ -0,0 +1,252 @@
use parsing::{Hint, SUPPORTED_FUNCTIONS};
use std::collections::HashMap;
#[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!(
parsing::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)>>()
);
}
/// Returns if function with string `func_str` is valid after processing through [`process_func_str`]
fn func_is_valid(func_str: &str) -> bool {
parsing::BackingFunction::new(&parsing::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`]
fn test_process_helper(input: &str, expected: &str) {
assert_eq!(&parsing::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);
}
}
/// 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!(parsing::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 = parsing::split_function(&key);
if split.len() != 1 {
panic!("failed: {} (len: {}, split: {:?})", key, split.len(), split);
}
let generated_hint = parsing::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!(parsing::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!(
parsing::get_last_term(key.chars().collect::<Vec<char>>().as_slice()),
value
);
}
}