From 5467bffe1f4e24738acbf15a2aa418a3c0800489 Mon Sep 17 00:00:00 2001 From: vic1707 <28602203+vic1707@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:05:16 +0200 Subject: [PATCH] update tests --- .github/workflows/tests.yml | 10 +- src/tests/parser/mod.rs | 4 + .../parser/valid_comptime_optimizations.rs | 150 ++++++++++++++++++ 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 src/tests/parser/valid_comptime_optimizations.rs diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a1e050d..b6d7794 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/src/tests/parser/mod.rs b/src/tests/parser/mod.rs index 51c9512..8fb79c8 100644 --- a/src/tests/parser/mod.rs +++ b/src/tests/parser/mod.rs @@ -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; diff --git a/src/tests/parser/valid_comptime_optimizations.rs b/src/tests/parser/valid_comptime_optimizations.rs new file mode 100644 index 0000000..a3caaf2 --- /dev/null +++ b/src/tests/parser/valid_comptime_optimizations.rs @@ -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}"); + } +}