From f95a97ecf320bf0fa620ef61c81bd1a8f738e15d Mon Sep 17 00:00:00 2001 From: grisenti Date: Thu, 12 Sep 2024 08:28:52 +0200 Subject: [PATCH] awk: fix srand builtin function --- awk/src/interpreter/mod.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/awk/src/interpreter/mod.rs b/awk/src/interpreter/mod.rs index ec1e4c3ba..407da891b 100644 --- a/awk/src/interpreter/mod.rs +++ b/awk/src/interpreter/mod.rs @@ -1648,6 +1648,7 @@ impl Interpreter { .expect("time went backwards") .as_secs() }; + stack.push_value(self.rand_seed as f64)?; self.rand_seed = seed; self.rng = SmallRng::seed_from_u64(self.rand_seed); } @@ -3625,4 +3626,32 @@ mod tests { AwkValue::field_ref(maybe_numeric_string("1 2 3 4 "), 0) ); } + + #[test] + fn test_srand_returns_the_previous_seed_value() { + let constants = vec![Constant::from(42.0)]; + let instructions = vec![ + OpCode::PushConstant(0), + OpCode::CallBuiltin { + function: BuiltinFunction::Srand, + argc: 1, + }, + ]; + let result = Test::new(instructions, constants.clone()).run_correct(); + assert_eq!(result.execution_result.unwrap_expr(), AwkValue::from(0.0)); + + let instructions = vec![ + OpCode::PushConstant(0), + OpCode::CallBuiltin { + function: BuiltinFunction::Srand, + argc: 1, + }, + OpCode::CallBuiltin { + function: BuiltinFunction::Srand, + argc: 0, + }, + ]; + let result = Test::new(instructions, constants).run_correct(); + assert_eq!(result.execution_result.unwrap_expr(), AwkValue::from(42.0)); + } }