1
exercism fetch elixir forth

forth_test.exs

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("forth.exs", __DIR__)
end

ExUnit.start
ExUnit.configure exclude: :pending, trace: true

defmodule ForthTest do
  use ExUnit.Case

  # @tag :pending
  test "no input, no stack" do
    s = Forth.new |> Forth.format_stack
    assert s == ""
  end

  @tag :pending
  test "numbers just get pushed onto the stack" do
    s = Forth.new
        |> Forth.eval("1 2 3 4 5")
        |> Forth.format_stack
    assert s == "1 2 3 4 5"
  end

  @tag :pending
  test "non-word characters are separators" do
    # Note the Ogham Space Mark ( ), this is a spacing character.
    s = Forth.new
        |> Forth.eval("1\x002\x013\n4\r5 6\t7")
        |> Forth.format_stack
    assert s == "1 2 3 4 5 6 7"
  end

  @tag :pending
  test "basic arithmetic" do
    s = Forth.new
        |> Forth.eval("1 2 + 4 -")
        |> Forth.format_stack
    assert s == "-1"
    s = Forth.new
        |> Forth.eval("2 4 * 3 /") # integer division
        |> Forth.format_stack
    assert s == "2"
  end

  @tag :pending
  test "division by zero" do
    assert_raise Forth.DivisionByZero, fn ->
      Forth.new |> Forth.eval("4 2 2 - /")
    end
  end

  @tag :pending
  test "dup" do
    s = Forth.new
        |> Forth.eval("1 DUP")
        |> Forth.format_stack
    assert s == "1 1"
    s = Forth.new
        |> Forth.eval("1 2 Dup")
        |> Forth.format_stack
    assert s == "1 2 2"
    assert_raise Forth.StackUnderflow, fn ->
      Forth.new |> Forth.eval("dup")
    end
  end

  @tag :pending
  test "drop" do
    s = Forth.new
        |> Forth.eval("1 drop")
        |> Forth.format_stack
    assert s == ""
    s = Forth.new
        |> Forth.eval("1 2 drop")
        |> Forth.format_stack
    assert s == "1"
    assert_raise Forth.StackUnderflow, fn ->
      Forth.new |> Forth.eval("drop")
    end
  end

  @tag :pending
  test "swap" do
    s = Forth.new
        |> Forth.eval("1 2 swap")
        |> Forth.format_stack
    assert s == "2 1"
    s = Forth.new
        |> Forth.eval("1 2 3 swap")
        |> Forth.format_stack
    assert s == "1 3 2"
    assert_raise Forth.StackUnderflow, fn ->
      Forth.new |> Forth.eval("1 swap")
    end
    assert_raise Forth.StackUnderflow, fn ->
      Forth.new |> Forth.eval("swap")
    end
  end

  @tag :pending
  test "over" do
    s = Forth.new
        |> Forth.eval("1 2 over")
        |> Forth.format_stack
    assert s == "1 2 1"
    s = Forth.new
        |> Forth.eval("1 2 3 over")
        |> Forth.format_stack
    assert s == "1 2 3 2"
    assert_raise Forth.StackUnderflow, fn ->
      Forth.new |> Forth.eval("1 over")
    end
    assert_raise Forth.StackUnderflow, fn ->
      Forth.new |> Forth.eval("over")
    end
  end

  @tag :pending
  test "defining a new word" do
    s = Forth.new
        |> Forth.eval(": dup-twice dup dup ;")
        |> Forth.eval("1 dup-twice")
        |> Forth.format_stack
    assert s == "1 1 1"
  end

  @tag :pending
  test "redefining an existing word" do
    s = Forth.new
        |> Forth.eval(": foo dup ;")
        |> Forth.eval(": foo dup dup ;")
        |> Forth.eval("1 foo")
        |> Forth.format_stack
    assert s == "1 1 1"
  end

  @tag :pending
  test "redefining an existing built-in word" do
    s = Forth.new
        |> Forth.eval(": swap dup ;")
        |> Forth.eval("1 swap")
        |> Forth.format_stack
    assert s == "1 1"
  end

  @tag :pending
  test "defining words with odd characters" do
    s = Forth.new
        |> Forth.eval(": € 220371 ; €")
        |> Forth.format_stack
    assert s == "220371"
  end

  @tag :pending
  test "defining a number" do
    assert_raise Forth.InvalidWord, fn ->
      Forth.new |> Forth.eval(": 1 2 ;")
    end
  end

  @tag :pending
  test "calling a non-existing word" do
    assert_raise Forth.UnknownWord, fn ->
      Forth.new |> Forth.eval("1 foo")
    end
  end
end