From bce31e24aea09367c11547b534a963cdf4836e69 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Wed, 30 Mar 2022 14:39:26 +0200 Subject: [PATCH] new function: random() random() returns a random number between 0 and 1 --- Cargo.toml | 3 ++- README.md | 1 + src/function/builtin.rs | 5 +++++ src/lib.rs | 1 + tests/rand.rs | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/rand.rs diff --git a/Cargo.toml b/Cargo.toml index 7868b66..96b68e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ path = "src/lib.rs" regex = { version = "1.5.5", optional = true} serde = { version = "1.0.133", optional = true} serde_derive = { version = "1.0.133", optional = true} +rand = { version = "0.8", optional = true} [features] serde_support = ["serde", "serde_derive"] @@ -33,5 +34,5 @@ regex_support = ["regex"] [dev-dependencies] ron = "0.7.0" -rand = "0.8.4" +rand = "0.8.5" rand_pcg = "0.3.1" diff --git a/README.md b/README.md index f493b02..d2fba5d 100644 --- a/README.md +++ b/README.md @@ -380,6 +380,7 @@ This crate offers a set of builtin functions. | `bitnot` | 1 | Int | Computes the bitwise not of the given integer | | `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer | | `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer | +| `random` | 0 | Empty | Return a random float between 0 and 1. Requires the `rand` feature flag. | The `min` and `max` functions can deal with a mixture of integer and floating point arguments. If the maximum or minimum is an integer, then an integer is returned. diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 7194da1..0b5f1fa 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -185,6 +185,11 @@ pub fn builtin_function(identifier: &str) -> Option { "str::from" => Some(Function::new(|argument| { Ok(Value::String(argument.to_string())) })), + #[cfg(feature = "rand")] + "random" => Some(Function::new(|argument| { + argument.as_empty()?; + Ok(Value::Float(rand::random())) + })), // Bitwise operators "bitand" => int_function!(bitand, 2), "bitor" => int_function!(bitor, 2), diff --git a/src/lib.rs b/src/lib.rs index f136143..9bf09d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -363,6 +363,7 @@ //! | `bitnot` | 1 | Int | Computes the bitwise not of the given integer | //! | `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer | //! | `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer | +//! | `random` | 0 | Empty | Return a random float between 0 and 1. Requires the `rand` feature flag. | //! //! The `min` and `max` functions can deal with a mixture of integer and floating point arguments. //! If the maximum or minimum is an integer, then an integer is returned. diff --git a/tests/rand.rs b/tests/rand.rs new file mode 100644 index 0000000..2e7ceb7 --- /dev/null +++ b/tests/rand.rs @@ -0,0 +1,22 @@ +#![cfg(feature = "rand")] + +use evalexpr::*; + +fn assert_expr(expr: &str) { + assert_eq!(eval(expr), Ok(Value::Boolean(true))) +} + +#[test] +fn test_random() { + for _ in 0..100 { + assert_expr("random() != random()"); + assert_expr("0 <= random()"); + assert_expr("random() <= 1"); + } +} + +#[test] +fn test_random_errors() { + assert!(eval("random(9)").is_err()); + assert!(eval("random(\"a\", \"b\")").is_err()); +} \ No newline at end of file