# 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 loads
from keks import NotEnoughData
from tests.strategies import junk_st


class TestInt(TestCase):
    @given(junk_st)
    def test_0(self, junk: bytes) -> None:
        encoded: bytes = dumps(0)
        self.assertSequenceEqual(encoded, bytes.fromhex("0C80"))
        decoded, tail = loads(encoded)
        self.assertEqual(decoded, 0)
        self.assertSequenceEqual(tail, b"")

    @given(junk_st)
    def test_1(self, junk: bytes) -> None:
        encoded: bytes = dumps(1)
        self.assertSequenceEqual(encoded, bytes.fromhex("0C8101"))
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, 1)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_123(self, junk: bytes) -> None:
        encoded: bytes = dumps(123)
        self.assertSequenceEqual(encoded, bytes.fromhex("0C817B"))
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, 123)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_1s64(self, junk: bytes) -> None:
        encoded: bytes = dumps(1 << 64)
        self.assertSequenceEqual(
            encoded,
            bytes.fromhex("0C89010000000000000000"),
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, 1 << 64)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_1s130(self, junk: bytes) -> None:
        encoded: bytes = dumps(1 << 130)
        self.assertSequenceEqual(
            encoded,
            bytes.fromhex("0C910400000000000000000000000000000000"),
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, 1 << 130)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_m1(self, junk: bytes) -> None:
        encoded: bytes = dumps(-1)
        self.assertSequenceEqual(encoded, bytes.fromhex("0D80"))
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, -1)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_m123(self, junk: bytes) -> None:
        encoded: bytes = dumps(-123)
        self.assertSequenceEqual(encoded, bytes.fromhex("0D817A"))
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, -123)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_m1s64(self, junk: bytes) -> None:
        encoded: bytes = dumps(-(1 << 64))
        self.assertSequenceEqual(encoded, bytes.fromhex("0D88FFFFFFFFFFFFFFFF"))
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, -(1 << 64))
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_m1s130(self, junk: bytes) -> None:
        encoded: bytes = dumps(-(1 << 130))
        self.assertSequenceEqual(
            encoded,
            bytes.fromhex("0D9103FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded, -(1 << 130))
        self.assertSequenceEqual(tail, junk)

    def test_decode_not_enough_data(self) -> None:
        with self.assertRaises(NotEnoughData) as err:
            loads(bytes.fromhex("0C830102"))
        self.assertEqual(err.exception.n, 1)
        with self.assertRaises(NotEnoughData) as err:
            loads(bytes.fromhex("0C8301"))
        self.assertEqual(err.exception.n, 2)

    def test_throws_when_nonminimal_int(self) -> None:
        with self.assertRaises(DecodeError) as err:
            encoded: bytes = bytes.fromhex("0C81007B")
            loads(encoded)
        self.assertEqual(str(err.exception), "non-minimal encoding")

    def test_throws_when_non_bin_in_int(self) -> None:
        with self.assertRaises(DecodeError) as err:
            encoded: bytes = bytes.fromhex("0C017B")
            loads(encoded)
        self.assertEqual(str(err.exception), "non-BIN in INT")

    @given(integers())
    def test_symmetric(self, i: int) -> None:
        decoded, _ = loads(dumps(i))
        self.assertEqual(decoded, i)
