Ver código fonte

HW2: A local version of distbubbletonic added

tags/v2.0
Christos Choutouridis 3 semanas atrás
pai
commit
496ee69f54
5 arquivos alterados com 735 adições e 401 exclusões
  1. +47
    -4
      homework_2/include/distbitonic.hpp
  2. +114
    -13
      homework_2/src/distbitonic.cpp
  3. +0
    -384
      homework_2/test/tests.cpp
  4. +390
    -0
      homework_2/test/tests_Bitonic.cpp
  5. +184
    -0
      homework_2/test/tests_Bubbletonic.cpp

+ 47
- 4
homework_2/include/distbitonic.hpp Ver arquivo

@@ -20,6 +20,14 @@
#include <mpi.h>
#endif

/*!
* Enumerator for the different versions of the sorting method
*/
enum class SortMode {
Bubbletonic, //!< The v0.5 of the algorithm where we use a bubble-sort like approach
Bitonic //!< The v1.0 of the algorithm where we use the bitonic data-exchange approach
};

using Data_t = std::vector<uint8_t>;
using AllData_t = std::vector<Data_t>;

@@ -31,14 +39,49 @@ struct mpi_t {

extern mpi_t mpi;

/*
* ============================== Sort utilities ==============================
*/

/*!
* The primary function template of ascending(). It is DISABLED since , it is explicitly specialized
* for each of the \c SortMode
*/
template <SortMode Mode> bool ascending(size_t, [[maybe_unused]] size_t) noexcept = delete;
template <> bool ascending<SortMode::Bubbletonic>(size_t node, [[maybe_unused]] size_t depth) noexcept;
template <> bool ascending<SortMode::Bitonic>(size_t node, size_t depth) noexcept;

/*!
* The primary function template of partner(). It is DISABLED since , it is explicitly specialized
* for each of the \c SortMode
*/
template <SortMode Mode> size_t partner(size_t, size_t) noexcept = delete;
template <> size_t partner<SortMode::Bubbletonic>(size_t node, size_t step) noexcept;
template <> size_t partner<SortMode::Bitonic>(size_t node, size_t step) noexcept;

/*!
* The primary function template of keepsmall(). It is DISABLED since , it is explicitly specialized
* for each of the \c SortMode
*/
template<SortMode Mode> bool keepsmall(size_t, size_t, [[maybe_unused]] size_t) noexcept = delete;
template<> bool keepsmall<SortMode::Bubbletonic>(size_t node, size_t partner, [[maybe_unused]] size_t depth) noexcept;
template<> bool keepsmall<SortMode::Bitonic>(size_t node, size_t partner, size_t depth) noexcept;

bool ascending(size_t node, size_t depth) noexcept;
size_t partner(size_t node, size_t step) noexcept;
bool keepsmall(size_t node, size_t partner, size_t depth) noexcept;
bool isActive(size_t node, size_t nodes) noexcept;

/*
* ============================== Data utilities ==============================
*/
void exchange(size_t node, size_t partner);
void minmax(AllData_t& data, size_t node, size_t partner, bool keepsmall);
void sort_network(AllData_t& data, size_t nodes, size_t depth);

/*
* ============================== Sort algorithms ==============================
*/
void bubbletonic_network(AllData_t& data, size_t nodes);
void distbubbletonic(size_t P, AllData_t& data);

void bitonic_network(AllData_t& data, size_t nodes, size_t depth);
void distbitonic(size_t P, AllData_t& data);

#endif //DISTBITONIC_H_

+ 114
- 13
homework_2/src/distbitonic.cpp Ver arquivo

@@ -13,6 +13,19 @@
#include <cassert>
#include "distbitonic.hpp"


/*!
* Returns the ascending or descending configuration of the node's sequence based on
* the current node (MPI process) and the depth of the sorting network
*
* @param node The current node (MPI process)
* @return True if we need ascending configuration, false otherwise
*/
template <>
bool ascending<SortMode::Bubbletonic>(size_t node, [[maybe_unused]] size_t depth) noexcept {
return (node % 2) == 0;
}

/*!
* Returns the ascending or descending configuration of the node's sequence based on
* the current node (MPI process) and the depth of the sorting network
@@ -22,33 +35,76 @@
*
* @return True if we need ascending configuration, false otherwise
*/
bool ascending(size_t node, size_t depth) noexcept {
template <>
bool ascending<SortMode::Bitonic>(size_t node, size_t depth) noexcept {
return !(node & (1 << depth));
}

/*!
* Returns the node's partner for data exchange during the sorting network iterations
* of Bubbletonic
*
* @param node The current node
* @param step The step of the sorting network
* @return The node id of the partner for data exchange
*/
size_t partner(size_t node, size_t step) noexcept {
template <>
size_t partner<SortMode::Bubbletonic>(size_t node, size_t step) noexcept {
// return (node % 2 == step % 2) ? node + 1 : node - 1;
return (((node+step) % 2) == 0) ? node + 1 : node - 1;
}

/*!
* Returns the node's partner for data exchange during the sorting network iterations
* of Bitonic
*
* @param node The current node
* @param step The step of the sorting network
* @return The node id of the partner for data exchange
*/
template <>
size_t partner<SortMode::Bitonic>(size_t node, size_t step) noexcept {
return (node ^ (1 << step));
}


/*!
* Predicate to check if a node keeps the small numbers during the bitonic
* sort network exchange.
* Predicate to check if a node keeps the small numbers during the bubbletonic sort network exchange.
*
* @param node The node for which we check
* @param partner The partner of the data exchange
* @return True if the node should keep the small values, false otherwise
*/
template <>
bool keepsmall<SortMode::Bubbletonic>(size_t node, size_t partner, [[maybe_unused]] size_t depth) noexcept {
assert(node != partner);
return (node < partner);
}

/*!
* Predicate to check if a node keeps the small numbers during the bitonic sort network exchange.
*
* @param node The node for which we check
* @param partner The partner of the data exchange
* @param depth The total depth of the sorting network (same for each step for a given network)
* @return True if the node should keep the small values, false otherwise
*/
bool keepsmall(size_t node, size_t partner, size_t depth) noexcept {
template <>
bool keepsmall<SortMode::Bitonic>(size_t node, size_t partner, size_t depth) noexcept {
assert(node != partner);
return ascending(node, depth) == (node < partner);
return ascending<SortMode::Bitonic>(node, depth) == (node < partner);
}

/*!
* Predicate to check if the node is active in the current iteration of the bubbletonic
* sort exchange.
*
* @param node The node to check
* @param nodes The total number of nodes
* @return True if the node is active, false otherwise
*/
bool isActive(size_t node, size_t nodes) noexcept {
return (node < nodes);
}

void exchange(size_t node, size_t partner) {
@@ -65,12 +121,57 @@ void minmax(AllData_t& data, size_t node, size_t partner, bool keepsmall) {
}
}

void sort_network(AllData_t& data, size_t nodes, size_t depth) {



void bubbletonic_network(AllData_t& data, size_t nodes, size_t depth) {
for (size_t node = 0 ; node < nodes ; ++node) { // Currently we do all nodes here!
auto part = partner<SortMode::Bubbletonic>(node, depth);
auto ks = keepsmall<SortMode::Bubbletonic>(node, part, 0);
if (isActive(node, nodes) && node < part) {
exchange(node, part);
minmax(data, node, part, ks);
// elbow-sort here
if (ascending<SortMode::Bubbletonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());

if (ascending<SortMode::Bubbletonic>(part, 0))
std::sort(data[part].begin(), data[part].end(), std::less<>());
else
std::sort(data[part].begin(), data[part].end(), std::greater<>());
}
}
}

void distbubbletonic(size_t P, AllData_t& data) {
for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here!
// Initially sort to create the half part of a bitonic
if (ascending<SortMode::Bubbletonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());
}

for (size_t depth = 0; depth < P-1; ++depth) {
bubbletonic_network(data, P, depth);
}

// Invert the descending ones
for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here!
if (!ascending<SortMode::Bubbletonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
}
}


void bitonic_network(AllData_t& data, size_t nodes, size_t depth) {
for (size_t step = depth; step > 0;) {
--step;
for (size_t node = 0; node < nodes; ++node) {
auto part = partner(node, step);
auto ks = keepsmall(node, part, depth);
for (size_t node = 0; node < nodes; ++node) { // Currently we do all nodes here!
auto part = partner<SortMode::Bitonic>(node, step);
auto ks = keepsmall<SortMode::Bitonic>(node, part, depth);
if (node < part) {
exchange(node, part);
minmax(data, node, part, ks);
@@ -84,7 +185,7 @@ void distbitonic(size_t P, AllData_t& data) {

for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here!
// Initially sort to create the half part of a bitonic
if (ascending(node, 0))
if (ascending<SortMode::Bitonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());
@@ -92,11 +193,11 @@ void distbitonic(size_t P, AllData_t& data) {

// Run through sort network using elbow-sort
for (size_t depth = 1; depth <= p; ++depth) {
sort_network(data, P, depth);
bitonic_network(data, P, depth);

for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here!
// elbow-sort here
if (ascending(node, depth))
if (ascending<SortMode::Bitonic>(node, depth))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());


+ 0
- 384
homework_2/test/tests.cpp Ver arquivo

@@ -1,384 +0,0 @@
/**
* \file
* \brief PDS HW2 tests
*
* \author
* Christos Choutouridis AEM:8997
* <cchoutou@ece.auth.gr>
*/

#include <gtest/gtest.h>

#include <algorithm> // rand/srand
#include <ctime> // rand/srand
#include "distbitonic.hpp"


/*
* ==========================================
*/

/*
* bool ascending(size_t node, size_t depth);
* depth 0 (the initial ascending pattern)
*/
TEST(Tdistbitonic_UT, ascending_test1) {

EXPECT_EQ(ascending(0, 0), true);
EXPECT_EQ(ascending(1, 0), false);
EXPECT_EQ(ascending(2, 0), true);
EXPECT_EQ(ascending(3, 0), false);
EXPECT_EQ(ascending(4, 0), true);
EXPECT_EQ(ascending(5, 0), false);
EXPECT_EQ(ascending(6, 0), true);
EXPECT_EQ(ascending(7, 0), false);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(ascending(node, 0), ((node % 2) ? false : true) );
}
}

/*
* bool ascending(size_t node, size_t depth);
* depth 1
*/
TEST(Tdistbitonic_UT, ascending_test2) {

EXPECT_EQ(ascending(0, 1), true);
EXPECT_EQ(ascending(1, 1), true);
EXPECT_EQ(ascending(2, 1), false);
EXPECT_EQ(ascending(3, 1), false);
EXPECT_EQ(ascending(4, 1), true);
EXPECT_EQ(ascending(5, 1), true);
EXPECT_EQ(ascending(6, 1), false);
EXPECT_EQ(ascending(7, 1), false);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(ascending(2*node, 1), ((node % 2) ? false:true));
EXPECT_EQ(ascending(2*node+1, 1), ((node % 2) ? false:true));
}
}

/*
* bool ascending(size_t node, size_t depth);
* various depths
*/
TEST(Tdistbitonic_UT, ascending_test3) {

// Depth = 3
size_t ts_depth = 3;

for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), true);
for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), false);
for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), true);
for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), false);

// Depth = 4
ts_depth = 4;

for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), true);
for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), false);
for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), true);
for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), false);

// Depth = 8
ts_depth = 8;

for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), true);
for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), false);
for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), true);
for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending(n, ts_depth), false);
}

/*
* size_t partner(size_t node, size_t step);
* step = 0
*/
TEST(Tdistbitonic_UT, partner_test1) {

EXPECT_EQ(partner(0, 0), 1UL);
EXPECT_EQ(partner(1, 0), 0UL);
EXPECT_EQ(partner(2, 0), 3UL);
EXPECT_EQ(partner(3, 0), 2UL);
EXPECT_EQ(partner(4, 0), 5UL);
EXPECT_EQ(partner(5, 0), 4UL);
EXPECT_EQ(partner(6, 0), 7UL);
EXPECT_EQ(partner(7, 0), 6UL);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(partner(node, 0), (node % 2) ? node-1 : node+1);
}
}

/*
* size_t partner(size_t node, size_t step);
* step = 1
*/
TEST(Tdistbitonic_UT, partner_test2) {

EXPECT_EQ(partner(0, 1), 2UL);
EXPECT_EQ(partner(1, 1), 3UL);
EXPECT_EQ(partner(2, 1), 0UL);
EXPECT_EQ(partner(3, 1), 1UL);
EXPECT_EQ(partner(4, 1), 6UL);
EXPECT_EQ(partner(5, 1), 7UL);
EXPECT_EQ(partner(6, 1), 4UL);
EXPECT_EQ(partner(7, 1), 5UL);

for (size_t n1 = 0 ; n1 < 256 ; n1 += 2) {
auto n2 = n1 + 1UL;
EXPECT_EQ(partner(n1, 1), ((n1 % 4) ? n1-2 : n1+2));
EXPECT_EQ(partner(n2, 1), ((n1 % 4) ? n2-2 : n2+2));
}
}

/*
* size_t partner(size_t node, size_t step);
* various steps
*/
TEST(Tdistbitonic_UT, partner_test3) {
// step = 2
size_t ts_step = 2;

for (size_t n1 = 0 ; n1 < 256 ; n1 += 4) {
auto n2 = n1 + 1UL;
auto n3 = n1 + 2UL;
auto n4 = n1 + 3UL;
EXPECT_EQ(partner(n1, ts_step), ((n1 % 8) ? n1-4 : n1+4));
EXPECT_EQ(partner(n2, ts_step), ((n1 % 8) ? n2-4 : n2+4));
EXPECT_EQ(partner(n3, ts_step), ((n1 % 8) ? n3-4 : n3+4));
EXPECT_EQ(partner(n4, ts_step), ((n1 % 8) ? n4-4 : n4+4));
}

// step = 3
ts_step = 3;

for (size_t n1 = 0 ; n1 < 256 ; n1 += 8) {
auto n2 = n1 + 1UL;
auto n3 = n1 + 2UL;
auto n4 = n1 + 3UL;
auto n5 = n1 + 4UL;
auto n6 = n1 + 5UL;
auto n7 = n1 + 6UL;
auto n8 = n1 + 7UL;
EXPECT_EQ(partner(n1, ts_step), ((n1 % 16) ? n1-8 : n1+8));
EXPECT_EQ(partner(n2, ts_step), ((n1 % 16) ? n2-8 : n2+8));
EXPECT_EQ(partner(n3, ts_step), ((n1 % 16) ? n3-8 : n3+8));
EXPECT_EQ(partner(n4, ts_step), ((n1 % 16) ? n4-8 : n4+8));
EXPECT_EQ(partner(n5, ts_step), ((n1 % 16) ? n5-8 : n5+8));
EXPECT_EQ(partner(n6, ts_step), ((n1 % 16) ? n6-8 : n6+8));
EXPECT_EQ(partner(n7, ts_step), ((n1 % 16) ? n7-8 : n7+8));
EXPECT_EQ(partner(n8, ts_step), ((n1 % 16) ? n8-8 : n8+8));
}

// step = 4
ts_step = 4;

for (size_t n1 = 0 ; n1 < 256 ; n1 += 16) {
auto n2 = n1 + 1UL;
auto n3 = n1 + 2UL;
auto n4 = n1 + 3UL;
auto n5 = n1 + 4UL;
auto n6 = n1 + 5UL;
auto n7 = n1 + 6UL;
auto n8 = n1 + 7UL;
auto n9 = n1 + 8UL;
auto n10 = n1 + 9UL;
auto n11 = n1 + 10UL;
auto n12 = n1 + 11UL;
auto n13 = n1 + 12UL;
auto n14 = n1 + 13UL;
auto n15 = n1 + 14UL;
auto n16 = n1 + 15UL;
EXPECT_EQ(partner(n1, ts_step), ((n1 % 32) ? n1-16 : n1+16));
EXPECT_EQ(partner(n2, ts_step), ((n1 % 32) ? n2-16 : n2+16));
EXPECT_EQ(partner(n3, ts_step), ((n1 % 32) ? n3-16 : n3+16));
EXPECT_EQ(partner(n4, ts_step), ((n1 % 32) ? n4-16 : n4+16));
EXPECT_EQ(partner(n5, ts_step), ((n1 % 32) ? n5-16 : n5+16));
EXPECT_EQ(partner(n6, ts_step), ((n1 % 32) ? n6-16 : n6+16));
EXPECT_EQ(partner(n7, ts_step), ((n1 % 32) ? n7-16 : n7+16));
EXPECT_EQ(partner(n8, ts_step), ((n1 % 32) ? n8-16 : n8+16));
EXPECT_EQ(partner(n9, ts_step), ((n1 % 32) ? n9-16 : n9+16));
EXPECT_EQ(partner(n10, ts_step), ((n1 % 32) ? n10-16 : n10+16));
EXPECT_EQ(partner(n11, ts_step), ((n1 % 32) ? n11-16 : n11+16));
EXPECT_EQ(partner(n12, ts_step), ((n1 % 32) ? n12-16 : n12+16));
EXPECT_EQ(partner(n13, ts_step), ((n1 % 32) ? n13-16 : n13+16));
EXPECT_EQ(partner(n14, ts_step), ((n1 % 32) ? n14-16 : n14+16));
EXPECT_EQ(partner(n15, ts_step), ((n1 % 32) ? n15-16 : n15+16));
EXPECT_EQ(partner(n16, ts_step), ((n1 % 32) ? n16-16 : n16+16));
}
}


/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
* Assertion check
*/
TEST(Tdistbitonic_UT, keepsmall_test1) {
ASSERT_DEATH(keepsmall(0, 0, 0), "");
ASSERT_DEATH(keepsmall(1, 1, 42), "");
ASSERT_DEATH(keepsmall(7, 7, 42), "");
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 1 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 0, 1, 1, 0, 0, 1]
*/
TEST(Tdistbitonic_UT, keepsmall_test2) {
size_t ts_depth = 1UL;
size_t ts_partner[] = { 1, 0, 3, 2, 5, 4, 7, 6};
bool ts_expected[] = {1, 0, 0, 1, 1, 0, 0, 1};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 2 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 0, 0, 1, 1]
*/
TEST(Tdistbitonic_UT, keepsmall_test3) {
size_t ts_depth = 2UL;
size_t ts_partner[] = { 2, 3, 0, 1, 6, 7, 4, 5};
bool ts_expected[] = {1, 1, 0, 0, 0, 0, 1, 1};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 2 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 0, 1, 0, 1]
*/
TEST(Tdistbitonic_UT, keepsmall_test4) {
size_t ts_depth = 2UL;
size_t ts_partner[] = { 1, 0, 3, 2, 5, 4, 7, 6};
bool ts_expected[] = {1, 0, 1, 0, 0, 1, 0, 1};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 3 | step: 2 | partner: [4, 5, 6, 7, 0, 1, 2, 3] | keepsmall: Bool[1, 1, 1, 1, 0, 0, 0, 0]
*/
TEST(Tdistbitonic_UT, keepsmall_test5) {
size_t ts_depth = 3UL;
size_t ts_partner[] = { 4, 5, 6, 7, 0, 1, 2, 3};
bool ts_expected[] = {1, 1, 1, 1, 0, 0, 0, 0};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 3 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 1, 1, 0, 0]
*/
TEST(Tdistbitonic_UT, keepsmall_test6) {
size_t ts_depth = 3UL;
size_t ts_partner[] = { 2, 3, 0, 1, 6, 7, 4, 5};
bool ts_expected[] = {1, 1, 0, 0, 1, 1, 0, 0};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 3 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 1, 0, 1, 0]
*/
TEST(Tdistbitonic_UT, keepsmall_test7) {
size_t ts_depth = 3UL;
size_t ts_partner[] = { 1, 0, 3, 2, 5, 4, 7, 6};
bool ts_expected[] = {1, 0, 1, 0, 1, 0, 1, 0};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth));
}
}

TEST(Tdistbitonic_UT, distbitonic_test1) {
AllData_t ts_Data {
Data_t (8), Data_t (8)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbitonic(2, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

TEST(Tdistbitonic_UT, distbitonic_test2) {
AllData_t ts_Data {
Data_t (8), Data_t (8), Data_t (8), Data_t (8)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbitonic(4, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

TEST(Tdistbitonic_UT, distbitonic_test3) {
AllData_t ts_Data {
Data_t (32), Data_t (32), Data_t (32), Data_t (32),
Data_t (32), Data_t (32), Data_t (32), Data_t (32)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbitonic(8, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

+ 390
- 0
homework_2/test/tests_Bitonic.cpp Ver arquivo

@@ -0,0 +1,390 @@
/**
* \file
* \brief PDS HW2 tests
*
* \author
* Christos Choutouridis AEM:8997
* <cchoutou@ece.auth.gr>
*/

#include <gtest/gtest.h>

#include <algorithm> // rand/srand
#include <ctime> // rand/srand
#include "distbitonic.hpp"



/* ================================== ascending ================================== */

/*
* bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
* depth 0 (the initial ascending pattern)
*/
TEST(TdistBitonic_UT, ascending_test1) {

EXPECT_EQ(ascending<SortMode::Bitonic>(0, 0), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(1, 0), false);
EXPECT_EQ(ascending<SortMode::Bitonic>(2, 0), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(3, 0), false);
EXPECT_EQ(ascending<SortMode::Bitonic>(4, 0), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(5, 0), false);
EXPECT_EQ(ascending<SortMode::Bitonic>(6, 0), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(7, 0), false);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(ascending<SortMode::Bitonic>(node, 0), ((node % 2) ? false : true) );
}
}

/*
* bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
* depth 1
*/
TEST(TdistBitonic_UT, ascending_test2) {

EXPECT_EQ(ascending<SortMode::Bitonic>(0, 1), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(1, 1), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(2, 1), false);
EXPECT_EQ(ascending<SortMode::Bitonic>(3, 1), false);
EXPECT_EQ(ascending<SortMode::Bitonic>(4, 1), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(5, 1), true);
EXPECT_EQ(ascending<SortMode::Bitonic>(6, 1), false);
EXPECT_EQ(ascending<SortMode::Bitonic>(7, 1), false);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(ascending<SortMode::Bitonic>(2*node, 1), ((node % 2) ? false:true));
EXPECT_EQ(ascending<SortMode::Bitonic>(2*node+1, 1), ((node % 2) ? false:true));
}
}

/*
* bool ascending<SortMode::Bitonic>(size_t node, size_t depth);
* various depths
*/
TEST(TdistBitonic_UT, ascending_test3) {

// Depth = 3
size_t ts_depth = 3;

for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);

// Depth = 4
ts_depth = 4;

for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);

// Depth = 8
ts_depth = 8;

for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), true);
for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n)
EXPECT_EQ(ascending<SortMode::Bitonic>(n, ts_depth), false);
}




/* ================================== partner ================================== */

/*
* size_t partner<SortMode::Bitonic>(size_t node, size_t step);
* step = 0
*/
TEST(TdistBitonic_UT, partner_test1) {

EXPECT_EQ(partner<SortMode::Bitonic>(0, 0), 1UL);
EXPECT_EQ(partner<SortMode::Bitonic>(1, 0), 0UL);
EXPECT_EQ(partner<SortMode::Bitonic>(2, 0), 3UL);
EXPECT_EQ(partner<SortMode::Bitonic>(3, 0), 2UL);
EXPECT_EQ(partner<SortMode::Bitonic>(4, 0), 5UL);
EXPECT_EQ(partner<SortMode::Bitonic>(5, 0), 4UL);
EXPECT_EQ(partner<SortMode::Bitonic>(6, 0), 7UL);
EXPECT_EQ(partner<SortMode::Bitonic>(7, 0), 6UL);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(partner<SortMode::Bitonic>(node, 0), (node % 2) ? node-1 : node+1);
}
}

/*
* size_t partner<SortMode::Bitonic>(size_t node, size_t step);
* step = 1
*/
TEST(TdistBitonic_UT, partner_test2) {

EXPECT_EQ(partner<SortMode::Bitonic>(0, 1), 2UL);
EXPECT_EQ(partner<SortMode::Bitonic>(1, 1), 3UL);
EXPECT_EQ(partner<SortMode::Bitonic>(2, 1), 0UL);
EXPECT_EQ(partner<SortMode::Bitonic>(3, 1), 1UL);
EXPECT_EQ(partner<SortMode::Bitonic>(4, 1), 6UL);
EXPECT_EQ(partner<SortMode::Bitonic>(5, 1), 7UL);
EXPECT_EQ(partner<SortMode::Bitonic>(6, 1), 4UL);
EXPECT_EQ(partner<SortMode::Bitonic>(7, 1), 5UL);

for (size_t n1 = 0 ; n1 < 256 ; n1 += 2) {
auto n2 = n1 + 1UL;
EXPECT_EQ(partner<SortMode::Bitonic>(n1, 1), ((n1 % 4) ? n1-2 : n1+2));
EXPECT_EQ(partner<SortMode::Bitonic>(n2, 1), ((n1 % 4) ? n2-2 : n2+2));
}
}

/*
* size_t partner(size_t node, size_t step);
* various steps
*/
TEST(TdistBitonic_UT, partner_test3) {
// step = 2
size_t ts_step = 2;

for (size_t n1 = 0 ; n1 < 256 ; n1 += 4) {
auto n2 = n1 + 1UL;
auto n3 = n1 + 2UL;
auto n4 = n1 + 3UL;
EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 8) ? n1-4 : n1+4));
EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 8) ? n2-4 : n2+4));
EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 8) ? n3-4 : n3+4));
EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 8) ? n4-4 : n4+4));
}

// step = 3
ts_step = 3;

for (size_t n1 = 0 ; n1 < 256 ; n1 += 8) {
auto n2 = n1 + 1UL;
auto n3 = n1 + 2UL;
auto n4 = n1 + 3UL;
auto n5 = n1 + 4UL;
auto n6 = n1 + 5UL;
auto n7 = n1 + 6UL;
auto n8 = n1 + 7UL;
EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 16) ? n1-8 : n1+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 16) ? n2-8 : n2+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 16) ? n3-8 : n3+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 16) ? n4-8 : n4+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n5, ts_step), ((n1 % 16) ? n5-8 : n5+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n6, ts_step), ((n1 % 16) ? n6-8 : n6+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n7, ts_step), ((n1 % 16) ? n7-8 : n7+8));
EXPECT_EQ(partner<SortMode::Bitonic>(n8, ts_step), ((n1 % 16) ? n8-8 : n8+8));
}

// step = 4
ts_step = 4;

for (size_t n1 = 0 ; n1 < 256 ; n1 += 16) {
auto n2 = n1 + 1UL;
auto n3 = n1 + 2UL;
auto n4 = n1 + 3UL;
auto n5 = n1 + 4UL;
auto n6 = n1 + 5UL;
auto n7 = n1 + 6UL;
auto n8 = n1 + 7UL;
auto n9 = n1 + 8UL;
auto n10 = n1 + 9UL;
auto n11 = n1 + 10UL;
auto n12 = n1 + 11UL;
auto n13 = n1 + 12UL;
auto n14 = n1 + 13UL;
auto n15 = n1 + 14UL;
auto n16 = n1 + 15UL;
EXPECT_EQ(partner<SortMode::Bitonic>(n1, ts_step), ((n1 % 32) ? n1-16 : n1+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n2, ts_step), ((n1 % 32) ? n2-16 : n2+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n3, ts_step), ((n1 % 32) ? n3-16 : n3+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n4, ts_step), ((n1 % 32) ? n4-16 : n4+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n5, ts_step), ((n1 % 32) ? n5-16 : n5+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n6, ts_step), ((n1 % 32) ? n6-16 : n6+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n7, ts_step), ((n1 % 32) ? n7-16 : n7+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n8, ts_step), ((n1 % 32) ? n8-16 : n8+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n9, ts_step), ((n1 % 32) ? n9-16 : n9+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n10, ts_step), ((n1 % 32) ? n10-16 : n10+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n11, ts_step), ((n1 % 32) ? n11-16 : n11+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n12, ts_step), ((n1 % 32) ? n12-16 : n12+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n13, ts_step), ((n1 % 32) ? n13-16 : n13+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n14, ts_step), ((n1 % 32) ? n14-16 : n14+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n15, ts_step), ((n1 % 32) ? n15-16 : n15+16));
EXPECT_EQ(partner<SortMode::Bitonic>(n16, ts_step), ((n1 % 32) ? n16-16 : n16+16));
}
}



/* ================================== keepsmall ================================== */
/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
* Assertion check
*/
TEST(TdistBitonic_UT, keepsmall_test1) {
ASSERT_DEATH(keepsmall<SortMode::Bitonic>(0, 0, 0), "");
ASSERT_DEATH(keepsmall<SortMode::Bitonic>(1, 1, 42), "");
ASSERT_DEATH(keepsmall<SortMode::Bitonic>(7, 7, 42), "");
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 1 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 0, 1, 1, 0, 0, 1]
*/
TEST(TdistBitonic_UT, keepsmall_test2) {
size_t ts_depth = 1UL;
size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
bool ts_expected[] = {1, 0, 0, 1, 1, 0, 0, 1};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 2 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 0, 0, 1, 1]
*/
TEST(TdistBitonic_UT, keepsmall_test3) {
size_t ts_depth = 2UL;
size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5};
bool ts_expected[] = {1, 1, 0, 0, 0, 0, 1, 1};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 2 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 0, 1, 0, 1]
*/
TEST(TdistBitonic_UT, keepsmall_test4) {
size_t ts_depth = 2UL;
size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
bool ts_expected[] = {1, 0, 1, 0, 0, 1, 0, 1};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 3 | step: 2 | partner: [4, 5, 6, 7, 0, 1, 2, 3] | keepsmall: Bool[1, 1, 1, 1, 0, 0, 0, 0]
*/
TEST(TdistBitonic_UT, keepsmall_test5) {
size_t ts_depth = 3UL;
size_t ts_partner[] = {4, 5, 6, 7, 0, 1, 2, 3};
bool ts_expected[] = {1, 1, 1, 1, 0, 0, 0, 0};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 3 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 1, 1, 0, 0]
*/
TEST(TdistBitonic_UT, keepsmall_test6) {
size_t ts_depth = 3UL;
size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5};
bool ts_expected[] = {1, 1, 0, 0, 1, 1, 0, 0};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
}
}

/*
* bool keepsmall(size_t node, size_t partner, size_t depth);
*
* depth: 3 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 1, 0, 1, 0]
*/
TEST(TdistBitonic_UT, keepsmall_test7) {
size_t ts_depth = 3UL;
size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6};
bool ts_expected[] = {1, 0, 1, 0, 1, 0, 1, 0};

for (size_t node = 0 ; node < 8UL ; ++node ) {
EXPECT_EQ(ts_expected[node], keepsmall<SortMode::Bitonic>(node, ts_partner[node], ts_depth));
}
}

TEST(TdistBitonic_UT, distbitonic_test1) {
AllData_t ts_Data {
Data_t (8), Data_t (8)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbitonic(2, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

TEST(TdistBitonic_UT, distbitonic_test2) {
AllData_t ts_Data {
Data_t (8), Data_t (8), Data_t (8), Data_t (8)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbitonic(4, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

TEST(TdistBitonic_UT, distbitonic_test3) {
AllData_t ts_Data {
Data_t (32), Data_t (32), Data_t (32), Data_t (32),
Data_t (32), Data_t (32), Data_t (32), Data_t (32)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbitonic(8, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

+ 184
- 0
homework_2/test/tests_Bubbletonic.cpp Ver arquivo

@@ -0,0 +1,184 @@
/**
* \file
* \brief PDS HW2 tests
*
* \author
* Christos Choutouridis AEM:8997
* <cchoutou@ece.auth.gr>
*/

#include <gtest/gtest.h>

#include <algorithm> // rand/srand
#include <ctime> // rand/srand
#include "distbitonic.hpp"



/* ================================== ascending ================================== */

/*
* bool ascending<SortMode::Bubbletonic>(size_t node, size_t depth);
*/
TEST(TdistBubbletonic_UT, ascending_Bubbletonic_test1) {
EXPECT_EQ(ascending<SortMode::Bubbletonic>(0, 0), true);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(1, 0), false);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(2, 0), true);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(3, 0), false);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(4, 0), true);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(5, 0), false);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(6, 0), true);
EXPECT_EQ(ascending<SortMode::Bubbletonic>(7, 0), false);

for (size_t node = 0 ; node < 256 ; ++node) {
EXPECT_EQ(ascending<SortMode::Bubbletonic>(node, 7), ((node % 2) ? false : true) );
}
}


/* ================================== partner ================================== */

/*
* size_t partner<SortMode::Bubbletonic>(size_t node, size_t step);
* step = 0
*/
TEST(TdistBubbletonic_UT, partner_Bubbletonic_test1) {
size_t ts_step = 0;
size_t ts_expected[] = {1, 0, 3, 2, 5, 4, 7, 6};

for (size_t node = 0 ; node < 8 ; ++node) {
EXPECT_EQ(partner<SortMode::Bubbletonic>(node, ts_step), ts_expected[node]);
}
}

/*
* size_t partner<SortMode::Bubbletonic>(size_t node, size_t step);
* step = 1
*/
TEST(TdistBubbletonic_UT, partner_Bubbletonic_test2) {
size_t ts_step = 1;
size_t ts_expected[] = {(size_t)-1, 2, 1, 4, 3, 6, 5, 8};

for (size_t node = 0 ; node < 8 ; ++node) {
EXPECT_EQ(partner<SortMode::Bubbletonic>(node, ts_step), ts_expected[node]);
}
}

/*
* size_t partner<SortMode::Bubbletonic>(size_t node, size_t step);
* various steps
*/
TEST(TdistBubbletonic_UT, partner_Bubbletonic_test3) {

size_t ts_even_expected[] = {
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14
};
size_t ts_odd_expected[] = {
(size_t)-1, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16
};

for (size_t step = 0 ; step < 32 ; ++step) {
if (step % 2) {
for (size_t node = 0; node < 16; ++node) {
EXPECT_EQ(partner<SortMode::Bubbletonic>(node, step), ts_odd_expected[node]);
}
}
else {
for (size_t node = 0; node < 16; ++node) {
EXPECT_EQ(partner<SortMode::Bubbletonic>(node, step), ts_even_expected[node]);
}
}
}
}


/* ================================== keepsmall ================================== */
/*
* bool keepsmall<SortMode::Bubbletonic>(size_t node, size_t partner, size_t depth);
* Assertion check
*/
TEST(TdistBubbletonic_UT, keepsmall_test1) {
ASSERT_DEATH(keepsmall<SortMode::Bubbletonic>(0, 0, 0), "");
ASSERT_DEATH(keepsmall<SortMode::Bubbletonic>(1, 1, 42), "");
ASSERT_DEATH(keepsmall<SortMode::Bubbletonic>(7, 7, 42), "");
}

/*
* bool keepsmall<SortMode::Bubbletonic>(size_t node, size_t partner, size_t depth);
*/
TEST(TdistBubbletonic_UT, keepsmall_test2) {
// Check various combinations
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(0, 1, 42), true);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(0, 3, 42), true);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(2, 1, 42), false);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(7, 1, 42), false);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(0, 1, 42), true);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(7, 32,42), true);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(7, 1, 42), false);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(4, 0, 42), false);
EXPECT_EQ(keepsmall<SortMode::Bubbletonic>(4, 9, 42), true);
}



TEST(TdistBubbletonic_UT, distbubbletonic_test1) {
AllData_t ts_Data {
Data_t (8), Data_t (8)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbubbletonic(2, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}


TEST(TdistBubbletonic_UT, distbubbletonic_test2) {
AllData_t ts_Data {
Data_t (8), Data_t (8), Data_t (8), Data_t (8)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbubbletonic(4, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

TEST(TdistBubbletonic_UT, distbubbletonic_test3) {
AllData_t ts_Data {
Data_t (32), Data_t (32), Data_t (32), Data_t (32),
Data_t (32), Data_t (32), Data_t (32), Data_t (32)
};

std::srand(unsigned(std::time(nullptr)));
for (auto& v : ts_Data) {
std::generate(v.begin(), v.end(), std::rand);
}

distbubbletonic(8, ts_Data);

auto max = std::numeric_limits<Data_t::value_type>::min();
for (auto& v : ts_Data) {
EXPECT_EQ((max <= v[0]), true);
EXPECT_EQ(std::is_sorted(v.begin(), v.end()), true);
max = v.back();
}
}

Carregando…
Cancelar
Salvar