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