Fix PR#202520 - predicate called too many times in list::remove_if. Add tests for list, forward_list, and the std::remove_if algorithm

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@214736 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2014-08-04 17:32:25 +00:00
parent a1d32fcd0e
commit f0f1bca861
5 changed files with 79 additions and 19 deletions

View File

@@ -16,6 +16,8 @@
#include <cassert>
#include "min_allocator.h"
#include "counting_predicates.hpp"
bool g(int i)
{
@@ -26,98 +28,128 @@ int main()
{
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T> C;
const T t1[] = {0, 5, 5, 0, 0, 0, 5};
const T t2[] = {5, 5, 5};
C c1(std::begin(t1), std::end(t1));
C c2(std::begin(t2), std::end(t2));
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T> C;
const T t1[] = {0, 0, 0, 0};
C c1(std::begin(t1), std::end(t1));
C c2;
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T> C;
const T t1[] = {5, 5, 5};
const T t2[] = {5, 5, 5};
C c1(std::begin(t1), std::end(t1));
C c2(std::begin(t2), std::end(t2));
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T> C;
C c1;
C c2;
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == 0);
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T> C;
const T t1[] = {5, 5, 5, 0};
const T t2[] = {5, 5, 5};
C c1(std::begin(t1), std::end(t1));
C c2(std::begin(t2), std::end(t2));
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
#if __cplusplus >= 201103L
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T, min_allocator<T>> C;
const T t1[] = {0, 5, 5, 0, 0, 0, 5};
const T t2[] = {5, 5, 5};
C c1(std::begin(t1), std::end(t1));
C c2(std::begin(t2), std::end(t2));
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T, min_allocator<T>> C;
const T t1[] = {0, 0, 0, 0};
C c1(std::begin(t1), std::end(t1));
C c2;
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T, min_allocator<T>> C;
const T t1[] = {5, 5, 5};
const T t2[] = {5, 5, 5};
C c1(std::begin(t1), std::end(t1));
C c2(std::begin(t2), std::end(t2));
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T, min_allocator<T>> C;
C c1;
C c2;
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == 0);
}
{
typedef int T;
typedef unary_counting_predicate<bool(*)(T), T> Predicate;
typedef std::forward_list<T, min_allocator<T>> C;
const T t1[] = {5, 5, 5, 0};
const T t2[] = {5, 5, 5};
C c1(std::begin(t1), std::end(t1));
C c2(std::begin(t2), std::end(t2));
c1.remove_if(g);
Predicate cp(g);
c1.remove_if(std::ref(cp));
assert(c1 == c2);
assert(cp.count() == std::distance(std::begin(t1), std::end(t1)));
}
#endif
}

View File

@@ -16,28 +16,49 @@
#include <functional>
#include "min_allocator.h"
#include "counting_predicates.hpp"
bool even(int i)
{
return i % 2 == 0;
}
bool g(int i)
{
return i < 3;
}
typedef unary_counting_predicate<bool(*)(int), int> Predicate;
int main()
{
{
int a1[] = {1, 2, 3, 4};
int a2[] = {3, 4};
std::list<int> c(a1, a1+4);
c.remove_if(g);
Predicate cp(g);
c.remove_if(std::ref(cp));
assert(c == std::list<int>(a2, a2+2));
assert(cp.count() == 4);
}
{
int a1[] = {1, 2, 3, 4};
int a2[] = {1, 3};
std::list<int> c(a1, a1+4);
Predicate cp(even);
c.remove_if(std::ref(cp));
assert(c == std::list<int>(a2, a2+2));
assert(cp.count() == 4);
}
#if __cplusplus >= 201103L
{
int a1[] = {1, 2, 3, 4};
int a2[] = {3, 4};
std::list<int, min_allocator<int>> c(a1, a1+4);
c.remove_if(g);
Predicate cp(g);
c.remove_if(std::ref(cp));
assert((c == std::list<int, min_allocator<int>>(a2, a2+2)));
assert(cp.count() == 4);
}
#endif
}