summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/25_algorithms/heap
diff options
context:
space:
mode:
Diffstat (limited to 'libstdc++-v3/testsuite/25_algorithms/heap')
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/heap/1.cc145
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/heap/moveable.cc156
-rw-r--r--libstdc++-v3/testsuite/25_algorithms/heap/moveable2.cc157
3 files changed, 458 insertions, 0 deletions
diff --git a/libstdc++-v3/testsuite/25_algorithms/heap/1.cc b/libstdc++-v3/testsuite/25_algorithms/heap/1.cc
new file mode 100644
index 000000000..1683a83f7
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/heap/1.cc
@@ -0,0 +1,145 @@
+// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009
+// Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// 25.3.6 Heap operations [lib.alg.heap.operations]
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+
+const int A[] = {1, 11, 12, 3, 10, 6, 17, 4, 8, 2, 5, 13, 9, 15, 14, 16, 7};
+const int B[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
+const int C[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
+const int N = sizeof(A) / sizeof(int);
+
+// This functor has the equivalent functionality of std::greater<>,
+// but there is no dependency on <functional> and it also tracks the
+// number of invocations since creation.
+class Gt
+{
+public:
+ static int count() { return _M_count; }
+ static void reset() { _M_count = 0; }
+
+ bool
+ operator()(const int& x, const int& y)
+ {
+ ++_M_count;
+ return x > y;
+ }
+
+private:
+ static int _M_count;
+};
+
+int Gt::_M_count = 0;
+
+// Exercise all of the heap functions for operator<. The intermediate
+// results between push_heap and pop_heap and make_heap and sort_heap
+// are not checked (they could be).
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ // sort array s1 using push_heap/pop_heap
+ int s1[N];
+ std::copy(A, A + N, s1);
+ VERIFY(std::equal(s1, s1 + N, A));
+
+ for (int i = 2; i <= N; ++i)
+ std::push_heap(s1, s1 + i);
+
+ for (int i = N; i >= 2; --i)
+ std::pop_heap(s1, s1 + i);
+
+ VERIFY(std::equal(s1, s1 + N, B));
+
+ // sort array s2 using make_heap/sort_heap
+ int s2[N];
+ std::copy(A, A + N, s2);
+ VERIFY(std::equal(s2, s2 + N, A));
+
+ std::make_heap(s2, s2 + N);
+ std::sort_heap(s2, s2 + N);
+ VERIFY(std::equal(s2, s2 + N, B));
+}
+
+// Perform same tests as above but with the comparison predicate
+// versions, and add complexity constraint checks.
+void
+test02()
+{
+ bool test __attribute__((unused)) = true;
+
+ Gt gt;
+
+#ifndef _GLIBCXX_DEBUG
+ //const int logN = static_cast<int>(std::log(static_cast<double>(N)) + 0.5);
+ const int logN = 3;
+#endif
+
+ int s1[N];
+ std::copy(A, A + N, s1);
+ VERIFY(std::equal(s1, s1 + N, A));
+
+ for (int i = 2; i <= N; ++i)
+ {
+ std::push_heap(s1, s1 + i, gt);
+#ifndef _GLIBCXX_DEBUG
+ VERIFY(gt.count() <= logN);
+#endif
+ gt.reset();
+ }
+
+ for (int i = N; i >= 2; --i)
+ {
+ std::pop_heap(s1, s1 + i, gt);
+#ifndef _GLIBCXX_DEBUG
+ VERIFY(gt.count() <= 2 * logN);
+#endif
+ gt.reset();
+ }
+
+ VERIFY(std::equal(s1, s1 + N, C));
+
+ // sort array s2 using make_heap/sort_heap
+ int s2[N];
+ std::copy(A, A + N, s2);
+ VERIFY(std::equal(s2, s2 + N, A));
+
+ std::make_heap(s2, s2 + N, gt);
+#ifndef _GLIBCXX_DEBUG
+ VERIFY(gt.count() <= 3 * N);
+#endif
+ gt.reset();
+
+ std::sort_heap(s2, s2 + N, gt);
+#ifndef _GLIBCXX_DEBUG
+ VERIFY(gt.count() <= N * logN);
+#endif
+
+ VERIFY(std::equal(s2, s2 + N, C));
+}
+
+int
+main()
+{
+ test01();
+ test02();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/heap/moveable.cc b/libstdc++-v3/testsuite/25_algorithms/heap/moveable.cc
new file mode 100644
index 000000000..a744c001f
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/heap/moveable.cc
@@ -0,0 +1,156 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
+// Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++0x -DITERATIONS=5" { target simulator } }
+
+// 25.3.6 Heap operations [lib.alg.heap.operations]
+
+#undef _GLIBCXX_CONCEPT_CHECKS
+
+// XXX FIXME: parallel-mode should deal correctly with moveable-only types
+// per C++0x, at minimum smoothly fall back to serial.
+#undef _GLIBCXX_PARALLEL
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+#include <testsuite_rvalref.h>
+
+#ifndef ITERATIONS
+#define ITERATIONS 9
+#endif
+
+using __gnu_test::test_container;
+using __gnu_test::random_access_iterator_wrapper;
+using __gnu_test::rvalstruct;
+
+typedef test_container<rvalstruct, random_access_iterator_wrapper> container;
+typedef test_container<int, random_access_iterator_wrapper> container_ref;
+
+void
+check_make(int* array, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct makeheap[9];
+ int makeheap_ref[9];
+ std::copy(array, array + length, makeheap);
+ std::copy(array, array + length, makeheap_ref);
+ container makecon(makeheap, makeheap + length);
+ container_ref makecon_ref(makeheap_ref, makeheap_ref + length);
+ std::make_heap(makecon.begin(), makecon.end());
+ std::make_heap(makecon_ref.begin(), makecon_ref.end());
+ for (int z = 0; z < length; ++z)
+ VERIFY( makeheap[z] == makeheap_ref[z] );
+ VERIFY( std::__is_heap(makecon.begin(), makecon.end()) );
+ for (int z = 0; z < length; ++z)
+ VERIFY( makeheap[z].valid );
+}
+
+void
+check_pop(int* array, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct popheap[9];
+ int popheap_ref[9];
+ std::copy(array, array + length, popheap);
+ std::copy(array, array + length, popheap_ref);
+ container popcon(popheap, popheap + length);
+ container_ref popcon_ref(popheap_ref, popheap_ref + length);
+ std::pop_heap(popcon.begin(), popcon.end());
+ std::pop_heap(popcon_ref.begin(), popcon_ref.end());
+ for (int z = 0; z < length; ++z)
+ VERIFY( popheap[z] == popheap_ref[z] );
+ VERIFY( (std::__is_heap(popheap, popheap + length - 1)) );
+ for (int z = 0; z < length; ++z)
+ VERIFY( popheap[z].val <= popheap[length-1].val && popheap[z].valid );
+}
+
+void
+check_sort(int* array, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct sortheap[9];
+ int sortheap_ref[9];
+ std::copy(array, array + length, sortheap);
+ std::copy(array, array + length, sortheap_ref);
+ container sortcon(sortheap, sortheap + length);
+ container_ref sortcon_ref(sortheap_ref, sortheap_ref + length);
+ std::sort_heap(sortcon.begin(), sortcon.end());
+ std::sort_heap(sortcon_ref.begin(), sortcon_ref.end());
+ for (int z = 0; z < length; ++z)
+ VERIFY( sortheap[z] == sortheap_ref[z] );
+ for (int z = 0; z < length - 1; ++z)
+ VERIFY( sortheap[z].val <= sortheap[z + 1].val && sortheap[z].valid );
+ VERIFY( sortheap[length - 1].valid );
+}
+
+void
+check_push(int* array, int pushval, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct pushheap[10];
+ int pushheap_ref[10];
+ std::copy(array, array + length, pushheap);
+ std::copy(array, array + length, pushheap_ref);
+ pushheap[length] = pushval;
+ pushheap_ref[length] = pushval;
+ container pushcon(pushheap, pushheap + length + 1);
+ container_ref pushcon_ref(pushheap_ref, pushheap_ref + length + 1);
+ std::push_heap(pushcon.begin(), pushcon.end());
+ std::push_heap(pushcon_ref.begin(), pushcon_ref.end());
+ for (int z = 0; z < length + 1; ++z)
+ VERIFY( pushheap[z] == pushheap_ref[z] );
+ VERIFY( std::__is_heap(pushheap, pushheap + length + 1) );
+ for (int z = 0; z < length + 1; ++z)
+ VERIFY( pushheap[z].valid );
+}
+
+void
+test01()
+{
+ int array[9];
+ for (int i = 1; i < ITERATIONS; ++i)
+ {
+ for(int z = 0; z < i; ++z)
+ array[z] = z;
+ while (std::next_permutation(array, array + i))
+ {
+ check_make(array, i);
+ if (std::__is_heap(array, array + i))
+ {
+ check_pop(array, i);
+ check_sort(array, i);
+ for (int pushval = -1; pushval <= i; ++pushval)
+ check_push(array, pushval, i);
+ }
+ }
+ }
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/25_algorithms/heap/moveable2.cc b/libstdc++-v3/testsuite/25_algorithms/heap/moveable2.cc
new file mode 100644
index 000000000..562ab466a
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/heap/moveable2.cc
@@ -0,0 +1,157 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2009 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++0x -DITERATIONS=5" { target simulator } }
+
+// 25.3.6 Heap operations [lib.alg.heap.operations]
+
+#undef _GLIBCXX_CONCEPT_CHECKS
+
+#include <algorithm>
+#include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
+#include <testsuite_rvalref.h>
+
+#ifndef ITERATIONS
+#define ITERATIONS 9
+#endif
+
+using __gnu_test::test_container;
+using __gnu_test::random_access_iterator_wrapper;
+using __gnu_test::rvalstruct;
+
+typedef test_container<rvalstruct, random_access_iterator_wrapper> container;
+typedef test_container<int, random_access_iterator_wrapper> container_ref;
+
+bool are_ordered(const rvalstruct& lhs, const rvalstruct& rhs)
+{ return lhs < rhs; }
+
+bool are_ordered_int(const int& lhs, const int& rhs)
+{ return lhs < rhs; }
+
+void
+check_make(int* array, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct makeheap[9];
+ int makeheap_ref[9];
+ std::copy(array, array + length, makeheap);
+ std::copy(array, array + length, makeheap_ref);
+ container makecon(makeheap, makeheap + length);
+ container_ref makecon_ref(makeheap_ref, makeheap_ref + length);
+ std::make_heap(makecon.begin(), makecon.end(), are_ordered);
+ std::make_heap(makecon_ref.begin(), makecon_ref.end(), are_ordered_int);
+ for (int z = 0; z < length; ++z)
+ VERIFY( makeheap[z] == makeheap_ref[z] );
+ VERIFY( std::__is_heap(makecon.begin(), makecon.end(), are_ordered) );
+ for (int z = 0; z < length; ++z)
+ VERIFY( makeheap[z].valid );
+}
+
+void
+check_pop(int* array, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct popheap[9];
+ int popheap_ref[9];
+ std::copy(array, array + length, popheap);
+ std::copy(array, array + length, popheap_ref);
+ container popcon(popheap, popheap + length);
+ container_ref popcon_ref(popheap_ref, popheap_ref + length);
+ std::pop_heap(popcon.begin(), popcon.end(), are_ordered);
+ std::pop_heap(popcon_ref.begin(), popcon_ref.end(), are_ordered_int);
+ for (int z = 0; z < length; ++z)
+ VERIFY( popheap[z] == popheap_ref[z] );
+ VERIFY( (std::__is_heap(popheap, popheap + length - 1), are_ordered) );
+ for (int z = 0; z < length; ++z)
+ VERIFY( popheap[z].val <= popheap[length-1].val && popheap[z].valid );
+}
+
+void
+check_sort(int* array, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct sortheap[9];
+ int sortheap_ref[9];
+ std::copy(array, array + length, sortheap);
+ std::copy(array, array + length, sortheap_ref);
+ container sortcon(sortheap, sortheap + length);
+ container_ref sortcon_ref(sortheap_ref, sortheap_ref + length);
+ std::sort_heap(sortcon.begin(), sortcon.end(), are_ordered);
+ std::sort_heap(sortcon_ref.begin(), sortcon_ref.end(), are_ordered_int);
+ for (int z = 0; z < length; ++z)
+ VERIFY( sortheap[z] == sortheap_ref[z] );
+ for (int z = 0; z < length - 1; ++z)
+ VERIFY( sortheap[z].val <= sortheap[z + 1].val && sortheap[z].valid );
+ VERIFY( sortheap[length - 1].valid );
+}
+
+void
+check_push(int* array, int pushval, int length)
+{
+ bool test __attribute__((unused)) = true;
+
+ rvalstruct pushheap[10];
+ int pushheap_ref[10];
+ std::copy(array, array + length, pushheap);
+ std::copy(array, array + length, pushheap_ref);
+ pushheap[length] = pushval;
+ pushheap_ref[length] = pushval;
+ container pushcon(pushheap, pushheap + length + 1);
+ container_ref pushcon_ref(pushheap_ref, pushheap_ref + length + 1);
+ std::push_heap(pushcon.begin(), pushcon.end(), are_ordered);
+ std::push_heap(pushcon_ref.begin(), pushcon_ref.end(), are_ordered_int);
+ for (int z = 0; z < length + 1; ++z)
+ VERIFY( pushheap[z] == pushheap_ref[z] );
+ VERIFY( std::__is_heap(pushheap, pushheap + length + 1) );
+ for (int z = 0; z < length + 1; ++z)
+ VERIFY( pushheap[z].valid );
+}
+
+void
+test01()
+{
+ int array[9];
+ for (int i = 1; i < ITERATIONS; ++i)
+ {
+ for(int z = 0; z < i; ++z)
+ array[z] = z;
+ while (std::next_permutation(array, array + i))
+ {
+ check_make(array, i);
+ if (std::__is_heap(array, array + i, are_ordered_int))
+ {
+ check_pop(array, i);
+ check_sort(array, i);
+ for (int pushval = -1; pushval <= i; ++pushval)
+ check_push(array, pushval, i);
+ }
+ }
+ }
+}
+
+int
+main()
+{
+ test01();
+ return 0;
+}