Skip to content

Commit 151a485

Browse files
committed
Implement sensing_dayssince2000 block
1 parent aba5f8e commit 151a485

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#include <scratchcpp/itimer.h>
77
#include "sensingblocks.h"
88

9+
#include "../engine/internal/clock.h"
10+
911
using namespace libscratchcpp;
1012

13+
IClock *SensingBlocks::clock = Clock::instance().get();
14+
1115
std::string SensingBlocks::name() const
1216
{
1317
return "Sensing";
@@ -18,6 +22,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
1822
// Blocks
1923
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
2024
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
25+
engine->addCompileFunction(this, "sensing_dayssince2000", &compileDaysSince2000);
2126
}
2227

2328
void SensingBlocks::compileTimer(Compiler *compiler)
@@ -30,6 +35,11 @@ void SensingBlocks::compileResetTimer(Compiler *compiler)
3035
compiler->addFunctionCall(&resetTimer);
3136
}
3237

38+
void SensingBlocks::compileDaysSince2000(Compiler *compiler)
39+
{
40+
compiler->addFunctionCall(&daysSince2000);
41+
}
42+
3343
unsigned int SensingBlocks::timer(VirtualMachine *vm)
3444
{
3545
vm->addReturnValue(vm->engine()->timer()->value());
@@ -41,3 +51,11 @@ unsigned int SensingBlocks::resetTimer(VirtualMachine *vm)
4151
vm->engine()->timer()->reset();
4252
return 0;
4353
}
54+
55+
unsigned int SensingBlocks::daysSince2000(VirtualMachine *vm)
56+
{
57+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(clock->currentSystemTime().time_since_epoch()).count();
58+
vm->addReturnValue(ms / 86400000.0 - 10957);
59+
60+
return 0;
61+
}

src/blocks/sensingblocks.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <scratchcpp/iblocksection.h>
6+
#include "../engine/internal/clock.h"
67

78
namespace libscratchcpp
89
{
@@ -17,9 +18,13 @@ class SensingBlocks : public IBlockSection
1718

1819
static void compileTimer(Compiler *compiler);
1920
static void compileResetTimer(Compiler *compiler);
21+
static void compileDaysSince2000(Compiler *compiler);
2022

2123
static unsigned int timer(VirtualMachine *vm);
2224
static unsigned int resetTimer(VirtualMachine *vm);
25+
static unsigned int daysSince2000(VirtualMachine *vm);
26+
27+
static IClock *clock;
2328
};
2429

2530
} // namespace libscratchcpp

test/blocks/sensing_blocks_test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <scratchcpp/block.h>
33
#include <enginemock.h>
44
#include <timermock.h>
5+
#include <clockmock.h>
56

67
#include "../common.h"
78
#include "blocks/sensingblocks.h"
@@ -24,6 +25,7 @@ class SensingBlocksTest : public testing::Test
2425
EngineMock m_engineMock;
2526
Engine m_engine;
2627
TimerMock m_timerMock;
28+
ClockMock m_clockMock;
2729
};
2830

2931
TEST_F(SensingBlocksTest, Name)
@@ -41,6 +43,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
4143
// Blocks
4244
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_timer", &SensingBlocks::compileTimer)).Times(1);
4345
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_resettimer", &SensingBlocks::compileResetTimer)).Times(1);
46+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_dayssince2000", &SensingBlocks::compileDaysSince2000)).Times(1);
4447

4548
m_section->registerBlocks(&m_engineMock);
4649
}
@@ -111,3 +114,39 @@ TEST_F(SensingBlocksTest, ResetTimerImpl)
111114

112115
ASSERT_EQ(vm.registerCount(), 0);
113116
}
117+
118+
TEST_F(SensingBlocksTest, DaysSince2000)
119+
{
120+
Compiler compiler(&m_engineMock);
121+
122+
auto block = std::make_shared<Block>("a", "sensing_dayssince2000");
123+
124+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::daysSince2000)).WillOnce(Return(0));
125+
126+
compiler.init();
127+
compiler.setBlock(block);
128+
SensingBlocks::compileDaysSince2000(&compiler);
129+
compiler.end();
130+
131+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
132+
}
133+
134+
TEST_F(SensingBlocksTest, DaysSince2000Impl)
135+
{
136+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
137+
static BlockFunc functions[] = { &SensingBlocks::daysSince2000 };
138+
139+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
140+
vm.setFunctions(functions);
141+
142+
std::chrono::system_clock::time_point time(std::chrono::milliseconds(1011243120562)); // Jan 17 2002 04:52:00
143+
EXPECT_CALL(m_clockMock, currentSystemTime()).WillOnce(Return(time));
144+
145+
SensingBlocks::clock = &m_clockMock;
146+
vm.setBytecode(bytecode);
147+
vm.run();
148+
SensingBlocks::clock = Clock::instance().get();
149+
150+
ASSERT_EQ(vm.registerCount(), 1);
151+
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 747.20278428240817);
152+
}

0 commit comments

Comments
 (0)