Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Oct 18, 2023
1 parent 37202c1 commit 5467bff
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ jobs:
- uses: actions/checkout@v3
- name: Install Rust
uses: IronCoreLabs/rust-toolchain@v1
- name: Run tests (default features, currently `pemdas`)
run: cargo test
### Pemdas
- name: Run tests (`pemdas` & `compile-time-optimizations`)
run: cargo test --no-default-features --features pemdas --features compile-time-optimizations
- name: Run tests (`pemdas` feature)
run: cargo test --no-default-features --features pemdas
### Pejmdas
- name: Run tests (`pejmdas` & `compile-time-optimizations` feature)
run: cargo test --no-default-features --features pejmdas --features compile-time-optimizations
- name: Run tests (`pejmdas` feature)
run: cargo test --no-default-features --features pejmdas
4 changes: 4 additions & 0 deletions src/tests/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* Modules */
#[cfg(not(feature = "compile-time-optimizations"))]
mod ctx;
mod error;
mod implicit_multiplication;
#[cfg(not(feature = "compile-time-optimizations"))]
mod valid;
#[cfg(feature = "compile-time-optimizations")]
mod valid_comptime_optimizations;
150 changes: 150 additions & 0 deletions src/tests/parser/valid_comptime_optimizations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* Built-in imports */
use core::f64;
/* Crate imports */
use crate::{
element::{BinOp, Element},
token::Operator,
Parser, Xprs,
};

///// Tests the following expressions:
/// 2 + pi
/// 2 * y
/// 2 + 3 * 4
/// 2 * 4 + 1
/// 3 / 2 * 4
/// 3 % 2 * 4
/// 2 + -5
/// 2 + 3 ^ 2 * 3 + 4
/// 2^2^(2^2 + 1)
/// 2 * (3 + (4 - 1))
/// sin(-cos(2))
////// With variables
/// x - x
#[allow(clippy::too_many_lines)]
fn get_valid_test_cases<'a>() -> [(&'static str, Xprs<'a>); 12] {
[
(
"2 + pi",
Xprs {
root: Element::Number(f64::consts::PI + 2.),
vars: [].into(),
},
),
(
"2 * y",
Xprs {
root: Element::BinOp(Box::new(BinOp::new(
Operator::Times,
Element::Number(2.),
Element::Variable("y"),
))),
vars: ["y"].into(),
},
),
(
"2 + 3 * 4",
Xprs {
root: Element::Number(14.),
vars: [].into(),
},
),
(
"2 * 4 + 1",
Xprs {
root: Element::BinOp(Box::new(BinOp::new(
Operator::Plus,
Element::Number(8.),
Element::Number(1.),
))),
vars: [].into(),
},
),
(
"3 / 2 * 4",
Xprs {
root: Element::BinOp(Box::new(BinOp::new(
Operator::Times,
Element::Number(1.5),
Element::Number(4.),
))),
vars: [].into(),
},
),
(
"3 % 2 * 4",
Xprs {
root: Element::BinOp(Box::new(BinOp::new(
Operator::Times,
Element::Number(1.),
Element::Number(4.),
))),
vars: [].into(),
},
),
(
"2 + -5",
Xprs {
root: Element::Number(-3.),
vars: [].into(),
},
),
(
"2 + 3 ^ 2 * 3 + 4",
Xprs {
root: Element::BinOp(Box::new(BinOp::new(
Operator::Plus,
Element::BinOp(Box::new(BinOp::new(
Operator::Plus,
Element::Number(2.),
Element::Number(27.),
))),
Element::Number(4.),
))),
vars: [].into(),
},
),
(
"2^2^(2^2 + 1)",
Xprs {
root: Element::BinOp(Box::new(BinOp::new(
Operator::Power,
Element::Number(4.),
Element::Number(5.)
))),
vars: [].into(),
},
),
(
"2 * (3 + (4 - 1))",
Xprs {
root: Element::Number(12.),
vars: [].into(),
},
),
(
"sin(-cos(2))",
Xprs {
root: Element::Number(f64::sin(-f64::cos(2.))),
vars: [].into(),
},
),
(
"x - x",
Xprs {
root: Element::Number(0.),
vars: ["x"].into(),
},
),
]
}

#[test]
fn test_valid() {
let parser = Parser::default();
for (expr, expected) in get_valid_test_cases() {
let res = parser.parse(expr);
assert!(res.is_ok(), "\nShould have passed for {expr}\n{res:?}");
assert_eq!(res.unwrap(), expected, "\n{expr}");
}
}

0 comments on commit 5467bff

Please sign in to comment.