Cockatrice/tests/server_counter_test.cpp
tooomm fb3f895cfd
[CI] Update Linux Dockerfiles (#6894)
- Install the distribution's gtest (libgtest-dev / gtest-devel) in the
  Debian, Ubuntu and Fedora images, so configuring the test suite no
  longer downloads googletest; Arch already had it.
- Drop the toolchain meta packages. g++, Ninja and cmake are all that is
  used, and make is not: Ninja is the generator on every Linux leg. The
  DEB legs keep dpkg-dev, which CPack needs for dpkg-shlibdeps when
  CPACK_DEBIAN_PACKAGE_SHLIBDEPS is on.
- Use the slim Debian bases and install Fedora packages with --nodocs.
- Define the global rng in the tests that pull the server game classes
  out of server_remote, as the other server tests already do. Debian's
  libgtest.a leaves a std::string reference undefined, so the linker
  drags server_protocolhandler.cpp and the classes behind it into any
  test that links the library, and those need the symbol.

git, ca-certificates, libcurl and OpenSSL stay: sentry-native is cloned
over git at configure time on every packaging leg, and OpenSSL also backs
CryptoUtil. mariadb-devel stays on Fedora for the same reason it is
installed elsewhere: without it Servatrice configures successfully but
silently loses its MySQL connector.

Co-authored-by: Lukas Brübach <44814898+BruebachL@users.noreply.github.com>
2026-11-08 09:01:56 +01:00

94 lines
2.8 KiB
C++

/** @file server_counter_test.cpp
* @brief Tests for Server_Counter operations.
* @ingroup Tests
*/
#include "libcockatrice/protocol/pb/color.pb.h"
#include <QString>
#include <gtest/gtest.h>
#include <libcockatrice/network/server/remote/game/server_counter.h>
#include <libcockatrice/rng/rng_abstract.h>
#include <limits>
#include <string>
// Referenced by other translation units in the server_remote library.
RNG_Abstract *rng = nullptr;
TEST(ServerCounter, IncrementDoesNotOverflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max());
bool changed = c.incrementCount(1);
EXPECT_FALSE(changed);
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::max());
}
TEST(ServerCounter, DecrementDoesNotUnderflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::min());
bool changed = c.incrementCount(-1);
EXPECT_FALSE(changed);
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::min());
}
TEST(ServerCounter, SetCountReturnsFalseWhenUnchanged)
{
Server_Counter c(1, "test", color(), 10, 50);
bool changed = c.setCount(50);
EXPECT_FALSE(changed);
}
TEST(ServerCounter, IncrementReturnsChangeStatus)
{
Server_Counter c(1, "test", color(), 10, 50);
EXPECT_TRUE(c.incrementCount(10));
EXPECT_EQ(c.getCount(), 60);
EXPECT_FALSE(c.incrementCount(0));
EXPECT_EQ(c.getCount(), 60);
}
TEST(ServerCounter, LargePositiveDeltaDoesNotOverflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max() - 10);
bool changed = c.incrementCount(std::numeric_limits<int>::max());
EXPECT_TRUE(changed); // Value changes from INT_MAX-10 to INT_MAX (clamped)
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::max());
}
TEST(ServerCounter, LargeNegativeDeltaDoesNotUnderflow)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::min() + 10);
bool changed = c.incrementCount(std::numeric_limits<int>::min());
EXPECT_TRUE(changed); // Value changes from INT_MIN+10 to INT_MIN (clamped)
EXPECT_EQ(c.getCount(), std::numeric_limits<int>::min());
}
TEST(ServerCounter, SetCountReturnsTrueWhenChanged)
{
Server_Counter c(1, "test", color(), 10, 50);
EXPECT_TRUE(c.setCount(100));
EXPECT_EQ(c.getCount(), 100);
}
TEST(ServerCounter, BasicIncrementWorks)
{
Server_Counter c(1, "test", color(), 10, 50);
EXPECT_TRUE(c.incrementCount(10));
EXPECT_EQ(c.getCount(), 60);
EXPECT_TRUE(c.incrementCount(-20));
EXPECT_EQ(c.getCount(), 40);
}
TEST(ServerCounter, MixedExtremesDoNotClamp)
{
Server_Counter c(1, "test", color(), 10, std::numeric_limits<int>::max());
bool changed = c.incrementCount(std::numeric_limits<int>::min());
EXPECT_TRUE(changed);
EXPECT_EQ(c.getCount(), -1);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}