Skip to content

Commit 27fa0e8

Browse files
committed
Add Clock class
1 parent d264d7e commit 27fa0e8

File tree

8 files changed

+112
-0
lines changed

8 files changed

+112
-0
lines changed

src/engine/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ target_sources(scratchcpp
1111
script_p.h
1212
internal/engine.cpp
1313
internal/engine.h
14+
internal/clock.cpp
15+
internal/clock.h
16+
internal/iclock.h
1417
internal/blocksectioncontainer.cpp
1518
internal/blocksectioncontainer.h
1619
internal/global.h

src/engine/internal/clock.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "clock.h"
4+
5+
using namespace libscratchcpp;
6+
7+
std::shared_ptr<Clock> Clock::m_instance = std::make_shared<Clock>();
8+
9+
Clock::Clock()
10+
{
11+
}
12+
13+
std::shared_ptr<Clock> Clock::instance()
14+
{
15+
return m_instance;
16+
}
17+
18+
std::chrono::steady_clock::time_point Clock::currentTime() const
19+
{
20+
return std::chrono::steady_clock::now();
21+
}

src/engine/internal/clock.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <memory>
6+
#include "iclock.h"
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class Clock : public IClock
12+
{
13+
public:
14+
Clock();
15+
Clock(const Clock &) = delete;
16+
17+
static std::shared_ptr<Clock> instance();
18+
19+
std::chrono::steady_clock::time_point currentTime() const override;
20+
21+
private:
22+
static std::shared_ptr<Clock> m_instance;
23+
};
24+
25+
} // namespace libscratchcpp

src/engine/internal/iclock.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <chrono>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class IClock
11+
{
12+
public:
13+
virtual ~IClock() { }
14+
15+
virtual std::chrono::steady_clock::time_point currentTime() const = 0;
16+
};
17+
18+
} // namespace libscratchcpp

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ add_subdirectory(assets)
2828
add_subdirectory(script)
2929
add_subdirectory(extensions)
3030
add_subdirectory(engine)
31+
add_subdirectory(clock)

test/clock/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(
2+
clock_test
3+
clock_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
clock_test
8+
GTest::gtest_main
9+
scratchcpp
10+
)
11+
12+
gtest_discover_tests(clock_test)

test/clock/clock_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <engine/internal/clock.h>
2+
#include <cmath>
3+
4+
#include "../common.h"
5+
6+
using namespace libscratchcpp;
7+
8+
TEST(ClockTest, CurrentTime)
9+
{
10+
auto clock = Clock::instance();
11+
ASSERT_TRUE(clock);
12+
13+
auto currentTime = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now());
14+
double currentTimeMs = std::round(currentTime.time_since_epoch().count() / 10) * 10;
15+
16+
auto reportedTime = std::chrono::time_point_cast<std::chrono::milliseconds>(clock->currentTime());
17+
double reportedTimeMs = std::round(reportedTime.time_since_epoch().count() / 10) * 10;
18+
19+
ASSERT_EQ(reportedTimeMs, currentTimeMs);
20+
}

test/mocks/clockmock.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <engine/internal/iclock.h>
4+
#include <gmock/gmock.h>
5+
6+
using namespace libscratchcpp;
7+
8+
class ClockMock : public IClock
9+
{
10+
public:
11+
MOCK_METHOD(std::chrono::steady_clock::time_point, currentTime, (), (const override));
12+
};

0 commit comments

Comments
 (0)