// KEKS -- Go KEKS codec implementation
// Copyright (C) 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/>.

package keks

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

func TestEncodingFloat(t *testing.T) {
	_, err := EncodeBuf(1.5, nil)
	if err == nil {
		t.Fatal(err)
	}
}

func TestFloatNaN(t *testing.T) {
	bin := []byte{0x10}
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	casted, ok := decoded.(*Float)
	if !ok {
		t.Fatal("failed to cast")
	}
	if !casted.NaN {
		t.Fatal("got non NaN")
	}
	if casted.Pinf || casted.Ninf || casted.M != nil || casted.E != nil {
		t.Fatal("unexpectedly filled fields")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestFloatPinf(t *testing.T) {
	bin := []byte{0x11}
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	casted, ok := decoded.(*Float)
	if !ok {
		t.Fatal("failed to cast")
	}
	if !casted.Pinf {
		t.Fatal("got non +inf")
	}
	if casted.NaN || casted.Ninf || casted.M != nil || casted.E != nil {
		t.Fatal("unexpectedly filled fields")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestFloatNinf(t *testing.T) {
	bin := []byte{0x12}
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	casted, ok := decoded.(*Float)
	if !ok {
		t.Fatal("failed to cast")
	}
	if !casted.Ninf {
		t.Fatal("got non -inf")
	}
	if casted.NaN || casted.Pinf || casted.M != nil || casted.E != nil {
		t.Fatal("unexpectedly filled fields")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestFloat0(t *testing.T) {
	var b bytes.Buffer
	b.Write([]byte{0x13})
	UIntEncode(&b, 0)
	UIntEncode(&b, 0)
	b.Write(Junk)
	decoder := NewDecoderFromBytes(b.Bytes(), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	casted, ok := decoded.(*Float)
	if !ok {
		t.Fatal("failed to cast")
	}
	if casted.NaN || casted.Pinf || casted.Ninf {
		t.Fatal("unexpectedly filled fields")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
	if casted.M.Cmp(big.NewInt(0)) != 0 {
		t.Fatal("m differs")
	}
	if casted.E.Cmp(big.NewInt(0)) != 0 {
		t.Fatal("m differs")
	}
	f, _ := casted.BigFloat().Float64()
	if f != 0.0 {
		t.Fatal("non zero")
	}
}

func TestFloatNotEnough(t *testing.T) {
	decoder := NewDecoderFromBytes([]byte{0x13}, nil)
	_, err := decoder.Decode()
	if err != io.ErrUnexpectedEOF {
		t.Fatal(err)
	}
}

func TestFloatSingleInt(t *testing.T) {
	var b bytes.Buffer
	b.Write([]byte{0x13})
	UIntEncode(&b, 0)
	decoder := NewDecoderFromBytes(b.Bytes(), nil)
	_, err := decoder.Decode()
	if err != io.ErrUnexpectedEOF {
		t.Fatal(err)
	}
}

func TestFloatNonInt0(t *testing.T) {
	var b bytes.Buffer
	b.Write([]byte{0x13})
	BoolEncode(&b, true)
	decoder := NewDecoderFromBytes(b.Bytes(), nil)
	_, err := decoder.Decode()
	if err != ErrFloatBadInt {
		t.Fatal(err)
	}
}

func TestFloatNonInt1(t *testing.T) {
	var b bytes.Buffer
	b.Write([]byte{0x13})
	UIntEncode(&b, 0)
	BoolEncode(&b, true)
	decoder := NewDecoderFromBytes(b.Bytes(), nil)
	_, err := decoder.Decode()
	if err != ErrFloatBadInt {
		t.Fatal(err)
	}
}

func TestFloatSymmetric(t *testing.T) {
	f := func(m, e int64) bool {
		fp := Float{M: big.NewInt(-181), E: big.NewInt(-2)}
		var b bytes.Buffer
		if _, err := FloatEncode(&b, &fp); err != nil {
			t.Fatal(err)
		}
		b.Write(Junk)
		decoder := NewDecoderFromBytes(b.Bytes(), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.(*Float)
		if !ok {
			t.Fatal("failed to cast")
		}
		if casted.NaN || casted.Pinf || casted.Ninf {
			t.Fatal("unexpectedly filled fields")
		}
		if !bytes.Equal(decoder.B, Junk) {
			t.Fatal("tail differs")
		}
		return (casted.M.Cmp(fp.M) == 0) && (casted.E.Cmp(fp.E) == 0)
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestFloatExamples(t *testing.T) {
	var b bytes.Buffer
	if _, err := FloatEncode(&b, &Float{
		M: big.NewInt(-181),
		E: big.NewInt(-2),
	}); err != nil {
		t.Fatal(err)
	}
	decoded, err := NewDecoderFromBytes(b.Bytes(), nil).Decode()
	if err != nil {
		t.Fatal(err)
	}
	fp, _ := decoded.(*Float).BigFloat().Float64()
	if fp != -45.25 {
		t.Fatal("-45.25")
	}

	b.Reset()
	if _, err = FloatEncode(&b, &Float{
		M: big.NewInt(5),
		E: big.NewInt(-5),
	}); err != nil {
		t.Fatal(err)
	}
	decoded, err = NewDecoderFromBytes(b.Bytes(), nil).Decode()
	if err != nil {
		t.Fatal(err)
	}
	fp, _ = decoded.(*Float).BigFloat().Float64()
	if fp != 0.15625 {
		t.Fatal("0.15625")
	}

	b.Reset()
	if _, err = FloatEncode(&b, &Float{
		M: big.NewInt(-8687443681197687),
		E: big.NewInt(-46),
	}); err != nil {
		t.Fatal(err)
	}
	decoded, err = NewDecoderFromBytes(b.Bytes(), nil).Decode()
	if err != nil {
		t.Fatal(err)
	}
	fp, _ = decoded.(*Float).BigFloat().Float64()
	if fp != -123.456 {
		t.Fatal("-123.456")
	}
}
