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)); + } }