]> git.lizzy.rs Git - rust.git/blob - tests/consts.rs
Fix deploy.sh III
[rust.git] / tests / consts.rs
1 #![allow(plugin_as_library)]
2 #![feature(rustc_private)]
3
4 extern crate clippy;
5 extern crate rustc;
6 extern crate rustc_const_eval;
7 extern crate rustc_const_math;
8 extern crate syntax;
9
10 use clippy::consts::{constant_simple, Constant, FloatWidth};
11 use rustc_const_math::ConstInt;
12 use rustc::hir::*;
13 use syntax::ast::{LitIntType, LitKind, StrStyle};
14 use syntax::codemap::{Spanned, COMMAND_LINE_SP};
15 use syntax::parse::token::InternedString;
16 use syntax::ptr::P;
17 use syntax::util::ThinVec;
18
19 fn spanned<T>(t: T) -> Spanned<T> {
20     Spanned {
21         node: t,
22         span: COMMAND_LINE_SP,
23     }
24 }
25
26 fn expr(n: Expr_) -> Expr {
27     Expr {
28         id: 1,
29         node: n,
30         span: COMMAND_LINE_SP,
31         attrs: ThinVec::new(),
32     }
33 }
34
35 fn lit(l: LitKind) -> Expr {
36     expr(ExprLit(P(spanned(l))))
37 }
38
39 fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr {
40     expr(ExprBinary(spanned(op), P(l), P(r)))
41 }
42
43 fn check(expect: Constant, expr: &Expr) {
44     assert_eq!(Some(expect), constant_simple(expr))
45 }
46
47 const TRUE: Constant = Constant::Bool(true);
48 const FALSE: Constant = Constant::Bool(false);
49 const ZERO: Constant = Constant::Int(ConstInt::Infer(0));
50 const ONE: Constant = Constant::Int(ConstInt::Infer(1));
51 const TWO: Constant = Constant::Int(ConstInt::Infer(2));
52
53 #[test]
54 fn test_lit() {
55     check(TRUE, &lit(LitKind::Bool(true)));
56     check(FALSE, &lit(LitKind::Bool(false)));
57     check(ZERO, &lit(LitKind::Int(0, LitIntType::Unsuffixed)));
58     check(Constant::Str("cool!".into(), StrStyle::Cooked),
59           &lit(LitKind::Str(InternedString::new("cool!"), StrStyle::Cooked)));
60 }
61
62 #[test]
63 fn test_ops() {
64     check(TRUE, &binop(BiOr, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
65     check(FALSE, &binop(BiAnd, lit(LitKind::Bool(false)), lit(LitKind::Bool(true))));
66
67     let litzero = lit(LitKind::Int(0, LitIntType::Unsuffixed));
68     let litone = lit(LitKind::Int(1, LitIntType::Unsuffixed));
69     check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone()));
70     check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone()));
71     check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone()));
72     check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone()));
73     check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone()));
74     check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone()));
75
76     check(ZERO, &binop(BiAdd, litzero.clone(), litzero.clone()));
77     check(TWO, &binop(BiAdd, litone.clone(), litone.clone()));
78     check(ONE, &binop(BiSub, litone.clone(), litzero.clone()));
79     check(ONE, &binop(BiMul, litone.clone(), litone.clone()));
80     check(ONE, &binop(BiDiv, litone.clone(), litone.clone()));
81
82     let half_any = Constant::Float("0.5".into(), FloatWidth::Any);
83     let half32 = Constant::Float("0.5".into(), FloatWidth::F32);
84     let half64 = Constant::Float("0.5".into(), FloatWidth::F64);
85     let pos_zero = Constant::Float("0.0".into(), FloatWidth::F64);
86     let neg_zero = Constant::Float("-0.0".into(), FloatWidth::F64);
87
88     assert_eq!(pos_zero, pos_zero);
89     assert_eq!(neg_zero, neg_zero);
90     assert_eq!(None, pos_zero.partial_cmp(&neg_zero));
91
92     assert_eq!(half_any, half32);
93     assert_eq!(half_any, half64);
94     assert_eq!(half32, half64); // for transitivity
95
96     assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::U8(0)));
97     assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::I8(0)));
98     assert_eq!(Constant::Int(ConstInt::InferSigned(-1)), Constant::Int(ConstInt::I8(-1)));
99 }