From 7ebf7e61d5a66595b8946601b7e24b54778e0ef0 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 13 Jul 2021 13:39:47 +0300 Subject: [PATCH] Increase test coverage. Implement more tests and exclude modules from test that do not make sense to be tested. --- src/error/mod.rs | 2 ++ src/token/mod.rs | 29 +++++++++++++++++++++++++++++ src/tree/mod.rs | 2 ++ tests/integration.rs | 9 +++++++++ 4 files changed, 42 insertions(+) diff --git a/src/error/mod.rs b/src/error/mod.rs index 5c1a0df..73f8d47 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -9,6 +9,8 @@ use crate::{token::PartialToken, value::value_type::ValueType}; use crate::{operator::Operator, value::Value}; +// Exclude error display code from test coverage, as the code does not make sense to test. +#[cfg(not(tarpaulin_include))] mod display; /// Errors used in this crate. diff --git a/src/token/mod.rs b/src/token/mod.rs index 26478c2..ddecaf9 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -421,3 +421,32 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult EvalexprResult> { partial_tokens_to_tokens(&str_to_partial_tokens(string)?) } + +#[cfg(test)] +mod tests { + use crate::token::{char_to_partial_token, tokenize}; + + #[test] + fn test_partial_token_display() { + let chars = vec![ + '+', '-', '*', '/', '%', '^', '(', ')', ',', ';', '=', '!', '>', '<', '&', '|', ' ', + ]; + + for char in chars { + assert_eq!(format!("{}", char), format!("{}", char_to_partial_token(char))); + } + } + + #[test] + fn test_token_display() { + let token_string = "+ - * / % ^ == != > < >= <= && || ! ( ) = += -= *= /= %= ^= &&= ||= , ; "; + let tokens = tokenize(token_string).unwrap(); + let mut result_string = String::new(); + + for token in tokens { + result_string += &format!("{} ", token); + } + + assert_eq!(token_string, result_string); + } +} diff --git a/src/tree/mod.rs b/src/tree/mod.rs index d5f2527..ef5b344 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -11,6 +11,8 @@ use crate::{ }; use std::mem; +// Exclude display module from coverage, as it prints not well-defined prefix notation. +#[cfg(not(tarpaulin_include))] mod display; mod iter; diff --git a/tests/integration.rs b/tests/integration.rs index 0b95eac..7d7180e 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,3 +1,5 @@ +#![cfg(not(tarpaulin_include))] + use evalexpr::{error::*, *}; #[test] @@ -274,6 +276,10 @@ fn test_builtin_functions() { assert_eq!(eval("math::log(9, 9)"), Ok(Value::Float(1.0))); assert_eq!(eval("math::log2(2)"), Ok(Value::Float(1.0))); assert_eq!(eval("math::log10(10)"), Ok(Value::Float(1.0))); + // Powers + assert_eq!(eval("math::exp(2)"), Ok(Value::Float(2.0_f64.exp()))); + assert_eq!(eval("math::exp2(2)"), Ok(Value::Float(2.0_f64.exp2()))); + assert_eq!(eval("math::pow(1.5, 1.3)"), Ok(Value::Float(1.5_f64.powf(1.3)))); // Cos assert_eq!(eval("math::cos(0)"), Ok(Value::Float(1.0))); assert_eq!(eval("math::acos(1)"), Ok(Value::Float(0.0))); @@ -289,9 +295,12 @@ fn test_builtin_functions() { assert_eq!(eval("math::atan(0)"), Ok(Value::Float(0.0))); assert_eq!(eval("math::tanh(0)"), Ok(Value::Float(0.0))); assert_eq!(eval("math::atanh(0)"), Ok(Value::Float(0.0))); + assert_eq!(eval("math::atan2(1.2, -5.5)"), Ok(Value::Float(1.2_f64.atan2(-5.5)))); // Root assert_eq!(eval("math::sqrt(25)"), Ok(Value::Float(5.0))); assert_eq!(eval("math::cbrt(8)"), Ok(Value::Float(2.0))); + // Hypotenuse + assert_eq!(eval("math::hypot(8.2, 1.1)"), Ok(Value::Float(8.2_f64.hypot(1.1)))); // Rounding assert_eq!(eval("floor(1.1)"), Ok(Value::Float(1.0))); assert_eq!(eval("floor(1.9)"), Ok(Value::Float(1.0)));