mirror of https://github.com/OpenRCT2/OpenRCT2.git
Merge 846467a9b5
into d58673105d
This commit is contained in:
commit
850ba5119d
|
@ -0,0 +1,119 @@
|
|||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2024 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// TODO: Move or remove these constants if
|
||||
// we can use the constants defined
|
||||
// in Context.h
|
||||
namespace TickConversion
|
||||
{
|
||||
constexpr uint32_t kGameUpdateFPS = 40;
|
||||
constexpr uint32_t kGameUpdateMs = 1000 / kGameUpdateFPS;
|
||||
} // namespace TickConversion
|
||||
|
||||
template<typename T, typename Tag> struct GameTicks
|
||||
{
|
||||
using ValueType = T;
|
||||
|
||||
ValueType Value;
|
||||
|
||||
static constexpr GameTicks FromSeconds(uint32_t seconds)
|
||||
{
|
||||
GameTicks result = FromMilliseconds(seconds * 1000);
|
||||
return result;
|
||||
}
|
||||
static constexpr GameTicks FromMilliseconds(uint32_t milliseconds)
|
||||
{
|
||||
GameTicks<T, Tag> result;
|
||||
result.Value = (milliseconds + (TickConversion::kGameUpdateTimeMs - 1)) /
|
||||
TickConversion::kGameUpdateTimeMs /* 25 */;
|
||||
return result;
|
||||
}
|
||||
|
||||
constexpr void operator=(T rhs)
|
||||
{
|
||||
Value = rhs;
|
||||
}
|
||||
|
||||
constexpr GameTicks& operator++()
|
||||
{
|
||||
++Value;
|
||||
return *this;
|
||||
}
|
||||
constexpr GameTicks operator++(int)
|
||||
{
|
||||
GameTicks<T, Tag> temp = *this;
|
||||
Value++;
|
||||
return temp;
|
||||
}
|
||||
constexpr GameTicks& operator--()
|
||||
{
|
||||
--Value;
|
||||
return *this;
|
||||
}
|
||||
constexpr GameTicks operator--(int)
|
||||
{
|
||||
GameTicks<T, Tag> temp = *this;
|
||||
Value--;
|
||||
return temp;
|
||||
}
|
||||
|
||||
constexpr GameTicks operator+(GameTicks rhs) const
|
||||
{
|
||||
return GameTicks(Value + rhs.Value);
|
||||
}
|
||||
constexpr GameTicks operator-(GameTicks rhs) const
|
||||
{
|
||||
return GameTicks(Value - rhs.Value);
|
||||
}
|
||||
constexpr GameTicks operator*(GameTicks rhs) const
|
||||
{
|
||||
return GameTicks(Value * rhs.Value);
|
||||
}
|
||||
constexpr GameTicks operator/(GameTicks rhs) const
|
||||
{
|
||||
return GameTicks(Value / rhs.GameTicks);
|
||||
}
|
||||
|
||||
constexpr GameTicks& operator+=(GameTicks rhs)
|
||||
{
|
||||
Value += rhs.Value;
|
||||
return *this;
|
||||
}
|
||||
constexpr GameTicks& operator-=(GameTicks rhs)
|
||||
{
|
||||
Value -= rhs.Value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr bool operator==(const GameTicks& rhs) const
|
||||
{
|
||||
return Value == rhs.Value;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(const GameTicks& rhs) const
|
||||
{
|
||||
return Value != rhs.Value;
|
||||
}
|
||||
|
||||
constexpr bool operator<=>(const GameTicks& rhs) const
|
||||
{
|
||||
return Value <=> rhs.Value;
|
||||
}
|
||||
|
||||
constexpr GameTicks& operator%(const GameTicks& rhs)
|
||||
{
|
||||
GameTicks result;
|
||||
result.Value = Value % rhs.Value;
|
||||
return result;
|
||||
}
|
||||
};
|
|
@ -29,6 +29,7 @@ set(test_files
|
|||
"${CMAKE_CURRENT_SOURCE_DIR}/S6ImportExportTests.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/SawyerCodingTest.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/StringTest.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/GameTicksTest.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/TestData.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/TestData.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/tests.cpp"
|
||||
|
|
|
@ -0,0 +1,131 @@
|
|||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2024 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <openrct2/GameTicks.h>
|
||||
|
||||
struct GameTickTag {};
|
||||
|
||||
using TestGameTick = GameTicks<uint32_t, GameTickTag>;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Tests for GameTicks<T, Tag>
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST(GameTicksTest, Construction)
|
||||
{
|
||||
TestGameTick tick(10);
|
||||
EXPECT_EQ(10, tick.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, FromSeconds)
|
||||
{
|
||||
TestGameTick actual = TestGameTick::FromMilliseconds(0);
|
||||
EXPECT_EQ(0, actual.Value);
|
||||
|
||||
TestGameTick actual = TestGameTick::FromSeconds(1);
|
||||
EXPECT_EQ(40, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, FromMilliseconds)
|
||||
{
|
||||
TestGameTick actual = TestGameTick::FromMilliseconds(0);
|
||||
EXPECT_EQ(0, actual.Value);
|
||||
|
||||
TestGameTick actual = TestGameTick::FromMilliseconds(1);
|
||||
EXPECT_EQ(1, actual.Value);
|
||||
|
||||
TestGameTick actual = TestGameTick::FromMilliseconds(24);
|
||||
EXPECT_EQ(1, actual.Value);
|
||||
|
||||
TestGameTick actual = TestGameTick::FromMilliseconds(1000);
|
||||
EXPECT_EQ(40, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, Addition)
|
||||
{
|
||||
TestGameTick tick1(10);
|
||||
TestGameTick tick2(20);
|
||||
|
||||
TestGameTick actual = tick1 + tick2;
|
||||
EXPECT_EQ(30, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, Subtraction)
|
||||
{
|
||||
TestGameTick tick1(10);
|
||||
TestGameTick tick2(20);
|
||||
|
||||
TestGameTick actual = tick2 - tick1;
|
||||
EXPECT_EQ(10, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, Multiplication)
|
||||
{
|
||||
TestGameTick tick1(10);
|
||||
TestGameTick tick2(20);
|
||||
|
||||
TestGameTick actual = tick1 * tick2;
|
||||
EXPECT_EQ(200, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, Division)
|
||||
{
|
||||
TestGameTick tick1(10);
|
||||
TestGameTick tick2(20);
|
||||
|
||||
TestGameTick actual = tick2 / tick1;
|
||||
EXPECT_EQ(2, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, Modulus)
|
||||
{
|
||||
TestGameTick tick1(10);
|
||||
TestGameTick tick2(20);
|
||||
|
||||
TestGameTick actual = tick2 % tick1;
|
||||
EXPECT_EQ(0, actual.Value);
|
||||
|
||||
TestGameTick tick3(3);
|
||||
TestGameTick tick4(2);
|
||||
|
||||
TestGameTick actual = tick3 % tick4;
|
||||
EXPECT_EQ(1, actual.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, IncrementDecrement)
|
||||
{
|
||||
TestGameTick tick(10);
|
||||
|
||||
++tick;
|
||||
EXPECT_EQ(11, tick.Value);
|
||||
|
||||
tick++;
|
||||
EXPECT_EQ(12, tick.Value);
|
||||
|
||||
--tick;
|
||||
EXPECT_EQ(11, tick.Value);
|
||||
|
||||
tick--;
|
||||
EXPECT_EQ(10, tick.Value);
|
||||
}
|
||||
|
||||
TEST(GameTicksTest, Comparisons)
|
||||
{
|
||||
TestGameTick tick1(10);
|
||||
TestGameTick tick2(20);
|
||||
TestGameTick tick3(10);
|
||||
|
||||
EXPECT_TRUE(tick1 == tick3);
|
||||
EXPECT_TRUE(tick1 != tick2);
|
||||
EXPECT_TRUE(tick2 > tick1);
|
||||
EXPECT_TRUE(tick1 < tick2);
|
||||
EXPECT_TRUE(tick1 <= tick3);
|
||||
EXPECT_TRUE(tick2 >= tick1);
|
||||
}
|
Loading…
Reference in New Issue