Skip to content

Commit 39ee6b4

Browse files
committed
Implement operator_divide
1 parent c6d4834 commit 39ee6b4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/dev/blocks/operatorblocks.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
2222
engine->addCompileFunction(this, "operator_add", &compileAdd);
2323
engine->addCompileFunction(this, "operator_subtract", &compileSubtract);
2424
engine->addCompileFunction(this, "operator_multiply", &compileMultiply);
25+
engine->addCompileFunction(this, "operator_divide", &compileDivide);
2526
}
2627

2728
CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
@@ -38,3 +39,8 @@ CompilerValue *OperatorBlocks::compileMultiply(Compiler *compiler)
3839
{
3940
return compiler->createMul(compiler->addInput("NUM1"), compiler->addInput("NUM2"));
4041
}
42+
43+
CompilerValue *OperatorBlocks::compileDivide(Compiler *compiler)
44+
{
45+
return compiler->createDiv(compiler->addInput("NUM1"), compiler->addInput("NUM2"));
46+
}

test/dev/blocks/operator_blocks_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ TEST_F(OperatorBlocksTest, Multiply)
8383
ASSERT_EQ(valueList->size(), 1);
8484
ASSERT_EQ(Value(values[0]), 14.25);
8585
}
86+
87+
TEST_F(OperatorBlocksTest, Divide)
88+
{
89+
auto target = std::make_shared<Sprite>();
90+
ScriptBuilder builder(m_extension.get(), m_engine, target);
91+
92+
builder.addBlock("operator_divide");
93+
builder.addValueInput("NUM1", 5.7);
94+
builder.addValueInput("NUM2", 2.5);
95+
builder.captureBlockReturnValue();
96+
97+
builder.build();
98+
builder.run();
99+
100+
List *valueList = builder.capturedValues();
101+
ValueData *values = valueList->data();
102+
ASSERT_EQ(valueList->size(), 1);
103+
ASSERT_EQ(std::round(value_toDouble(&values[0]) * 100) / 100, 2.28);
104+
}

0 commit comments

Comments
 (0)