[libcxx-commits] [libcxx] e3d3755 - [libc++] Complexity regression tests for make_heap and sort_heap.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 8 10:48:38 PST 2022


Author: Arthur O'Dwyer
Date: 2022-03-08T13:48:20-05:00
New Revision: e3d3755c4745aee33b55ad4e5ab72c46e4133a8e

URL: https://github.com/llvm/llvm-project/commit/e3d3755c4745aee33b55ad4e5ab72c46e4133a8e
DIFF: https://github.com/llvm/llvm-project/commit/e3d3755c4745aee33b55ad4e5ab72c46e4133a8e.diff

LOG: [libc++] Complexity regression tests for make_heap and sort_heap.

Reviewed as part of D118003.

Added: 
    libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
    libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp b/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
new file mode 100644
index 0000000000000..fcabbb9f74a24
--- /dev/null
+++ b/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
@@ -0,0 +1,74 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11
+
+// <algorithm>
+
+// template<class Iter>
+//   void make_heap(Iter first, Iter last);
+
+#include <algorithm>
+#include <cassert>
+#include <random>
+
+#include "test_macros.h"
+
+struct Stats {
+  int compared = 0;
+  int copied = 0;
+  int moved = 0;
+} stats;
+
+struct MyInt {
+  int value;
+  explicit MyInt(int xval) : value(xval) {}
+  MyInt(const MyInt& other) : value(other.value) { ++stats.copied; }
+  MyInt(MyInt&& other) : value(other.value) { ++stats.moved; }
+  MyInt& operator=(const MyInt& other) {
+    value = other.value;
+    ++stats.copied;
+    return *this;
+  }
+  MyInt& operator=(MyInt&& other) {
+    value = other.value;
+    ++stats.moved;
+    return *this;
+  }
+  friend bool operator<(const MyInt& a, const MyInt& b) {
+    ++stats.compared;
+    return a.value < b.value;
+  }
+};
+
+int main(int, char**)
+{
+  const int N = 100'000;
+  std::vector<MyInt> v;
+  v.reserve(N);
+  std::mt19937 g;
+  for (int i = 0; i < N; ++i)
+    v.emplace_back(g());
+
+  // The exact stats of our current implementation are recorded here.
+  // If something changes to make them go a bit up or down, that's probably fine,
+  // and we can just update this test.
+  // But if they suddenly leap upward, that's a bad thing.
+
+  stats = {};
+  std::make_heap(v.begin(), v.end());
+  assert(stats.copied == 0);
+  assert(stats.moved == 153'486);
+#ifndef _LIBCPP_DEBUG
+  assert(stats.compared == 188'285);
+#endif
+
+  assert(std::is_heap(v.begin(), v.end()));
+
+  return 0;
+}

diff  --git a/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp b/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
new file mode 100644
index 0000000000000..160798382304b
--- /dev/null
+++ b/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
@@ -0,0 +1,75 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11
+
+// <algorithm>
+
+// template<class Iter>
+//   void sort_heap(Iter first, Iter last);
+
+#include <algorithm>
+#include <cassert>
+#include <random>
+
+#include "test_macros.h"
+
+struct Stats {
+  int compared = 0;
+  int copied = 0;
+  int moved = 0;
+} stats;
+
+struct MyInt {
+  int value;
+  explicit MyInt(int xval) : value(xval) {}
+  MyInt(const MyInt& other) : value(other.value) { ++stats.copied; }
+  MyInt(MyInt&& other) : value(other.value) { ++stats.moved; }
+  MyInt& operator=(const MyInt& other) {
+    value = other.value;
+    ++stats.copied;
+    return *this;
+  }
+  MyInt& operator=(MyInt&& other) {
+    value = other.value;
+    ++stats.moved;
+    return *this;
+  }
+  friend bool operator<(const MyInt& a, const MyInt& b) {
+    ++stats.compared;
+    return a.value < b.value;
+  }
+};
+
+int main(int, char**)
+{
+  const int N = 100'000;
+  std::vector<MyInt> v;
+  v.reserve(N);
+  std::mt19937 g;
+  for (int i = 0; i < N; ++i)
+    v.emplace_back(g());
+  std::make_heap(v.begin(), v.end());
+
+  // The exact stats of our current implementation are recorded here.
+  // If something changes to make them go a bit up or down, that's probably fine,
+  // and we can just update this test.
+  // But if they suddenly leap upward, that's a bad thing.
+
+  stats = {};
+  std::sort_heap(v.begin(), v.end());
+  assert(stats.copied == 0);
+  assert(stats.moved == 1'900'731);
+#ifndef _LIBCPP_DEBUG
+  assert(stats.compared == 2'831'757);
+#endif
+
+  assert(std::is_sorted(v.begin(), v.end()));
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list