From 1098d08fd81c7d18ce1c9c2deb4483a59f92d727 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sat, 30 Sep 2023 14:16:01 +0200 Subject: [PATCH] fix #222: Add support for random decimals --- src/engine/virtualmachine_p.cpp | 5 +- test/virtual_machine/virtual_machine_test.cpp | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/engine/virtualmachine_p.cpp b/src/engine/virtualmachine_p.cpp index f28f76fc..2c747c30 100644 --- a/src/engine/virtualmachine_p.cpp +++ b/src/engine/virtualmachine_p.cpp @@ -365,7 +365,10 @@ do_loop_end : { DISPATCH(); do_random: - REPLACE_RET_VALUE(rng->randint(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2); + if ((READ_REG(0, 2)->type() == Value::Type::Integer) && (READ_REG(1, 2)->type() == Value::Type::Integer)) + REPLACE_RET_VALUE(rng->randint(READ_REG(0, 2)->toInt(), READ_REG(1, 2)->toInt()), 2); + else + REPLACE_RET_VALUE(rng->randintDouble(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2); FREE_REGS(1); DISPATCH(); diff --git a/test/virtual_machine/virtual_machine_test.cpp b/test/virtual_machine/virtual_machine_test.cpp index 7b4b1a19..2f4010a4 100644 --- a/test/virtual_machine/virtual_machine_test.cpp +++ b/test/virtual_machine/virtual_machine_test.cpp @@ -362,6 +362,57 @@ TEST(VirtualMachineTest, OP_MOD) ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 1.5); } +TEST(VirtualMachineTest, OP_RANDOM) +{ + static unsigned int bytecode1[] = { OP_START, OP_CONST, 0, OP_CONST, 1, OP_RANDOM, OP_HALT }; + static unsigned int bytecode2[] = { OP_START, OP_CONST, 1, OP_CONST, 2, OP_RANDOM, OP_HALT }; + static unsigned int bytecode3[] = { OP_START, OP_CONST, 3, OP_CONST, 0, OP_RANDOM, OP_HALT }; + static unsigned int bytecode4[] = { OP_START, OP_CONST, 2, OP_CONST, 3, OP_RANDOM, OP_HALT }; + static Value constValues[] = { -45, 12, 6.05, -78.686 }; + + VirtualMachinePrivate vm(nullptr, nullptr, nullptr, nullptr); + vm.constValues = constValues; + + RandomGeneratorMock rng; + vm.rng = &rng; + + EXPECT_CALL(rng, randint(-45, 12)).WillOnce(Return(-18)); + vm.bytecode = bytecode1; + vm.pos = bytecode1; + vm.regCount = 0; + vm.run(vm.pos); + + ASSERT_EQ(vm.regCount, 1); + ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -18); + + EXPECT_CALL(rng, randintDouble(12, 6.05)).WillOnce(Return(3.486789)); + vm.bytecode = bytecode2; + vm.pos = bytecode2; + vm.regCount = 0; + vm.run(vm.pos); + + ASSERT_EQ(vm.regCount, 1); + ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), 3.486789); + + EXPECT_CALL(rng, randintDouble(-78.686, -45)).WillOnce(Return(-59.468873)); + vm.bytecode = bytecode3; + vm.pos = bytecode3; + vm.regCount = 0; + vm.run(vm.pos); + + ASSERT_EQ(vm.regCount, 1); + ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -59.468873); + + EXPECT_CALL(rng, randintDouble(6.05, -78.686)).WillOnce(Return(-28.648764)); + vm.bytecode = bytecode4; + vm.pos = bytecode4; + vm.regCount = 0; + vm.run(vm.pos); + + ASSERT_EQ(vm.regCount, 1); + ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -28.648764); +} + TEST(VirtualMachineTest, OP_ROUND) { static unsigned int bytecode[] = {