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

std::string EXAMPLE_FILENAME = std::string(getenv("MESON_SOURCE_ROOT")) + "/src/resources/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(MeshIO meshIO(EXAMPLE_FILENAME));
}

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

TEST_F(meshIOTest, GetMesh) {
    MeshIO meshIO(EXAMPLE_FILENAME);
    mfem::Mesh& mesh = meshIO.GetMesh();
    EXPECT_EQ(mesh.GetNE(), 670);
    EXPECT_EQ(mesh.GetNV(), 201);
    double volume = ComputeMeshVolume(mesh);
    EXPECT_DOUBLE_EQ(volume, 3.9357596288315868);
}