Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions awk/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
}