/**
 * \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 "distsort.hpp"



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

/*
 * bool ascending<SortMode::Bubbletonic>(mpi_id_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 (mpi_id_t node = 0 ; node < 256 ; ++node) {
        EXPECT_EQ(ascending<SortMode::Bubbletonic>(node, 7), ((node % 2) ? false : true) );
    }
}


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

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

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

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

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

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

    mpi_id_t ts_even_expected[] = {
            1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14
    };
    mpi_id_t ts_odd_expected[]  = {
            (mpi_id_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 (mpi_id_t node = 0; node < 16; ++node) {
                EXPECT_EQ(partner<SortMode::Bubbletonic>(node, step), ts_odd_expected[node]);
            }
        }
        else {
            for (mpi_id_t node = 0; node < 16; ++node) {
                EXPECT_EQ(partner<SortMode::Bubbletonic>(node, step), ts_even_expected[node]);
            }
        }
    }
}


/* ================================== keepSmall ================================== */
/*
 * bool keepSmall<SortMode::Bubbletonic>(mpi_id_t node, mpi_id_t partner, size_t depth);
 * Throw check (Not assert - ASSERT_DEATH)
 */
TEST(TdistBubbletonic_UT, keepsmall_test1) {
    // node and partner must differ or else ...
    EXPECT_THROW(keepSmall<SortMode::Bubbletonic>(0, 0, 0), std::runtime_error);
    EXPECT_THROW(keepSmall<SortMode::Bubbletonic>(1, 1, 42), std::runtime_error);
    EXPECT_THROW(keepSmall<SortMode::Bubbletonic>(7, 7, 42), std::runtime_error);
}

/*
 * bool keepSmall<SortMode::Bubbletonic>(mpi_id_t node, mpi_id_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);
}

/* ================================== isActive ================================== */
/*
 * bool isActive(mpi_id_t node, size_t nodes);
 * Throw check
 */
TEST(TdistBubbletonic_UT, isActive_test1) {
    EXPECT_THROW(isActive(0, 0), std::runtime_error);
    EXPECT_THROW(isActive(0, static_cast<size_t>(std::numeric_limits<mpi_id_t>::max()) + 1), std::runtime_error);
}

/*
 * bool isActive(mpi_id_t node, size_t nodes);
 * Boundary 3 BVA
 */
TEST(TdistBubbletonic_UT, isActive_test2) {
    EXPECT_EQ(isActive(-1, 8), false);
    EXPECT_EQ(isActive(0, 8), true);
    EXPECT_EQ(isActive(1, 8), true);
    EXPECT_EQ(isActive(7, 8), true);
    EXPECT_EQ(isActive(8, 8), false);
    EXPECT_EQ(isActive(9, 8), false);
}
