Properly handle whitespace for Infinity and NaN, r=rnewman (#246)
Signed-off-by: Victor Porof <vporof@mozilla.com>
This commit is contained in:
parent
7db74953d6
commit
42580539b8
3 changed files with 22 additions and 9 deletions
|
@ -32,10 +32,10 @@ pub nil -> Value =
|
|||
"nil" { Value::Nil }
|
||||
|
||||
pub nan -> Value =
|
||||
"#f" __ "NaN" { Value::Float(OrderedFloat(NAN)) }
|
||||
"#f" whitespace+ "NaN" { Value::Float(OrderedFloat(NAN)) }
|
||||
|
||||
pub infinity -> Value =
|
||||
"#f" __ s:$(sign) "Infinity" {
|
||||
"#f" whitespace+ s:$(sign) "Infinity" {
|
||||
Value::Float(OrderedFloat(if s == "+" { INFINITY } else { NEG_INFINITY }))
|
||||
}
|
||||
|
||||
|
|
|
@ -55,14 +55,14 @@ impl Display for Value {
|
|||
Boolean(v) => write!(f, "{}", v),
|
||||
Integer(v) => write!(f, "{}", v),
|
||||
BigInteger(ref v) => write!(f, "{}N", v),
|
||||
Float(ref v) => {
|
||||
// TODO: make sure float syntax is correct.
|
||||
Float(ref v) => {
|
||||
if *v == OrderedFloat(f64::INFINITY) {
|
||||
write!(f, "#f {}", "+Infinity")
|
||||
write!(f, "#f +Infinity")
|
||||
} else if *v == OrderedFloat(f64::NEG_INFINITY) {
|
||||
write!(f, "#f {}", "-Infinity")
|
||||
write!(f, "#f -Infinity")
|
||||
} else if *v == OrderedFloat(f64::NAN) {
|
||||
write!(f, "#f {}", "NaN")
|
||||
write!(f, "#f NaN")
|
||||
} else {
|
||||
write!(f, "{}", v)
|
||||
}
|
||||
|
|
|
@ -53,20 +53,33 @@ fn test_nil() {
|
|||
|
||||
#[test]
|
||||
fn test_nan() {
|
||||
assert!(nan("#fNaN").is_err());
|
||||
assert!(nan("#f;x\nNaN").is_err());
|
||||
|
||||
assert_eq!(nan("#f NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
|
||||
assert_eq!(nan("#f NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
|
||||
assert_eq!(nan("#f\t NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
|
||||
assert_eq!(nan("#f,NaN").unwrap(), Float(OrderedFloat(f64::NAN)));
|
||||
|
||||
assert!(nan("true").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_infinity() {
|
||||
assert_eq!(infinity("#f-Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
|
||||
assert_eq!(infinity("#f+Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
|
||||
assert!(infinity("#f-Infinity").is_err());
|
||||
assert!(infinity("#f+Infinity").is_err());
|
||||
|
||||
assert!(infinity("#f;x\n-Infinity").is_err());
|
||||
assert!(infinity("#f;x\n+Infinity").is_err());
|
||||
|
||||
assert_eq!(infinity("#f -Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
|
||||
assert_eq!(infinity("#f +Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
|
||||
|
||||
assert_eq!(infinity("#f\t -Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
|
||||
assert_eq!(infinity("#f\t +Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
|
||||
|
||||
assert_eq!(infinity("#f,-Infinity").unwrap(), Float(OrderedFloat(f64::NEG_INFINITY)));
|
||||
assert_eq!(infinity("#f,+Infinity").unwrap(), Float(OrderedFloat(f64::INFINITY)));
|
||||
|
||||
assert!(infinity("true").is_err());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue