forked from llvm-mirror/libcxx
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e15dd4e
commit 2a65cc9
Showing
2 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
test/libcxx/algorithms/alg.sorting/alg.heap.operations/poke.heap/poke_heap.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,64 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <algorithm> | ||
|
||
// template<RandomAccessIterator Iter> | ||
// requires ShuffleIterator<Iter> | ||
// && LessThanComparable<Iter::value_type> | ||
// void | ||
// poke_heap(Iter first, Iter last); | ||
|
||
#include <algorithm> | ||
#include <random> | ||
#include <cassert> | ||
#include <stdio.h> | ||
|
||
std::mt19937 randomness; | ||
|
||
void test(int M, int N) | ||
{ | ||
int* inputs = new int [N]; | ||
for (int i = 0; i < N; ++i) | ||
inputs[i] = i; | ||
std::shuffle(inputs, inputs+N, randomness); | ||
int* heap = new int[M]; | ||
int* control = new int[M]; | ||
std::make_heap(inputs, inputs+M); | ||
std::copy(inputs, inputs+M, heap); | ||
std::copy(inputs, inputs+M, control); | ||
for (int i = 0; i < N; ++i) { | ||
std::pop_heap(control, control+M); | ||
control[M-1] = inputs[i]; | ||
heap[0] = inputs[i]; | ||
std::push_heap(control, control+M); | ||
std::poke_heap(heap, heap+M); | ||
assert(std::is_heap(control, control+M)); | ||
assert(std::is_heap(heap, heap+M)); | ||
assert(std::is_permutation(heap, heap+M, control)); | ||
} | ||
delete [] control; | ||
delete [] heap; | ||
delete [] inputs; | ||
} | ||
|
||
int main() | ||
{ | ||
test(100, 1000); | ||
|
||
// Test small cases | ||
int ia[] = {1, 2, 3}; | ||
std::poke_heap(ia, ia); // no-op | ||
assert(ia[0] == 1); | ||
std::poke_heap(ia, ia+1); // no-op | ||
assert(ia[0] == 1); | ||
std::poke_heap(ia, ia+2); | ||
assert(ia[0] == 2 && ia[1] == 1); | ||
|
||
return 0; | ||
} |
66 changes: 66 additions & 0 deletions
66
test/libcxx/algorithms/alg.sorting/alg.heap.operations/poke.heap/poke_heap_comp.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,66 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// <algorithm> | ||
|
||
// template<RandomAccessIterator Iter> | ||
// requires ShuffleIterator<Iter> | ||
// && LessThanComparable<Iter::value_type> | ||
// void | ||
// poke_heap(Iter first, Iter last); | ||
|
||
#include <algorithm> | ||
#include <functional> | ||
#include <random> | ||
#include <cassert> | ||
|
||
#include "test_macros.h" | ||
|
||
std::mt19937 randomness; | ||
|
||
void test(int M, int N) | ||
{ | ||
int* inputs = new int [N]; | ||
for (int i = 0; i < N; ++i) | ||
inputs[i] = i; | ||
std::shuffle(inputs, inputs+N, randomness); | ||
int* heap = new int[M]; | ||
int* control = new int[M]; | ||
std::make_heap(inputs, inputs+M, std::greater<int>()); | ||
std::copy(inputs, inputs+M, heap); | ||
std::copy(inputs, inputs+M, control); | ||
for (int i = 0; i < N; ++i) { | ||
std::pop_heap(control, control+M, std::greater<int>()); | ||
control[M-1] = inputs[i]; | ||
heap[0] = inputs[i]; | ||
std::push_heap(control, control+M, std::greater<int>()); | ||
std::poke_heap(heap, heap+M, std::greater<int>()); | ||
assert(std::is_heap(control, control+M, std::greater<int>())); | ||
assert(std::is_heap(heap, heap+M, std::greater<int>())); | ||
assert(std::is_permutation(heap, heap+M, control)); | ||
} | ||
delete [] control; | ||
delete [] heap; | ||
delete [] inputs; | ||
} | ||
|
||
int main() | ||
{ | ||
test(100, 1000); | ||
|
||
// Test small cases | ||
int ia[] = {3, 2, 1}; | ||
std::poke_heap(ia, ia, std::greater<int>()); // no-op | ||
assert(ia[0] == 3); | ||
std::poke_heap(ia, ia+1, std::greater<int>()); // no-op | ||
assert(ia[0] == 3); | ||
std::poke_heap(ia, ia+2, std::greater<int>()); | ||
assert(ia[0] == 2 && ia[1] == 3); | ||
|
||
return 0; | ||
} |