Skip to content

Implement timer blocks #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int main(int argc, char **argv) {
- [x] Variables (monitors are not implemented yet)
- [x] Lists (monitors are not implemented yet)
- [x] Clones
- [ ] Timer
- [x] Timer
- [ ] API for monitors
- [ ] Project metadata
- [ ] Turbo mode
Expand Down
29 changes: 29 additions & 0 deletions src/blocks/sensingblocks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

#include <scratchcpp/virtualmachine.h>
#include <scratchcpp/compiler.h>
#include <scratchcpp/iengine.h>
#include <scratchcpp/itimer.h>
#include "sensingblocks.h"

using namespace libscratchcpp;
Expand All @@ -11,4 +15,29 @@ std::string SensingBlocks::name() const

void SensingBlocks::registerBlocks(IEngine *engine)
{
// Blocks
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
}

void SensingBlocks::compileTimer(Compiler *compiler)
{
compiler->addFunctionCall(&timer);
}

void SensingBlocks::compileResetTimer(Compiler *compiler)
{
compiler->addFunctionCall(&resetTimer);
}

unsigned int SensingBlocks::timer(VirtualMachine *vm)
{
vm->addReturnValue(vm->engine()->timer()->value());
return 0;
}

unsigned int SensingBlocks::resetTimer(VirtualMachine *vm)
{
vm->engine()->timer()->reset();
return 0;
}
6 changes: 6 additions & 0 deletions src/blocks/sensingblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class SensingBlocks : public IBlockSection
std::string name() const override;

void registerBlocks(IEngine *engine) override;

static void compileTimer(Compiler *compiler);
static void compileResetTimer(Compiler *compiler);

static unsigned int timer(VirtualMachine *vm);
static unsigned int resetTimer(VirtualMachine *vm);
};

} // namespace libscratchcpp
16 changes: 16 additions & 0 deletions test/blocks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ target_link_libraries(
)

gtest_discover_tests(custom_blocks_test)

# sensing_blocks_test
add_executable(
sensing_blocks_test
sensing_blocks_test.cpp
)

target_link_libraries(
sensing_blocks_test
GTest::gtest_main
GTest::gmock_main
scratchcpp
scratchcpp_mocks
)

gtest_discover_tests(sensing_blocks_test)
113 changes: 113 additions & 0 deletions test/blocks/sensing_blocks_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <scratchcpp/compiler.h>
#include <scratchcpp/block.h>
#include <enginemock.h>
#include <timermock.h>

#include "../common.h"
#include "blocks/sensingblocks.h"
#include "engine/internal/engine.h"

using namespace libscratchcpp;

using ::testing::Return;

class SensingBlocksTest : public testing::Test
{
public:
void SetUp() override
{
m_section = std::make_unique<SensingBlocks>();
m_section->registerBlocks(&m_engine);
}

std::unique_ptr<IBlockSection> m_section;
EngineMock m_engineMock;
Engine m_engine;
TimerMock m_timerMock;
};

TEST_F(SensingBlocksTest, Name)
{
ASSERT_EQ(m_section->name(), "Sensing");
}

TEST_F(SensingBlocksTest, CategoryVisible)
{
ASSERT_TRUE(m_section->categoryVisible());
}

TEST_F(SensingBlocksTest, RegisterBlocks)
{
// Blocks
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_timer", &SensingBlocks::compileTimer)).Times(1);
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_resettimer", &SensingBlocks::compileResetTimer)).Times(1);

m_section->registerBlocks(&m_engineMock);
}

TEST_F(SensingBlocksTest, Timer)
{
Compiler compiler(&m_engineMock);

auto block = std::make_shared<Block>("a", "sensing_timer");

EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::timer)).WillOnce(Return(0));

compiler.init();
compiler.setBlock(block);
SensingBlocks::compileTimer(&compiler);
compiler.end();

ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
}

TEST_F(SensingBlocksTest, TimerImpl)
{
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
static BlockFunc functions[] = { &SensingBlocks::timer };

VirtualMachine vm(nullptr, &m_engineMock, nullptr);
vm.setFunctions(functions);

EXPECT_CALL(m_engineMock, timer()).WillOnce(Return(&m_timerMock));
EXPECT_CALL(m_timerMock, value()).WillOnce(Return(2.375));

vm.setBytecode(bytecode);
vm.run();

ASSERT_EQ(vm.registerCount(), 1);
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 2.375);
}

TEST_F(SensingBlocksTest, ResetTimer)
{
Compiler compiler(&m_engineMock);

auto block = std::make_shared<Block>("a", "sensing_resettimer");

EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::resetTimer)).WillOnce(Return(0));

compiler.init();
compiler.setBlock(block);
SensingBlocks::compileResetTimer(&compiler);
compiler.end();

ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
}

TEST_F(SensingBlocksTest, ResetTimerImpl)
{
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
static BlockFunc functions[] = { &SensingBlocks::resetTimer };

VirtualMachine vm(nullptr, &m_engineMock, nullptr);
vm.setFunctions(functions);

EXPECT_CALL(m_engineMock, timer()).WillOnce(Return(&m_timerMock));
EXPECT_CALL(m_timerMock, reset()).Times(1);

vm.setBytecode(bytecode);
vm.run();

ASSERT_EQ(vm.registerCount(), 0);
}