reorganize testing
This commit is contained in:
@@ -9,7 +9,7 @@ use egui::{
|
||||
Checkbox, Context,
|
||||
};
|
||||
use epaint::Color32;
|
||||
use parsing::parsing::{process_func_str, BackingFunction};
|
||||
use parsing::{process_func_str, BackingFunction};
|
||||
use std::{
|
||||
fmt::{self, Debug},
|
||||
intrinsics::assume,
|
||||
@@ -500,7 +500,7 @@ impl FunctionEntry {
|
||||
pub fn invalidate_nth(&mut self) { self.nth_derivative_data = None }
|
||||
|
||||
/// Runs asserts to make sure everything is the expected value
|
||||
#[cfg(test)]
|
||||
#[allow(dead_code)]
|
||||
pub fn tests(
|
||||
&mut self, settings: AppSettings, back_target: Vec<(f64, f64)>,
|
||||
derivative_target: Vec<(f64, f64)>, area_target: f64, min_x: f64, max_x: f64,
|
||||
@@ -553,77 +553,3 @@ impl FunctionEntry {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn app_settings_constructor(
|
||||
sum: Riemann, integral_min_x: f64, integral_max_x: f64, pixel_width: usize,
|
||||
integral_num: usize,
|
||||
) -> AppSettings {
|
||||
crate::math_app::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_entry_test() {
|
||||
do_test(Riemann::Left, 0.9600000000000001);
|
||||
do_test(Riemann::Middle, 0.92);
|
||||
do_test(Riemann::Right, 0.8800000000000001);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::function_entry::FunctionEntry;
|
||||
use crate::widgets::{widgets_ontop, Movement};
|
||||
use egui::{Button, Id, Key, Modifiers, TextEdit, WidgetText};
|
||||
use emath::vec2;
|
||||
use parsing::suggestions::Hint;
|
||||
use parsing::Hint;
|
||||
use std::ops::BitXorAssign;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -204,7 +204,9 @@ impl FunctionManager {
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize { self.functions.len() }
|
||||
|
||||
#[inline]
|
||||
pub fn get_entries_mut(&mut self) -> &mut Vec<(Id, FunctionEntry)> { &mut self.functions }
|
||||
|
||||
#[inline]
|
||||
pub fn get_entries(&self) -> &Vec<(Id, FunctionEntry)> { &self.functions }
|
||||
}
|
||||
|
||||
@@ -20,6 +20,13 @@ mod math_app;
|
||||
mod misc;
|
||||
mod widgets;
|
||||
|
||||
pub use crate::{
|
||||
function_entry::{FunctionEntry, Riemann},
|
||||
math_app::AppSettings,
|
||||
misc::{decimal_round, option_vec_printer, resolution_helper, SteppedVector},
|
||||
widgets::{AutoComplete, Movement},
|
||||
};
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
94
src/misc.rs
94
src/misc.rs
@@ -283,97 +283,3 @@ pub fn resolution_helper(max_i: usize, min_x: &f64, resolution: &f64) -> Vec<f64
|
||||
pub fn step_helper(max_i: usize, min_x: &f64, step: &f64) -> Vec<f64> {
|
||||
(0..max_i).map(|x| (x as f64 * step) + min_x).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Tests [`SteppedVector`] to ensure everything works properly (helped me
|
||||
/// find a bunch of issues)
|
||||
#[test]
|
||||
fn stepped_vector_test() {
|
||||
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_test() {
|
||||
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_test() {
|
||||
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_test() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
148
src/widgets.rs
148
src/widgets.rs
@@ -1,6 +1,6 @@
|
||||
use std::intrinsics::assume;
|
||||
|
||||
use parsing::suggestions::{self, generate_hint, Hint};
|
||||
use parsing::{generate_hint, Hint, HINT_EMPTY};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum Movement {
|
||||
@@ -10,6 +10,10 @@ pub enum Movement {
|
||||
None,
|
||||
}
|
||||
|
||||
impl Movement {
|
||||
pub const fn is_none(&self) -> bool { matches!(&self, Self::None) }
|
||||
}
|
||||
|
||||
impl const Default for Movement {
|
||||
fn default() -> Self { Self::None }
|
||||
}
|
||||
@@ -28,7 +32,7 @@ impl<'a> const Default for AutoComplete<'a> {
|
||||
impl<'a> AutoComplete<'a> {
|
||||
const EMPTY: AutoComplete<'a> = Self {
|
||||
i: 0,
|
||||
hint: &suggestions::HINT_EMPTY,
|
||||
hint: &HINT_EMPTY,
|
||||
string: String::new(),
|
||||
};
|
||||
|
||||
@@ -52,7 +56,7 @@ impl<'a> AutoComplete<'a> {
|
||||
}
|
||||
|
||||
pub fn register_movement(&mut self, movement: &Movement) {
|
||||
if movement == &Movement::None {
|
||||
if movement.is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -109,141 +113,3 @@ pub fn widgets_ontop<R>(
|
||||
|
||||
area.show(ui.ctx(), |ui| add_contents(ui)).inner
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod autocomplete_tests {
|
||||
use super::*;
|
||||
|
||||
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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user