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


/* ================================== fullSort ================================== */

/*
 *
 */
TEST(TdistCommonUT, fullSort_test1) {
    std::vector<uint8_t> ts_data     = {3, 2, 1, 4, 5, 7, 8, 6};
    std::vector<uint8_t> ts_expected = {1, 2, 3, 4, 5, 6, 7, 8};
    bool ts_ascending = true;

    fullSort(ts_data, ts_ascending);
    EXPECT_EQ((ts_data == ts_expected), true);
}

TEST(TdistCommonUT, fullSort_test2) {
    std::vector<uint8_t> ts_data     = {3, 2, 1, 4, 5, 7, 8, 6};
    std::vector<uint8_t> ts_expected = {8, 7, 6, 5, 4, 3, 2, 1};
    bool ts_ascending = false;

    fullSort(ts_data, ts_ascending);
    EXPECT_EQ((ts_data == ts_expected), true);
}

/* ================================== elbowSort ================================== */

TEST(TdistCommonUT, elbowSort_test1) {
    ShadowedVec_t<uint8_t> ts_data1(std::vector<uint8_t>{3, 2, 1, 4, 5, 5, 7, 8});
    ShadowedVec_t<uint8_t> ts_data2(std::vector<uint8_t>{4, 5, 7, 8, 5, 3, 2, 1});
    ShadowedVec_t<uint8_t> ts_data3(std::vector<uint8_t>{1, 2, 3, 4, 5, 5, 7, 8});
    ShadowedVec_t<uint8_t> ts_data4(std::vector<uint8_t>{8, 7, 5, 5, 4, 3, 2, 1});
    std::vector<uint8_t> ts_expected = {1, 2, 3, 4, 5, 5, 7, 8};
    bool ts_ascending = true;

    elbowSort(ts_data1, ts_ascending);
    elbowSort(ts_data2, ts_ascending);
    elbowSort(ts_data3, ts_ascending);
    elbowSort(ts_data4, ts_ascending);
    EXPECT_EQ((ts_data1 == ts_expected), true);
    EXPECT_EQ((ts_data2 == ts_expected), true);
    EXPECT_EQ((ts_data3 == ts_expected), true);
    EXPECT_EQ((ts_data4 == ts_expected), true);
}

TEST(TdistCommonUT, elbowSort_test2) {
    ShadowedVec_t<uint8_t> ts_data1(std::vector<uint8_t>{3, 2, 1, 4, 5, 5, 7, 8});
    ShadowedVec_t<uint8_t> ts_data2(std::vector<uint8_t>{4, 5, 7, 8, 5, 3, 2, 1});
    ShadowedVec_t<uint8_t> ts_data3(std::vector<uint8_t>{1, 2, 3, 4, 5, 5, 7, 8});
    ShadowedVec_t<uint8_t> ts_data4(std::vector<uint8_t>{8, 7, 5, 5, 4, 3, 2, 1});
    std::vector<uint8_t> ts_expected = {8, 7, 5, 5, 4, 3, 2, 1};
    bool ts_ascending = false;

    elbowSort(ts_data1, ts_ascending);
    elbowSort(ts_data2, ts_ascending);
    elbowSort(ts_data3, ts_ascending);
    elbowSort(ts_data4, ts_ascending);
    EXPECT_EQ((ts_data1 == ts_expected), true);
    EXPECT_EQ((ts_data2 == ts_expected), true);
    EXPECT_EQ((ts_data3 == ts_expected), true);
    EXPECT_EQ((ts_data4 == ts_expected), true);
}

TEST(TdistCommonUT, elbowSort_test3) {
    ShadowedVec_t<uint8_t> ts_data(std::vector<uint8_t>{8, 7, 5, 5, 4, 3, 2, 1});
    std::vector<uint8_t> ts_expected_asc = {1, 2, 3, 4, 5, 5, 7, 8};
    std::vector<uint8_t> ts_expected_des = {8, 7, 5, 5, 4, 3, 2, 1};

    // Check alternation for active-shadow vector inside Buffer and elbow algorithm
    elbowSort(ts_data, true);
    EXPECT_EQ((ts_data == ts_expected_asc), true);
    elbowSort(ts_data, false);
    EXPECT_EQ((ts_data == ts_expected_des), true);
    elbowSort(ts_data, true);
    EXPECT_EQ((ts_data == ts_expected_asc), true);
    elbowSort(ts_data, false);
    EXPECT_EQ((ts_data == ts_expected_des), true);
}

/*
 * Tag generator test without stage calls
 */
TEST(TdistCommonUT, tagGenerator_test1) {
    // The maximum MPI size we support
    // static constexpr size_t MAX_MPI_SIZE = 1024UL;
    // The maximum pipeline size we support
    // static constexpr size_t MAX_PIPELINE_SIZE = 64UL;

    std::vector<int> ts_tags;
    auto ts_logSize = static_cast<uint32_t>(std::log2(MAX_MPI_SIZE));

    for (size_t depth = 0; depth <= ts_logSize; ++depth) {
        for (size_t step = 0 ; step < MAX_MPI_SIZE; ++step) {
            int tag = static_cast<int>(tagGenerator(depth, step));
            ts_tags.push_back(tag);     // Exchange optimization
            for (size_t stage = 0; stage < MAX_PIPELINE_SIZE; ++stage) {
                ts_tags.push_back(++tag);   // stages
            }
        }
    }
    std::sort(ts_tags.begin(), ts_tags.end());
    for (size_t i = 0 ; i < ts_tags.size() - 1 ; ++i)
        EXPECT_NE(ts_tags[i], ts_tags[i+1]);
}