Skip to content

Commit

Permalink
Add expressions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Nov 25, 2024
1 parent d714272 commit b766fcf
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cmake/compiler_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,11 @@ function(compiler_get_warnings_as_errors_setup VARNAME)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if (ZSERIO_ENABLE_WERROR)
set(WARNINGS_SETUP "-Werror")
set(WARNINGS_SETUP # used by zserio @deprecated feature (DeprecatedAttribute.h)
"${WARNINGS_SETUP} -Wno-deprecated-declarations")
set(WARNINGS_SETUP_LIST
"-Wno-deprecated-declarations" # used by zserio @deprecated feature (DeprecatedAttribute.h)
"-Wno-unreachable-code" # used by expressions tests
)
string(REPLACE ";" " " WARNINGS_SETUP "${WARNINGS_SETUP} ${WARNINGS_SETUP_LIST}")
endif ()
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "8.0.0")
set(WARNINGS_SETUP_LIST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ else if (expr.op1().getExprZserioType() instanceof BitmaskType)
final BitmaskType bitmaskType = (BitmaskType)expr.op1().getExprZserioType();
final CppNativeType bitmaskNativeType = cppNativeMapper.getCppType(bitmaskType);
return new UnaryExpressionFormatting(
"static_cast<" + bitmaskNativeType.getFullName() + "::ZserioType::ValueType>(", ")");
"::zserio::bitmaskToValue<" + bitmaskNativeType.getFullName() + ">(", ")");
}
else
{
Expand Down
13 changes: 13 additions & 0 deletions runtime/src/zserio/Bitmasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
namespace zserio
{

template <typename T>
constexpr std::enable_if_t<is_bitmask_v<T>, typename T::ZserioType> bitmaskToValue(T value)
{
return value.getValue();
}

template <typename T>
constexpr std::enable_if_t<is_bitmask_v<T> && std::is_enum_v<typename T::Values>, typename T::ZserioType>
bitmaskToValue(typename T::Values rawValue)
{
return static_cast<typename T::ZserioType>(static_cast<typename T::ZserioType::ValueType>(rawValue));
}

namespace detail
{

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/zserio/Enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace zserio
/**
* Enum traits provides various information for Zserio enums.
*
* This information is provided via specializations of the EnumTraits strucure.
* This information is provided via specializations of the EnumTraits structure.
*/
template <typename T>
struct EnumTraits
Expand Down
24 changes: 24 additions & 0 deletions test/language/expressions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
add_library(expressions_zs STATIC ${TEST_ZS_ROOT}/expressions.zs)
zserio_generate_cpp(
TARGET expressions_zs
SRC_DIR ${TEST_ZS_ROOT}
GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/gen
EXTRA_ARGS ${ZSERIO_EXTRA_ARGS}
GENERATED_SOURCES_VAR GENERATED_SOURCES
OUTPUT_VAR ZSERIO_LOG
ERROR_VAR ZSERIO_LOG
)
target_link_libraries(expressions_zs PUBLIC ZserioCpp17Runtime)
if (ZSERIO_LOG)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/zserio_log.txt "${ZSERIO_LOG}")
check_zserio_warnings("${ZSERIO_LOG}" 0)
endif ()

add_custom_test(expressions
DEPENDS
expressions_zs
SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cpp/BitmaskTypeTest.cpp
GENERATED_SOURCES
${GENERATED_SOURCES}
)
21 changes: 21 additions & 0 deletions test/language/expressions/ClangTidySuppressions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
bugprone-exception-escape:gen/expressions/isset_operator/IsSetOperator.cpp
bugprone-exception-escape:gen/expressions/isset_operator/Parameterized.h
bugprone-exception-escape:gen/expressions/isset_operator/Parameterized.cpp

google-explicit-constructor:gen/expressions/bitmask_type/Colors.h
google-explicit-constructor:gen/expressions/isset_operator/TestBitmask.h

performance-move-const-arg:gen/expressions/field_type/FieldTypeExpression.cpp
performance-move-const-arg:gen/expressions/field_type_with_clash/FieldTypeExpression.cpp

readability-simplify-boolean-expr:gen/expressions/array_type/ArrayTypeExpression.cpp
readability-simplify-boolean-expr:gen/expressions/index_operator/Element.cpp
readability-simplify-boolean-expr:gen/expressions/parameterized_array_type/ParameterizedArrayElement.cpp
readability-simplify-boolean-expr:gen/expressions/question_mark/QuestionMarkExpression.cpp
readability-simplify-boolean-expr:gen/expressions/uint64_type/UInt64TypeExpression.cpp

readability-uppercase-literal-suffix:gen/expressions/float_type/FloatTypeExpression.cpp

readability-implicit-bool-conversion:gen/expressions/index_operator/Element.cpp
readability-implicit-bool-conversion:gen/expressions/parameterized_array_type/ParameterizedArrayElement.cpp
readability-implicit-bool-conversion:gen/expressions/parameterized_array_type/ParameterizedArrayHolder.cpp
75 changes: 75 additions & 0 deletions test/language/expressions/cpp/BitmaskTypeTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "expressions/bitmask_type/BitmaskTypeExpression.h"
#include "gtest/gtest.h"

namespace expressions
{
namespace bitmask_type
{

TEST(BitmaskTypeTest, bitSizeOfNoColor)
{
BitmaskTypeExpression data;
data.colors = Colors();
data.hasNotColorRed = true;

ASSERT_EQ(9, zserio::detail::bitSizeOf(zserio::View<BitmaskTypeExpression>(data)));
}

TEST(BitmaskTypeTest, bitSizeOfRed)
{
BitmaskTypeExpression data;
data.colors = Colors::Values::RED;
data.hasColorRed = true;

ASSERT_EQ(9, zserio::detail::bitSizeOf(zserio::View<BitmaskTypeExpression>(data)));
}

TEST(BitmaskTypeTest, bitSizeOfGreen)
{
BitmaskTypeExpression data;
data.colors = Colors::Values::GREEN;
data.hasColorGreen = true;
data.hasNotColorRed = true;
data.hasOtherColorThanRed = true;

ASSERT_EQ(11, zserio::detail::bitSizeOf(zserio::View<BitmaskTypeExpression>(data)));
}

TEST(BitmaskTypeTest, bitSizeOfBlue)
{
BitmaskTypeExpression data;
data.colors = Colors::Values::BLUE;
data.hasColorBlue = true;
data.hasNotColorRed = true;
data.hasOtherColorThanRed = true;

ASSERT_EQ(11, zserio::detail::bitSizeOf(zserio::View<BitmaskTypeExpression>(data)));
}

TEST(BitmaskTypeTest, bitSizeOfBlueGreen)
{
BitmaskTypeExpression data;
data.colors = Colors::Values::BLUE | Colors::Values::GREEN;
data.hasColorGreen = true;
data.hasColorBlue = true;
data.hasNotColorRed = true;
data.hasOtherColorThanRed = true;

ASSERT_EQ(12, zserio::detail::bitSizeOf(zserio::View<BitmaskTypeExpression>(data)));
}

TEST(BitmaskTypeTest, bitSizeOfAllColors)
{
BitmaskTypeExpression data;
data.colors = Colors::Values::RED | Colors::Values::GREEN | Colors::Values::BLUE;
data.hasColorRed = true;
data.hasColorGreen = true;
data.hasColorBlue = true;
data.hasAllColors = true;
data.hasOtherColorThanRed = true;

ASSERT_EQ(13, zserio::detail::bitSizeOf(zserio::View<BitmaskTypeExpression>(data)));
}

} // namespace bitmask_type
} // namespace expressions

0 comments on commit b766fcf

Please sign in to comment.