Skip to content

Commit

Permalink
Implement operator_round
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Dec 24, 2024
1 parent e237c7b commit 5ef36d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/dev/blocks/operatorblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
engine->addCompileFunction(this, "operator_length", &compileLength);
engine->addCompileFunction(this, "operator_contains", &compileContains);
engine->addCompileFunction(this, "operator_mod", &compileMod);
engine->addCompileFunction(this, "operator_round", &compileRound);
}

CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
Expand Down Expand Up @@ -128,6 +129,11 @@ CompilerValue *OperatorBlocks::compileMod(Compiler *compiler)
return compiler->createMod(compiler->addInput("NUM1"), compiler->addInput("NUM2"));
}

CompilerValue *OperatorBlocks::compileRound(Compiler *compiler)
{
return compiler->createRound(compiler->addInput("NUM"));
}

extern "C" char *operator_join(const char *string1, const char *string2)
{
const size_t len1 = strlen(string1);
Expand Down
1 change: 1 addition & 0 deletions src/dev/blocks/operatorblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class OperatorBlocks : public IExtension
static CompilerValue *compileLength(Compiler *compiler);
static CompilerValue *compileContains(Compiler *compiler);
static CompilerValue *compileMod(Compiler *compiler);
static CompilerValue *compileRound(Compiler *compiler);
};

} // namespace libscratchcpp
23 changes: 23 additions & 0 deletions test/dev/blocks/operator_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,26 @@ TEST_F(OperatorBlocksTest, Mod)
ASSERT_EQ(std::round(value_toDouble(&values[0]) * 100) / 100, 0.7);
ASSERT_EQ(std::round(value_toDouble(&values[1]) * 100) / 100, 1.8);
}

TEST_F(OperatorBlocksTest, Round)
{
auto target = std::make_shared<Sprite>();
ScriptBuilder builder(m_extension.get(), m_engine, target);

builder.addBlock("operator_round");
builder.addValueInput("NUM", 5.7);
builder.captureBlockReturnValue();

builder.addBlock("operator_round");
builder.addValueInput("NUM", 2.3);
builder.captureBlockReturnValue();

builder.build();
builder.run();

List *valueList = builder.capturedValues();
ValueData *values = valueList->data();
ASSERT_EQ(valueList->size(), 2);
ASSERT_EQ(Value(values[0]), 6);
ASSERT_EQ(Value(values[1]), 2);
}

0 comments on commit 5ef36d0

Please sign in to comment.