updated 109
This commit is contained in:
parent
53b8ececc8
commit
ed092abc3f
|
@ -42,3 +42,44 @@ fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_expression_one()
|
||||
{
|
||||
let value = evaluate("2*3+(4-5)+2^3/4".to_string());
|
||||
assert_eq!(value.unwrap(), 7.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expression_two()
|
||||
{
|
||||
let value = evaluate("5^7+8-33".to_string());
|
||||
assert_eq!(value.unwrap(), 78100.00);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expression_three()
|
||||
{
|
||||
let value = evaluate("2+2/4+10^2".to_string());
|
||||
assert_eq!(value.unwrap(), 102.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expression_four()
|
||||
{
|
||||
let value = evaluate("7^2+4*7".to_string());
|
||||
assert_eq!(value.unwrap(), 77.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_expression_five()
|
||||
{
|
||||
let value = evaluate("5*2+(5/2)+5".to_string());
|
||||
assert_eq!(value.unwrap(), 17.5);
|
||||
}
|
||||
}
|
|
@ -40,10 +40,41 @@ pub fn eval(expr: Node) -> Result<f64, Box<dyn error::Error>> {
|
|||
}
|
||||
}
|
||||
|
||||
//Unit tests
|
||||
// Unit tests
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
// Original Tests
|
||||
#[test]
|
||||
fn test_expr1()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("1+2-3").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 0.0);
|
||||
}
|
||||
#[test]
|
||||
fn test_expr2()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("3+2-1*5/4").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 3.75);
|
||||
}
|
||||
#[test]
|
||||
fn test_expr3()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("3+3|4").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 6.0);
|
||||
}
|
||||
|
||||
/* New Tests */
|
||||
// Addition Test
|
||||
#[test]
|
||||
fn test_add_expr()
|
||||
{
|
||||
|
@ -53,28 +84,48 @@ mod tests {
|
|||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 3.0);
|
||||
}
|
||||
|
||||
// Subtraction Test
|
||||
#[test]
|
||||
fn test_expr1() {
|
||||
fn test_sub_expr()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("1+2-3").unwrap().parse().unwrap();
|
||||
let ast = Parser::new("100-50").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 0.0);
|
||||
assert_eq!(value, 50.0);
|
||||
}
|
||||
|
||||
// Multiplication Test
|
||||
#[test]
|
||||
fn test_expr2() {
|
||||
fn test_multiply_expr()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("3+2-1*5/4").unwrap().parse().unwrap();
|
||||
let ast = Parser::new("30*25").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 3.75);
|
||||
assert_eq!(value, 750.0);
|
||||
}
|
||||
|
||||
// Division Tests
|
||||
#[test]
|
||||
fn test_expr3() {
|
||||
fn test_divide_expr()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("3+3|4").unwrap().parse().unwrap();
|
||||
let ast = Parser::new("1/2").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 6.0);
|
||||
assert_eq!(value, 0.5);
|
||||
}
|
||||
|
||||
// Power or Exponent Tests
|
||||
#[test]
|
||||
fn test_caret_expr()
|
||||
{
|
||||
use crate::parsemath::parser::Parser;
|
||||
|
||||
let ast = Parser::new("2^5").unwrap().parse().unwrap();
|
||||
let value = eval(ast).unwrap();
|
||||
assert_eq!(value, 32.0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -205,18 +205,63 @@ impl From<Box<dyn std::error::Error>> for ParseError {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::parsemath::ast::Node::{Add, Or, Number};
|
||||
use crate::parsemath::ast::Node::{And, Or, Add, Subtract, Multiply, Divide, Number, Caret};
|
||||
|
||||
// Original Tests
|
||||
#[test]
|
||||
fn test_addition() {
|
||||
fn test_addition()
|
||||
{
|
||||
let mut parser = Parser::new("1+2").unwrap();
|
||||
let expected = Add(Box::new(Number(1.0)), Box::new(Number(2.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_or() {
|
||||
fn test_bitwise_or()
|
||||
{
|
||||
let mut parser = Parser::new("6|2").unwrap();
|
||||
let expected = Or(Box::new(Number(6.0)), Box::new(Number(2.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
|
||||
// New Tests
|
||||
#[test]
|
||||
fn test_subtract()
|
||||
{
|
||||
let mut parser = Parser::new("60-32").unwrap();
|
||||
let expected = Subtract(Box::new(Number(60.0)), Box::new(Number(32.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mutiply()
|
||||
{
|
||||
let mut parser = Parser::new("7*2").unwrap();
|
||||
let expected = Multiply(Box::new(Number(7.0)), Box::new(Number(2.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divide()
|
||||
{
|
||||
let mut parser = Parser::new("95/10").unwrap();
|
||||
let expected = Divide(Box::new(Number(95.0)), Box::new(Number(10.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitwise_and()
|
||||
{
|
||||
let mut parser = Parser::new("50&32").unwrap();
|
||||
let expected = And(Box::new(Number(50.0)), Box::new(Number(32.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_caret()
|
||||
{
|
||||
let mut parser = Parser::new("4^5").unwrap();
|
||||
let expected = Caret(Box::new(Number(4.0)), Box::new(Number(5.0)));
|
||||
assert_eq!(parser.parse().unwrap(), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,6 +77,7 @@ mod tests
|
|||
{
|
||||
use super::*;
|
||||
|
||||
// Original Tests
|
||||
#[test]
|
||||
fn test_positive_integer()
|
||||
{
|
||||
|
@ -95,4 +96,33 @@ mod tests
|
|||
let mut tokenizer = Tokenizer::new("#$%");
|
||||
assert_eq!(tokenizer.next(), None);
|
||||
}
|
||||
|
||||
// New Tests
|
||||
#[test]
|
||||
fn test_addition_sign()
|
||||
{
|
||||
let mut tokenizer = Tokenizer::new("+");
|
||||
assert_eq!(tokenizer.next().unwrap(), Token::Add);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_subtraction_sign()
|
||||
{
|
||||
let mut tokenizer = Tokenizer::new("-");
|
||||
assert_eq!(tokenizer.next().unwrap(), Token::Subtract);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiplication_sign()
|
||||
{
|
||||
let mut tokenizer = Tokenizer::new("*");
|
||||
assert_eq!(tokenizer.next().unwrap(), Token::Multiply);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_division_sign()
|
||||
{
|
||||
let mut tokenizer = Tokenizer::new("/");
|
||||
assert_eq!(tokenizer.next().unwrap(), Token::Divide);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
{"rustc_fingerprint":14404728096486143510,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-unknown-linux-gnu\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/usr/local/rustup/toolchains/1.83.0-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
||||
{"rustc_fingerprint":15625702514836887422,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.83.0 (90b35a623 2024-11-26)\nbinary: rustc\ncommit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf\ncommit-date: 2024-11-26\nhost: x86_64-unknown-linux-gnu\nrelease: 1.83.0\nLLVM version: 19.1.1\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/usr/local/rustup/toolchains/1.83.0-x86_64-unknown-linux-gnu\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
|||
d7b20cdee7e69e82
|
|
@ -0,0 +1 @@
|
|||
{"rustc":11594289678289209806,"features":"[]","declared_features":"[]","target":16868798319853099954,"profile":11983525691607113661,"path":8347095127940528381,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/expression-eval-7472e588ccc66e95/dep-test-integration-test-integration_test","checksum":false}}],"rustflags":[],"metadata":2918638759235091062,"config":2202906307356721367,"compile_kind":0}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
This file has an mtime of when this was started.
|
|
@ -0,0 +1 @@
|
|||
77f5b134b916bbc7
|
|
@ -0,0 +1 @@
|
|||
{"rustc":11594289678289209806,"features":"[]","declared_features":"[]","target":16868798319853099954,"profile":15632368228915330634,"path":8347095127940528381,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/expression-eval-cc34484a884c65a5/dep-test-integration-test-integration_test","checksum":false}}],"rustflags":[],"metadata":2918638759235091062,"config":2202906307356721367,"compile_kind":0}
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
/workspaces/cmkl/spring-2025/sen-109/00020/eval/target/debug/deps/libintegration_test-7472e588ccc66e95.rmeta: tests/integration_test.rs
|
||||
|
||||
/workspaces/cmkl/spring-2025/sen-109/00020/eval/target/debug/deps/integration_test-7472e588ccc66e95.d: tests/integration_test.rs
|
||||
|
||||
tests/integration_test.rs:
|
Binary file not shown.
|
@ -0,0 +1,5 @@
|
|||
/workspaces/cmkl/spring-2025/sen-109/00020/eval/target/debug/deps/integration_test-cc34484a884c65a5: tests/integration_test.rs
|
||||
|
||||
/workspaces/cmkl/spring-2025/sen-109/00020/eval/target/debug/deps/integration_test-cc34484a884c65a5.d: tests/integration_test.rs
|
||||
|
||||
tests/integration_test.rs:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue