Skip to content

Commit 3367bcb

Browse files
authored
Merge pull request #1 from scratchcpp/utests
Add unit tests
2 parents 2cc5d55 + b8efce6 commit 3367bcb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3619
-9
lines changed

.github/workflows/utests.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Unit tests
2+
3+
on:
4+
push:
5+
branches: '*'
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
env:
10+
BUILD_TYPE: Debug
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
with:
19+
submodules: true
20+
21+
- name: Install dependencies
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y nlohmann-json3-dev libutfcpp-dev
25+
shell: bash
26+
- name: Configure CMake
27+
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DLIBSCRATCHCPP_BUILD_UNIT_TESTS=ON
28+
29+
- name: Build
30+
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
31+
32+
- name: Run unit tests
33+
run: ctest --test-dir build -V

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "thirdparty/zip"]
22
path = thirdparty/zip
33
url = https://github.com/kuba--/zip
4+
[submodule "thirdparty/googletest"]
5+
path = thirdparty/googletest
6+
url = https://github.com/google/googletest

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ set(CMAKE_CXX_STANDARD 17)
77
set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
set(ZIP_SRC thirdparty/zip/src)
99

10+
option(LIBSCRATCHCPP_BUILD_UNIT_TESTS "Build unit tests" ON)
11+
1012
find_package(nlohmann_json 3.9.1 REQUIRED)
1113
find_package(utf8cpp REQUIRED)
1214

1315
add_library(scratchcpp SHARED)
1416
add_subdirectory(src)
17+
include_directories(src)
1518

1619
add_library(zip SHARED
1720
${ZIP_SRC}/zip.c
@@ -25,3 +28,8 @@ target_link_libraries(scratchcpp PRIVATE utf8cpp)
2528
target_link_libraries(scratchcpp PRIVATE zip)
2629

2730
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_LIBRARY)
31+
32+
if (LIBSCRATCHCPP_BUILD_UNIT_TESTS)
33+
enable_testing()
34+
add_subdirectory(test)
35+
endif()

src/engine/engine.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ void Engine::clear()
2727
}
2828

2929
/*!
30-
* Compiles all scripts to bytecode.
31-
* \see Compiler
30+
* Resolves ID references and sets pointers of entities.
31+
* \note This function shouldn't normally be called because it's called from compile().
3232
*/
33-
void Engine::compile()
33+
void Engine::resolveIds()
3434
{
35-
// Resolve entities by ID
3635
for (auto target : m_targets) {
3736
std::cout << "Processing target " << target->name() << "..." << std::endl;
3837
auto blocks = target->blocks();
@@ -66,6 +65,16 @@ void Engine::compile()
6665
block->updateFieldMap();
6766
}
6867
}
68+
}
69+
70+
/*!
71+
* Compiles all scripts to bytecode.
72+
* \see Compiler
73+
*/
74+
void Engine::compile()
75+
{
76+
// Resolve entities by ID
77+
resolveIds();
6978

7079
// Compile scripts to bytecode
7180
for (auto target : m_targets) {
@@ -277,6 +286,12 @@ void Engine::breakFrame()
277286
m_breakFrame = true;
278287
}
279288

289+
/*! Returns true if breakFrame() was called. */
290+
bool libscratchcpp::Engine::breakingCurrentFrame()
291+
{
292+
return m_breakFrame;
293+
}
294+
280295
/*! Registers the given block section. */
281296
void Engine::registerSection(std::shared_ptr<IBlockSection> section)
282297
{
@@ -423,6 +438,12 @@ void Engine::setExtensions(const std::vector<std::string> &newExtensions)
423438
}
424439
}
425440

441+
/*! Returns the map of scripts (each top level block has a Script object). */
442+
const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &Engine::scripts() const
443+
{
444+
return m_scripts;
445+
}
446+
426447
/*! Returns the block with the given ID. */
427448
std::shared_ptr<Block> Engine::getBlock(std::string id)
428449
{

src/engine/engine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class LIBSCRATCHCPP_EXPORT Engine
3030
Engine(const Engine &) = delete;
3131

3232
void clear();
33+
void resolveIds();
3334
void compile();
3435

3536
void frame();
@@ -44,6 +45,7 @@ class LIBSCRATCHCPP_EXPORT Engine
4445
bool broadcastRunning(unsigned int index);
4546

4647
void breakFrame();
48+
bool breakingCurrentFrame();
4749

4850
void registerSection(std::shared_ptr<IBlockSection> section);
4951
unsigned int functionIndex(BlockFunc f);
@@ -65,6 +67,8 @@ class LIBSCRATCHCPP_EXPORT Engine
6567
std::vector<std::string> extensions() const;
6668
void setExtensions(const std::vector<std::string> &newExtensions);
6769

70+
const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &scripts() const;
71+
6872
private:
6973
std::shared_ptr<Block> getBlock(std::string id);
7074
std::shared_ptr<Variable> getVariable(std::string id);

src/engine/script.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ unsigned int *Script::bytecode() const
1717
return m_bytecode;
1818
}
1919

20+
/*! Returns the bytecode vector. */
21+
const std::vector<unsigned int> &Script::bytecodeVector() const
22+
{
23+
return m_bytecodeVector;
24+
}
25+
2026
/*! Sets the bytecode of the script. */
2127
void Script::setBytecode(const std::vector<unsigned int> &code)
2228
{

src/engine/script.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class LIBSCRATCHCPP_EXPORT Script
1515
Script(const Script &) = delete;
1616

1717
unsigned int *bytecode() const;
18+
const std::vector<unsigned int> &bytecodeVector() const;
1819
void setBytecode(const std::vector<unsigned int> &code);
1920

2021
void setProcedures(const std::vector<unsigned int *> &procedures);

src/engine/virtualmachine.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,12 @@ do_loop_end : {
431431

432432
do_round : {
433433
const Value *v = READ_REG(0, 1);
434-
if (!v->isInfinity() && !v->isNegativeInfinity())
435-
REPLACE_RET_VALUE(static_cast<long>(std::round(v->toDouble())), 1);
434+
if (!v->isInfinity() && !v->isNegativeInfinity()) {
435+
if (v->toDouble() < 0) {
436+
REPLACE_RET_VALUE(static_cast<long>(std::floor(v->toDouble() + 0.5)), 1);
437+
} else
438+
REPLACE_RET_VALUE(static_cast<long>(v->toDouble() + 0.5), 1);
439+
}
436440
DISPATCH();
437441
}
438442

@@ -491,7 +495,11 @@ do_tan : {
491495
if (v->isInfinity() || v->isNegativeInfinity())
492496
REPLACE_RET_VALUE(Value(Value::SpecialValue::NaN), 1);
493497
else {
494-
long mod = v->toLong() % 360;
498+
long mod;
499+
if (v->toLong() < 0)
500+
mod = (v->toLong() + 360) % 360;
501+
else
502+
mod = v->toLong() % 360;
495503
if (mod == 90)
496504
REPLACE_RET_VALUE(Value(Value::SpecialValue::Infinity), 1);
497505
else if (mod == 270)
@@ -522,8 +530,10 @@ do_acos : {
522530

523531
do_atan : {
524532
const Value &v = *READ_REG(0, 1);
525-
if (v < -1 || v > 1)
526-
REPLACE_RET_VALUE(Value(Value::SpecialValue::NaN), 1);
533+
if (v.isInfinity())
534+
REPLACE_RET_VALUE(90, 1);
535+
else if (v.isNegativeInfinity())
536+
REPLACE_RET_VALUE(-90, 1);
527537
else
528538
REPLACE_RET_VALUE(std::atan(v.toDouble()) * 180 / pi, 1);
529539
DISPATCH();

src/engine/virtualmachine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
102102

103103
void setBytecode(unsigned int *code);
104104

105+
/*! Returns the array of constant values. */
106+
const Value *constValues() const { return m_constValues; };
107+
105108
/*! Returns the bytecode array. */
106109
unsigned int *bytecode() const { return m_bytecode; };
107110

src/internal/iprojectreader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class LIBSCRATCHCPP_EXPORT IProjectReader
1919

2020
virtual bool load() = 0;
2121
virtual bool isValid() = 0;
22+
virtual void clear() = 0;
2223
virtual std::vector<std::shared_ptr<Target>> targets() = 0;
2324
virtual std::vector<std::shared_ptr<Broadcast>> broadcasts() = 0;
2425
virtual std::vector<std::string> extensions() = 0;

0 commit comments

Comments
 (0)