Skip to content

Commit 9d661b0

Browse files
committed
Use Clock in the wait block
1 parent 331fd98 commit 9d661b0

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/blocks/controlblocks.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#include <cassert>
1010

1111
#include "controlblocks.h"
12+
#include "../engine/internal/clock.h"
1213

1314
using namespace libscratchcpp;
1415

16+
IClock *ControlBlocks::clock = nullptr;
17+
1518
std::string ControlBlocks::name() const
1619
{
1720
return "Control";
@@ -207,14 +210,20 @@ unsigned int ControlBlocks::stopOtherScriptsInSprite(VirtualMachine *vm)
207210

208211
unsigned int ControlBlocks::startWait(VirtualMachine *vm)
209212
{
210-
auto currentTime = std::chrono::steady_clock::now();
213+
if (!clock)
214+
clock = Clock::instance().get();
215+
216+
auto currentTime = clock->currentSteadyTime();
211217
m_timeMap[vm] = { currentTime, vm->getInput(0, 1)->toDouble() * 1000 };
212218
return 1;
213219
}
214220

215221
unsigned int ControlBlocks::wait(VirtualMachine *vm)
216222
{
217-
auto currentTime = std::chrono::steady_clock::now();
223+
if (!clock)
224+
clock = Clock::instance().get();
225+
226+
auto currentTime = clock->currentSteadyTime();
218227
assert(m_timeMap.count(vm) == 1);
219228
if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - m_timeMap[vm].first).count() >= m_timeMap[vm].second) {
220229
m_timeMap.erase(vm);

src/blocks/controlblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace libscratchcpp
1111

1212
class Compiler;
1313
class VirtualMachine;
14+
class IClock;
1415

1516
/*! \brief The ControlBlocks class contains the implementation of control blocks. */
1617
class ControlBlocks : public IBlockSection
@@ -69,6 +70,8 @@ class ControlBlocks : public IBlockSection
6970
static unsigned int deleteThisClone(VirtualMachine *vm);
7071

7172
static inline std::unordered_map<VirtualMachine *, std::pair<std::chrono::steady_clock::time_point, int>> m_timeMap;
73+
74+
static IClock *clock;
7275
};
7376

7477
} // namespace libscratchcpp

test/blocks/control_blocks_test.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#include <scratchcpp/variable.h>
66
#include <scratchcpp/sprite.h>
77
#include <enginemock.h>
8+
#include <clockmock.h>
89

910
#include "../common.h"
1011
#include "blocks/controlblocks.h"
1112
#include "blocks/operatorblocks.h"
1213
#include "engine/internal/engine.h"
14+
#include "engine/internal/clock.h"
1315

1416
using namespace libscratchcpp;
1517

@@ -732,6 +734,11 @@ TEST_F(ControlBlocksTest, WaitImpl)
732734
vm.setConstValues(constValues);
733735
vm.setBytecode(bytecode);
734736

737+
ClockMock clock;
738+
ControlBlocks::clock = &clock;
739+
740+
std::chrono::steady_clock::time_point startTime(std::chrono::milliseconds(1000));
741+
EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
735742
EXPECT_CALL(m_engineMock, lockFrame()).Times(1);
736743
EXPECT_CALL(m_engineMock, breakFrame()).Times(1);
737744
vm.run();
@@ -740,18 +747,20 @@ TEST_F(ControlBlocksTest, WaitImpl)
740747
ASSERT_TRUE(ControlBlocks::m_timeMap.find(&vm) != ControlBlocks::m_timeMap.cend());
741748
ASSERT_FALSE(vm.atEnd());
742749

750+
std::chrono::steady_clock::time_point time1(std::chrono::milliseconds(6450));
751+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time1));
743752
EXPECT_CALL(m_engineMock, lockFrame()).Times(1);
744753
EXPECT_CALL(m_engineMock, breakFrame()).Times(1);
745-
ControlBlocks::m_timeMap[&vm].first = std::chrono::steady_clock::now() - std::chrono::milliseconds(5250);
746754
vm.run();
747755

748756
ASSERT_EQ(vm.registerCount(), 0);
749757
ASSERT_TRUE(ControlBlocks::m_timeMap.find(&vm) != ControlBlocks::m_timeMap.cend());
750758
ASSERT_FALSE(vm.atEnd());
751759

760+
std::chrono::steady_clock::time_point time2(std::chrono::milliseconds(6500));
761+
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
752762
EXPECT_CALL(m_engineMock, lockFrame()).Times(1);
753763
EXPECT_CALL(m_engineMock, breakFrame()).Times(1);
754-
ControlBlocks::m_timeMap[&vm].first = std::chrono::steady_clock::now() - std::chrono::milliseconds(5500);
755764
vm.run();
756765

757766
ASSERT_EQ(vm.registerCount(), 0);
@@ -765,6 +774,8 @@ TEST_F(ControlBlocksTest, WaitImpl)
765774
ASSERT_EQ(vm.registerCount(), 0);
766775
ASSERT_TRUE(ControlBlocks::m_timeMap.find(&vm) == ControlBlocks::m_timeMap.cend());
767776
ASSERT_TRUE(vm.atEnd());
777+
778+
ControlBlocks::clock = Clock::instance().get();
768779
}
769780

770781
TEST_F(ControlBlocksTest, WaitUntil)

0 commit comments

Comments
 (0)