AUnit icon indicating copy to clipboard operation
AUnit copied to clipboard

ASSERT_EQ does not work with refrence

Open pischky opened this issue 3 years ago • 3 comments

ASSERT_EQ from aunit/contrib/gtest.h does not compile when reference is used:

    uint8_t& ref = a[2];
    ASSERT_EQ( 0x83, ref );

Compiler message is:

invalid static_cast from type 'int' to type 'uint8_t& {aka unsigned char&}'

removing the cast from the macros make it compile.

#define ASSERT_EQ(expected, value) assertEqual((expected),(value))

May be there is a better (improved) version instead of static_cast<decltype(a)>(e)

Complete sketch: ArrayTest_with_AUnit.ino.txt

pischky avatar Jan 10 '22 08:01 pischky

May be a solution is to use remove_reference:

#define ASSERT_EQ(expected, actual)                     \
    assertEqual(                                        \
        static_cast< std::remove_reference<             \
                decltype(actual) >::type >(expected),   \
        actual)

But remove_reference is not support in current Arduino version (1.8.19) so you need also ArxTypeTraits 0.2.3 by Hideaki Tai.

pischky avatar Jan 10 '22 11:01 pischky

Maybe the solution is to remove the static_cast<decltype(a)>? See if the following helps: https://github.com/bxparks/AUnit/tree/gtest_ref_cast It fixes your specific problem.

I don't have any code that uses <gtest.h>, and I'm not entirely sure I understand all the edge cases of the Google Test framework, so not really sure if this is the best solution.

bxparks avatar Jan 10 '22 15:01 bxparks

Thanks for your suggestion to remove the static_cast. Yes that fixes my problem. I thought the static_cast has the intention to select the correct function from the set of overloaded assertEqual(). I hoped be the original author of gtest.h did know.

I'm porting some Tests from x86 to arduino. So I'm doing further testing with my variant using remove_reference. So far it seems to work fine. Only disadvantage is that ArxTypeTraits is required.

pischky avatar Jan 10 '22 15:01 pischky