diff options
Diffstat (limited to 'test/std/algorithms/alg.modifying.operations/alg.partitions')
5 files changed, 621 insertions, 0 deletions
diff --git a/test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp b/test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp new file mode 100644 index 0000000000000..aa23d19a8a3b3 --- /dev/null +++ b/test/std/algorithms/alg.modifying.operations/alg.partitions/is_partitioned.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> + +// template <class InputIterator, class Predicate> +// bool +// is_partitioned(InputIterator first, InputIterator last, Predicate pred); + +#include <algorithm> +#include <cassert> + +#include "test_iterators.h" +#include "counting_predicates.hpp" + +struct is_odd +{ + bool operator()(const int& i) const {return i & 1;} +}; + +int main() +{ + { + const int ia[] = {1, 2, 3, 4, 5, 6}; + unary_counting_predicate<is_odd, int> pred((is_odd())); + assert(!std::is_partitioned(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::end(ia)), + std::ref(pred))); + assert(pred.count() <= std::distance(std::begin(ia), std::end(ia))); + } + { + const int ia[] = {1, 3, 5, 2, 4, 6}; + unary_counting_predicate<is_odd, int> pred((is_odd())); + assert( std::is_partitioned(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::end(ia)), + std::ref(pred))); + assert(pred.count() <= std::distance(std::begin(ia), std::end(ia))); + } + { + const int ia[] = {2, 4, 6, 1, 3, 5}; + unary_counting_predicate<is_odd, int> pred((is_odd())); + assert(!std::is_partitioned(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::end(ia)), + std::ref(pred))); + assert(pred.count() <= std::distance(std::begin(ia), std::end(ia))); + } + { + const int ia[] = {1, 3, 5, 2, 4, 6, 7}; + unary_counting_predicate<is_odd, int> pred((is_odd())); + assert(!std::is_partitioned(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::end(ia)), + std::ref(pred))); + assert(pred.count() <= std::distance(std::begin(ia), std::end(ia))); + } + { + const int ia[] = {1, 3, 5, 2, 4, 6, 7}; + unary_counting_predicate<is_odd, int> pred((is_odd())); + assert( std::is_partitioned(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::begin(ia)), + std::ref(pred))); + assert(pred.count() <= std::distance(std::begin(ia), std::begin(ia))); + } + { + const int ia[] = {1, 3, 5, 7, 9, 11, 2}; + unary_counting_predicate<is_odd, int> pred((is_odd())); + assert( std::is_partitioned(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::end(ia)), + std::ref(pred))); + assert(pred.count() <= std::distance(std::begin(ia), std::end(ia))); + } +} diff --git a/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp b/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp new file mode 100644 index 0000000000000..8eddfbc4d0481 --- /dev/null +++ b/test/std/algorithms/alg.modifying.operations/alg.partitions/partition.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> + +// template<BidirectionalIterator Iter, Predicate<auto, Iter::value_type> Pred> +// requires ShuffleIterator<Iter> +// && CopyConstructible<Pred> +// Iter +// partition(Iter first, Iter last, Pred pred); + +#include <algorithm> +#include <cassert> +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#include <memory> +#endif + +#include "test_iterators.h" + +struct is_odd +{ + bool operator()(const int& i) const {return i & 1;} +}; + +template <class Iter> +void +test() +{ + // check mixed + int ia[] = {1, 2, 3, 4, 5, 6, 7, 8 ,9}; + const unsigned sa = sizeof(ia)/sizeof(ia[0]); + Iter r = std::partition(Iter(ia), Iter(ia + sa), is_odd()); + assert(base(r) == ia + 5); + for (int* i = ia; i < base(r); ++i) + assert(is_odd()(*i)); + for (int* i = base(r); i < ia+sa; ++i) + assert(!is_odd()(*i)); + // check empty + r = std::partition(Iter(ia), Iter(ia), is_odd()); + assert(base(r) == ia); + // check all false + for (unsigned i = 0; i < sa; ++i) + ia[i] = 2*i; + r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); + assert(base(r) == ia); + // check all true + for (unsigned i = 0; i < sa; ++i) + ia[i] = 2*i+1; + r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); + assert(base(r) == ia+sa); + // check all true but last + for (unsigned i = 0; i < sa; ++i) + ia[i] = 2*i+1; + ia[sa-1] = 10; + r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); + assert(base(r) == ia+sa-1); + for (int* i = ia; i < base(r); ++i) + assert(is_odd()(*i)); + for (int* i = base(r); i < ia+sa; ++i) + assert(!is_odd()(*i)); + // check all true but first + for (unsigned i = 0; i < sa; ++i) + ia[i] = 2*i+1; + ia[0] = 10; + r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); + assert(base(r) == ia+sa-1); + for (int* i = ia; i < base(r); ++i) + assert(is_odd()(*i)); + for (int* i = base(r); i < ia+sa; ++i) + assert(!is_odd()(*i)); + // check all false but last + for (unsigned i = 0; i < sa; ++i) + ia[i] = 2*i; + ia[sa-1] = 11; + r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); + assert(base(r) == ia+1); + for (int* i = ia; i < base(r); ++i) + assert(is_odd()(*i)); + for (int* i = base(r); i < ia+sa; ++i) + assert(!is_odd()(*i)); + // check all false but first + for (unsigned i = 0; i < sa; ++i) + ia[i] = 2*i; + ia[0] = 11; + r = std::partition(Iter(ia), Iter(ia+sa), is_odd()); + assert(base(r) == ia+1); + for (int* i = ia; i < base(r); ++i) + assert(is_odd()(*i)); + for (int* i = base(r); i < ia+sa; ++i) + assert(!is_odd()(*i)); +} + +int main() +{ + test<bidirectional_iterator<int*> >(); + test<random_access_iterator<int*> >(); + test<int*>(); +} diff --git a/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp b/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp new file mode 100644 index 0000000000000..67e1cccaf730c --- /dev/null +++ b/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> + +// template <class InputIterator, class OutputIterator1, +// class OutputIterator2, class Predicate> +// pair<OutputIterator1, OutputIterator2> +// partition_copy(InputIterator first, InputIterator last, +// OutputIterator1 out_true, OutputIterator2 out_false, +// Predicate pred); + +#include <algorithm> +#include <cassert> + +#include "test_iterators.h" + +struct is_odd +{ + bool operator()(const int& i) const {return i & 1;} +}; + +int main() +{ + { + const int ia[] = {1, 2, 3, 4, 6, 8, 5, 7}; + int r1[10] = {0}; + int r2[10] = {0}; + typedef std::pair<output_iterator<int*>, int*> P; + P p = std::partition_copy(input_iterator<const int*>(std::begin(ia)), + input_iterator<const int*>(std::end(ia)), + output_iterator<int*>(r1), r2, is_odd()); + assert(p.first.base() == r1 + 4); + assert(r1[0] == 1); + assert(r1[1] == 3); + assert(r1[2] == 5); + assert(r1[3] == 7); + assert(p.second == r2 + 4); + assert(r2[0] == 2); + assert(r2[1] == 4); + assert(r2[2] == 6); + assert(r2[3] == 8); + } +} diff --git a/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_point.pass.cpp b/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_point.pass.cpp new file mode 100644 index 0000000000000..f5b832c663970 --- /dev/null +++ b/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_point.pass.cpp @@ -0,0 +1,76 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> + +// template<class ForwardIterator, class Predicate> +// ForwardIterator +// partition_point(ForwardIterator first, ForwardIterator last, Predicate pred); + +#include <algorithm> +#include <cassert> + +#include "test_iterators.h" + +struct is_odd +{ + bool operator()(const int& i) const {return i & 1;} +}; + +int main() +{ + { + const int ia[] = {2, 4, 6, 8, 10}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia)); + } + { + const int ia[] = {1, 2, 4, 6, 8}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia + 1)); + } + { + const int ia[] = {1, 3, 2, 4, 6}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia + 2)); + } + { + const int ia[] = {1, 3, 5, 2, 4, 6}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia + 3)); + } + { + const int ia[] = {1, 3, 5, 7, 2, 4}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia + 4)); + } + { + const int ia[] = {1, 3, 5, 7, 9, 2}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia + 5)); + } + { + const int ia[] = {1, 3, 5, 7, 9, 11}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::end(ia)), + is_odd()) == forward_iterator<const int*>(ia + 6)); + } + { + const int ia[] = {1, 3, 5, 2, 4, 6, 7}; + assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)), + forward_iterator<const int*>(std::begin(ia)), + is_odd()) == forward_iterator<const int*>(ia)); + } +} diff --git a/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp b/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp new file mode 100644 index 0000000000000..7810dec2fe1b2 --- /dev/null +++ b/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp @@ -0,0 +1,314 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> + +// template<BidirectionalIterator Iter, Predicate<auto, Iter::value_type> Pred> +// requires ShuffleIterator<Iter> +// && CopyConstructible<Pred> +// Iter +// stable_partition(Iter first, Iter last, Pred pred); + +#include <algorithm> +#include <cassert> +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#include <memory> +#endif + +#include "test_iterators.h" + +struct is_odd +{ + bool operator()(const int& i) const {return i & 1;} +}; + +struct odd_first +{ + bool operator()(const std::pair<int,int>& p) const + {return p.first & 1;} +}; + +template <class Iter> +void +test() +{ + { // check mixed + typedef std::pair<int,int> P; + P array[] = + { + P(0, 1), + P(0, 2), + P(1, 1), + P(1, 2), + P(2, 1), + P(2, 2), + P(3, 1), + P(3, 2), + P(4, 1), + P(4, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + 4); + assert(array[0] == P(1, 1)); + assert(array[1] == P(1, 2)); + assert(array[2] == P(3, 1)); + assert(array[3] == P(3, 2)); + assert(array[4] == P(0, 1)); + assert(array[5] == P(0, 2)); + assert(array[6] == P(2, 1)); + assert(array[7] == P(2, 2)); + assert(array[8] == P(4, 1)); + assert(array[9] == P(4, 2)); + } + { + typedef std::pair<int,int> P; + P array[] = + { + P(0, 1), + P(0, 2), + P(1, 1), + P(1, 2), + P(2, 1), + P(2, 2), + P(3, 1), + P(3, 2), + P(4, 1), + P(4, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + 4); + assert(array[0] == P(1, 1)); + assert(array[1] == P(1, 2)); + assert(array[2] == P(3, 1)); + assert(array[3] == P(3, 2)); + assert(array[4] == P(0, 1)); + assert(array[5] == P(0, 2)); + assert(array[6] == P(2, 1)); + assert(array[7] == P(2, 2)); + assert(array[8] == P(4, 1)); + assert(array[9] == P(4, 2)); + // check empty + r = std::stable_partition(Iter(array), Iter(array), odd_first()); + assert(base(r) == array); + // check one true + r = std::stable_partition(Iter(array), Iter(array+1), odd_first()); + assert(base(r) == array+1); + assert(array[0] == P(1, 1)); + // check one false + r = std::stable_partition(Iter(array+4), Iter(array+5), odd_first()); + assert(base(r) == array+4); + assert(array[4] == P(0, 1)); + } + { // check all false + typedef std::pair<int,int> P; + P array[] = + { + P(0, 1), + P(0, 2), + P(2, 1), + P(2, 2), + P(4, 1), + P(4, 2), + P(6, 1), + P(6, 2), + P(8, 1), + P(8, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array); + assert(array[0] == P(0, 1)); + assert(array[1] == P(0, 2)); + assert(array[2] == P(2, 1)); + assert(array[3] == P(2, 2)); + assert(array[4] == P(4, 1)); + assert(array[5] == P(4, 2)); + assert(array[6] == P(6, 1)); + assert(array[7] == P(6, 2)); + assert(array[8] == P(8, 1)); + assert(array[9] == P(8, 2)); + } + { // check all true + typedef std::pair<int,int> P; + P array[] = + { + P(1, 1), + P(1, 2), + P(3, 1), + P(3, 2), + P(5, 1), + P(5, 2), + P(7, 1), + P(7, 2), + P(9, 1), + P(9, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + size); + assert(array[0] == P(1, 1)); + assert(array[1] == P(1, 2)); + assert(array[2] == P(3, 1)); + assert(array[3] == P(3, 2)); + assert(array[4] == P(5, 1)); + assert(array[5] == P(5, 2)); + assert(array[6] == P(7, 1)); + assert(array[7] == P(7, 2)); + assert(array[8] == P(9, 1)); + assert(array[9] == P(9, 2)); + } + { // check all false but first true + typedef std::pair<int,int> P; + P array[] = + { + P(1, 1), + P(0, 2), + P(2, 1), + P(2, 2), + P(4, 1), + P(4, 2), + P(6, 1), + P(6, 2), + P(8, 1), + P(8, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + 1); + assert(array[0] == P(1, 1)); + assert(array[1] == P(0, 2)); + assert(array[2] == P(2, 1)); + assert(array[3] == P(2, 2)); + assert(array[4] == P(4, 1)); + assert(array[5] == P(4, 2)); + assert(array[6] == P(6, 1)); + assert(array[7] == P(6, 2)); + assert(array[8] == P(8, 1)); + assert(array[9] == P(8, 2)); + } + { // check all false but last true + typedef std::pair<int,int> P; + P array[] = + { + P(0, 1), + P(0, 2), + P(2, 1), + P(2, 2), + P(4, 1), + P(4, 2), + P(6, 1), + P(6, 2), + P(8, 1), + P(1, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + 1); + assert(array[0] == P(1, 2)); + assert(array[1] == P(0, 1)); + assert(array[2] == P(0, 2)); + assert(array[3] == P(2, 1)); + assert(array[4] == P(2, 2)); + assert(array[5] == P(4, 1)); + assert(array[6] == P(4, 2)); + assert(array[7] == P(6, 1)); + assert(array[8] == P(6, 2)); + assert(array[9] == P(8, 1)); + } + { // check all true but first false + typedef std::pair<int,int> P; + P array[] = + { + P(0, 1), + P(1, 2), + P(3, 1), + P(3, 2), + P(5, 1), + P(5, 2), + P(7, 1), + P(7, 2), + P(9, 1), + P(9, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + size-1); + assert(array[0] == P(1, 2)); + assert(array[1] == P(3, 1)); + assert(array[2] == P(3, 2)); + assert(array[3] == P(5, 1)); + assert(array[4] == P(5, 2)); + assert(array[5] == P(7, 1)); + assert(array[6] == P(7, 2)); + assert(array[7] == P(9, 1)); + assert(array[8] == P(9, 2)); + assert(array[9] == P(0, 1)); + } + { // check all true but last false + typedef std::pair<int,int> P; + P array[] = + { + P(1, 1), + P(1, 2), + P(3, 1), + P(3, 2), + P(5, 1), + P(5, 2), + P(7, 1), + P(7, 2), + P(9, 1), + P(0, 2) + }; + const unsigned size = sizeof(array)/sizeof(array[0]); + Iter r = std::stable_partition(Iter(array), Iter(array+size), odd_first()); + assert(base(r) == array + size-1); + assert(array[0] == P(1, 1)); + assert(array[1] == P(1, 2)); + assert(array[2] == P(3, 1)); + assert(array[3] == P(3, 2)); + assert(array[4] == P(5, 1)); + assert(array[5] == P(5, 2)); + assert(array[6] == P(7, 1)); + assert(array[7] == P(7, 2)); + assert(array[8] == P(9, 1)); + assert(array[9] == P(0, 2)); + } +} + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + +struct is_null +{ + template <class P> + bool operator()(const P& p) {return p == 0;} +}; + +template <class Iter> +void +test1() +{ + const unsigned size = 5; + std::unique_ptr<int> array[size]; + Iter r = std::stable_partition(Iter(array), Iter(array+size), is_null()); +} + +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + +int main() +{ + test<bidirectional_iterator<std::pair<int,int>*> >(); + test<random_access_iterator<std::pair<int,int>*> >(); + test<std::pair<int,int>*>(); + +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + test1<bidirectional_iterator<std::unique_ptr<int>*> >(); +#endif +} |