package fast_test
import (
"math"
"testing"
"mercemay.top/src/lambdalog/internal/encoder/json/fast"
)
func TestAppendTwoDigits(t *testing.T) {
cases := []struct {
in int
want string
}{
{0, "00"}, {5, "05"}, {10, "10"}, {42, "42"}, {99, "99"},
}
for _, tc := range cases {
tc := tc
t.Run(tc.want, func(t *testing.T) {
t.Helper()
b := fast.Get()
t.Cleanup(func() { fast.Put(b) })
fast.AppendTwoDigits(b, tc.in)
if got := string(b.Bytes()); got != tc.want {
t.Fatalf("got %q, want %q", got, tc.want)
}
})
}
}
func TestAppendTwoDigits_PanicsOnOutOfRange(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("expected panic")
}
}()
b := fast.Get()
t.Cleanup(func() { fast.Put(b) })
fast.AppendTwoDigits(b, 100)
}
func TestAppendFloatSafe(t *testing.T) {
cases := []struct {
name string
in float64
want string
}{
{"nan", math.NaN(), "null"},
{"pos-inf", math.Inf(1), "null"},
{"neg-inf", math.Inf(-1), "null"},
{"plain", 3.5, "3.5"},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Helper()
b := fast.Get()
t.Cleanup(func() { fast.Put(b) })
fast.AppendFloatSafe(b, tc.in)
if got := string(b.Bytes()); got != tc.want {
t.Fatalf("got %q, want %q", got, tc.want)
}
})
}
}
func TestAppendHex(t *testing.T) {
b := fast.Get()
t.Cleanup(func() { fast.Put(b) })
fast.AppendHex(b, 0xdeadbeef, 8)
if got := string(b.Bytes()); got != "deadbeef" {
t.Fatalf("got %q, want deadbeef", got)
}
}
func TestAppendBool(t *testing.T) {
b := fast.Get()
t.Cleanup(func() { fast.Put(b) })
fast.AppendBool(b, true)
fast.AppendBool(b, false)
if got := string(b.Bytes()); got != "truefalse" {
t.Fatalf("got %q", got)
}
}