1
exercism fetch csharp forth

ForthTest.cs

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
using Xunit;

public class ForthTest
{
    [Fact]
    public void No_input()
    {
        Assert.Equal("", Forth.Eval(""));
    }

    [Fact(Skip = "Remove to run test")]
    public void Numbers_just_get_pushed_onto_the_stack()
    {
        Assert.Equal("1 2 3 4 5", Forth.Eval("1 2 3 4 5"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Non_word_characters_are_separators()
    {
        Assert.Equal("1 2 3 4 5 6 7", Forth.Eval("1\v2\t3\n4\r5 6\t7"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Basic_arithmetic()
    {
        Assert.Equal("-1", Forth.Eval("1 2 + 4 -"));
        Assert.Equal("2", Forth.Eval("2 4 * 3 /"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Division_by_zero()
    {
        var exception = Assert.Throws<ForthException>(() => Forth.Eval("4 2 2 - /"));
        Assert.Equal(ForthError.DivisionByZero, exception.Error);
    }

    [Fact(Skip = "Remove to run test")]
    public void dup()
    {
        Assert.Equal("1 1", Forth.Eval("1 DUP"));
        Assert.Equal("1 2 2", Forth.Eval("1 2 Dup"));

        var exception = Assert.Throws<ForthException>(() => Forth.Eval("dup"));
        Assert.Equal(ForthError.StackUnderflow, exception.Error);
    }

    [Fact(Skip = "Remove to run test")]
    public void drop()
    {
        Assert.Equal("", Forth.Eval("1 drop"));
        Assert.Equal("1", Forth.Eval("1 2 drop"));

        var exception = Assert.Throws<ForthException>(() => Forth.Eval("drop"));
        Assert.Equal(ForthError.StackUnderflow, exception.Error);
    }

    [Fact(Skip = "Remove to run test")]
    public void swap()
    {
        Assert.Equal("2 1", Forth.Eval("1 2 swap"));
        Assert.Equal("1 3 2", Forth.Eval("1 2 3 swap"));

        var exception1 = Assert.Throws<ForthException>(() => Forth.Eval("1 swap"));
        Assert.Equal(ForthError.StackUnderflow, exception1.Error);

        var exception2 = Assert.Throws<ForthException>(() => Forth.Eval("swap"));
        Assert.Equal(ForthError.StackUnderflow, exception2.Error);
    }

    [Fact(Skip = "Remove to run test")]
    public void over()
    {
        Assert.Equal("1 2 1", Forth.Eval("1 2 over"));
        Assert.Equal("1 2 3 2", Forth.Eval("1 2 3 over"));

        var exception1 = Assert.Throws<ForthException>(() => Forth.Eval("1 over"));
        Assert.Equal(ForthError.StackUnderflow, exception1.Error);

        var exception2 = Assert.Throws<ForthException>(() => Forth.Eval("over"));
        Assert.Equal(ForthError.StackUnderflow, exception2.Error);
    }

    [Fact(Skip = "Remove to run test")]
    public void Defining_a_new_word()
    {
        Assert.Equal("1 1 1", Forth.Eval(": dup-twice dup dup ; 1 dup-twice"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Redefining_an_existing_word()
    {
        Assert.Equal("1 1 1", Forth.Eval(": foo dup ; : foo dup dup ; 1 foo"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Redefining_an_existing_built_in_word()
    {
        Assert.Equal("1 1", Forth.Eval(": swap dup ; 1 swap"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Defining_words_with_odd_characters()
    {
        Assert.Equal("220371", Forth.Eval(": € 220371 ; €"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Defining_a_number()
    {
        var exception = Assert.Throws<ForthException>(() => Forth.Eval(": 1 2 ;"));
        Assert.Equal(ForthError.InvalidWord, exception.Error);
    }

    [Fact(Skip = "Remove to run test")]
    public void Calling_a_non_existing_word()
    {
        var exception = Assert.Throws<ForthException>(() => Forth.Eval("1 foo"));
        Assert.Equal(ForthError.UnknownWord, exception.Error);
    }
}