-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathbinary_ops.rs
142 lines (122 loc) · 7.35 KB
/
binary_ops.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use rhai::{Engine, INT};
#[test]
fn test_binary_ops() {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("10 + 4").unwrap(), 14);
assert_eq!(engine.eval::<INT>("10 - 4").unwrap(), 6);
assert_eq!(engine.eval::<INT>("10 * 4").unwrap(), 40);
assert_eq!(engine.eval::<INT>("10 / 4").unwrap(), 2);
assert_eq!(engine.eval::<INT>("10 % 4").unwrap(), 2);
assert_eq!(engine.eval::<INT>("10 ** 4").unwrap(), 10000);
assert_eq!(engine.eval::<INT>("10 << 4").unwrap(), 160);
assert_eq!(engine.eval::<INT>("10 >> 4").unwrap(), 0);
assert_eq!(engine.eval::<INT>("10 & 4").unwrap(), 0);
assert_eq!(engine.eval::<INT>("10 | 4").unwrap(), 14);
assert_eq!(engine.eval::<INT>("10 ^ 4").unwrap(), 14);
assert!(engine.eval::<bool>("42 == 42").unwrap());
assert!(!engine.eval::<bool>("42 != 42").unwrap());
assert!(!engine.eval::<bool>("42 > 42").unwrap());
assert!(engine.eval::<bool>("42 >= 42").unwrap());
assert!(!engine.eval::<bool>("42 < 42").unwrap());
assert!(engine.eval::<bool>("42 <= 42").unwrap());
assert_eq!(engine.eval::<INT>("let x = 10; x += 4; x").unwrap(), 14);
assert_eq!(engine.eval::<INT>("let x = 10; x -= 4; x").unwrap(), 6);
assert_eq!(engine.eval::<INT>("let x = 10; x *= 4; x").unwrap(), 40);
assert_eq!(engine.eval::<INT>("let x = 10; x /= 4; x").unwrap(), 2);
assert_eq!(engine.eval::<INT>("let x = 10; x %= 4; x").unwrap(), 2);
assert_eq!(engine.eval::<INT>("let x = 10; x **= 4; x").unwrap(), 10000);
assert_eq!(engine.eval::<INT>("let x = 10; x <<= 4; x").unwrap(), 160);
assert_eq!(engine.eval::<INT>("let x = 10; x <<= -1; x").unwrap(), 5);
assert_eq!(engine.eval::<INT>("let x = 10; x >>= 4; x").unwrap(), 0);
assert_eq!(engine.eval::<INT>("let x = 10; x >>= -2; x").unwrap(), 40);
assert_eq!(engine.eval::<INT>("let x = 10; x &= 4; x").unwrap(), 0);
assert_eq!(engine.eval::<INT>("let x = 10; x |= 4; x").unwrap(), 14);
assert_eq!(engine.eval::<INT>("let x = 10; x ^= 4; x").unwrap(), 14);
#[cfg(not(feature = "no_float"))]
{
use rhai::FLOAT;
assert_eq!(engine.eval::<FLOAT>("10.0 + 4.0").unwrap(), 14.0);
assert_eq!(engine.eval::<FLOAT>("10.0 - 4.0").unwrap(), 6.0);
assert_eq!(engine.eval::<FLOAT>("10.0 * 4.0").unwrap(), 40.0);
assert_eq!(engine.eval::<FLOAT>("10.0 / 4.0").unwrap(), 2.5);
assert_eq!(engine.eval::<FLOAT>("10.0 % 4.0").unwrap(), 2.0);
assert_eq!(engine.eval::<FLOAT>("10.0 ** 4.0").unwrap(), 10000.0);
assert_eq!(engine.eval::<FLOAT>("10.0 + 4").unwrap(), 14.0);
assert_eq!(engine.eval::<FLOAT>("10.0 - 4").unwrap(), 6.0);
assert_eq!(engine.eval::<FLOAT>("10.0 * 4").unwrap(), 40.0);
assert_eq!(engine.eval::<FLOAT>("10.0 / 4").unwrap(), 2.5);
assert_eq!(engine.eval::<FLOAT>("10.0 % 4").unwrap(), 2.0);
assert_eq!(engine.eval::<FLOAT>("10.0 ** 4").unwrap(), 10000.0);
assert_eq!(engine.eval::<FLOAT>("10 + 4.0").unwrap(), 14.0);
assert_eq!(engine.eval::<FLOAT>("10 - 4.0").unwrap(), 6.0);
assert_eq!(engine.eval::<FLOAT>("10 * 4.0").unwrap(), 40.0);
assert_eq!(engine.eval::<FLOAT>("10 / 4.0").unwrap(), 2.5);
assert_eq!(engine.eval::<FLOAT>("10 % 4.0").unwrap(), 2.0);
assert_eq!(engine.eval::<FLOAT>("10 ** 4.0").unwrap(), 10000.0);
assert!(engine.eval::<bool>("42 == 42.0").unwrap());
assert!(!engine.eval::<bool>("42 != 42.0").unwrap());
assert!(!engine.eval::<bool>("42 > 42.0").unwrap());
assert!(engine.eval::<bool>("42 >= 42.0").unwrap());
assert!(!engine.eval::<bool>("42 < 42.0").unwrap());
assert!(engine.eval::<bool>("42 <= 42.0").unwrap());
assert!(engine.eval::<bool>("42.0 == 42").unwrap());
assert!(!engine.eval::<bool>("42.0 != 42").unwrap());
assert!(!engine.eval::<bool>("42.0 > 42").unwrap());
assert!(engine.eval::<bool>("42.0 >= 42").unwrap());
assert!(!engine.eval::<bool>("42.0 < 42").unwrap());
assert!(engine.eval::<bool>("42.0 <= 42").unwrap());
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x += 4.0; x").unwrap(), 14.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x -= 4.0; x").unwrap(), 6.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x *= 4.0; x").unwrap(), 40.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x /= 4.0; x").unwrap(), 2.5);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x %= 4.0; x").unwrap(), 2.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x **= 4.0; x").unwrap(), 10000.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x += 4; x").unwrap(), 14.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x -= 4; x").unwrap(), 6.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x *= 4; x").unwrap(), 40.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x /= 4; x").unwrap(), 2.5);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x %= 4; x").unwrap(), 2.0);
assert_eq!(engine.eval::<FLOAT>("let x = 10.0; x **= 4; x").unwrap(), 10000.0);
}
assert_eq!(engine.eval::<String>(r#""hello" + ", world""#).unwrap(), "hello, world");
assert_eq!(engine.eval::<String>(r#""hello" + '!'"#).unwrap(), "hello!");
assert_eq!(engine.eval::<String>(r#""hello" - "el""#).unwrap(), "hlo");
assert_eq!(engine.eval::<String>(r#""hello" - 'l'"#).unwrap(), "heo");
assert!(!engine.eval::<bool>(r#""a" == "x""#).unwrap());
assert!(engine.eval::<bool>(r#""a" != "x""#).unwrap());
assert!(!engine.eval::<bool>(r#""a" > "x""#).unwrap());
assert!(!engine.eval::<bool>(r#""a" >= "x""#).unwrap());
assert!(engine.eval::<bool>(r#""a" < "x""#).unwrap());
assert!(engine.eval::<bool>(r#""a" <= "x""#).unwrap());
assert!(engine.eval::<bool>(r#""x" == 'x'"#).unwrap());
assert!(!engine.eval::<bool>(r#""x" != 'x'"#).unwrap());
assert!(!engine.eval::<bool>(r#""x" > 'x'"#).unwrap());
assert!(engine.eval::<bool>(r#""x" >= 'x'"#).unwrap());
assert!(!engine.eval::<bool>(r#""x" < 'x'"#).unwrap());
assert!(engine.eval::<bool>(r#""x" <= 'x'"#).unwrap());
assert!(engine.eval::<bool>(r#"'x' == "x""#).unwrap());
assert!(!engine.eval::<bool>(r#"'x' != "x""#).unwrap());
assert!(!engine.eval::<bool>(r#"'x' > "x""#).unwrap());
assert!(engine.eval::<bool>(r#"'x' >= "x""#).unwrap());
assert!(!engine.eval::<bool>(r#"'x' < "x""#).unwrap());
assert!(engine.eval::<bool>(r#"'x' <= "x""#).unwrap());
// Incompatible types compare to false
assert!(!engine.eval::<bool>("true == 42").unwrap());
assert!(engine.eval::<bool>("true != 42").unwrap());
assert!(!engine.eval::<bool>("true > 42").unwrap());
assert!(!engine.eval::<bool>("true >= 42").unwrap());
assert!(!engine.eval::<bool>("true < 42").unwrap());
assert!(!engine.eval::<bool>("true <= 42").unwrap());
assert!(!engine.eval::<bool>(r#""42" == 42"#).unwrap());
assert!(engine.eval::<bool>(r#""42" != 42"#).unwrap());
assert!(!engine.eval::<bool>(r#""42" > 42"#).unwrap());
assert!(!engine.eval::<bool>(r#""42" >= 42"#).unwrap());
assert!(!engine.eval::<bool>(r#""42" < 42"#).unwrap());
assert!(!engine.eval::<bool>(r#""42" <= 42"#).unwrap());
}
#[test]
fn test_binary_ops_null_coalesce() {
let engine = Engine::new();
assert_eq!(engine.eval::<INT>("let x = 42; x ?? 123").unwrap(), 42);
assert_eq!(engine.eval::<INT>("let x = (); x ?? 123").unwrap(), 123);
}