Skip to content

Commit 8f50de6

Browse files
authored
Merge pull request #349 from Abb1x/master
fix #317: the number of periods in a loop should be rounded
2 parents 52b41af + 1719c4b commit 8f50de6

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/engine/virtualmachine_p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
260260
DISPATCH();
261261

262262
do_repeat_loop:
263-
loopCount = READ_LAST_REG()->toLong();
263+
loopCount = std::round(READ_LAST_REG()->toDouble());
264264
FREE_REGS(1);
265265
if (loopCount <= 0) {
266266
loopEnd = pos;

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,33 @@ TEST(VirtualMachineTest, OP_FOREVER_LOOP)
254254
TEST(VirtualMachineTest, OP_REPEAT_LOOP)
255255
{
256256
static unsigned int bytecode[] = { OP_START, OP_CONST, 0, OP_REPEAT_LOOP, OP_CONST, 1, OP_PRINT, OP_LOOP_END, OP_HALT };
257-
static Value constValues[] = { 3, "test" };
257+
static Value constValues1[] = { 3, "test" };
258+
static Value constValues2[] = { 4.5, "test" };
259+
static Value constValues3[] = { 4.2, "test" };
258260

259261
EngineMock engineMock;
260262
VirtualMachine vm(nullptr, &engineMock, nullptr);
261263
vm.setBytecode(bytecode);
262-
vm.setConstValues(constValues);
264+
vm.setConstValues(constValues1);
263265
testing::internal::CaptureStdout();
264266
vm.run();
265267
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\ntest\ntest\n");
266268
ASSERT_EQ(vm.registerCount(), 0);
269+
270+
// For #317
271+
vm.setConstValues(constValues2);
272+
testing::internal::CaptureStdout();
273+
vm.reset();
274+
vm.run();
275+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\ntest\ntest\ntest\ntest\n");
276+
ASSERT_EQ(vm.registerCount(), 0);
277+
278+
vm.setConstValues(constValues3);
279+
testing::internal::CaptureStdout();
280+
vm.reset();
281+
vm.run();
282+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\ntest\ntest\ntest\n");
283+
ASSERT_EQ(vm.registerCount(), 0);
267284
}
268285

269286
TEST(VirtualMachineTest, OP_REPEAT_LOOP_INDEX)

0 commit comments

Comments
 (0)