## SPDX-License-Identifier: BSD-2-Clause
##
## Copyright 2020-2025 Inria & Université de Bordeaux
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
##
## 1. Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
##
## 2. Redistributions in binary form must reproduce the above
## copyright notice, this list of conditions and the following
## disclaimer in the documentation and/or other materials provided
## with the distribution.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
## CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
## TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
## ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
## TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
## THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
## SUCH DAMAGE.
##

import scotchpy as SCOTCH
import numpy as np
import os
import importlib.resources

_data_dir = importlib.resources.files('scotchpy').joinpath("data")

# taken from check/test_scotch_mesh_graph.c


def testMesh1():
    mesh = SCOTCH.Mesh()
    graf = SCOTCH.Graph()
    graf.init()
    with open(os.path.join(_data_dir, "small_55.msh")) as fp:
        try:
            mesh.load(fp)
        except SCOTCH.LibraryError:
            assert False, "cannot load mesh"
            raise
    mesh.check()
    try:
        mesh.graph(graf)
    except SCOTCH.LibraryError:
        assert False, "cannot create mesh from graph"
        raise
    graf.exit()
    mesh.exit()


def testMesh2():
    mesh = SCOTCH.Mesh()
    graf = SCOTCH.Graph()
    graf.init()
    with open(os.path.join(_data_dir, "small_55.msh")) as fp:
        try:
            mesh.load(fp)
        except SCOTCH.LibraryError:
            assert False, "cannot load mesh"
            raise
    mesh.check()
    filename = "./test_scotchpy_mesh_save_output.txt"
    with open(filename, "w+") as fp1:
        mesh.save(fp1)
    with open(filename) as fp2:
        h = SCOTCH.load_mesh(fp2)
    datag = mesh.data()
    datah = h.data()
    for valg, valh in zip(datag, datah):
        if isinstance(valg, int):
            assert valg == valh
        else:
            assert np.array_equal(valg, valh)
    os.remove(filename)
    h.exit()
    mesh.exit()
    kwargs = {
        "baseval": 1,
        "vertnbr": 7,
        "verttab": [1, 4, 10, 13, 16, 19, 22, 25],
        "vendtab": None,
        "velotab": [4, 1, 4, 4, 4, 4, 4],
        "vlbltab": None,
        "edgenbr": 24,
        "edgetab": [
            3,
            2,
            6,
            3,
            4,
            1,
            7,
            6,
            5,
            1,
            2,
            4,
            2,
            7,
            3,
            7,
            2,
            6,
            2,
            1,
            5,
            5,
            2,
            4,
        ],
        "edlotab": [
            1,
            1,
            1,
            2,
            2,
            1,
            2,
            3,
            3,
            1,
            2,
            2,
            2,
            1,
            2,
            1,
            3,
            3,
            3,
            1,
            3,
            1,
            2,
            1,
        ],
    }
    graf = SCOTCH.build_graph(**kwargs)
    mesh.graph(graf)  # more testing?
