1
// examples from the book Starting Forth..
2
//
3

            
4
#[cfg(test)]
5
mod tests {
6
    //use super::*;
7
    use forth::forth_compiler::ForthCompiler;
8
    use forth::stack_machine::GasLimit;
9
    //  use forth::ForthError;
10

            
11
    /*
12
      macro_rules! clean_stack (
13
          {$fc: ident, $expr: expr, $result: expr} => {
14
              $fc.sm.st.data_stack.clear();
15
              $fc.execute_string($expr, GasLimit::Limited(100)).unwrap();
16
              assert_eq!($fc.sm.st.data_stack, $result);
17
          }
18
      );
19
    */
20
    macro_rules! output (
21
        {$fc: ident, $expr: expr, $result: expr} => {
22
            assert_eq!($fc.execute_string($expr, GasLimit::Limited(400)).unwrap(), $result);
23
        }
24
    );
25

            
26
    /*
27
    macro_rules! error (
28
        {$fc: ident, $expr: expr, $result: expr} => {
29
            $fc.sm.st.data_stack.clear();
30
            assert_eq!($fc.execute_string($expr, GasLimit::Limited(100)).unwrap_err(), $result);
31
        }
32
    );
33
    */
34

            
35
    macro_rules! run (
36
        {$fc: ident, $expr: expr} => {
37
            $fc.execute_string($expr, GasLimit::Limited(100)).unwrap()
38
        }
39
    );
40

            
41
    #[test]
42
2
    fn chapter5() {
43
1
        let mut fc = ForthCompiler::new();
44

            
45
        // page 117
46
1
        output!(fc, r"2000 34 100 */ .", "680 ");
47

            
48
        // page 118
49
1
        output!(fc, "227 32 10 */ .", "726 ");
50

            
51
1
        run!(fc, ": R% 10 */  5 +  10 / ;");
52
1
        output!(fc, "227 32 R% .", "73 ");
53

            
54
        // page 119
55
1
        output!(fc, "171 2 3 */ .", "114 ");
56

            
57
        // page 122
58
1
        run!(fc, ": PI DUP * 31416 10000 */ ;");
59
1
        output!(fc, "10 PI .", "314 ");
60

            
61
1
        run!(fc, ": PI DUP * 355 113 */ ;");
62
1
        output!(fc, "10 PI .", "314 ");
63
2
    }
64

            
65
    #[test]
66
2
    fn chapter6() {
67
1
        let mut fc = ForthCompiler::new();
68

            
69
        // page 127
70

            
71
1
        run!(fc, r#": test 10 0 do cr ." Hello" loop ; "#);
72
1
        output!(
73
            fc,
74
            "test",
75
            "\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello"
76
        );
77

            
78
        // page 128
79
1
        run!(fc, r#": test 3 0 do cr ." I'm going loopy! " loop ;"#);
80
1
        output!(
81
            fc,
82
            "test",
83
            "\nI'm going loopy! \nI'm going loopy! \nI'm going loopy! "
84
        );
85

            
86
        // page 130
87
1
        run!(fc, r#": decade 10 0 do I . loop ;"#);
88
1
        output!(fc, "decade", "0 1 2 3 4 5 6 7 8 9 ");
89

            
90
1
        run!(fc, r#": sample -243 -250 do I . loop ; "#);
91
1
        output!(fc, "sample", "-250 -249 -248 -247 -246 -245 -244 ");
92

            
93
1
        run!(fc, r#": mult CR 11 1 DO DUP I * . LOOP DROP ;"#);
94
1
        output!(fc, "7 mult", "\n7 14 21 28 35 42 49 56 63 70 ");
95

            
96
        // page 131
97

            
98
        //run!(fc, ": 2/ 1 RSHIFT ;");
99
        //run!(fc, ": R% 50 / 1+ 2/ ;");
100
1
        run!(fc, ": R% 10 */  5 + 10 / ;");
101

            
102
1
        run!(
103
            fc,
104
            r#"
105
                : compound ( amt int -- )
106
                    swap 4 1 do ." YEAR " I . 3 spaces
107
                    2dup R% + DUP ." BALANCE " . CR LOOP 2DROP ;"#
108
        );
109
1
        output!(
110
            fc,
111
            "1000 6 compound",
112
            "YEAR 1    BALANCE 1060 \nYEAR 2    BALANCE 1124 \nYEAR 3    BALANCE 1191 \n"
113
        );
114
2
    }
115
}