-
Notifications
You must be signed in to change notification settings - Fork 52
Add mul function #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add mul function #347
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae00dae
add mul function
tgauth 14f87ab
use debug instead of trace
tgauth 2d87447
test for int overflow
tgauth 172f5db
Merge branch 'main' into add-mul-function
tgauth ff70bf8
Merge branch 'main' into add-mul-function
tgauth 8c2e017
change unwrap call
tgauth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{AcceptedArgKind, Function}; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Mul {} | ||
|
|
||
| impl Function for Mul { | ||
| fn min_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn max_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
| vec![AcceptedArgKind::Number] | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
| debug!("mul function"); | ||
| let value = args[0].as_i64().unwrap() * args[1].as_i64().unwrap(); | ||
| Ok(Value::Number(value.into())) | ||
tgauth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::configure::context::Context; | ||
| use crate::parser::Statement; | ||
|
|
||
| #[test] | ||
| fn numbers() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[mul(2, 3)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, 6); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[mul(2, mul(3, 4))]", &Context::new()).unwrap(); | ||
| assert_eq!(result, 24); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_one_parameter() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[mul(5)]", &Context::new()); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn overflow_result() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| // max value for i64 is 2^63 -1 (or 9,223,372,036,854,775,807) | ||
| let result = parser.parse_and_execute("[mul(9223372036854775807, 2)]", &Context::new()); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn overflow_input() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[mul(9223372036854775808, 2)]", &Context::new()); | ||
| assert!(result.is_err()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.