libstdc++: Fix return value of std::ranges::advance [PR 100833]

The three-argument form of ranges::advance is supposed to return the
difference between the second argument and the distance the iterator was
advanced. When a non-random-access iterator is not advanced (because it
already equals the sentinel) we were returning 0 rather than n - 0.

libstdc++-v3/ChangeLog:

	PR libstdc++/100833
	* include/bits/ranges_base.h (ranges::advance(iter, n, sentinel)):
	Fix return value for no-op case.
	* testsuite/24_iterators/range_operations/advance.cc: Test
	return values of three-argument overload.
This commit is contained in:
Jonathan Wakely 2021-06-01 16:02:45 +01:00
parent cf2b7020ee
commit d832629169
2 changed files with 33 additions and 17 deletions

View File

@ -749,7 +749,7 @@ namespace ranges
}
}
else if (__it == __bound || __n == 0)
return iter_difference_t<_It>(0);
return __n;
else if (__n > 0)
{
iter_difference_t<_It> __m = 0;

View File

@ -49,10 +49,12 @@ test01()
std::ranges::advance(iter, r.begin());
VERIFY( iter == r.begin() );
std::ranges::advance(iter, 99, r.end());
auto diff = std::ranges::advance(iter, 99, r.end());
VERIFY( iter == r.end() );
std::ranges::advance(iter, -222, r.begin());
VERIFY( diff == 97 );
diff = std::ranges::advance(iter, -222, r.begin());
VERIFY( iter == r.begin() );
VERIFY( diff == -220 );
}
void
@ -77,10 +79,12 @@ test02()
std::ranges::advance(iter, r.begin());
VERIFY( iter == r.begin() );
std::ranges::advance(iter, 99, r.end());
auto diff = std::ranges::advance(iter, 99, r.end());
VERIFY( iter == r.end() );
std::ranges::advance(iter, -222, r.begin());
VERIFY( diff == 97 );
diff = std::ranges::advance(iter, -222, r.begin());
VERIFY( iter == r.begin() );
VERIFY( diff == -220 );
}
void
@ -108,15 +112,19 @@ test03()
std::ranges::advance(iter, r.end());
VERIFY( iter == r.end() );
std::ranges::advance(iter, 99, r.end());
auto diff = std::ranges::advance(iter, 99, r.end());
VERIFY( iter == r.end() );
std::ranges::advance(iter, 99, r.end());
VERIFY( diff == 99 ); // PR libstdc++/100833
diff = std::ranges::advance(iter, 99, r.end());
VERIFY( iter == r.end() );
VERIFY( diff == 99 );
iter = r.begin();
std::ranges::advance(iter, 99, r.end());
diff = std::ranges::advance(iter, 99, r.end());
VERIFY( iter == r.end() );
std::ranges::advance(iter, 99, r.end());
VERIFY( diff == 97 );
diff = std::ranges::advance(iter, 99, r.end());
VERIFY( iter == r.end() );
VERIFY( diff == 99 );
}
void
@ -145,17 +153,21 @@ test04()
std::ranges::advance(iter, r2.end());
VERIFY( iter == r2.end() );
std::ranges::advance(iter, 99, r2.end());
auto diff = std::ranges::advance(iter, 99, r2.end());
VERIFY( iter == r2.end() );
std::ranges::advance(iter, 99, r2.end());
VERIFY( diff == 99 );
diff = std::ranges::advance(iter, 99, r2.end());
VERIFY( iter == r2.end() );
VERIFY( diff == 99 );
test_range<int, input_iterator_wrapper> r3(a);
iter = r3.begin();
std::ranges::advance(iter, 99, r3.end());
diff = std::ranges::advance(iter, 99, r3.end());
VERIFY( iter == r3.end() );
std::ranges::advance(iter, 99, r3.end());
VERIFY( diff == 97 );
diff = std::ranges::advance(iter, 99, r3.end());
VERIFY( iter == r3.end() );
VERIFY( diff == 99 );
}
void
@ -180,17 +192,21 @@ test05()
std::ranges::advance(iter, r2.end());
VERIFY( iter == r2.end() );
std::ranges::advance(iter, 99, r2.end());
auto diff = std::ranges::advance(iter, 99, r2.end());
VERIFY( iter == r2.end() );
std::ranges::advance(iter, 99, r2.end());
VERIFY( diff == 99 );
diff = std::ranges::advance(iter, 99, r2.end());
VERIFY( iter == r2.end() );
VERIFY( diff == 99 );
test_range<int, output_iterator_wrapper> r3(a);
iter = r3.begin();
std::ranges::advance(iter, 99, r3.end());
diff = std::ranges::advance(iter, 99, r3.end());
VERIFY( iter == r3.end() );
std::ranges::advance(iter, 99, r3.end());
VERIFY( diff == 97 );
diff = std::ranges::advance(iter, 99, r3.end());
VERIFY( iter == r3.end() );
VERIFY( diff == 99 );
}
int