Skip to content

Commit b9c8a77

Browse files
authored
Merge pull request #205 from grisenti/main
awk: fix srand builtin function
2 parents b1b8710 + f95a97e commit b9c8a77

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

awk/src/interpreter/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,7 @@ impl Interpreter {
16481648
.expect("time went backwards")
16491649
.as_secs()
16501650
};
1651+
stack.push_value(self.rand_seed as f64)?;
16511652
self.rand_seed = seed;
16521653
self.rng = SmallRng::seed_from_u64(self.rand_seed);
16531654
}
@@ -3625,4 +3626,32 @@ mod tests {
36253626
AwkValue::field_ref(maybe_numeric_string("1 2 3 4 "), 0)
36263627
);
36273628
}
3629+
3630+
#[test]
3631+
fn test_srand_returns_the_previous_seed_value() {
3632+
let constants = vec![Constant::from(42.0)];
3633+
let instructions = vec![
3634+
OpCode::PushConstant(0),
3635+
OpCode::CallBuiltin {
3636+
function: BuiltinFunction::Srand,
3637+
argc: 1,
3638+
},
3639+
];
3640+
let result = Test::new(instructions, constants.clone()).run_correct();
3641+
assert_eq!(result.execution_result.unwrap_expr(), AwkValue::from(0.0));
3642+
3643+
let instructions = vec![
3644+
OpCode::PushConstant(0),
3645+
OpCode::CallBuiltin {
3646+
function: BuiltinFunction::Srand,
3647+
argc: 1,
3648+
},
3649+
OpCode::CallBuiltin {
3650+
function: BuiltinFunction::Srand,
3651+
argc: 0,
3652+
},
3653+
];
3654+
let result = Test::new(instructions, constants).run_correct();
3655+
assert_eq!(result.execution_result.unwrap_expr(), AwkValue::from(42.0));
3656+
}
36283657
}

0 commit comments

Comments
 (0)