// KEKS -- Go KEKS codec implementation
// Copyright (C) 2024-2025 Anton Rudenko <rudenko.ad@phystech.edu>
//                         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/>.

package keks

import (
	"bytes"
	"io"
	"math/big"
	"testing"
	"testing/quick"
)

func TestInt0(t *testing.T) {
	obj := int(0)
	bin := mustHexDec("0C80")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(uint64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != uint64(obj) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestInt1(t *testing.T) {
	obj := int(1)
	bin := mustHexDec("0C8101")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(uint64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != uint64(obj) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntM1(t *testing.T) {
	obj := int(-1)
	bin := mustHexDec("0D80")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(int64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != int64(obj) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestInt123(t *testing.T) {
	obj := uint(123)
	bin := mustHexDec("0C817B")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(uint64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != uint64(obj) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntM123(t *testing.T) {
	obj := int(-123)
	bin := mustHexDec("0D817A")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(int64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != int64(obj) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntMaxAllowable(t *testing.T) {
	obj := uint64(1<<63) - 1
	bin := mustHexDec("0C887FFFFFFFFFFFFFFF")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(uint64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != uint64(obj) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntNMaxAllowable(t *testing.T) {
	obj := -int64(uint64(1<<63) - 1)
	bin := mustHexDec("0D887FFFFFFFFFFFFFFE")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(int64)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted != obj {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestInt64bit(t *testing.T) {
	obj := big.NewInt(0)
	obj = obj.SetBit(obj, 64, 1)
	bin := mustHexDec("0C89010000000000000000")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(*big.Int)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted.Cmp(obj) != 0 {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntM64bit(t *testing.T) {
	obj := big.NewInt(0)
	obj = obj.SetBit(obj, 64, 1)
	obj = obj.Neg(obj)
	bin := mustHexDec("0D88FFFFFFFFFFFFFFFF")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(*big.Int)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted.Cmp(obj) != 0 {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntM64bitAnd1(t *testing.T) {
	obj := big.NewInt(0)
	obj = obj.SetBit(obj, 64, 1)
	obj = obj.SetBit(obj, 0, 1)
	obj = obj.Neg(obj)
	bin := mustHexDec("0D89010000000000000000")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(*big.Int)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted.Cmp(obj) != 0 {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestInt131bit(t *testing.T) {
	obj := big.NewInt(0)
	obj = obj.SetBit(obj, 130, 1)
	bin := mustHexDec("0C910400000000000000000000000000000000")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(*big.Int)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted.Cmp(obj) != 0 {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntM131bit(t *testing.T) {
	obj := big.NewInt(0)
	obj = obj.SetBit(obj, 130, 1)
	obj = obj.Neg(obj)
	bin := mustHexDec("0D9103FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.(*big.Int)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted.Cmp(obj) != 0 {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestIntNotEnoughData(t *testing.T) {
	_, err := NewDecoderFromBytes(mustHexDec("0C830102"), nil).Decode()
	if err != io.ErrUnexpectedEOF {
		t.Fatal(err)
	}
	_, err = NewDecoderFromBytes(mustHexDec("0C8301"), nil).Decode()
	if err != io.ErrUnexpectedEOF {
		t.Fatal(err)
	}
}

func TestIntNonMinimalInt(t *testing.T) {
	encoded := mustHexDec("0C81007B")
	_, err := NewDecoderFromBytes(encoded, nil).Decode()
	if err != ErrIntNonMinimal {
		t.Fatal(err)
	}
}

func TestIntNonBinInInt(t *testing.T) {
	encoded := mustHexDec("0C017B")
	_, err := NewDecoderFromBytes(encoded, nil).Decode()
	if err != ErrIntBad {
		t.Fatal(err)
	}
}

func TestInt64Symmetric(t *testing.T) {
	f := func(x uint64) bool {
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(uint64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == x
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt64NSymmetric(t *testing.T) {
	f := func(x int64) bool {
		if x >= 0 {
			return true
		}
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(int64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == x
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt32Symmetric(t *testing.T) {
	f := func(x uint32) bool {
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(uint64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == uint64(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt32NSymmetric(t *testing.T) {
	f := func(x int32) bool {
		if x >= 0 {
			return true
		}
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(int64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == int64(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt16Symmetric(t *testing.T) {
	f := func(x uint16) bool {
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(uint64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == uint64(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt16NSymmetric(t *testing.T) {
	f := func(x int16) bool {
		if x >= 0 {
			return true
		}
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(int64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == int64(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt8Symmetric(t *testing.T) {
	f := func(x uint8) bool {
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(uint64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == uint64(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestInt8NSymmetric(t *testing.T) {
	f := func(x int8) bool {
		if x >= 0 {
			return true
		}
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(int64)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted == int64(x)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestIntBigSymmetric(t *testing.T) {
	f := func(l uint8) bool {
		data := make([]byte, 8+1+int(l))
		if _, err := io.ReadFull(PRNG, data); err != nil {
			panic(err)
		}
		data[0] |= 0x80 // be sure that is really a big number
		x := big.NewInt(0).SetBytes(data)
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(*big.Int)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted.Cmp(x) == 0
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestIntNBigSymmetric(t *testing.T) {
	f := func(l uint8) bool {
		data := make([]byte, 8+1+int(l))
		if _, err := io.ReadFull(PRNG, data); err != nil {
			panic(err)
		}
		data[0] |= 0x80 // be sure that is really a big number
		x := big.NewInt(0).SetBytes(data)
		x = x.Neg(x)
		encoded, err := EncodeBuf(x, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(*big.Int)
		if !ok {
			t.Fatal("failed to cast")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return casted.Cmp(x) == 0
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestIntTooDeep(t *testing.T) {
	if _, err := NewDecoderFromBytes(append(
		[]byte{byte(AtomPInt)},
		bytes.Repeat([]byte{byte(AtomList)}, 1000)...,
	), nil).Decode(); err != ErrIntBad {
		t.Fatal(err)
	}
}
