Search code examples
c++unit-testingtddcatch-unit-test

Good way to call overloaded const vs non-const method in C++ TDD?


All

wrote some fancy container with iterators. So I have

iterator begin() {
}

iterator end() {
}

const_iterator begin() const {
}

const_iterator end() const {
}

In test I instantiate container, fill it and testing it. What is an accepted/good way to call both const and non-const version? Say, simple test like

TEST( c.end() - c.begin() == c.size() );

should run for iterators as well as for const iterators. Const reference? Some ugly cast?


Solution

  • I would just make a const& to it:

    const container& constC= c;
    TEST(constC.end() - constC.begin() == constC.size());