package fpconv

import (
	"math"
	"testing"
)

func TestFormatFloat(t *testing.T) {
	eq := func(t *testing.T, want string, n float64) {
		t.Helper()
		have := Dtoa(n)
		if have != want {
			t.Errorf("have: %s, want: %s", have, want)
		}
	}

	eq(t, "3.1415", 3.1415)
	eq(t, "0", 0.0)
	eq(t, "inf", math.Inf(1))
	eq(t, "-inf", math.Inf(-1))
	eq(t, "nan", math.NaN())
	eq(t, "1", 1.0)
	eq(t, "2", 2.0)
	eq(t, "3", 3.0)
	eq(t, "4", 4.0)
	eq(t, "10", 10.0)
	eq(t, "100", 100.0)
	eq(t, "1000", 1_000.0)
	eq(t, "2000", 2_000.0)
	eq(t, "10000", 10_000.0)
	eq(t, "100000", 100_000.0)
	eq(t, "1000000", 1_000_000.0)
	eq(t, "10000000", 10_000_000.0)
	eq(t, "1e+8", 100_000_000.0)
	eq(t, "1e+9", 1_000_000_000.0)
	eq(t, "1e+10", 10_000_000_000.0)
	eq(t, "1e+11", 100_000_000_000.0)
	eq(t, "1e+12", 1_000_000_000_000.0)
	eq(t, "-1", -1.0)
	eq(t, "-1.1", -1.1)
	eq(t, "0.0001", 0.0001)
	eq(t, "9e-20", 0.00000000000000000009)
	eq(t, "1.1", 1.1)
	eq(t, "1.01", 1.01)
	eq(t, "1.001", 1.001)
	eq(t, "1.0001", 1.0001)
	eq(t, "2.5339988685347402e-65", 0.000000000000000000000000000000000000000000000000000000000000000025339988685347402)
	eq(t, "2.5339988685347402e-65", 2.5339988685347402e-65)
	eq(t, "3479099956230698", 3479099956230698)
	eq(t, "3.479099956230698e+7", 34790999.56230698123123123)
	eq(t, "0.12345678901234568", 0.12345678901234567890123456789)
	eq(t, "1.2345678901234567", 1.2345678901234567890123456789)
	eq(t, "12.345678901234567", 12.345678901234567890123456789)
	eq(t, "123.45678901234568", 123.45678901234567890123456789)
	eq(t, "1234.567890123457", 1234.5678901234567890123456789)
	eq(t, "1.2345678901234567e+4", 12345.678901234567890123456789)
	eq(t, "1.2345678901234567e+5", 123456.78901234567890123456789)
	eq(t, "1.2345678901234567e+6", 1234567.8901234567890123456789)

	eq(t, "1.2", 1.2)
	eq(t, "2.4", 2*1.2)
	eq(t, "3.6", 3*1.2)
	eq(t, "4.8", 4*1.2)
	a := 1.2
	eq(t, "1.2", a)
	a += 1.2
	eq(t, "2.4", a)
	a += 1.2
	eq(t, "3.5999999999999996", a)
	a += 1.2
	eq(t, "4.8", a)

	// fuzz cases
	eq(t, "386804001288986340000000", 3.8680400128898634e+23)   // not a fuzz case...
	eq(t, "-386804001288986340000000", -3.8680400128898634e+23) // ...but this one crashed
}
