Skip to content

Commit f222177

Browse files
authored
Merge pull request #238 from scratchcpp/random_block_double
Fix #222: Add support for random decimals
2 parents 66c1817 + 1098d08 commit f222177

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,10 @@ do_loop_end : {
365365
DISPATCH();
366366

367367
do_random:
368-
REPLACE_RET_VALUE(rng->randint(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2);
368+
if ((READ_REG(0, 2)->type() == Value::Type::Integer) && (READ_REG(1, 2)->type() == Value::Type::Integer))
369+
REPLACE_RET_VALUE(rng->randint(READ_REG(0, 2)->toInt(), READ_REG(1, 2)->toInt()), 2);
370+
else
371+
REPLACE_RET_VALUE(rng->randintDouble(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2);
369372
FREE_REGS(1);
370373
DISPATCH();
371374

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,57 @@ TEST(VirtualMachineTest, OP_MOD)
362362
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 1.5);
363363
}
364364

365+
TEST(VirtualMachineTest, OP_RANDOM)
366+
{
367+
static unsigned int bytecode1[] = { OP_START, OP_CONST, 0, OP_CONST, 1, OP_RANDOM, OP_HALT };
368+
static unsigned int bytecode2[] = { OP_START, OP_CONST, 1, OP_CONST, 2, OP_RANDOM, OP_HALT };
369+
static unsigned int bytecode3[] = { OP_START, OP_CONST, 3, OP_CONST, 0, OP_RANDOM, OP_HALT };
370+
static unsigned int bytecode4[] = { OP_START, OP_CONST, 2, OP_CONST, 3, OP_RANDOM, OP_HALT };
371+
static Value constValues[] = { -45, 12, 6.05, -78.686 };
372+
373+
VirtualMachinePrivate vm(nullptr, nullptr, nullptr, nullptr);
374+
vm.constValues = constValues;
375+
376+
RandomGeneratorMock rng;
377+
vm.rng = &rng;
378+
379+
EXPECT_CALL(rng, randint(-45, 12)).WillOnce(Return(-18));
380+
vm.bytecode = bytecode1;
381+
vm.pos = bytecode1;
382+
vm.regCount = 0;
383+
vm.run(vm.pos);
384+
385+
ASSERT_EQ(vm.regCount, 1);
386+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -18);
387+
388+
EXPECT_CALL(rng, randintDouble(12, 6.05)).WillOnce(Return(3.486789));
389+
vm.bytecode = bytecode2;
390+
vm.pos = bytecode2;
391+
vm.regCount = 0;
392+
vm.run(vm.pos);
393+
394+
ASSERT_EQ(vm.regCount, 1);
395+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), 3.486789);
396+
397+
EXPECT_CALL(rng, randintDouble(-78.686, -45)).WillOnce(Return(-59.468873));
398+
vm.bytecode = bytecode3;
399+
vm.pos = bytecode3;
400+
vm.regCount = 0;
401+
vm.run(vm.pos);
402+
403+
ASSERT_EQ(vm.regCount, 1);
404+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -59.468873);
405+
406+
EXPECT_CALL(rng, randintDouble(6.05, -78.686)).WillOnce(Return(-28.648764));
407+
vm.bytecode = bytecode4;
408+
vm.pos = bytecode4;
409+
vm.regCount = 0;
410+
vm.run(vm.pos);
411+
412+
ASSERT_EQ(vm.regCount, 1);
413+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -28.648764);
414+
}
415+
365416
TEST(VirtualMachineTest, OP_ROUND)
366417
{
367418
static unsigned int bytecode[] = {

0 commit comments

Comments
 (0)