Browse Source

Partition works and 100% covered for uint32_t

master
Ludovic 'Archivist' Lagouardette 2 years ago
parent
commit
556359ccca
3 changed files with 91 additions and 6 deletions
  1. +5
    -5
      include/gp/algorithms/partition.hpp
  2. +0
    -1
      include/gp/algorithms/sort.hpp
  3. +86
    -0
      tests/partition_test.cpp

+ 5
- 5
include/gp/algorithms/partition.hpp View File

@ -4,18 +4,18 @@
namespace gp {
template<typename it_t, typename pred>
it_t lomuto_partition(it_t first, it_t last, pred predicate = pred{}) {
auto pivot = *--it_t(last);
auto i = --it_t(first);
for(auto j = first; j != last; ++j) {
if(!predicate(*j, pivot)){
auto j = first;
++j;
for(; j != last; ++j) {
if(predicate(*j)){
++i;
gp::swap(*i, *j);
}
}
return i;
return o">++i;
}
template<typename it_t, typename pred>

+ 0
- 1
include/gp/algorithms/sort.hpp View File

@ -1,7 +1,6 @@
#pragma once
#include <gp/algorithms/move.hpp>
#include <gp/algorithms/partition.hpp>
#include <iostream>
namespace gp {

+ 86
- 0
tests/partition_test.cpp View File

@ -0,0 +1,86 @@
#include "gp/containers/array.hpp"
#include "gp/containers/vector.hpp"
#include "gp/algorithms/partition.hpp"
#include "test_scaffold.h"
#include "allocator.hpp"
#include <memory>
#include <random>
#include <iostream>
#include <string>
#include <algorithm>
typedef std::mt19937_64 cheap_rand;
struct partition_array_test : public test_scaffold {
uint32_t seed;
partition_array_test() {
name = __FILE__ ":1_seed";
seed = std::random_device{}();
name += std::to_string(seed);
}
virtual int run() {
cheap_rand setter(seed);
for(int i = 0; i < 250; ++i) {
gp::array<uint32_t, 200> ary;
for(auto& elem : ary) {
elem = setter();
}
auto pivot = gp::hoare_partition(ary.begin(), ary.end(), [](const auto& v){return v % 2;});
for(auto it = ary.begin(); it != pivot;++it) {
gp_config::assertion(!!(*it % 2), "partition failed");
}
for(auto it = pivot; it != ary.end();++it) {
gp_config::assertion(!(*it % 2), "partition failed");
}
}
return 0;
}
};
append_test dummy_s5f468e43(new partition_array_test{});
struct partition_array_lomuto_test : public test_scaffold {
uint32_t seed;
partition_array_lomuto_test() {
name = __FILE__ ":2_seed";
seed = std::random_device{}();
name += std::to_string(seed);
}
virtual int run() {
cheap_rand setter(seed);
for(int i = 0; i < 250; ++i) {
gp::array<uint32_t, 200> ary;
for(auto& elem : ary) {
elem = setter();
}
auto pivot = gp::lomuto_partition(ary.begin(), ary.end(), [](const auto& v){return v % 2;});
for(auto it = ary.begin(); it != pivot;++it) {
gp_config::assertion(!!(*it % 2), "partition failed");
}
for(auto it = pivot; it != ary.end();++it) {
gp_config::assertion(!(*it % 2), "partition failed");
}
}
return 0;
}
};
append_test dummy_lcvudg8e43(new partition_array_lomuto_test{});

Loading…
Cancel
Save