#include <array>
#include <cstddef>
#include <filesystem>
#include <system_error>

#include <gmock/gmock.h>
#include <hdf5.h>
#include <mkl.h>
#include <oneapi/tbb/global_control.h>
#include <oneapi/tbb/parallel_for.h>

namespace {

static_assert(sizeof(MKL_INT) == 4, "FESA requires the oneMKL LP64 interface.");

TEST(DependencySmoke, CallsOneMklVectorOperation) {
    constexpr std::array<double, 3> x{1.0, 2.0, 3.0};
    std::array<double, 3> y{4.0, 5.0, 6.0};

    cblas_daxpy(
        static_cast<MKL_INT>(x.size()), 2.0, x.data(), 1, y.data(), 1);

    EXPECT_THAT(y, ::testing::ElementsAre(6.0, 9.0, 12.0));
}

TEST(DependencySmoke, RunsLimitedOneTbbParallelLoop) {
    std::array<std::size_t, 16> values{};
    const oneapi::tbb::global_control limit{
        oneapi::tbb::global_control::max_allowed_parallelism, 2};

    oneapi::tbb::parallel_for(
        std::size_t{0}, values.size(), [&values](const std::size_t index) {
            values[index] = index + 1;
        });

    EXPECT_THAT(values, ::testing::ElementsAre(
                            1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U,
                            9U, 10U, 11U, 12U, 13U, 14U, 15U, 16U));
}

TEST(DependencySmoke, CreatesAndClosesHdf5File) {
    const auto path =
        std::filesystem::path{::testing::TempDir()} /
        "fesa_dependency_smoke.h5";

    const hid_t file = H5Fcreate(
        path.string().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    ASSERT_GE(file, 0);
    EXPECT_GE(H5Fclose(file), 0);

    std::error_code error;
    std::filesystem::remove(path, error);
    EXPECT_FALSE(error);
}

}  // namespace
