mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-26 00:23:55 -07:00
Unify counter clamp arithmetic into shared addClamped() helper
Rename MAX_COUNTERS_ON_CARD to MAX_COUNTER_VALUE Add addClamped() in trice_limits.h, which uses a 64-bit intermediate so the addition cannot overflow int. Both Server_Card and Server_Counter use it. Add optional [minValue, maxValue] bounds to Server_Counter; setCount() and incrementCount() both clamp. Defaults are unbounded, so existing callers are unaffected.
This commit is contained in:
parent
687e6644bc
commit
80946b2fd4
8 changed files with 114 additions and 50 deletions
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <gtest/gtest.h>
|
||||
#include <libcockatrice/network/server/remote/game/server_counter.h>
|
||||
#include <libcockatrice/utility/trice_limits.h>
|
||||
#include <limits>
|
||||
|
||||
TEST(ServerCounter, IncrementDoesNotOverflow)
|
||||
|
|
@ -79,6 +80,37 @@ TEST(ServerCounter, MixedExtremesDoNotClamp)
|
|||
EXPECT_EQ(c.getCount(), -1);
|
||||
}
|
||||
|
||||
TEST(ServerCounter, SetCountClampsToCustomBounds)
|
||||
{
|
||||
Server_Counter c(1, "test", color(), 10, 50, 0, 100);
|
||||
EXPECT_TRUE(c.setCount(150));
|
||||
EXPECT_EQ(c.getCount(), 100);
|
||||
EXPECT_TRUE(c.setCount(-10));
|
||||
EXPECT_EQ(c.getCount(), 0);
|
||||
}
|
||||
|
||||
TEST(ServerCounter, IncrementClampsToCustomBounds)
|
||||
{
|
||||
Server_Counter c(1, "test", color(), 10, 50, 0, 100);
|
||||
EXPECT_TRUE(c.incrementCount(100));
|
||||
EXPECT_EQ(c.getCount(), 100);
|
||||
EXPECT_FALSE(c.incrementCount(1));
|
||||
EXPECT_EQ(c.getCount(), 100);
|
||||
EXPECT_TRUE(c.incrementCount(-200));
|
||||
EXPECT_EQ(c.getCount(), 0);
|
||||
EXPECT_FALSE(c.incrementCount(-1));
|
||||
EXPECT_EQ(c.getCount(), 0);
|
||||
}
|
||||
|
||||
TEST(ServerCounter, CustomBoundsClampToMaxCounterValue)
|
||||
{
|
||||
Server_Counter c(1, "test", color(), 20, 0, 0, MAX_COUNTER_VALUE);
|
||||
EXPECT_TRUE(c.setCount(1000));
|
||||
EXPECT_EQ(c.getCount(), MAX_COUNTER_VALUE);
|
||||
EXPECT_TRUE(c.setCount(-5));
|
||||
EXPECT_EQ(c.getCount(), 0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue