From 47f5f127592cc1befe944bde36d744330e9722f7 Mon Sep 17 00:00:00 2001 From: xplorld Date: Mon, 3 Sep 2018 22:28:18 -0700 Subject: [PATCH] sort: treat "NaN" as string in numeric sort --- src/sort/sort.rs | 2 ++ tests/fixtures/sort/numeric_floats_with_nan.expected | 3 +++ tests/fixtures/sort/numeric_floats_with_nan.txt | 3 +++ tests/test_sort.rs | 5 +++++ 4 files changed, 13 insertions(+) create mode 100644 tests/fixtures/sort/numeric_floats_with_nan.expected create mode 100644 tests/fixtures/sort/numeric_floats_with_nan.txt diff --git a/src/sort/sort.rs b/src/sort/sort.rs index 9c25af938..8f90cb9b1 100644 --- a/src/sort/sort.rs +++ b/src/sort/sort.rs @@ -382,8 +382,10 @@ fn permissive_f64_parse(a: &str) -> f64 { // On the flip side, this will give NEG_INFINITY for "1,234", which might be OK // because there's no way to handle both CSV and thousands separators without a new flag. // GNU sort treats "1,234" as "1" in numeric, so maybe it's fine. + // GNU sort treats "NaN" as non-number in numeric, so it needs special care. let sa: &str = a.split_whitespace().next().unwrap(); match sa.parse::() { + Ok(a) if a.is_nan() => std::f64::NEG_INFINITY, Ok(a) => a, Err(_) => std::f64::NEG_INFINITY, } diff --git a/tests/fixtures/sort/numeric_floats_with_nan.expected b/tests/fixtures/sort/numeric_floats_with_nan.expected new file mode 100644 index 000000000..d4bdc6bd9 --- /dev/null +++ b/tests/fixtures/sort/numeric_floats_with_nan.expected @@ -0,0 +1,3 @@ +NaN +.02 +.03 \ No newline at end of file diff --git a/tests/fixtures/sort/numeric_floats_with_nan.txt b/tests/fixtures/sort/numeric_floats_with_nan.txt new file mode 100644 index 000000000..d40d01c62 --- /dev/null +++ b/tests/fixtures/sort/numeric_floats_with_nan.txt @@ -0,0 +1,3 @@ +.03 +.02 +NaN \ No newline at end of file diff --git a/tests/test_sort.rs b/tests/test_sort.rs index 32ca1b6d3..d68e384d1 100644 --- a/tests/test_sort.rs +++ b/tests/test_sort.rs @@ -12,6 +12,11 @@ fn test_numeric_floats() { test_helper("numeric_floats", "-n"); } +#[test] +fn test_numeric_floats_with_nan() { + test_helper("numeric_floats_with_nan", "-n"); +} + #[test] fn test_numeric_unfixed_floats() { test_helper("numeric_unfixed_floats", "-n");