added test and fixed negativity check in Partial{Eq, Ord} impl

This commit is contained in:
llogiq 2015-08-17 16:24:57 +02:00
parent 59d0ae8739
commit a2ee637be6
2 changed files with 38 additions and 11 deletions

View File

@ -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,

View File

@ -21,27 +21,53 @@ fn ctx() -> &'static Context<'static, 'static> {
}
}
fn lit(l: Lit_) -> Expr {
fn spanned<T>(t: T) -> Spanned<T> {
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()));
}