Alex Rivera | Logout

Google Test and std::vector range exceptions

Asked 2013-01-11T15:50:21.373
10

I test my C++ code with googletest. When a vector::_M_range_check exception is thrown because a std::vector is accessed with a wrong index, googletest reports:

C++ exception with description "vector::_M_range_check" thrown in the test body.

Great, now I'd also like to know which vector, which index and which range. How can I easily get this information, keeping the test code in googletest unit test cases?

(I almost begin long for Java with its good old IndexOutOfBoundsException...)

Edit
Report

1 Answer

7

Google Test isn't involved here. Your C++ Standard Library implementation is throwing an exception, and it's up to your C++ Standard Library implementation to decide how verbose to make its exceptions.

Since you're getting an exception, I assume that you're using std::vector::at instead of std::vector::operator[]. There are a couple of possible approaches you could take to getting more information.

First, you could replace calls to at with calls to operator[] (personally, I don't find at's exception-throwing range checking to be very useful, and it does have a performance overhead) and use your C++ Standard Library implementation's iterator debugging. For example, with g++, if I use operator[] and compile with -D_GLIBCXX_DEBUG to turn on range checking for operator[], I get an error similar to the following:

/usr/include/c++/4.3/debug/vector:237:error: attempt to subscript container
    with out-of-bounds index 0, but container only holds 0 elements.

Second, you could replace calls to at with calls to test_at or similar: (untested)

template <typename T>
T& test_at(std::vector<T>& v, size_t n) {
    // Use Google Test to display details on out of bounds.
    // We can stream additional information here if we like.
    EXPECT_LT(n, v.size()) << "for vector at address " << &v;

    // Fall back to at, and let it throw its exception, so that our
    // test will terminate as expected.
    return v.at(n);
}
answered 2013-01-11T16:08:07.367

Your Answer