"""Test for cross-correlation"""
from typing import Final

import numpy as np

from figio.hello import hello
from figio.xymodel import cross_correlation as cc


TOL: Final[float] = 1.0e-6  # small tolerance


def same(a, b, verbosity=False):
    """Return True if two arrays are the same within tolerance."""

    _same = False
    l2norm_diff = np.linalg.norm(a - b)

    if verbosity:
        print(f"array a = {a}")
        print(f"array b = {b}")
        print(f"l2norm_diff = {l2norm_diff}")

    if np.abs(l2norm_diff) < TOL:
        _same = True

    return _same


def test_hello():
    """Basic test."""
    result = hello()
    assert result == "Hello World!"


def test_000_hat_1_hat_2():
    """Test the cross-correlation of two hat functions."""
    verbosity = True

    ref_t = [0.0, 1.0, 2.0]
    ref_y = [0.0, 1.0, 0.0]  # hat at 1
    reference = np.transpose([ref_t, ref_y])

    sub_t = [1.0, 2.0, 3.0]
    sub_y = [0.0, 1.0, 0.0]  # hat at 2
    subject = np.transpose([sub_t, sub_y])

    calculated_t, calculated_y, cc_rel_error, l2_error = cc(
        reference, subject, verbosity
    )

    known_t = [-1.0, 0.0, 1.0, 2.0]
    known_y = [0.0, 0.0, 1.0, 0.0]

    assert same(known_t, calculated_t, verbosity)
    assert same(known_y, calculated_y, verbosity)
    assert cc_rel_error < TOL
    assert l2_error < TOL
