-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.rs
128 lines (110 loc) · 2.61 KB
/
test.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
use super::{Executor, Mode};
#[test]
fn calculate() {
let mut executor = Executor::new(Mode::Script);
assert_eq!(
{
executor.evaluate_program("5 8 add".to_string());
executor.pop_stack().get_number()
},
13f64
);
assert_eq!(
{
executor.evaluate_program("8 3 sub".to_string());
executor.pop_stack().get_number()
},
5f64
);
assert_eq!(
{
executor.evaluate_program("5 8 mul".to_string());
executor.pop_stack().get_number()
},
40f64
);
assert_eq!(
{
executor.evaluate_program("10 5 div".to_string());
executor.pop_stack().get_number()
},
2f64
);
assert_eq!(
{
executor.evaluate_program("3 2 pow".to_string());
executor.pop_stack().get_number()
},
9f64
);
}
#[test]
fn variables() {
let mut executor = Executor::new(Mode::Script);
assert_eq!(
{
executor.evaluate_program("5987 (x) var x".to_string());
executor.pop_stack().get_number()
},
5987f64
);
assert_eq!(
{
executor.evaluate_program("5987 (x) var x 1 add (x) var x".to_string());
executor.pop_stack().get_number()
},
5988f64
);
}
#[test]
fn control_if() {
let mut executor = Executor::new(Mode::Script);
assert_eq!(
{
executor.evaluate_program("(true) (false) 10 2 div 5 equal if".to_string());
executor.pop_stack().get_bool()
},
true
);
assert_eq!(
{
executor.evaluate_program("(true) (false) 10 2 div 4 equal if".to_string());
executor.pop_stack().get_bool()
},
false
);
}
#[test]
fn control_while() {
let mut executor = Executor::new(Mode::Script);
assert_eq!(
{
executor
.evaluate_program("5 (i) var (i 1 add (i) var) (i 10 less) while i".to_string());
executor.pop_stack().get_number()
},
10f64
);
}
#[test]
fn equal_true() {
let mut executor = Executor::new(Mode::Script);
assert_eq!(
{
executor.evaluate_program("1 1 add 2 equal".to_string());
executor.pop_stack().get_bool()
},
true
);
}
#[test]
fn equal_false() {
let mut executor = Executor::new(Mode::Script);
assert_eq!(
{
executor.evaluate_program("1 1 mul 999 equal".to_string());
executor.pop_stack().get_bool()
},
false
);
}