# KEKS -- Python KEKS implementation
# Copyright (C) 2024-2025 Anton Rudenko <rudenko.ad@phystech.edu>
#               2024-2025 Sergey Matveev <stargrave@stargrave.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program.  If not, see <http://www.gnu.org/licenses/>.

from unittest import TestCase

from hypothesis import given
from hypothesis.strategies import integers

from keks import DecodeError
from keks import dumps
from keks import Float
from keks import loads
from keks import NaN
from keks import NotEnoughData

from tests.strategies import junk_st


class TestFloat(TestCase):
    def test_throws_when_dumps_float(self) -> None:
        with self.assertRaises(NotImplementedError) as err:
            dumps(1.5)
        self.assertEqual(str(err.exception), "no full FLOAT support")

    @given(junk_st)
    def test_nan(self, junk: bytes) -> None:
        b = dumps(float("nan"))
        self.assertSequenceEqual(b, b"\x10")
        f, tail = loads(b + junk)
        self.assertSequenceEqual(tail, junk)
        self.assertIs(f, NaN)

    @given(junk_st)
    def test_pinf(self, junk: bytes) -> None:
        b = dumps(float("+inf"))
        self.assertSequenceEqual(b, b"\x11")
        f, tail = loads(b + junk)
        self.assertSequenceEqual(tail, junk)
        self.assertEqual(f, float("+inf"))

    @given(junk_st)
    def test_ninf(self, junk: bytes) -> None:
        b = dumps(float("-inf"))
        self.assertSequenceEqual(b, b"\x12")
        f, tail = loads(b + junk)
        self.assertSequenceEqual(tail, junk)
        self.assertEqual(f, float("-inf"))

    @given(junk_st)
    def test_0(self, junk: bytes) -> None:
        b = dumps(0.0)
        self.assertSequenceEqual(b, b"\x13" + dumps(0) + dumps(0))
        f, tail = loads(b + junk)
        self.assertSequenceEqual(tail, junk)
        self.assertEqual(f, 0.0)

    def test_not_enough(self) -> None:
        with self.assertRaises(NotEnoughData) as err:
            loads(b"\x13")
        self.assertEqual(err.exception.n, 1)

    def test_not_enough_int(self) -> None:
        with self.assertRaises(NotEnoughData) as err:
            loads(b"\x13" + dumps(123))
        self.assertEqual(err.exception.n, 1)

    def test_non_int0(self) -> None:
        with self.assertRaises(DecodeError) as err:
            loads(b"\x13" + dumps(False) + dumps(123))
        self.assertEqual(str(err.exception), "non-int m float")

    def test_non_int1(self) -> None:
        with self.assertRaises(DecodeError) as err:
            loads(b"\x13" + dumps(123) + dumps(False))
        self.assertEqual(str(err.exception), "non-int e float")

    @given(integers(), integers(), junk_st)
    def test_symmetric(self, m: int, e: int, junk: bytes) -> None:
        f, tail = loads(dumps(Float(m, e)) + junk)
        self.assertSequenceEqual(tail, junk)
        if (m == 0) and (e == 0):
            self.assertEqual(f, 0.0)
        else:
            self.assertEqual(f.m, m)
            self.assertEqual(f.e, e)
