Search code examples
c++googletestbitset

Test Bitset using GTest


How can I test whether every element in a bitset has same value using gmock and gtest. I am looking for something like below which does not compile

bitset<25> flags;
ASSERT_THAT(flags, AllOf(Eq(true)));

Solution

  • "How can I test whether every element in a bitset has same value using gmock and gtest."

    The AllOf() matcher is meant to combine other matchers as explained in the reference documentation.
    The single Eq(true) requires to have flags an automatic cast operator for bool (or at least int), which actually isn't available with std::bitset<>. That's why your approach doesn't compile.

    You can easily do something like

    bitset<25> flags;
    // ...
    ASSERT_TRUE(flags.all());
    

    That function is available from std::bitset<>.


    Some more alternatives:

    ASSERT_TRUE(flags.any());
    

    ASSERT_TRUE(flags.none());
    

    bitset<25> expected_value("1100111001110011100111001");
    
    ASSERT_EQUAL(expected_value,flags);