// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com) // All rights reserved. package dbf import ( "bytes" "testing" ) // TestEncodeNumericKey_SortOrder pins the sortable-numeric encoding // used by NTX/CDX indexes. Lexicographic byte order of the encoded // keys must match numeric order across signs and magnitudes — the // previous `%20.10f` encoding sorted -100 AFTER 99 because '-' // (0x2D) > ' ' (0x20). func TestEncodeNumericKey_SortOrder(t *testing.T) { // Strictly ascending input values. values := []float64{-1e9, -1e6, -1000, -100, -99, -1, -0.5, 0, 0.5, 1, 99, 100, 1000, 1e6, 1e9} encoded := make([][]byte, len(values)) for i, v := range values { encoded[i] = encodeNumericKey(v) } for i := 1; i < len(encoded); i++ { if bytes.Compare(encoded[i-1], encoded[i]) >= 0 { t.Errorf("encoding[%v]=%q !< encoding[%v]=%q", values[i-1], encoded[i-1], values[i], encoded[i]) } } } // TestEncodeNumericKey_NegativeBeforePositive guards the specific // inversion called out in the bug report: -100 must encode to a key // that sorts before 99. func TestEncodeNumericKey_NegativeBeforePositive(t *testing.T) { neg100 := encodeNumericKey(-100) pos99 := encodeNumericKey(99) if bytes.Compare(neg100, pos99) >= 0 { t.Errorf("expected -100 < 99 lexicographically; got %q !< %q", neg100, pos99) } } // TestEncodeNumericKey_NegativeMagnitudeOrder ensures that within // the negative range, more-negative values sort earlier. func TestEncodeNumericKey_NegativeMagnitudeOrder(t *testing.T) { neg200 := encodeNumericKey(-200) neg100 := encodeNumericKey(-100) neg1 := encodeNumericKey(-1) if bytes.Compare(neg200, neg100) >= 0 { t.Errorf("-200 should sort before -100: %q vs %q", neg200, neg100) } if bytes.Compare(neg100, neg1) >= 0 { t.Errorf("-100 should sort before -1: %q vs %q", neg100, neg1) } }