From a2ee637be61ef1848e2f5c47a6d3a202057e992f Mon Sep 17 00:00:00 2001 From: llogiq Date: Mon, 17 Aug 2015 16:24:57 +0200 Subject: [PATCH] added test and fixed negativity check in Partial{Eq, Ord} impl --- src/consts.rs | 5 +++-- tests/consts.rs | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/consts.rs b/src/consts.rs index 01fb3520f2c..0f212e3b8ef 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -109,7 +109,7 @@ impl PartialEq for ConstantVariant { (&ConstantByte(l), &ConstantByte(r)) => l == r, (&ConstantChar(l), &ConstantChar(r)) => l == r, (&ConstantInt(lv, lty), &ConstantInt(rv, rty)) => lv == rv && - is_negative(lty) == is_negative(rty), + (is_negative(lty) & (lv != 0)) == (is_negative(rty) & (rv != 0)), (&ConstantFloat(ref ls, lw), &ConstantFloat(ref rs, rw)) => if match (lw, rw) { (FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true, @@ -138,7 +138,8 @@ impl PartialOrd for ConstantVariant { (&ConstantByte(ref l), &ConstantByte(ref r)) => Some(l.cmp(r)), (&ConstantChar(ref l), &ConstantChar(ref r)) => Some(l.cmp(r)), (&ConstantInt(ref lv, lty), &ConstantInt(ref rv, rty)) => - Some(match (is_negative(lty), is_negative(rty)) { + Some(match (is_negative(lty) && *lv != 0, + is_negative(rty) && *rv != 0) { (true, true) => lv.cmp(rv), (false, false) => rv.cmp(lv), (true, false) => Greater, diff --git a/tests/consts.rs b/tests/consts.rs index a7d84ee0ae9..3b05dd67ad5 100644 --- a/tests/consts.rs +++ b/tests/consts.rs @@ -21,27 +21,53 @@ fn ctx() -> &'static Context<'static, 'static> { } } -fn lit(l: Lit_) -> Expr { +fn spanned(t: T) -> Spanned { + Spanned{ node: t, span: COMMAND_LINE_SP } +} + +fn expr(n: Expr_) -> Expr { Expr{ id: 1, - node: ExprLit(P(Spanned{ - node: l, - span: COMMAND_LINE_SP, - })), + node: n, span: COMMAND_LINE_SP, } } +fn lit(l: Lit_) -> Expr { + expr(ExprLit(P(spanned(l)))) +} + +fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr { + expr(ExprBinary(spanned(op), P(l), P(r))) +} + fn check(expect: ConstantVariant, expr: &Expr) { assert_eq!(Some(expect), constant(ctx(), expr).map(|x| x.constant)) } +const TRUE : ConstantVariant = ConstantBool(true); +const FALSE : ConstantVariant = ConstantBool(false); +const ZERO : ConstantVariant = ConstantInt(0, UnsuffixedIntLit(Plus)); + #[test] fn test_lit() { - check(ConstantBool(true), &lit(LitBool(true))); - check(ConstantBool(false), &lit(LitBool(false))); - check(ConstantInt(0, UnsuffixedIntLit(Plus)), - &lit(LitInt(0, UnsuffixedIntLit(Plus)))); + check(TRUE, &lit(LitBool(true))); + check(FALSE, &lit(LitBool(false))); + check(ZERO, &lit(LitInt(0, UnsuffixedIntLit(Plus)))); check(ConstantStr("cool!".into(), CookedStr), &lit(LitStr( InternedString::new("cool!"), CookedStr))); } + +#[test] +fn test_ops() { + check(TRUE, &binop(BiOr, lit(LitBool(false)), lit(LitBool(true)))); + check(FALSE, &binop(BiAnd, lit(LitBool(false)), lit(LitBool(true)))); + + let litzero = lit(LitInt(0, UnsuffixedIntLit(Plus))); + check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone())); + check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone())); + check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone())); + check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone())); + check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone())); + check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone())); +}