From fe9c611936e31d308d6dc69133d64d6121e4df89 Mon Sep 17 00:00:00 2001 From: Milan Kriz Date: Fri, 13 Dec 2024 10:15:16 +0100 Subject: [PATCH] [#53] Fix operator++/-- in NumericTypeWrapper --- runtime/src/zserio/Types.h | 4 ++-- runtime/test/zserio/TypesTest.cpp | 33 ++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/runtime/src/zserio/Types.h b/runtime/src/zserio/Types.h index b0a4f4e..6c1812d 100644 --- a/runtime/src/zserio/Types.h +++ b/runtime/src/zserio/Types.h @@ -83,7 +83,7 @@ class NumericTypeWrapper constexpr NumericTypeWrapper operator++(int) noexcept { - NumericTypeWrapper old; + NumericTypeWrapper old = *this; operator++(); return old; } @@ -96,7 +96,7 @@ class NumericTypeWrapper constexpr NumericTypeWrapper operator--(int) noexcept { - NumericTypeWrapper old; + NumericTypeWrapper old = *this; operator--(); return old; } diff --git a/runtime/test/zserio/TypesTest.cpp b/runtime/test/zserio/TypesTest.cpp index 36b45d5..439a09b 100644 --- a/runtime/test/zserio/TypesTest.cpp +++ b/runtime/test/zserio/TypesTest.cpp @@ -58,10 +58,13 @@ void testIntOperators(typename T::ValueType minValue = NumericLimits::min()) } // increment / decrement - ++value; - --value; - value++; - value--; + Int64 origValue = value; + ASSERT_EQ(origValue + 1, ++value); + ASSERT_EQ(origValue, --value); + ASSERT_EQ(origValue, value++); + ASSERT_EQ(origValue + 1, value); + ASSERT_EQ(origValue + 1, value--); + ASSERT_EQ(origValue, value); // logical ASSERT_FALSE(!valueMin); @@ -133,10 +136,13 @@ void testUIntOperators(typename T::ValueType minValue = NumericLimits::min()) } // increment / decrement - ++value; - --value; - value++; - value--; + UInt64 origValue = value; + ASSERT_EQ(origValue + 1, ++value); + ASSERT_EQ(origValue, --value); + ASSERT_EQ(origValue, value++); + ASSERT_EQ(origValue + 1, value); + ASSERT_EQ(origValue + 1, value--); + ASSERT_EQ(origValue, value); // logical ASSERT_FALSE(!valueMax); @@ -210,10 +216,13 @@ void testFloatOperators() value = valueMax / valueMin; // increment / decrement - ++value; - --value; - value++; - value--; + FloatWrapper origValue = value; + ASSERT_DOUBLE_EQ(origValue + 1, ++value); + ASSERT_DOUBLE_EQ(origValue, --value); + ASSERT_DOUBLE_EQ(origValue, value++); + ASSERT_DOUBLE_EQ(origValue + 1, value); + ASSERT_DOUBLE_EQ(origValue + 1, value--); + ASSERT_DOUBLE_EQ(origValue, value); // comparison ASSERT_TRUE(valueMin < valueMax);