This is an archive of the discontinued LLVM Phabricator instance.

[libc++] [test] Improve test coverage for std::{c,}{begin,end}.
ClosedPublic

Authored by Quuxplusone on Feb 13 2022, 12:51 PM.

Details

Summary

Incidentally, this removes some unqualified ADL calls to begin and end. (See also D119687.)

Diff Detail

Event Timeline

Quuxplusone requested review of this revision.Feb 13 2022, 12:51 PM
Quuxplusone created this revision.
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald TranscriptFeb 13 2022, 12:51 PM
cor3ntin resigned from this revision.Feb 13 2022, 2:30 PM
Mordante accepted this revision as: Mordante.Feb 14 2022, 10:31 AM

Nice cleanup! LGTM modulo some minor nits.

libcxx/test/std/iterators/iterator.range/begin-end.pass.cpp
68

I would prefer to keep these two lines of comment. Else using il.begin() instead of il.cbegin() in`assert(std::cbegin(cil) == il.begin());` looks wrong.

Same for the backward initializer list tests.

I agree with removing the commented out asserts.

124

Why is this test TEST_CONSTEXPR_CXX17 and the previous TEST_CONSTEXPR_CXX14?

160

I like to see std::cfoo() == c.cfoo() in the tests. Testing against c.foo() looks wrong.

Quuxplusone marked an inline comment as done.Feb 14 2022, 12:00 PM
Quuxplusone added inline comments.
libcxx/test/std/iterators/iterator.range/begin-end.pass.cpp
124

std::rbegin was added in C++14, but reverse_iterator wasn't made constexpr-friendly until C++17. So rbegin went from nonexistent ('11) to runtime-only ('14) to fully constexpr ('17).

160

Well, this is the flip side of your other comment about il.begin(). For every STL type that supports cbegin, it's an invariant that c.begin() == c.cbegin() and c.end() == c.cend(). But there are some STL types (e.g. initializer_list) that syntactically support only c.begin() and not c.cbegin(). So, if we were to try to use x.cbegin() everywhere in this test, we'd find there were some places that it wasn't supported and we'd have to fall back to x.begin(). Which, as you say, looks a bit wrong.
OTOH, if we just use x.begin() / x.end() consistently throughout (and a / a + 3 for C arrays), then at least to me it doesn't look wrong at all. (Note my general rule that cbegin and cend are stupid, because we've had a perfectly fine begin() const and end() const since C++98. So I'm very used to just saying .begin() when I mean an iterator to the first element, and letting the compiler figure out whether it needs to const-qualify or not. I know some people do use cbegin, though.)

The other thing to notice here — not directly relevant but might affect your mental model — is that std::cbegin(c) does not in any way invoke c.cbegin() on the container. std::cbegin(c) actually invokes std::begin(std::as_const(c)), which invokes c.begin() const. So (if everything is piped together correctly) this code is basically testing that c.begin() const == c.begin() non-const.

Mordante added inline comments.Feb 14 2022, 12:31 PM
libcxx/test/std/iterators/iterator.range/begin-end.pass.cpp
124

I see. Something nice for another pub-quiz.

160

The other thing to notice here — not directly relevant but might affect your mental model — is that std::cbegin(c) does not in any way invoke c.cbegin() on the container. std::cbegin(c) actually invokes std::begin(std::as_const(c)), which invokes c.begin() const. So (if everything is piped together correctly) this code is basically testing that c.begin() const == c.begin() non-const.

I see. I assumed std::cbegin(c)' invoked c.begin()… I was aware of the gotcha in initializer_list`. In that case I agree that the current code is better.

I suggest to add some comment regarding this design choice. This avoids future "fixes" to change c.foo() to c.cfoo().

This revision was not accepted when it landed; it landed in state Needs Review.Feb 15 2022, 8:32 AM
This revision was automatically updated to reflect the committed changes.
EricWF added a subscriber: EricWF.Mar 1 2022, 11:53 AM

What happened to all the tests for operator!=?

Quuxplusone marked an inline comment as done.Mar 1 2022, 12:13 PM

What happened to all the tests for operator!=?

Not sure I understand. D119677 affects the tests for std::{c,}{r,}{begin,end}, nothing to do with any particular iterator's operator!=.

Herald added a project: Restricted Project. · View Herald TranscriptMar 1 2022, 12:13 PM
libcxx/test/std/iterators/iterator.range/begin_non_const.pass.cpp