Skip to content

Commit b678de0

Browse files
committed
Implement operator_random
1 parent a83dae7 commit b678de0

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/dev/blocks/operatorblocks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <scratchcpp/iengine.h>
44
#include <scratchcpp/dev/compiler.h>
5+
#include <scratchcpp/dev/compilervalue.h>
56

67
#include "operatorblocks.h"
78

@@ -23,6 +24,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
2324
engine->addCompileFunction(this, "operator_subtract", &compileSubtract);
2425
engine->addCompileFunction(this, "operator_multiply", &compileMultiply);
2526
engine->addCompileFunction(this, "operator_divide", &compileDivide);
27+
engine->addCompileFunction(this, "operator_random", &compileRandom);
2628
}
2729

2830
CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
@@ -44,3 +46,10 @@ CompilerValue *OperatorBlocks::compileDivide(Compiler *compiler)
4446
{
4547
return compiler->createDiv(compiler->addInput("NUM1"), compiler->addInput("NUM2"));
4648
}
49+
50+
CompilerValue *OperatorBlocks::compileRandom(Compiler *compiler)
51+
{
52+
auto from = compiler->addInput("FROM");
53+
auto to = compiler->addInput("TO");
54+
return compiler->createRandom(from, to);
55+
}

src/dev/blocks/operatorblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace libscratchcpp
88
{
99

10+
class IRandomGenerator;
11+
1012
class OperatorBlocks : public IExtension
1113
{
1214
public:
@@ -20,6 +22,7 @@ class OperatorBlocks : public IExtension
2022
static CompilerValue *compileSubtract(Compiler *compiler);
2123
static CompilerValue *compileMultiply(Compiler *compiler);
2224
static CompilerValue *compileDivide(Compiler *compiler);
25+
static CompilerValue *compileRandom(Compiler *compiler);
2326
};
2427

2528
} // namespace libscratchcpp

test/dev/blocks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ if (LIBSCRATCHCPP_ENABLE_OPERATOR_BLOCKS)
132132
GTest::gmock_main
133133
scratchcpp
134134
scratchcpp_mocks
135+
block_test_deps
135136
)
136137

137138
gtest_discover_tests(operator_blocks_test)

test/dev/blocks/operator_blocks_test.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22
#include <scratchcpp/sprite.h>
33
#include <scratchcpp/list.h>
44
#include <scratchcpp/dev/compiler.h>
5+
#include <scratchcpp/block.h>
6+
#include <scratchcpp/input.h>
7+
#include <scratchcpp/script.h>
8+
#include <scratchcpp/thread.h>
9+
#include <scratchcpp/dev/executablecode.h>
10+
#include <scratchcpp/dev/executioncontext.h>
511
#include <scratchcpp/dev/test/scriptbuilder.h>
612
#include <enginemock.h>
13+
#include <randomgeneratormock.h>
714

815
#include "../common.h"
916
#include "dev/blocks/operatorblocks.h"
17+
#include "util.h"
1018

1119
using namespace libscratchcpp;
1220
using namespace libscratchcpp::test;
1321

22+
using ::testing::Return;
23+
1424
class OperatorBlocksTest : public testing::Test
1525
{
1626
public:
@@ -19,12 +29,14 @@ class OperatorBlocksTest : public testing::Test
1929
m_extension = std::make_unique<OperatorBlocks>();
2030
m_engine = m_project.engine().get();
2131
m_extension->registerBlocks(m_engine);
32+
registerBlocks(m_engine, m_extension.get());
2233
}
2334

2435
std::unique_ptr<IExtension> m_extension;
2536
Project m_project;
2637
IEngine *m_engine = nullptr;
2738
EngineMock m_engineMock;
39+
RandomGeneratorMock m_rng;
2840
};
2941

3042
TEST_F(OperatorBlocksTest, Add)
@@ -102,3 +114,52 @@ TEST_F(OperatorBlocksTest, Divide)
102114
ASSERT_EQ(valueList->size(), 1);
103115
ASSERT_EQ(std::round(value_toDouble(&values[0]) * 100) / 100, 2.28);
104116
}
117+
118+
TEST_F(OperatorBlocksTest, Random)
119+
{
120+
auto target = std::make_shared<Sprite>();
121+
ScriptBuilder builder(m_extension.get(), m_engine, target);
122+
123+
auto addRandomTest = [&builder](const Value &from, const Value &to) {
124+
auto block = std::make_shared<Block>("", "operator_random");
125+
auto input = std::make_shared<Input>("FROM", Input::Type::Shadow);
126+
input->setPrimaryValue(from);
127+
block->addInput(input);
128+
input = std::make_shared<Input>("TO", Input::Type::Shadow);
129+
input->setPrimaryValue(to);
130+
block->addInput(input);
131+
132+
builder.addBlock("test_print");
133+
builder.addObscuredInput("STRING", block);
134+
return builder.currentBlock();
135+
};
136+
137+
auto block = addRandomTest(-45, 12);
138+
addRandomTest(12, 6.05);
139+
addRandomTest(-78.686, -45);
140+
addRandomTest(6.05, -78.686);
141+
142+
builder.build();
143+
144+
Compiler compiler(&m_engineMock, target.get());
145+
auto code = compiler.compile(block);
146+
Script script(target.get(), block, &m_engineMock);
147+
script.setCode(code);
148+
Thread thread(target.get(), &m_engineMock, &script);
149+
auto ctx = code->createExecutionContext(&thread);
150+
ctx->setRng(&m_rng);
151+
152+
static const std::string expected =
153+
"-18\n"
154+
"3.486789\n"
155+
"-59.468873\n"
156+
"-28.648764\n";
157+
158+
EXPECT_CALL(m_rng, randint(-45, 12)).WillOnce(Return(-18));
159+
EXPECT_CALL(m_rng, randintDouble(12, 6.05)).WillOnce(Return(3.486789));
160+
EXPECT_CALL(m_rng, randintDouble(-78.686, -45)).WillOnce(Return(-59.468873));
161+
EXPECT_CALL(m_rng, randintDouble(6.05, -78.686)).WillOnce(Return(-28.648764));
162+
testing::internal::CaptureStdout();
163+
code->run(ctx.get());
164+
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
165+
}

0 commit comments

Comments
 (0)