commit
c55a9517ba
@ -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.5", 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"
|
||||
|
@ -385,6 +385,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.
|
||||
|
@ -207,6 +207,11 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
|
||||
"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),
|
||||
|
@ -368,6 +368,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.
|
||||
|
23
tests/rand.rs
Normal file
23
tests/rand.rs
Normal 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());
|
||||
}
|
Loading…
Reference in New Issue
Block a user