cargo clippy + fmt
This commit is contained in:
@@ -1,159 +1,159 @@
|
||||
use parsing::{AutoComplete, Hint, Movement};
|
||||
|
||||
enum Action<'a> {
|
||||
AssertIndex(usize),
|
||||
AssertString(&'a str),
|
||||
AssertHint(&'a str),
|
||||
SetString(&'a str),
|
||||
Move(Movement),
|
||||
AssertIndex(usize),
|
||||
AssertString(&'a str),
|
||||
AssertHint(&'a str),
|
||||
SetString(&'a str),
|
||||
Move(Movement),
|
||||
}
|
||||
use Action::*;
|
||||
|
||||
fn ac_tester(actions: &[Action]) {
|
||||
let mut ac = AutoComplete::default();
|
||||
for action in actions.iter() {
|
||||
match action {
|
||||
AssertIndex(target_i) => {
|
||||
if &ac.i != target_i {
|
||||
panic!(
|
||||
"AssertIndex failed: Current: '{}' Expected: '{}'",
|
||||
ac.i, target_i
|
||||
)
|
||||
}
|
||||
}
|
||||
AssertString(target_string) => {
|
||||
if &ac.string != target_string {
|
||||
panic!(
|
||||
"AssertString failed: Current: '{}' Expected: '{}'",
|
||||
ac.string, target_string
|
||||
)
|
||||
}
|
||||
}
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
SetString(target_string) => {
|
||||
ac.update_string(target_string);
|
||||
}
|
||||
Move(target_movement) => {
|
||||
ac.register_movement(target_movement);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut ac = AutoComplete::default();
|
||||
for action in actions.iter() {
|
||||
match action {
|
||||
AssertIndex(target_i) => {
|
||||
if &ac.i != target_i {
|
||||
panic!(
|
||||
"AssertIndex failed: Current: '{}' Expected: '{}'",
|
||||
ac.i, target_i
|
||||
)
|
||||
}
|
||||
}
|
||||
AssertString(target_string) => {
|
||||
if &ac.string != target_string {
|
||||
panic!(
|
||||
"AssertString failed: Current: '{}' Expected: '{}'",
|
||||
ac.string, target_string
|
||||
)
|
||||
}
|
||||
}
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
SetString(target_string) => {
|
||||
ac.update_string(target_string);
|
||||
}
|
||||
Move(target_movement) => {
|
||||
ac.register_movement(target_movement);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single() {
|
||||
ac_tester(&[
|
||||
SetString(""),
|
||||
AssertHint("x^2"),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString(""),
|
||||
AssertHint("x^2"),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString(""),
|
||||
AssertHint("x^2"),
|
||||
Move(Movement::Complete),
|
||||
AssertString("x^2"),
|
||||
AssertHint(""),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
ac_tester(&[
|
||||
SetString(""),
|
||||
AssertHint("x^2"),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString(""),
|
||||
AssertHint("x^2"),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString(""),
|
||||
AssertHint("x^2"),
|
||||
Move(Movement::Complete),
|
||||
AssertString("x^2"),
|
||||
AssertHint(""),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi() {
|
||||
ac_tester(&[
|
||||
SetString("s"),
|
||||
AssertHint("in("),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(3),
|
||||
AssertString("s"),
|
||||
AssertHint("ignum("),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString("s"),
|
||||
AssertHint("in("),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(1),
|
||||
AssertString("s"),
|
||||
AssertHint("qrt("),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString("s"),
|
||||
AssertHint("in("),
|
||||
Move(Movement::Complete),
|
||||
AssertString("sin("),
|
||||
AssertHint(")"),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
ac_tester(&[
|
||||
SetString("s"),
|
||||
AssertHint("in("),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(3),
|
||||
AssertString("s"),
|
||||
AssertHint("ignum("),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString("s"),
|
||||
AssertHint("in("),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(1),
|
||||
AssertString("s"),
|
||||
AssertHint("qrt("),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString("s"),
|
||||
AssertHint("in("),
|
||||
Move(Movement::Complete),
|
||||
AssertString("sin("),
|
||||
AssertHint(")"),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn none() {
|
||||
// string that should give no hints
|
||||
let random = "qwert987gybhj";
|
||||
assert_eq!(parsing::generate_hint(random), &Hint::None);
|
||||
// string that should give no hints
|
||||
let random = "qwert987gybhj";
|
||||
assert_eq!(parsing::generate_hint(random), &Hint::None);
|
||||
|
||||
ac_tester(&[
|
||||
SetString(random),
|
||||
AssertHint(""),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString(random),
|
||||
AssertHint(""),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString(random),
|
||||
AssertHint(""),
|
||||
Move(Movement::Complete),
|
||||
AssertString(random),
|
||||
AssertHint(""),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
ac_tester(&[
|
||||
SetString(random),
|
||||
AssertHint(""),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString(random),
|
||||
AssertHint(""),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString(random),
|
||||
AssertHint(""),
|
||||
Move(Movement::Complete),
|
||||
AssertString(random),
|
||||
AssertHint(""),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parens() {
|
||||
ac_tester(&[
|
||||
SetString("sin(x"),
|
||||
AssertHint(")"),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString("sin(x"),
|
||||
AssertHint(")"),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString("sin(x"),
|
||||
AssertHint(")"),
|
||||
Move(Movement::Complete),
|
||||
AssertString("sin(x)"),
|
||||
AssertHint(""),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
ac_tester(&[
|
||||
SetString("sin(x"),
|
||||
AssertHint(")"),
|
||||
Move(Movement::Up),
|
||||
AssertIndex(0),
|
||||
AssertString("sin(x"),
|
||||
AssertHint(")"),
|
||||
Move(Movement::Down),
|
||||
AssertIndex(0),
|
||||
AssertString("sin(x"),
|
||||
AssertHint(")"),
|
||||
Move(Movement::Complete),
|
||||
AssertString("sin(x)"),
|
||||
AssertHint(""),
|
||||
AssertIndex(0),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,262 +1,273 @@
|
||||
use ytbn_graphing_software::{AppSettings, EguiHelper, FunctionEntry, Riemann};
|
||||
|
||||
fn app_settings_constructor(
|
||||
sum: Riemann, integral_min_x: f64, integral_max_x: f64, pixel_width: usize,
|
||||
integral_num: usize, min_x: f64, max_x: f64,
|
||||
sum: Riemann,
|
||||
integral_min_x: f64,
|
||||
integral_max_x: f64,
|
||||
pixel_width: usize,
|
||||
integral_num: usize,
|
||||
min_x: f64,
|
||||
max_x: f64,
|
||||
) -> AppSettings {
|
||||
AppSettings {
|
||||
riemann_sum: sum,
|
||||
integral_min_x,
|
||||
integral_max_x,
|
||||
min_x,
|
||||
max_x,
|
||||
integral_changed: true,
|
||||
integral_num,
|
||||
do_extrema: false,
|
||||
do_roots: false,
|
||||
plot_width: pixel_width,
|
||||
}
|
||||
AppSettings {
|
||||
riemann_sum: sum,
|
||||
integral_min_x,
|
||||
integral_max_x,
|
||||
min_x,
|
||||
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),
|
||||
(-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),
|
||||
(-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),
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
fn do_test(sum: Riemann, area_target: f64) {
|
||||
let settings = app_settings_constructor(sum, -1.0, 1.0, 10, 10, -1.0, 1.0);
|
||||
let settings = app_settings_constructor(sum, -1.0, 1.0, 10, 10, -1.0, 1.0);
|
||||
|
||||
let mut function = FunctionEntry::default();
|
||||
function.update_string("x^2");
|
||||
function.integral = true;
|
||||
function.derivative = true;
|
||||
let mut function = FunctionEntry::default();
|
||||
function.update_string("x^2");
|
||||
function.integral = true;
|
||||
function.derivative = true;
|
||||
|
||||
let mut settings = settings;
|
||||
{
|
||||
function.calculate(true, true, false, settings);
|
||||
assert!(!function.back_data.is_empty());
|
||||
assert_eq!(function.back_data.len(), settings.plot_width + 1);
|
||||
let mut settings = settings;
|
||||
{
|
||||
function.calculate(true, true, false, settings);
|
||||
assert!(!function.back_data.is_empty());
|
||||
assert_eq!(function.back_data.len(), settings.plot_width + 1);
|
||||
|
||||
assert!(function.integral);
|
||||
assert!(function.derivative);
|
||||
assert!(function.integral);
|
||||
assert!(function.derivative);
|
||||
|
||||
assert_eq!(!function.root_data.is_empty(), settings.do_roots);
|
||||
assert_eq!(!function.extrema_data.is_empty(), settings.do_extrema);
|
||||
assert!(!function.derivative_data.is_empty());
|
||||
assert!(function.integral_data.is_some());
|
||||
assert_eq!(!function.root_data.is_empty(), settings.do_roots);
|
||||
assert_eq!(!function.extrema_data.is_empty(), settings.do_extrema);
|
||||
assert!(!function.derivative_data.is_empty());
|
||||
assert!(function.integral_data.is_some());
|
||||
|
||||
assert_eq!(function.integral_data.clone().unwrap().1, area_target);
|
||||
assert_eq!(function.integral_data.clone().unwrap().1, area_target);
|
||||
|
||||
let a = function.derivative_data.clone().to_tuple();
|
||||
let a = function.derivative_data.clone().to_tuple();
|
||||
|
||||
assert_eq!(a.len(), DERIVATIVE_TARGET.len());
|
||||
assert_eq!(a.len(), DERIVATIVE_TARGET.len());
|
||||
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a[i].0 as f32, DERIVATIVE_TARGET[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a[i].1 as f32, DERIVATIVE_TARGET[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a, DERIVATIVE_TARGET);
|
||||
}
|
||||
}
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a[i].0 as f32, DERIVATIVE_TARGET[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a[i].1 as f32, DERIVATIVE_TARGET[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a, DERIVATIVE_TARGET);
|
||||
}
|
||||
}
|
||||
|
||||
let a_1 = function.back_data.clone().to_tuple();
|
||||
let a_1 = function.back_data.clone().to_tuple();
|
||||
|
||||
assert_eq!(a_1.len(), BACK_TARGET.len());
|
||||
assert_eq!(a_1.len(), BACK_TARGET.len());
|
||||
|
||||
assert_eq!(a.len(), BACK_TARGET.len());
|
||||
assert_eq!(a.len(), BACK_TARGET.len());
|
||||
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a_1[i].0 as f32, BACK_TARGET[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a_1[i].1 as f32, BACK_TARGET[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a_1, BACK_TARGET);
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a_1[i].0 as f32, BACK_TARGET[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a_1[i].1 as f32, BACK_TARGET[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a_1, BACK_TARGET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
settings.min_x += 1.0;
|
||||
settings.max_x += 1.0;
|
||||
function.calculate(true, true, false, settings);
|
||||
{
|
||||
settings.min_x += 1.0;
|
||||
settings.max_x += 1.0;
|
||||
function.calculate(true, true, false, settings);
|
||||
|
||||
let a = function
|
||||
.derivative_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let a = function
|
||||
.derivative_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
let b = DERIVATIVE_TARGET
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let b = DERIVATIVE_TARGET
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
assert_eq!(a.len(), b.len());
|
||||
assert_eq!(a.len(), b.len());
|
||||
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a[i].0 as f32, b[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a[i].1 as f32, b[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a, b);
|
||||
}
|
||||
}
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a[i].0 as f32, b[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a[i].1 as f32, b[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a, b);
|
||||
}
|
||||
}
|
||||
|
||||
let a_1 = function
|
||||
.back_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let a_1 = function
|
||||
.back_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
let b_1 = BACK_TARGET
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let b_1 = BACK_TARGET
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
assert_eq!(a_1.len(), b_1.len());
|
||||
assert_eq!(a_1.len(), b_1.len());
|
||||
|
||||
assert_eq!(a.len(), b_1.len());
|
||||
assert_eq!(a.len(), b_1.len());
|
||||
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a_1[i].0 as f32, b_1[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a_1[i].1 as f32, b_1[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a_1, b_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a_1[i].0 as f32, b_1[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a_1[i].1 as f32, b_1[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a_1, b_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
settings.min_x -= 2.0;
|
||||
settings.max_x -= 2.0;
|
||||
function.calculate(true, true, false, settings);
|
||||
{
|
||||
settings.min_x -= 2.0;
|
||||
settings.max_x -= 2.0;
|
||||
function.calculate(true, true, false, settings);
|
||||
|
||||
let a = function
|
||||
.derivative_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let a = function
|
||||
.derivative_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
let b = DERIVATIVE_TARGET
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let b = DERIVATIVE_TARGET
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
assert_eq!(a.len(), b.len());
|
||||
assert_eq!(a.len(), b.len());
|
||||
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a[i].0 as f32, b[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a[i].1 as f32, b[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a, b);
|
||||
}
|
||||
}
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a[i].0 as f32, b[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a[i].1 as f32, b[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a, b);
|
||||
}
|
||||
}
|
||||
|
||||
let a_1 = function
|
||||
.back_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let a_1 = function
|
||||
.back_data
|
||||
.clone()
|
||||
.to_tuple()
|
||||
.iter()
|
||||
.rev()
|
||||
.take(6)
|
||||
.rev()
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
let b_1 = BACK_TARGET
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
let b_1 = BACK_TARGET
|
||||
.iter()
|
||||
.take(6)
|
||||
.cloned()
|
||||
.collect::<Vec<(f64, f64)>>();
|
||||
|
||||
assert_eq!(a_1.len(), b_1.len());
|
||||
assert_eq!(a_1.len(), b_1.len());
|
||||
|
||||
assert_eq!(a.len(), b_1.len());
|
||||
assert_eq!(a.len(), b_1.len());
|
||||
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a_1[i].0 as f32, b_1[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a_1[i].1 as f32, b_1[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a_1, b_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0..a.len() {
|
||||
if !emath::almost_equal(a_1[i].0 as f32, b_1[i].0 as f32, f32::EPSILON)
|
||||
| !emath::almost_equal(a_1[i].1 as f32, b_1[i].1 as f32, f32::EPSILON)
|
||||
{
|
||||
panic!("Expected: {:?}\nGot: {:?}", a_1, b_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
function.update_string("sin(x)");
|
||||
assert!(function.get_test_result().is_none());
|
||||
assert_eq!(&function.raw_func_str, "sin(x)");
|
||||
{
|
||||
function.update_string("sin(x)");
|
||||
assert!(function.get_test_result().is_none());
|
||||
assert_eq!(&function.raw_func_str, "sin(x)");
|
||||
|
||||
function.integral = false;
|
||||
function.derivative = false;
|
||||
function.integral = false;
|
||||
function.derivative = false;
|
||||
|
||||
assert!(!function.integral);
|
||||
assert!(!function.derivative);
|
||||
assert!(!function.integral);
|
||||
assert!(!function.derivative);
|
||||
|
||||
assert!(function.back_data.is_empty());
|
||||
assert!(function.integral_data.is_none());
|
||||
assert!(function.root_data.is_empty());
|
||||
assert!(function.extrema_data.is_empty());
|
||||
assert!(function.derivative_data.is_empty());
|
||||
assert!(function.back_data.is_empty());
|
||||
assert!(function.integral_data.is_none());
|
||||
assert!(function.root_data.is_empty());
|
||||
assert!(function.extrema_data.is_empty());
|
||||
assert!(function.derivative_data.is_empty());
|
||||
|
||||
settings.min_x -= 1.0;
|
||||
settings.max_x -= 1.0;
|
||||
settings.min_x -= 1.0;
|
||||
settings.max_x -= 1.0;
|
||||
|
||||
function.calculate(true, true, false, settings);
|
||||
function.calculate(true, true, false, settings);
|
||||
|
||||
assert!(!function.back_data.is_empty());
|
||||
assert!(function.integral_data.is_none());
|
||||
assert!(function.root_data.is_empty());
|
||||
assert!(function.extrema_data.is_empty());
|
||||
assert!(!function.derivative_data.is_empty());
|
||||
}
|
||||
assert!(!function.back_data.is_empty());
|
||||
assert!(function.integral_data.is_none());
|
||||
assert!(function.root_data.is_empty());
|
||||
assert!(function.extrema_data.is_empty());
|
||||
assert!(!function.derivative_data.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn left_function() { do_test(Riemann::Left, 0.9600000000000001); }
|
||||
fn left_function() {
|
||||
do_test(Riemann::Left, 0.9600000000000001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn middle_function() { do_test(Riemann::Middle, 0.92); }
|
||||
fn middle_function() {
|
||||
do_test(Riemann::Middle, 0.92);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn right_function() { do_test(Riemann::Right, 0.8800000000000001); }
|
||||
fn right_function() {
|
||||
do_test(Riemann::Right, 0.8800000000000001);
|
||||
}
|
||||
|
||||
172
tests/misc.rs
172
tests/misc.rs
@@ -2,90 +2,90 @@
|
||||
/// Ensures [`decimal_round`] returns correct values
|
||||
#[test]
|
||||
fn decimal_round() {
|
||||
use ytbn_graphing_software::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.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(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);
|
||||
assert_eq!(decimal_round(1.9, 0), 2.0);
|
||||
assert_eq!(decimal_round(1.9, 1), 1.9);
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn step_helper() {
|
||||
use ytbn_graphing_software::step_helper;
|
||||
use ytbn_graphing_software::step_helper;
|
||||
|
||||
assert_eq!(
|
||||
step_helper(10, 2.0, 3.0),
|
||||
vec![2.0, 5.0, 8.0, 11.0, 14.0, 17.0, 20.0, 23.0, 26.0, 29.0]
|
||||
);
|
||||
assert_eq!(
|
||||
step_helper(10, 2.0, 3.0),
|
||||
vec![2.0, 5.0, 8.0, 11.0, 14.0, 17.0, 20.0, 23.0, 26.0, 29.0]
|
||||
);
|
||||
}
|
||||
|
||||
/// Tests [`option_vec_printer`]
|
||||
#[test]
|
||||
fn option_vec_printer() {
|
||||
use std::collections::HashMap;
|
||||
use ytbn_graphing_software::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]"),
|
||||
]);
|
||||
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);
|
||||
}
|
||||
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]"),
|
||||
]);
|
||||
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);
|
||||
}
|
||||
for (key, value) in values_nums {
|
||||
assert_eq!(option_vec_printer(&key), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hashed_storage() {
|
||||
use ytbn_graphing_software::{hashed_storage_create, hashed_storage_read};
|
||||
use ytbn_graphing_software::{hashed_storage_create, hashed_storage_read};
|
||||
|
||||
let commit = "abcdefeg".chars().map(|c| c as u8).collect::<Vec<u8>>();
|
||||
let data = "really cool data"
|
||||
.chars()
|
||||
.map(|c| c as u8)
|
||||
.collect::<Vec<u8>>();
|
||||
let storage_tmp: [u8; 8] = commit
|
||||
.as_slice()
|
||||
.try_into()
|
||||
.expect("cannot turn into [u8; 8]");
|
||||
let storage = hashed_storage_create(storage_tmp, data.as_slice());
|
||||
let commit = "abcdefeg".chars().map(|c| c as u8).collect::<Vec<u8>>();
|
||||
let data = "really cool data"
|
||||
.chars()
|
||||
.map(|c| c as u8)
|
||||
.collect::<Vec<u8>>();
|
||||
let storage_tmp: [u8; 8] = commit
|
||||
.as_slice()
|
||||
.try_into()
|
||||
.expect("cannot turn into [u8; 8]");
|
||||
let storage = hashed_storage_create(storage_tmp, data.as_slice());
|
||||
|
||||
let read = hashed_storage_read(&storage);
|
||||
assert_eq!(
|
||||
read.map(|(a, b)| (a.to_vec(), b.to_vec())),
|
||||
Some((commit.to_vec(), data.to_vec()))
|
||||
);
|
||||
let read = hashed_storage_read(&storage);
|
||||
assert_eq!(
|
||||
read.map(|(a, b)| (a.to_vec(), b.to_vec())),
|
||||
Some((commit.to_vec(), data.to_vec()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_hashed_storage() {
|
||||
use ytbn_graphing_software::hashed_storage_read;
|
||||
assert_eq!(hashed_storage_read("aaaa"), None);
|
||||
use ytbn_graphing_software::hashed_storage_read;
|
||||
assert_eq!(hashed_storage_read("aaaa"), None);
|
||||
}
|
||||
|
||||
// #[test]
|
||||
@@ -141,45 +141,45 @@ fn invalid_hashed_storage() {
|
||||
|
||||
#[test]
|
||||
fn newtons_method() {
|
||||
use parsing::BackingFunction;
|
||||
use parsing::FlatExWrapper;
|
||||
fn get_flatexwrapper(func: &str) -> FlatExWrapper {
|
||||
let mut backing_func = BackingFunction::new(func).unwrap();
|
||||
backing_func.get_function_derivative(0).clone()
|
||||
}
|
||||
use parsing::BackingFunction;
|
||||
use parsing::FlatExWrapper;
|
||||
fn get_flatexwrapper(func: &str) -> FlatExWrapper {
|
||||
let mut backing_func = BackingFunction::new(func).unwrap();
|
||||
backing_func.get_function_derivative(0).clone()
|
||||
}
|
||||
|
||||
use ytbn_graphing_software::newtons_method;
|
||||
use ytbn_graphing_software::newtons_method;
|
||||
|
||||
let data = newtons_method(
|
||||
&get_flatexwrapper("x^2 -1"),
|
||||
&get_flatexwrapper("2x"),
|
||||
3.0,
|
||||
&(0.0..5.0),
|
||||
f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, Some(1.0));
|
||||
let data = newtons_method(
|
||||
&get_flatexwrapper("x^2 -1"),
|
||||
&get_flatexwrapper("2x"),
|
||||
3.0,
|
||||
&(0.0..5.0),
|
||||
f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, Some(1.0));
|
||||
|
||||
let data = newtons_method(
|
||||
&get_flatexwrapper("sin(x)"),
|
||||
&get_flatexwrapper("cos(x)"),
|
||||
3.0,
|
||||
&(2.95..3.18),
|
||||
f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, Some(std::f64::consts::PI));
|
||||
let data = newtons_method(
|
||||
&get_flatexwrapper("sin(x)"),
|
||||
&get_flatexwrapper("cos(x)"),
|
||||
3.0,
|
||||
&(2.95..3.18),
|
||||
f64::EPSILON,
|
||||
);
|
||||
assert_eq!(data, Some(std::f64::consts::PI));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_unicode_hash() {
|
||||
use ytbn_graphing_software::to_unicode_hash;
|
||||
assert_eq!(to_unicode_hash('\u{1f31e}'), "\\U1F31E");
|
||||
use ytbn_graphing_software::to_unicode_hash;
|
||||
assert_eq!(to_unicode_hash('\u{1f31e}'), "\\U1F31E");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_chars_array() {
|
||||
use ytbn_graphing_software::to_chars_array;
|
||||
assert_eq!(
|
||||
to_chars_array(vec!['\u{1f31e}', '\u{2d12c}']),
|
||||
r"['\u{1f31e}', '\u{2d12c}']"
|
||||
);
|
||||
use ytbn_graphing_software::to_chars_array;
|
||||
assert_eq!(
|
||||
to_chars_array(vec!['\u{1f31e}', '\u{2d12c}']),
|
||||
r"['\u{1f31e}', '\u{2d12c}']"
|
||||
);
|
||||
}
|
||||
|
||||
428
tests/parsing.rs
428
tests/parsing.rs
@@ -3,292 +3,292 @@ use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn hashmap_gen_test() {
|
||||
let data = ["time", "text", "test"];
|
||||
let expect = vec![
|
||||
("t", "Hint::Many(&[\"ime(\", \"ext(\", \"est(\"])"),
|
||||
("te", "Hint::Many(&[\"xt(\", \"st(\"])"),
|
||||
("tes", "Hint::Single(\"t(\")"),
|
||||
("test", "Hint::Single(\"(\")"),
|
||||
("tex", "Hint::Single(\"t(\")"),
|
||||
("text", "Hint::Single(\"(\")"),
|
||||
("ti", "Hint::Single(\"me(\")"),
|
||||
("tim", "Hint::Single(\"e(\")"),
|
||||
("time", "Hint::Single(\"(\")"),
|
||||
];
|
||||
let data = ["time", "text", "test"];
|
||||
let expect = vec![
|
||||
("t", "Hint::Many(&[\"ime(\", \"ext(\", \"est(\"])"),
|
||||
("te", "Hint::Many(&[\"xt(\", \"st(\"])"),
|
||||
("tes", "Hint::Single(\"t(\")"),
|
||||
("test", "Hint::Single(\"(\")"),
|
||||
("tex", "Hint::Single(\"t(\")"),
|
||||
("text", "Hint::Single(\"(\")"),
|
||||
("ti", "Hint::Single(\"me(\")"),
|
||||
("tim", "Hint::Single(\"e(\")"),
|
||||
("time", "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)>>()
|
||||
);
|
||||
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()
|
||||
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
|
||||
);
|
||||
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);
|
||||
}
|
||||
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),
|
||||
]);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
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)"),
|
||||
]);
|
||||
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 (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);
|
||||
}
|
||||
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("])),
|
||||
]);
|
||||
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);
|
||||
}
|
||||
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),
|
||||
]);
|
||||
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);
|
||||
}
|
||||
for (key, value) in values {
|
||||
assert_eq!(value.to_string(), key);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_function() {
|
||||
use parsing::SplitType;
|
||||
use parsing::SplitType;
|
||||
|
||||
SUPPORTED_FUNCTIONS
|
||||
.iter()
|
||||
.flat_map(|func1| {
|
||||
SUPPORTED_FUNCTIONS
|
||||
.iter()
|
||||
.map(|func2| func1.to_string() + func2)
|
||||
.collect::<Vec<String>>()
|
||||
})
|
||||
.filter(|func| !SUPPORTED_FUNCTIONS.contains(&func.as_str()))
|
||||
.for_each(|key| {
|
||||
let split = parsing::split_function(&key, SplitType::Multiplication);
|
||||
SUPPORTED_FUNCTIONS
|
||||
.iter()
|
||||
.flat_map(|func1| {
|
||||
SUPPORTED_FUNCTIONS
|
||||
.iter()
|
||||
.map(|func2| func1.to_string() + func2)
|
||||
.collect::<Vec<String>>()
|
||||
})
|
||||
.filter(|func| !SUPPORTED_FUNCTIONS.contains(&func.as_str()))
|
||||
.for_each(|key| {
|
||||
let split = parsing::split_function(&key, SplitType::Multiplication);
|
||||
|
||||
if split.len() != 1 {
|
||||
panic!("failed: {} (len: {}, split: {:?})", key, split.len(), split);
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
let generated_hint = parsing::generate_hint(&key);
|
||||
if generated_hint.is_none() {
|
||||
println!("success: {}", key);
|
||||
} else {
|
||||
panic!("failed: {} (Hint: '{}')", key, generated_hint);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_function_multiplication() {
|
||||
use parsing::SplitType;
|
||||
use parsing::SplitType;
|
||||
|
||||
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"]),
|
||||
("a1b2c3d4", vec!["a1b2c3d4"]),
|
||||
("cos(sin(x)cos(x))", vec!["cos(sin(x)", "cos(x))"]),
|
||||
("", Vec::new()),
|
||||
]);
|
||||
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"]),
|
||||
("a1b2c3d4", vec!["a1b2c3d4"]),
|
||||
("cos(sin(x)cos(x))", vec!["cos(sin(x)", "cos(x))"]),
|
||||
("", Vec::new()),
|
||||
]);
|
||||
|
||||
for (key, value) in values {
|
||||
assert_eq!(
|
||||
parsing::split_function(key, SplitType::Multiplication),
|
||||
value
|
||||
);
|
||||
}
|
||||
for (key, value) in values {
|
||||
assert_eq!(
|
||||
parsing::split_function(key, SplitType::Multiplication),
|
||||
value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn split_function_terms() {
|
||||
use parsing::SplitType;
|
||||
use parsing::SplitType;
|
||||
|
||||
let values = HashMap::from([
|
||||
(
|
||||
"cos(sin(x)cos(x))",
|
||||
vec!["cos(", "sin(", "x)", "cos(", "x))"],
|
||||
),
|
||||
("", Vec::new()),
|
||||
]);
|
||||
let values = HashMap::from([
|
||||
(
|
||||
"cos(sin(x)cos(x))",
|
||||
vec!["cos(", "sin(", "x)", "cos(", "x))"],
|
||||
),
|
||||
("", Vec::new()),
|
||||
]);
|
||||
|
||||
for (key, value) in values {
|
||||
assert_eq!(parsing::split_function(key, SplitType::Term), value);
|
||||
}
|
||||
for (key, value) in values {
|
||||
assert_eq!(parsing::split_function(key, SplitType::Term), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hint_tests() {
|
||||
{
|
||||
let hint = Hint::None;
|
||||
assert!(hint.is_none());
|
||||
assert!(!hint.is_some());
|
||||
assert!(!hint.is_single());
|
||||
}
|
||||
{
|
||||
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::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());
|
||||
}
|
||||
{
|
||||
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"),
|
||||
("exp(cos(exp(sin", "sin"),
|
||||
]);
|
||||
let values = HashMap::from([
|
||||
("cos(x)", "x)"),
|
||||
("cos(", "cos("),
|
||||
("aaaaaaaaaaa", "aaaaaaaaaaa"),
|
||||
("x", "x"),
|
||||
("xxx", "x"),
|
||||
("x*x", "x"),
|
||||
("10*10", "10"),
|
||||
("sin(cos", "cos"),
|
||||
("exp(cos(exp(sin", "sin"),
|
||||
]);
|
||||
|
||||
for (key, value) in values {
|
||||
assert_eq!(
|
||||
parsing::get_last_term(key.chars().collect::<Vec<char>>().as_slice()),
|
||||
Some(value.to_owned())
|
||||
);
|
||||
}
|
||||
for (key, value) in values {
|
||||
assert_eq!(
|
||||
parsing::get_last_term(key.chars().collect::<Vec<char>>().as_slice()),
|
||||
Some(value.to_owned())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hint_accessor() {
|
||||
assert_eq!(Hint::Single("hint").many(), None);
|
||||
assert_eq!(Hint::Single("hint").single(), Some("hint"));
|
||||
assert_eq!(Hint::Single("hint").many(), None);
|
||||
assert_eq!(Hint::Single("hint").single(), Some("hint"));
|
||||
|
||||
assert_eq!(Hint::Many(&["hint", "hint2"]).single(), None);
|
||||
assert_eq!(
|
||||
Hint::Many(&["hint", "hint2"]).many(),
|
||||
Some(["hint", "hint2"].as_slice())
|
||||
);
|
||||
assert_eq!(Hint::Many(&["hint", "hint2"]).single(), None);
|
||||
assert_eq!(
|
||||
Hint::Many(&["hint", "hint2"]).many(),
|
||||
Some(["hint", "hint2"].as_slice())
|
||||
);
|
||||
|
||||
assert_eq!(Hint::None.single(), None);
|
||||
assert_eq!(Hint::None.many(), None);
|
||||
assert_eq!(Hint::None.single(), None);
|
||||
assert_eq!(Hint::None.many(), None);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user