summaryrefslogtreecommitdiff
path: root/src/lib/sexpr.rs
blob: a6fbe493b6cf346fbc94a16bc2c5cb8ede514cf5 (plain)
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
use super::types::Type;

#[derive(PartialEq, Debug)]
pub enum SExpr {
    Atom(Type),
    Sexpr(Vec<SExpr>)
}

#[test]
fn construct() {
    let atom1 = SExpr::Atom(Type::Number(Number::Int(1)));
    let atom2 = SExpr::Atom(Type::Number(Number::Int(2)));
    let atom3 = SExpr::Atom(Type::Number(Number::Int(3)));
    let atom4 = SExpr::Atom(Type::Number(Number::Int(4)));
    let sexp = SExpr::Sexpr(vec!(atom1, atom2, atom3, atom4));
    match sexp {
        SExpr::Sexpr(ref x) => {
            assert_eq!(x[0], SExpr::Atom(Type::Number(Number::Int(1))));
        },
        _ => panic!("What")
    }
}

#[test]
fn mutability() {
    let atom1 = SExpr::Atom(Type::Number(Number::Int(1)));
    let atom2 = SExpr::Atom(Type::Number(Number::Int(2)));
    let atom3 = SExpr::Atom(Type::Number(Number::Int(3)));
    let atom4 = SExpr::Atom(Type::Number(Number::Int(4)));
    let mut sexp = SExpr::Sexpr(vec!(atom1, atom2, atom3, atom4));
    match sexp {
        SExpr::Sexpr(ref mut x) => match x[0] {
            SExpr::Atom(Type::Number(Number::Int(ref mut x))) => *x += 7,
            _ => panic!("What")
        },
        _ => panic!("What")
    }

    match sexp {
        SExpr::Sexpr(ref x) => {
            assert_eq!(x[0], SExpr::Atom(Type::Number(Number::Int(8))));
        },
        _ => panic!("What")
    }
}