Googletest Parametrized tests crash

不羁的心 提交于 2019-12-11 02:07:13

问题


I've just learned about value-parametrized unit tests in googletest and would like to use them in my project.

I wrote a simple parametrized test.

Header:

#include <gtest/gtest.h>

namespace EnsembleClustering {

class ParametrizedGTest: public testing::TestWithParam<int> {
public:
    ParametrizedGTest();
    virtual ~ParametrizedGTest();
};

} /* namespace EnsembleClustering */

Source:

#include "ParametrizedGTest.h"

namespace EnsembleClustering {

ParametrizedGTest::ParametrizedGTest() {
    // TODO Auto-generated constructor stub

}

ParametrizedGTest::~ParametrizedGTest() {
    // TODO Auto-generated destructor stub
}


TEST_P(ParametrizedGTest, testParameter) {
    int n = GetParam();
    EXPECT_EQ(n, GetParam());
}


INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance,
                        ParametrizedGTest,
                        ::testing::Values(100));

} /* namespace EnsembleClustering */

Now, when I run googletest as usual, the program crashes without any output. The gdb stack trace is

EnsembleClustering-D [C/C++ Application]    
    EnsembleClustering  
        Thread [1] (Suspended : Signal : EXC_BAD_ACCESS:Could not access memory)    
            __gnu_debug::_Safe_sequence_base::_M_attach_single() at 0x100528add 
            __gnu_debug::_Safe_sequence_base::_M_attach() at 0x100528a74    
            __gnu_debug::_Safe_iterator_base::_M_attach() at 0x100528bfe    
            __gnu_debug::_Safe_iterator_base::_Safe_iterator_base() at safe_base.h:90 0x1000016e9   
            __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<testing::internal::ParameterizedTestCaseInfoBase**, std::__cxx1998::vector<testing::internal::ParameterizedTestCaseInfoBase*, std::allocator<testing::internal::ParameterizedTestCaseInfoBase*> > >, std::__debug::vector<testing::internal::ParameterizedTestCaseInfoBase*, std::allocator<testing::internal::ParameterizedTestCaseInfoBase*> > >::_Safe_iterator() at safe_iterator.h:154 0x100002e9c    
            std::__debug::vector<testing::internal::ParameterizedTestCaseInfoBase*, std::allocator<testing::internal::ParameterizedTestCaseInfoBase*> >::begin() at vector:207 0x100001fbe  
            testing::internal::ParameterizedTestCaseRegistry::GetTestCasePatternHolder<EnsembleClustering::ParametrizedGTest>() at gtest-param-util.h:574 0x1000025b0   
            EnsembleClustering::ParametrizedGTest_testParameter_Test::AddToRegistry() at ParametrizedGTest.cpp:22 0x100001d3f   
            __static_initialization_and_destruction_0() at ParametrizedGTest.cpp:22 0x100001349 
            _GLOBAL__sub_I_ParametrizedGTest.cpp() at ParametrizedGTest.cpp:32 0x100001424  
            <...more frames...> 
    gdb 

Am I doing something wrong or is this a bug in googletest? Can you reproduce this error?

EDIT: I am on Mac OS X 10.8.


回答1:


From looking at the source code of gtest the only case if there are no parametrized tests available is on Windows using VC7.1 with disabled exceptions:

// We don't support MSVC 7.1 with exceptions disabled now.  Therefore
// all the compilers we care about are adequate for supporting
// value-parameterized tests.
#define GTEST_HAS_PARAM_TEST 1

So, you'll need to check how your MinGW was built and probably update it? And can you run the gtest unit tests to see if they execute the typed parameters test?

More information on MinGW:

On their FAQ they report that when using MinGW the following compile option for building gtest is required: PATH/TO/configure CC="gcc -mno-cygwin" CXX="g++ -mno-cygwin".

Complete Example:

#include <gtest/gtest.h>
namespace EnsembleClustering {

    class ParametrizedGTest: public testing::TestWithParam<int> {
    public:
        ParametrizedGTest();
        virtual ~ParametrizedGTest();
    };

    ParametrizedGTest::ParametrizedGTest() {
    }

    ParametrizedGTest::~ParametrizedGTest() {
    }

    TEST_P(ParametrizedGTest, testParameter) {
        int n = GetParam();
        EXPECT_EQ(n, GetParam());
    }

    INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance,
                            ParametrizedGTest,
                            ::testing::Values(100));

} /* namespace EnsembleClustering */


int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

I compiled this code using the following compiler call on Mac OS X 10.8:

g++ -IGTEST_INCLUDE_DIR -LGTEST_LIB_DIR -lgtest -o tt2 tt2.cpp

Where GTEST_INCLUDE_DIR and GTEST_LIB_DIR are the path where header and library files are stored. When you compile and execute, what happens?




回答2:


Thanks @ChristianStaudt and @grundprinzip

I would like to point future readers to following link that explains this problem. http://libcwd.sourceforge.net/reference-manual/group__enable__glibcxx__debug.html

This is a link to the documentation for GLIBCXX_DEBUG flag. It states the following important points.

  • "Note that this flag changes the sizes and behavior of standard class templates such as std::vector, and therefore you can only link code compiled with debug mode and code compiled without debug mode if no instantiation of a container is passed between the two translation units."

  • "When to use it

    It is a good idea to use this if you suspect problems related to iterators."

Now, if you look at the stack trace posted originally, the crash happens due to vector<testing::internal::ParameterizedTestCaseInfoBase*> as gtest tries to get an iterator on this container, using begin() method.

In my case, gtest lib was compiled without GLICXX_DEBUG flag, but my test code was compiled with this flag. The test code worked like a charm when I compiled without this flag.



来源:https://stackoverflow.com/questions/14363874/googletest-parametrized-tests-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!