mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-24 03:06:48 +01:00
2e12119a9e
Natural sorting is needed for sort_by_label_natural() and sort_by_label_natural_desc()
functions in MetricsQL - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6192
and https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6256
Natural sorting will be also used by `| sort ...` pipe in VictoriaLogs -
see https://docs.victoriametrics.com/victorialogs/logsql/#sort-pipe
(cherry picked from commit 707f3a69db
)
91 lines
2.5 KiB
Go
91 lines
2.5 KiB
Go
package stringsutil
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestLessNatural(t *testing.T) {
|
|
f := func(a, b string, resultExpected bool) {
|
|
t.Helper()
|
|
|
|
result := LessNatural(a, b)
|
|
if result != resultExpected {
|
|
t.Fatalf("unexpected result for LessNatural(%q, %q); got %v; want %v", a, b, result, resultExpected)
|
|
}
|
|
}
|
|
|
|
// comparison with empty string
|
|
f("", "", false)
|
|
f("", "foo", true)
|
|
f("foo", "", false)
|
|
f("", "123", true)
|
|
f("123", "", false)
|
|
|
|
// identical values
|
|
f("foo", "foo", false)
|
|
f("123", "123", false)
|
|
f("foo123", "foo123", false)
|
|
f("123foo", "123foo", false)
|
|
f("000", "000", false)
|
|
f("00123", "00123", false)
|
|
f("00foo", "00foo", false)
|
|
f("abc00foo0123", "abc00foo0123", false)
|
|
|
|
// identical values with different number of zeroes in front of them
|
|
f("00123", "0123", false)
|
|
f("0123", "00123", true)
|
|
|
|
// numeric comparsion
|
|
f("123", "99", false)
|
|
f("99", "123", true)
|
|
|
|
// floating-point comparsion (works unexpectedly - this is OK for natural sort order)
|
|
f("1.23", "1.123", true)
|
|
f("1.123", "1.23", false)
|
|
|
|
// non-numeric comparison
|
|
f("foo", "bar", false)
|
|
f("fo", "bar", false)
|
|
f("bar", "foo", true)
|
|
f("bar", "fo", true)
|
|
|
|
// comparison with common non-numeric prefix
|
|
f("abc_foo", "abc_bar", false)
|
|
f("abc_bar", "abc_foo", true)
|
|
f("abc_foo", "abc_", false)
|
|
f("abc_", "abc_foo", true)
|
|
f("abc_123", "abc_foo", true)
|
|
f("abc_foo", "abc_123", false)
|
|
|
|
// comparison with common numeric prefix
|
|
f("123foo", "123bar", false)
|
|
f("123bar", "123foo", true)
|
|
f("123", "123bar", true)
|
|
f("123bar", "123", false)
|
|
f("123_456", "123_78", false)
|
|
f("123_78", "123_456", true)
|
|
|
|
// too big integers - fall back to string order
|
|
f("1234567890123456789012345", "1234567890123456789012345", false)
|
|
f("1234567890123456789012345", "123456789012345678901234", false)
|
|
f("123456789012345678901234", "1234567890123456789012345", true)
|
|
f("193456789012345678901234", "1234567890123456789012345", false)
|
|
f("123456789012345678901234", "1934567890123456789012345", true)
|
|
f("1934", "1234567890123456789012345", false)
|
|
f("1234567890123456789012345", "1934", true)
|
|
|
|
// integers with many zeroes in front
|
|
f("00000000000000000000000000123", "0000000000000000000000000045", false)
|
|
f("0000000000000000000000000045", "00000000000000000000000000123", true)
|
|
|
|
// unicode strings
|
|
f("бвг", "мирг", true)
|
|
f("мирг", "бвг", false)
|
|
f("abcde", "мирг", true)
|
|
f("мирг", "abcde", false)
|
|
f("123", "мирг", true)
|
|
f("мирг", "123", false)
|
|
f("12345", "мирг", true)
|
|
f("мирг", "12345", false)
|
|
}
|