Skip to content

Commit 7fa6865

Browse files
committed
Fix a couple of unguarded operator, calls in algorithm. Fixes PR#43063. Updated all the heap tests to check this.
llvm-svn: 369448
1 parent d503572 commit 7fa6865

13 files changed

+106
-13
lines changed

libcxx/include/algorithm

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,10 +3117,10 @@ _SampleIterator __sample(_PopulationIterator __first,
31173117
input_iterator_tag) {
31183118

31193119
_Distance __k = 0;
3120-
for (; __first != __last && __k < __n; ++__first, (void)++__k)
3120+
for (; __first != __last && __k < __n; ++__first, (void) ++__k)
31213121
__output_iter[__k] = *__first;
31223122
_Distance __sz = __k;
3123-
for (; __first != __last; ++__first, (void)++__k) {
3123+
for (; __first != __last; ++__first, (void) ++__k) {
31243124
_Distance __r = _VSTD::uniform_int_distribution<_Distance>(0, __k)(__g);
31253125
if (__r < __sz)
31263126
__output_iter[__r] = *__first;
@@ -3190,7 +3190,7 @@ template<class _RandomAccessIterator, class _UniformRandomNumberGenerator>
31903190
if (__d > 1)
31913191
{
31923192
_Dp __uid;
3193-
for (--__last, --__d; __first < __last; ++__first, --__d)
3193+
for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
31943194
{
31953195
difference_type __i = __uid(__g, _Pp(0, __d));
31963196
if (__i != difference_type(0))
@@ -3373,7 +3373,7 @@ __stable_partition(_ForwardIterator __first, _ForwardIterator __last, _Predicate
33733373
// All trues now at start of range, all falses in buffer
33743374
// Move falses back into range, but don't mess up __first which points to first false
33753375
__i = __first;
3376-
for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3376+
for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
33773377
*__i = _VSTD::move(*__t2);
33783378
// __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
33793379
return __first;
@@ -3505,7 +3505,7 @@ __stable_partition(_BidirectionalIterator __first, _BidirectionalIterator __last
35053505
__i = ++__first;
35063506
// All trues now at start of range, all falses in buffer
35073507
// Move falses back into range, but don't mess up __first which points to first false
3508-
for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, ++__i)
3508+
for (value_type* __t2 = __p.first; __t2 < __t; ++__t2, (void) ++__i)
35093509
*__i = _VSTD::move(*__t2);
35103510
// __h destructs moved-from values out of the temp buffer, but doesn't deallocate buffer
35113511
return __first;
@@ -4428,14 +4428,14 @@ __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator
44284428
if (__len1 <= __len2)
44294429
{
44304430
value_type* __p = __buff;
4431-
for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4431+
for (_BidirectionalIterator __i = __first; __i != __middle; __d.__incr((value_type*)0), (void) ++__i, (void) ++__p)
44324432
::new(__p) value_type(_VSTD::move(*__i));
44334433
__half_inplace_merge(__buff, __p, __middle, __last, __first, __comp);
44344434
}
44354435
else
44364436
{
44374437
value_type* __p = __buff;
4438-
for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, ++__p)
4438+
for (_BidirectionalIterator __i = __middle; __i != __last; __d.__incr((value_type*)0), (void) ++__i, (void) ++__p)
44394439
::new(__p) value_type(_VSTD::move(*__i));
44404440
typedef reverse_iterator<_BidirectionalIterator> _RBi;
44414441
typedef reverse_iterator<value_type*> _Rv;
@@ -4575,14 +4575,14 @@ __merge_move_construct(_InputIterator1 __first1, _InputIterator1 __last1,
45754575
{
45764576
if (__first1 == __last1)
45774577
{
4578-
for (; __first2 != __last2; ++__first2, ++__result, __d.__incr((value_type*)0))
4578+
for (; __first2 != __last2; ++__first2, ++__result, (void) __d.__incr((value_type*)0))
45794579
::new (__result) value_type(_VSTD::move(*__first2));
45804580
__h.release();
45814581
return;
45824582
}
45834583
if (__first2 == __last2)
45844584
{
4585-
for (; __first1 != __last1; ++__first1, ++__result, __d.__incr((value_type*)0))
4585+
for (; __first1 != __last1; ++__first1, ++__result, (void) __d.__incr((value_type*)0))
45864586
::new (__result) value_type(_VSTD::move(*__first1));
45874587
__h.release();
45884588
return;
@@ -4612,7 +4612,7 @@ __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
46124612
{
46134613
if (__first2 == __last2)
46144614
{
4615-
for (; __first1 != __last1; ++__first1, ++__result)
4615+
for (; __first1 != __last1; ++__first1, (void) ++__result)
46164616
*__result = _VSTD::move(*__first1);
46174617
return;
46184618
}
@@ -4627,7 +4627,7 @@ __merge_move_assign(_InputIterator1 __first1, _InputIterator1 __last1,
46274627
++__first1;
46284628
}
46294629
}
4630-
for (; __first2 != __last2; ++__first2, ++__result)
4630+
for (; __first2 != __last2; ++__first2, (void) ++__result)
46314631
*__result = _VSTD::move(*__first2);
46324632
}
46334633

@@ -4995,7 +4995,7 @@ void
49954995
__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
49964996
{
49974997
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
4998-
for (difference_type __n = __last - __first; __n > 1; --__last, --__n)
4998+
for (difference_type __n = __last - __first; __n > 1; --__last, (void) --__n)
49994999
__pop_heap<_Compare>(__first, __last, __comp, __n);
50005000
}
50015001

@@ -5065,7 +5065,7 @@ __partial_sort_copy(_InputIterator __first, _InputIterator __last,
50655065
_RandomAccessIterator __r = __result_first;
50665066
if (__r != __result_last)
50675067
{
5068-
for (; __first != __last && __r != __result_last; (void) ++__first, ++__r)
5068+
for (; __first != __last && __r != __result_last; ++__first, (void) ++__r)
50695069
*__r = *__first;
50705070
__make_heap<_Compare>(__result_first, __r, __comp);
50715071
typename iterator_traits<_RandomAccessIterator>::difference_type __len = __r - __result_first;

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/is.heap/is_heap.pass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cassert>
1818

1919
#include "test_macros.h"
20+
#include "test_iterators.h"
2021

2122
#if TEST_STD_VER > 17
2223
TEST_CONSTEXPR bool test_constexpr() {
@@ -29,9 +30,13 @@ TEST_CONSTEXPR bool test_constexpr() {
2930

3031
void test()
3132
{
33+
typedef random_access_iterator<int *> RI;
3234
int i1[] = {0, 0};
3335
assert(std::is_heap(i1, i1));
3436
assert(std::is_heap(i1, i1+1) == (std::is_heap_until(i1, i1+1) == i1+1));
37+
assert(std::is_heap(RI(i1), RI(i1)));
38+
assert(std::is_heap(RI(i1), RI(i1+1)) == (std::is_heap_until(RI(i1), RI(i1+1)) == RI(i1+1)));
39+
3540
int i2[] = {0, 1};
3641
int i3[] = {1, 0};
3742
assert(std::is_heap(i1, i1+2) == (std::is_heap_until(i1, i1+2) == i1+2));

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/is.heap/is_heap_comp.pass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cassert>
1919

2020
#include "test_macros.h"
21+
#include "test_iterators.h"
2122

2223
#if TEST_STD_VER > 17
2324
TEST_CONSTEXPR bool test_constexpr() {
@@ -30,9 +31,13 @@ TEST_CONSTEXPR bool test_constexpr() {
3031

3132
void test()
3233
{
34+
typedef random_access_iterator<int *> RI;
3335
int i1[] = {0, 0};
3436
assert(std::is_heap(i1, i1, std::greater<int>()));
3537
assert(std::is_heap(i1, i1+1, std::greater<int>()) == (std::is_heap_until(i1, i1+1, std::greater<int>()) == i1+1));
38+
assert(std::is_heap(RI(i1), RI(i1), std::greater<int>()));
39+
assert(std::is_heap(RI(i1), RI(i1+1), std::greater<int>()) == (std::is_heap_until(RI(i1), RI(i1+1), std::greater<int>()) == RI(i1+1)));
40+
3641
int i2[] = {0, 1};
3742
int i3[] = {1, 0};
3843
assert(std::is_heap(i1, i1+2, std::greater<int>()) == (std::is_heap_until(i1, i1+2, std::greater<int>()) == i1+2));

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/is.heap/is_heap_until.pass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cassert>
1818

1919
#include "test_macros.h"
20+
#include "test_iterators.h"
2021

2122
#if TEST_STD_VER > 17
2223
TEST_CONSTEXPR bool test_constexpr() {
@@ -29,9 +30,13 @@ TEST_CONSTEXPR bool test_constexpr() {
2930

3031
void test()
3132
{
33+
typedef random_access_iterator<int *> RI;
3234
int i1[] = {0, 0};
3335
assert(std::is_heap_until(i1, i1) == i1);
3436
assert(std::is_heap_until(i1, i1+1) == i1+1);
37+
assert(std::is_heap_until(RI(i1), RI(i1)) == RI(i1));
38+
assert(std::is_heap_until(RI(i1), RI(i1+1)) == RI(i1+1));
39+
3540
int i2[] = {0, 1};
3641
int i3[] = {1, 0};
3742
assert(std::is_heap_until(i1, i1+2) == i1+2);

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/is.heap/is_heap_until_comp.pass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cassert>
1919

2020
#include "test_macros.h"
21+
#include "test_iterators.h"
2122

2223
#if TEST_STD_VER > 17
2324
TEST_CONSTEXPR bool test_constexpr() {
@@ -30,9 +31,13 @@ TEST_CONSTEXPR bool test_constexpr() {
3031

3132
void test()
3233
{
34+
typedef random_access_iterator<int *> RI;
3335
int i1[] = {0, 0};
3436
assert(std::is_heap_until(i1, i1, std::greater<int>()) == i1);
3537
assert(std::is_heap_until(i1, i1+1, std::greater<int>()) == i1+1);
38+
assert(std::is_heap_until(RI(i1), RI(i1), std::greater<int>()) == RI(i1));
39+
assert(std::is_heap_until(RI(i1), RI(i1+1), std::greater<int>()) == RI(i1+1));
40+
3641
int i2[] = {0, 1};
3742
int i3[] = {1, 0};
3843
assert(std::is_heap_until(i1, i1+2, std::greater<int>()) == i1+2);

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/make.heap/make_heap.pass.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cassert>
1919

2020
#include "test_macros.h"
21+
#include "test_iterators.h"
2122

2223
std::mt19937 randomness;
2324

@@ -29,6 +30,12 @@ void test(int N)
2930
std::shuffle(ia, ia+N, randomness);
3031
std::make_heap(ia, ia+N);
3132
assert(std::is_heap(ia, ia+N));
33+
34+
typedef random_access_iterator<int *> RI;
35+
std::shuffle(RI(ia), RI(ia+N), randomness);
36+
std::make_heap(RI(ia), RI(ia+N));
37+
assert(std::is_heap(RI(ia), RI(ia+N)));
38+
3239
delete [] ia;
3340
}
3441

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/make.heap/make_heap_comp.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "test_macros.h"
2323
#include "counting_predicates.hpp"
24+
#include "test_iterators.h"
2425

2526
struct indirect_less
2627
{
@@ -40,6 +41,11 @@ void test(int N)
4041
std::shuffle(ia, ia+N, randomness);
4142
std::make_heap(ia, ia+N, std::greater<int>());
4243
assert(std::is_heap(ia, ia+N, std::greater<int>()));
44+
45+
std::shuffle(ia, ia+N, randomness);
46+
std::make_heap(random_access_iterator<int *>(ia),
47+
random_access_iterator<int *>(ia+N), std::greater<int>());
48+
assert(std::is_heap(ia, ia+N, std::greater<int>()));
4349
}
4450

4551
// Ascending

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/pop.heap/pop_heap.pass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cassert>
1919

2020
#include "test_macros.h"
21+
#include "test_iterators.h"
2122

2223
std::mt19937 randomness;
2324

@@ -34,6 +35,18 @@ void test(int N)
3435
assert(std::is_heap(ia, ia+i-1));
3536
}
3637
std::pop_heap(ia, ia);
38+
39+
40+
typedef random_access_iterator<int *> RI;
41+
std::shuffle(RI(ia), RI(ia+N), randomness);
42+
std::make_heap(RI(ia), RI(ia+N));
43+
for (int i = N; i > 0; --i)
44+
{
45+
std::pop_heap(RI(ia), RI(ia+i));
46+
assert(std::is_heap(RI(ia), RI(ia+i-1)));
47+
}
48+
std::pop_heap(RI(ia), RI(ia));
49+
3750
delete [] ia;
3851
}
3952

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/pop.heap/pop_heap_comp.pass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <memory>
2121

2222
#include "test_macros.h"
23+
#include "test_iterators.h"
2324

2425
struct indirect_less
2526
{
@@ -44,6 +45,17 @@ void test(int N)
4445
assert(std::is_heap(ia, ia+i-1, std::greater<int>()));
4546
}
4647
std::pop_heap(ia, ia, std::greater<int>());
48+
49+
typedef random_access_iterator<int *> RI;
50+
std::shuffle(RI(ia), RI(ia+N), randomness);
51+
std::make_heap(RI(ia), RI(ia+N), std::greater<int>());
52+
for (int i = N; i > 0; --i)
53+
{
54+
std::pop_heap(RI(ia), RI(ia+i), std::greater<int>());
55+
assert(std::is_heap(RI(ia), RI(ia+i-1), std::greater<int>()));
56+
}
57+
std::pop_heap(RI(ia), RI(ia), std::greater<int>());
58+
4759
delete [] ia;
4860
}
4961

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/push.heap/push_heap.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <cassert>
2020

2121
#include "test_macros.h"
22+
#include "test_iterators.h"
2223

2324
std::mt19937 randomness;
2425

@@ -33,6 +34,15 @@ void test(int N)
3334
std::push_heap(ia, ia+i);
3435
assert(std::is_heap(ia, ia+i));
3536
}
37+
38+
typedef random_access_iterator<int *> RI;
39+
std::shuffle(RI(ia), RI(ia+N), randomness);
40+
for (int i = 0; i <= N; ++i)
41+
{
42+
std::push_heap(RI(ia), RI(ia+i));
43+
assert(std::is_heap(RI(ia), RI(ia+i)));
44+
}
45+
3646
delete [] ia;
3747
}
3848

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/push.heap/push_heap_comp.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <memory>
2222

2323
#include "test_macros.h"
24+
#include "test_iterators.h"
2425

2526
struct indirect_less
2627
{
@@ -42,6 +43,15 @@ void test(int N)
4243
std::push_heap(ia, ia+i, std::greater<int>());
4344
assert(std::is_heap(ia, ia+i, std::greater<int>()));
4445
}
46+
47+
typedef random_access_iterator<int *> RI;
48+
std::shuffle(RI(ia), RI(ia+N), randomness);
49+
for (int i = 0; i <= N; ++i)
50+
{
51+
std::push_heap(RI(ia), RI(ia+i), std::greater<int>());
52+
assert(std::is_heap(RI(ia), RI(ia+i), std::greater<int>()));
53+
}
54+
4555
delete [] ia;
4656
}
4757

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/sort_heap.pass.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cassert>
1919

2020
#include "test_macros.h"
21+
#include "test_iterators.h"
2122

2223
std::mt19937 randomness;
2324

@@ -30,6 +31,13 @@ void test(int N)
3031
std::make_heap(ia, ia+N);
3132
std::sort_heap(ia, ia+N);
3233
assert(std::is_sorted(ia, ia+N));
34+
35+
typedef random_access_iterator<int *> RI;
36+
std::shuffle(RI(ia), RI(ia+N), randomness);
37+
std::make_heap(RI(ia), RI(ia+N));
38+
std::sort_heap(RI(ia), RI(ia+N));
39+
assert(std::is_sorted(RI(ia), RI(ia+N)));
40+
3341
delete [] ia;
3442
}
3543

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/sort_heap_comp.pass.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <memory>
2121

2222
#include "test_macros.h"
23+
#include "test_iterators.h"
2324

2425
struct indirect_less
2526
{
@@ -39,6 +40,12 @@ void test(int N)
3940
std::make_heap(ia, ia+N, std::greater<int>());
4041
std::sort_heap(ia, ia+N, std::greater<int>());
4142
assert(std::is_sorted(ia, ia+N, std::greater<int>()));
43+
44+
typedef random_access_iterator<int *> RI;
45+
std::shuffle(RI(ia), RI(ia+N), randomness);
46+
std::make_heap(RI(ia), RI(ia+N), std::greater<int>());
47+
std::sort_heap(RI(ia), RI(ia+N), std::greater<int>());
48+
assert(std::is_sorted(RI(ia), RI(ia+N), std::greater<int>()));
4249
delete [] ia;
4350
}
4451

0 commit comments

Comments
 (0)