Skip to content

Commit

Permalink
Implement operator_lt
Browse files Browse the repository at this point in the history
  • Loading branch information
adazem009 committed Dec 23, 2024
1 parent b678de0 commit 3a39a57
Show file tree
Hide file tree
Showing 3 changed files with 38 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 @@ -25,6 +25,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
engine->addCompileFunction(this, "operator_multiply", &compileMultiply);
engine->addCompileFunction(this, "operator_divide", &compileDivide);
engine->addCompileFunction(this, "operator_random", &compileRandom);
engine->addCompileFunction(this, "operator_lt", &compileLt);
}

CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
Expand Down Expand Up @@ -53,3 +54,8 @@ CompilerValue *OperatorBlocks::compileRandom(Compiler *compiler)
auto to = compiler->addInput("TO");
return compiler->createRandom(from, to);
}

CompilerValue *OperatorBlocks::compileLt(Compiler *compiler)
{
return compiler->createCmpLT(compiler->addInput("OPERAND1"), compiler->addInput("OPERAND2"));
}
1 change: 1 addition & 0 deletions src/dev/blocks/operatorblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class OperatorBlocks : public IExtension
static CompilerValue *compileMultiply(Compiler *compiler);
static CompilerValue *compileDivide(Compiler *compiler);
static CompilerValue *compileRandom(Compiler *compiler);
static CompilerValue *compileLt(Compiler *compiler);
};

} // namespace libscratchcpp
31 changes: 31 additions & 0 deletions test/dev/blocks/operator_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,34 @@ TEST_F(OperatorBlocksTest, Random)
code->run(ctx.get());
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
}

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

builder.addBlock("operator_lt");
builder.addValueInput("OPERAND1", 5.4645);
builder.addValueInput("OPERAND2", 12.486);
builder.captureBlockReturnValue();

builder.addBlock("operator_lt");
builder.addValueInput("OPERAND1", 153.25);
builder.addValueInput("OPERAND2", 96.5);
builder.captureBlockReturnValue();

builder.addBlock("operator_lt");
builder.addValueInput("OPERAND1", 2.8465);
builder.addValueInput("OPERAND2", 2.8465);
builder.captureBlockReturnValue();

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

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

0 comments on commit 3a39a57

Please sign in to comment.