Skip to content

Commit

Permalink
[libc++] Complexity regression tests for make_heap and sort_heap.
Browse files Browse the repository at this point in the history
Reviewed as part of D118003.
  • Loading branch information
Arthur O'Dwyer committed Mar 8, 2022
1 parent 687e4af commit e3d3755
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
@@ -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;
}
@@ -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;
}

0 comments on commit e3d3755

Please sign in to comment.