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
|
use super::super::lib::types::Op::*;
use super::super::lib::types::Type::*;
use super::super::lib::types::Number;
use super::super::lib::parse::{is_paren, is_op, is_bool, is_var, is_number};
use super::super::lib::parse::Token::*;
use super::super::lib::parse::TokenStream;
#[test]
pub fn token_test() {
let add_string = "(+ 1 1)".to_string();
let add_tokens = vec!(LParen, Value(Operator(Add)), Value(Number(Number::Int(1))),
Value(Number(Number::Int(1))), RParen);
let tokenstream = TokenStream::new(add_string,
vec!(is_paren, is_op, is_bool, is_var, is_number));
let mut tokens = Vec::new();
for token in tokenstream {
match token {
Ok(x) => tokens.push(x),
Err(f) => panic!(f)
}
}
assert!(tokens.len() == add_tokens.len());
for (token1, token2) in tokens.iter().zip(add_tokens) {
assert!(token1 == &token2);
}
}
#[test]
pub fn types_test() {
let foo = "(= (> 3.0 0) true)".to_string();
let tokenstream = TokenStream::new(foo,
vec!(is_paren, is_op, is_bool, is_var, is_number));
let correct_tokens = vec!(LParen, Value(Operator(Equals)), LParen,
Value(Operator(Greater)),
Value(Number(Number::Float(3.0))),
Value(Number(Number::Int(0))),
RParen, Value(Bool(true)), RParen);
for (foo_token, correct_token) in tokenstream.zip(correct_tokens.into_iter()) {
match foo_token {
Ok(x) => assert!(x == correct_token),
Err(f) => panic!(f)
}
}
}
#[test]
pub fn string_test() {
let bar = "(define foo \t \n \t \"Bar \\\" Baz\")";
let tokenstream = TokenStream::default(bar);
let correct_tokens = vec!(LParen, Value(Operator(Define)), Value(Symbol("foo".to_string())),
Value(Str("Bar \\\" Baz".to_string())), RParen);
for (bar_token, correct_token) in tokenstream.zip(correct_tokens.into_iter()) {
match bar_token {
Ok(x) => {
if x != correct_token {
panic!(format!("{:?} != {:?}", x, correct_token))
}
},
Err(f) => panic!(f)
}
}
}
|