Merge pull request #102 from lovasoa/random

new function: random()
This commit is contained in:
ISibboI 2022-07-04 14:07:43 +03:00 committed by GitHub
commit c55a9517ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 1 deletions

View File

@ -26,6 +26,7 @@ path = "src/lib.rs"
regex = { version = "1.5.5", optional = true} regex = { version = "1.5.5", optional = true}
serde = { version = "1.0.133", optional = true} serde = { version = "1.0.133", optional = true}
serde_derive = { version = "1.0.133", optional = true} serde_derive = { version = "1.0.133", optional = true}
rand = { version = "0.8.5", optional = true}
[features] [features]
serde_support = ["serde", "serde_derive"] serde_support = ["serde", "serde_derive"]
@ -33,5 +34,5 @@ regex_support = ["regex"]
[dev-dependencies] [dev-dependencies]
ron = "0.7.0" ron = "0.7.0"
rand = "0.8.4" rand = "0.8.5"
rand_pcg = "0.3.1" rand_pcg = "0.3.1"

View File

@ -385,6 +385,7 @@ This crate offers a set of builtin functions.
| `bitnot` | 1 | Int | Computes the bitwise not of the given integer | | `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 | | `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 | | `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. 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. If the maximum or minimum is an integer, then an integer is returned.

View File

@ -207,6 +207,11 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
"str::from" => Some(Function::new(|argument| { "str::from" => Some(Function::new(|argument| {
Ok(Value::String(argument.to_string())) Ok(Value::String(argument.to_string()))
})), })),
#[cfg(feature = "rand")]
"random" => Some(Function::new(|argument| {
argument.as_empty()?;
Ok(Value::Float(rand::random()))
})),
// Bitwise operators // Bitwise operators
"bitand" => int_function!(bitand, 2), "bitand" => int_function!(bitand, 2),
"bitor" => int_function!(bitor, 2), "bitor" => int_function!(bitor, 2),

View File

@ -368,6 +368,7 @@
//! | `bitnot` | 1 | Int | Computes the bitwise not of the given integer | //! | `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 | //! | `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 | //! | `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. //! 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. //! If the maximum or minimum is an integer, then an integer is returned.

23
tests/rand.rs Normal file
View File

@ -0,0 +1,23 @@
#![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 {
// This has a probability of 1e-20 of failing
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());
}