#include <gtest/gtest.h>
#include "meshIO.h"
#include <string>
#include "mfem.hpp"

std::string EXAMPLE_FILENAME = std::string(getenv("MESON_SOURCE_ROOT")) + "/assets/dynamic/mesh/sphere.msh";

double ComputeMeshVolume(mfem::Mesh &mesh)
{
    double totalVolume = 0.0;
    for (int i = 0; i < mesh.GetNE(); i++) // Loop over all elements
    {
        totalVolume += mesh.GetElementVolume(i);
    }
    return totalVolume;
}


class meshIOTest : public ::testing::Test {};

TEST_F(meshIOTest, DefaultConstructor) {
    EXPECT_NO_THROW(serif::mesh::MeshIO meshIO(EXAMPLE_FILENAME));
}

TEST_F(meshIOTest, IsLoaded) {
    serif::mesh::MeshIO meshIO(EXAMPLE_FILENAME);
    EXPECT_EQ(meshIO.IsLoaded(), true);
}

TEST_F(meshIOTest, GetMesh) {
    serif::mesh::MeshIO meshIO(EXAMPLE_FILENAME);
    mfem::Mesh& mesh = meshIO.GetMesh();
    EXPECT_EQ(mesh.GetNE(), 18351);
    EXPECT_EQ(mesh.GetNV(), 3768);
    double volume = ComputeMeshVolume(mesh);
    EXPECT_DOUBLE_EQ(volume, 4.160516453529322);
}

TEST_F(meshIOTest, LinearRescale) {
    serif::mesh::MeshIO meshIO(EXAMPLE_FILENAME, 5);
    mfem::Mesh& mesh = meshIO.GetMesh();
    double volume = ComputeMeshVolume(mesh);
    EXPECT_DOUBLE_EQ(volume, 520.06455669116463);
}