Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] Complexity regression tests for make_heap and sort_heap.
Reviewed as part of D118003.
- Loading branch information
Arthur O'Dwyer
committed
Mar 8, 2022
1 parent
687e4af
commit e3d3755
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
75 changes: 75 additions & 0 deletions
75
libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |