fOOrth 0.6.10 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +13 -2
  3. data/fOOrth.gemspec +2 -2
  4. data/integration/array_lib_tests.rb +10 -0
  5. data/integration/exception_lib_tests.rb +4 -0
  6. data/integration/hash_lib_tests.rb +9 -0
  7. data/integration/numeric_lib_tests.rb +326 -321
  8. data/integration/procedure_lib_tests.rb +16 -0
  9. data/integration/queue_lib_tests.rb +2 -1
  10. data/integration/stack_lib_tests.rb +2 -1
  11. data/integration/string_lib_tests.rb +11 -0
  12. data/integration/thread_lib_tests.rb +11 -8
  13. data/lib/fOOrth/compiler/context.rb +64 -64
  14. data/lib/fOOrth/compiler/context/locals.rb +34 -34
  15. data/lib/fOOrth/compiler/context/map_name.rb +85 -85
  16. data/lib/fOOrth/compiler/context/tags.rb +60 -48
  17. data/lib/fOOrth/compiler/process/procedure.rb +40 -0
  18. data/lib/fOOrth/library.rb +1 -0
  19. data/lib/fOOrth/library/array_library.rb +41 -21
  20. data/lib/fOOrth/library/command_library.rb +1 -1
  21. data/lib/fOOrth/library/compile_library.rb +266 -266
  22. data/lib/fOOrth/library/complex_library.rb +82 -80
  23. data/lib/fOOrth/library/float_library.rb +37 -0
  24. data/lib/fOOrth/library/hash_library.rb +14 -6
  25. data/lib/fOOrth/library/mutex_library.rb +4 -2
  26. data/lib/fOOrth/library/numeric_library.rb +359 -380
  27. data/lib/fOOrth/library/procedure_library.rb +69 -65
  28. data/lib/fOOrth/library/queue_library.rb +6 -1
  29. data/lib/fOOrth/library/rational_library.rb +89 -89
  30. data/lib/fOOrth/library/stack_library.rb +6 -1
  31. data/lib/fOOrth/library/string_library.rb +18 -6
  32. data/lib/fOOrth/monkey_patch/exceptions.rb +2 -6
  33. data/lib/fOOrth/version.rb +1 -1
  34. data/tests/compiler/context_tests.rb +188 -177
  35. data/tests/compiler/file_source_tests.rb +130 -130
  36. data/tests/compiler/parser_tests.rb +4 -4
  37. data/tests/compiler/string_source_tests.rb +4 -4
  38. data/tests/core_tests.rb +138 -138
  39. data/tests/monkey_patch/complex_test.rb +24 -24
  40. data/tests/monkey_patch/object_test.rb +49 -49
  41. data/tests/monkey_patch/string_test.rb +61 -61
  42. metadata +10 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4dfd6771eb18fc86592d2a4b33e399645cb650c0
4
- data.tar.gz: f77126f64fd1ee6304e353405c161ee872416075
2
+ SHA256:
3
+ metadata.gz: ce6a84ad49645378ec632ed55e731ff89d113cf22de2517ebef69e87b1e6d01e
4
+ data.tar.gz: 610a2c6be6d202ca568fc8ff62d338f003fc5c19a63016975892820c0193c580
5
5
  SHA512:
6
- metadata.gz: d1af30311ef3fd32ab44da18c61aac59b065dd0a9723fa900cd1ef7bb2caaec2ea551cba4f5ca934b2c879660d5bcfaf21515ae1e9b4dbd8e9d879f0e113196a
7
- data.tar.gz: a5d9c3c9c6e7374b541f8ed449d1447f7445b05b34b6a2e30f94964ddfa9807817a5f5cb4b7a60a875bcd0dd71293eb97175433dd1f28e0044842643744258f7
6
+ metadata.gz: 3e9bb940f04ac87128cf57a5f235ddc0ab8210f044be38de9a285312348ebd9a9d41a6732fa0e43b9c509ca7e315b43b0d2a41cde1ce480b8a4ed7170050c1b1
7
+ data.tar.gz: 594473cca8c187f4fe28aaee1f33ea2d7c713bed1d5a0ef68e5ed8167c3aee36e379311ea258a56c99002c49e1553b6cf9149b0fc498626fc6c474e8c3277108
data/README.md CHANGED
@@ -71,7 +71,7 @@ This code repo also has PDF formatted guides for major revisions.
71
71
 
72
72
  ### If you get stuck.
73
73
 
74
- The fOOrth language REPL has several commands inspired by the APL interpreter
74
+ The fOOrth language REPL has several commands inspired by the APL interpreter
75
75
  of the old PDP-10 system I used so many years ago. The commands take the form
76
76
  of )command. That's a right paren then the command name. Some useful ones
77
77
  to know are:
@@ -87,7 +87,7 @@ to know are:
87
87
  )noshow -- don't do that.
88
88
  )version -- display current language version info.
89
89
  )words -- list all defined method names.
90
-
90
+
91
91
 
92
92
  ## Contributing
93
93
 
@@ -109,3 +109,14 @@ http://nvie.com/posts/a-successful-git-branching-model/
109
109
 
110
110
  Go to the GitHub repository and raise an issue calling attention to some
111
111
  aspect that could use some TLC or a suggestion or an idea.
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the
116
+ [MIT License](./LICENSE.txt).
117
+
118
+ ## Code of Conduct
119
+
120
+ Everyone interacting in the fully_freeze project’s codebases, issue trackers,
121
+ chat rooms and mailing lists is expected to follow the
122
+ [code of conduct](./CODE_OF_CONDUCT.md).
data/fOOrth.gemspec CHANGED
@@ -24,8 +24,8 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.required_ruby_version = '>=1.9.3'
26
26
 
27
- spec.add_development_dependency "bundler", "~> 1.3"
28
- spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency "bundler", ">= 2.1.0"
28
+ spec.add_development_dependency "rake", ">= 12.3.3"
29
29
  spec.add_development_dependency 'reek', "~> 3.0"
30
30
  spec.add_development_dependency 'minitest', "~> 5.7"
31
31
  spec.add_development_dependency 'minitest_visible', ">= 0.1.1"
@@ -359,4 +359,14 @@ class ArrayLibraryTester < Minitest::Test
359
359
  foorth_equal('[ 2 4 6 8 ] .keys ', [[0,1,2,3]])
360
360
  end
361
361
 
362
+ def test_array_emptiness
363
+ foorth_equal('[ ] .empty?', [true])
364
+ foorth_equal('[ 1 ] .empty?', [false])
365
+
366
+ foorth_equal('[ ] .present?', [false])
367
+ foorth_equal('[ 1 ] .present?', [true])
368
+
369
+ foorth_equal('[ 1 ] dup .clear!', [[]])
370
+ end
371
+
362
372
  end
@@ -83,4 +83,8 @@ class ExceptionLibraryTester < Minitest::Test
83
83
 
84
84
  end
85
85
 
86
+ def test_a_frozen_exception
87
+ foorth_equal('try [ 1 ] protect 4 << catch error end', ["E11: can't modify protected Array"])
88
+ end
89
+
86
90
  end
@@ -116,5 +116,14 @@ class HashLibraryTester < Minitest::Test
116
116
  foorth_equal('{ 0 2 -> 1 4 -> 2 6 -> 3 8 -> } .select{{ v 2/ 1 and 0= }}', [{1=>4, 3=>8}])
117
117
  end
118
118
 
119
+ def test_hash_emptiness
120
+ foorth_equal('{ } .empty?', [true])
121
+ foorth_equal('{ 1 "a" -> } .empty?', [false])
122
+
123
+ foorth_equal('{ } .present?', [false])
124
+ foorth_equal('{ 1 "a" -> } .present?', [true])
125
+
126
+ foorth_equal('{ 1 "a" -> } dup .clear!', [{}])
127
+ end
119
128
 
120
129
  end
@@ -1,321 +1,326 @@
1
- # coding: utf-8
2
-
3
- require_relative '../lib/fOOrth'
4
- require_relative 'support/foorth_testing'
5
- gem 'minitest'
6
- require 'minitest/autorun'
7
- require 'minitest_visible'
8
-
9
- #Test the standard fOOrth numeric (and related) library.
10
- class NumericLibraryTester < Minitest::Test
11
-
12
- include XfOOrthTestExtensions
13
-
14
- #Track mini-test progress.
15
- include MinitestVisible
16
-
17
- def test_some_conversions
18
- foorth_equal('5 .to_n', [5])
19
- foorth_equal('5.0 .to_n', [5.0])
20
- foorth_equal('5/1 .to_n', ['5/1'.to_r])
21
- foorth_equal('5+0i .to_n', [Complex(5,0)])
22
- foorth_equal('"xx" .to_n', [nil])
23
-
24
- foorth_equal('5 .to_n!', [5])
25
- foorth_equal('5.0 .to_n!', [5.0])
26
- foorth_equal('5/1 .to_n!', ['5/1'.to_r])
27
- foorth_equal('5+0i .to_n!', [Complex(5,0)])
28
- foorth_raises('"xx" .to_n!')
29
-
30
- foorth_equal('5 .to_i', [5])
31
- foorth_equal('5.0 .to_i', [5])
32
- foorth_equal('5/1 .to_i', [5])
33
- foorth_equal('5+0i .to_i', [5])
34
-
35
- foorth_equal('5 .to_i!', [5])
36
- foorth_equal('5.0 .to_i!', [5])
37
- foorth_equal('5/1 .to_i!', [5])
38
- foorth_equal('5+0i .to_i!', [5])
39
-
40
- foorth_equal('"xx" .to_i', [nil])
41
- foorth_equal('5+3i .to_i', [nil])
42
-
43
- foorth_raises('"xx" .to_i!')
44
- foorth_raises('5+3i .to_i!')
45
-
46
- foorth_equal('"2.0" .to_f ', [2.0])
47
- foorth_equal('"apple" .to_f ', [nil])
48
- foorth_equal('"2.0" .to_f!', [2.0])
49
- foorth_raises('"apple" .to_f!')
50
-
51
- foorth_equal('7 .to_r', [Rational(7,1)])
52
- foorth_equal('7.0 .to_r', [Rational(7,1)])
53
- foorth_equal('1.3 .to_r', [Rational(13,10)])
54
- foorth_equal('2.5 .to_r', [Rational(5,2)])
55
- foorth_equal('"5/2" .to_r', [Rational(5,2)])
56
- foorth_equal('"apple" .to_r', [nil])
57
-
58
- foorth_equal('7 .to_r!', [Rational(7,1)])
59
- foorth_equal('7.0 .to_r!', [Rational(7,1)])
60
- foorth_equal('1.3 .to_r!', [Rational(13,10)])
61
- foorth_equal('2.5 .to_r!', [Rational(5,2)])
62
- foorth_equal('"5/2" .to_r!', [Rational(5,2)])
63
- foorth_raises('"apple" .to_r!')
64
-
65
- foorth_equal('5 .to_x', [Complex(5,0)])
66
- foorth_equal('5.2 .to_x', [Complex(5.2,0)])
67
- foorth_equal('"5" .to_x', [Complex(5,0)])
68
- foorth_equal('1+2i .to_x', [Complex(1,2)])
69
- foorth_equal('"apple" .to_x', [nil])
70
-
71
- foorth_equal('5 .to_x!', [Complex(5,0)])
72
- foorth_equal('5.2 .to_x!', [Complex(5.2,0)])
73
- foorth_equal('"5" .to_x!', [Complex(5,0)])
74
- foorth_equal('1+2i .to_x!', [Complex(1,2)])
75
- foorth_raises('"apple" .to_x!')
76
-
77
- foorth_equal('5 .real', [5])
78
- foorth_equal('5 .imaginary', [0])
79
-
80
- foorth_equal('5+7i .real', [5])
81
- foorth_equal('5+7i .imaginary', [7])
82
-
83
- foorth_equal('3 .magnitude', [3])
84
- foorth_equal('3 .angle .r2d', [0])
85
- foorth_equal('-3 .angle .r2d', [180])
86
-
87
- foorth_equal('3+4i .magnitude', [5])
88
- foorth_equal('1+1i .angle .r2d', [45.0])
89
-
90
- foorth_equal('42 .conjugate', [42])
91
- foorth_equal('1+1i .conjugate', [Complex(1,-1)])
92
-
93
- foorth_equal('1+1i .polar .r2d', [Math.sqrt(2.0), 45.0])
94
- end
95
-
96
- def test_some_computations
97
- foorth_equal('5 3 +', [8])
98
- foorth_equal('5 3 -', [2])
99
- foorth_equal('5 3 *', [15])
100
- foorth_equal('5 3 /', [1])
101
- foorth_equal('5 3 mod', [2])
102
- foorth_raises('1+1i 3 mod')
103
- foorth_raises('3 1+1i mod')
104
-
105
- foorth_equal('5 "3" +', [8])
106
- foorth_equal('5 "3" -', [2])
107
- foorth_equal('5 "3" *', [15])
108
- foorth_equal('5 "3" /', [1])
109
- foorth_equal('5 "3" mod', [2])
110
-
111
- foorth_equal('5 neg', [-5])
112
- foorth_equal('0 neg', [0])
113
- foorth_equal('-5 neg', [5])
114
-
115
- foorth_equal('5.0 neg', [-5.0])
116
- foorth_equal('0.0 neg', [0.0])
117
- foorth_equal('-5.0 neg', [5.0])
118
-
119
- foorth_equal('5 3 <<', [40])
120
- foorth_equal('40 3 >>', [5])
121
-
122
- foorth_equal('2 10 **', [1024])
123
- foorth_equal('2.0 .1/x', [0.5])
124
-
125
- foorth_equal(' 2.0 .abs', [2.0])
126
- foorth_equal('-2.0 .abs', [2.0])
127
-
128
- foorth_equal(' 2.0 .ceil', [2])
129
- foorth_equal(' 2.1 .ceil', [3])
130
- foorth_equal(' 2.9 .ceil', [3])
131
- foorth_raises('1+1i .ceil')
132
-
133
- foorth_equal(' 2.0 .floor', [2])
134
- foorth_equal(' 2.1 .floor', [2])
135
- foorth_equal(' 2.9 .floor', [2])
136
- foorth_raises('1+1i .floor')
137
-
138
- foorth_equal(' 2.0 .round', [2])
139
- foorth_equal(' 2.1 .round', [2])
140
- foorth_equal(' 2.9 .round', [3])
141
- foorth_raises('1+1i .round')
142
-
143
- foorth_equal(' 1.5 .numerator', [3])
144
- foorth_equal(' 1.5 .denominator', [2])
145
-
146
- foorth_equal(' 3/2 .numerator', [3])
147
- foorth_equal(' 3/2 .denominator', [2])
148
-
149
- end
150
-
151
- def test_some_bitwise_ops
152
- foorth_equal("5 3 and", [1])
153
- foorth_equal("5 3 or", [7])
154
- foorth_equal("5 3 xor", [6])
155
- foorth_equal("5 com", [-6])
156
-
157
- foorth_equal("5 3 <<", [40])
158
- foorth_equal("40 3 >>", [5])
159
-
160
- end
161
-
162
- def test_some_trig
163
- foorth_equal("pi", [Math::PI])
164
- foorth_equal("e", [Math::E])
165
- foorth_equal("dpr", [XfOOrth::DegreesPerRadian])
166
-
167
- foorth_equal("45 .d2r dup .sin dup * swap .cos dup * + ", [1.0])
168
-
169
- foorth_equal("0 .tan ", [0.0])
170
-
171
- foorth_equal("1 .asin .r2d", [90.0])
172
- foorth_equal("1 .acos .r2d", [ 0.0])
173
- foorth_equal("1 .atan .r2d", [45.0])
174
- foorth_equal("1 1 .atan2 .r2d", [45.0])
175
-
176
- foorth_raises('1+1i .sin')
177
- foorth_raises('1+1i .cos')
178
- foorth_raises('1+1i .tan')
179
- foorth_raises('1+1i .asin')
180
- foorth_raises('1+1i .acos')
181
- foorth_raises('1+1i .atan')
182
- foorth_raises('1+1i 3 .atan2')
183
- foorth_raises('1 1+1i .atan2')
184
- end
185
-
186
- def test_some_exagerated_trig
187
- foorth_equal("0 .sinh ", [0.0])
188
- foorth_equal("0 .cosh ", [1.0])
189
- foorth_equal("0 .tanh ", [0.0])
190
-
191
- foorth_equal("0 .asinh ", [0.0])
192
- foorth_equal("1 .acosh ", [0.0])
193
- foorth_equal("0 .atanh ", [0.0])
194
-
195
- foorth_raises('1+1i .sinh')
196
- foorth_raises('1+1i .cosh')
197
- foorth_raises('1+1i .tanh')
198
- foorth_raises('1+1i .asinh')
199
- foorth_raises('1+1i .acosh')
200
- foorth_raises('1+1i .atanh')
201
- end
202
-
203
- def test_some_powers
204
- foorth_equal("0 .e**", [1.0])
205
- foorth_equal("1 .ln", [0.0])
206
- foorth_equal("e .ln", [1.0])
207
-
208
- foorth_equal("2 .10**", [100.0])
209
- foorth_equal("100 .log10", [2.0])
210
-
211
- foorth_equal("10 .2**", [1024.0])
212
- foorth_equal("1024 .log2", [10.0])
213
-
214
- foorth_equal("16 .sqr ", [256])
215
- foorth_equal("16.0 .sqr ", [256.0])
216
- foorth_equal("1+1i .sqr ", [Complex(0,2)])
217
- foorth_equal("16 .cube", [4096])
218
- foorth_equal("16.0 .cube", [4096.0])
219
- foorth_equal("1+1i .cube", [Complex(-2,2)])
220
-
221
- foorth_equal("1024 .sqrt", [32.0])
222
- foorth_equal("0+1i .sqrt", [Complex(0.7071067811865476, 0.7071067811865475)])
223
-
224
- foorth_equal("8 .cbrt", [ 2.0])
225
- foorth_equal("64 .cbrt", [ 4.0])
226
-
227
- foorth_equal("3 4 .hypot", [5.0])
228
- end
229
-
230
- def test_some_integer_ops
231
- foorth_equal("100 64 .gcd", [4])
232
- foorth_equal("100 64 .lcm", [1600])
233
-
234
- foorth_equal("100 .even?", [true])
235
- foorth_equal("101 .even?", [false])
236
- foorth_equal("16666600 .even?", [true])
237
- foorth_equal("16666601 .even?", [false])
238
-
239
- foorth_equal("100 .odd?", [false])
240
- foorth_equal("101 .odd?", [true])
241
- foorth_equal("16666600 .odd?", [false])
242
- foorth_equal("16666601 .odd?", [true])
243
-
244
- end
245
-
246
- def test_being_rational
247
- foorth_equal("1/2 .split", [1, 2])
248
-
249
- foorth_equal("1 2 rational", ['1/2'.to_r])
250
- foorth_equal("3.1 4 rational", ['31/40'.to_r])
251
- foorth_equal('"3.1" 4 rational', ['31/40'.to_r])
252
- foorth_equal('pi 1 rational', ['245_850_922/78_256_779'.to_r])
253
- foorth_equal('"apple" 4 rational', [nil])
254
-
255
- foorth_equal("1 2 rational!", ['1/2'.to_r])
256
- foorth_equal("3.1 4 rational!", ['31/40'.to_r])
257
- foorth_equal('"3.1" 4 rational!', ['31/40'.to_r])
258
- foorth_raises('"apple" 4 rational!')
259
- foorth_raises('nil 4 rational!')
260
- foorth_raises('false 4 rational!')
261
- foorth_raises('true 4 rational!')
262
- end
263
-
264
- def test_that_we_do_not_rationalize_too_much
265
- foorth_equal('0.01 pi .rationalize_to', ['22/7'.to_r])
266
- foorth_equal('0.001 pi .rationalize_to', ['201/64'.to_r])
267
- foorth_equal('0.0001 pi .rationalize_to', ['333/106'.to_r])
268
- foorth_equal('0.00001 pi .rationalize_to', ['355/113'.to_r])
269
- foorth_equal('0.000001 pi .rationalize_to', ['355/113'.to_r])
270
- foorth_equal('0.0000001 pi .rationalize_to', ['75948/24175'.to_r])
271
-
272
- foorth_equal('0.01 1234/55 .rationalize_to', ['157/7'.to_r])
273
-
274
- foorth_raises('0.01 1+5i .rationalize_to')
275
- foorth_raises('0.01 "apple" .rationalize_to')
276
- end
277
-
278
- def test_being_complex
279
- foorth_equal("1 2 complex", [Complex(1,2)])
280
- foorth_equal("1+2i .split", [1, 2])
281
- end
282
-
283
- def test_the_polar_vortex
284
- foorth_equal("1 1 .c2p .r2d", [Math::sqrt(2.0), 45.0])
285
- #foorth_equal("2.0 .sqrt 45 .d2r .p2c", [1.0, 1.0])
286
- end
287
-
288
- def test_short_cut_methods
289
- foorth_equal('10 1+' , [11])
290
- foorth_equal('10 1-' , [9])
291
-
292
- foorth_equal('10 2+' , [12])
293
- foorth_equal('10 2-' , [8])
294
-
295
- foorth_equal('10 2*' , [20])
296
- foorth_equal('10 2/' , [5])
297
-
298
- foorth_equal('10.0 1+' , [11.0])
299
- foorth_equal('10.0 1-' , [9.0])
300
-
301
- foorth_equal('10.0 2+' , [12.0])
302
- foorth_equal('10.0 2-' , [8.0])
303
-
304
- foorth_equal('10.0 2*' , [20.0])
305
- foorth_equal('10.0 2/' , [5.0])
306
- end
307
-
308
- def test_some_complex_conversions
309
- foorth_equal('4 5 complex', [Complex(4,5)])
310
- foorth_raises('"apple" 5 complex')
311
- end
312
-
313
- def test_some_rounding
314
- foorth_equal('2 pi .round_to', [3.14])
315
-
316
- foorth_raises('2 "apple" .round_to')
317
- foorth_raises('"apple" pi .round_to')
318
- end
319
-
320
-
321
- end
1
+ # coding: utf-8
2
+
3
+ require_relative '../lib/fOOrth'
4
+ require_relative 'support/foorth_testing'
5
+ gem 'minitest'
6
+ require 'minitest/autorun'
7
+ require 'minitest_visible'
8
+
9
+ #Test the standard fOOrth numeric (and related) library.
10
+ class NumericLibraryTester < Minitest::Test
11
+
12
+ include XfOOrthTestExtensions
13
+
14
+ #Track mini-test progress.
15
+ include MinitestVisible
16
+
17
+ def test_some_conversions
18
+ foorth_equal('5 .to_n', [5])
19
+ foorth_equal('5.0 .to_n', [5.0])
20
+ foorth_equal('5/1 .to_n', ['5/1'.to_r])
21
+ foorth_equal('5+0i .to_n', [Complex(5,0)])
22
+ foorth_equal('"xx" .to_n', [nil])
23
+
24
+ foorth_equal('5 .to_n!', [5])
25
+ foorth_equal('5.0 .to_n!', [5.0])
26
+ foorth_equal('5/1 .to_n!', ['5/1'.to_r])
27
+ foorth_equal('5+0i .to_n!', [Complex(5,0)])
28
+ foorth_raises('"xx" .to_n!')
29
+
30
+ foorth_equal('5 .to_i', [5])
31
+ foorth_equal('5.0 .to_i', [5])
32
+ foorth_equal('5/1 .to_i', [5])
33
+ foorth_equal('5+0i .to_i', [5])
34
+
35
+ foorth_equal('5 .to_i!', [5])
36
+ foorth_equal('5.0 .to_i!', [5])
37
+ foorth_equal('5/1 .to_i!', [5])
38
+ foorth_equal('5+0i .to_i!', [5])
39
+
40
+ foorth_equal('"xx" .to_i', [nil])
41
+ foorth_equal('5+3i .to_i', [nil])
42
+
43
+ foorth_raises('"xx" .to_i!')
44
+ foorth_raises('5+3i .to_i!')
45
+
46
+ foorth_equal('"2.0" .to_f ', [2.0])
47
+ foorth_equal('"apple" .to_f ', [nil])
48
+ foorth_equal('"2.0" .to_f!', [2.0])
49
+ foorth_raises('"apple" .to_f!')
50
+
51
+ foorth_equal('7 .to_r', [Rational(7,1)])
52
+ foorth_equal('7.0 .to_r', [Rational(7,1)])
53
+ foorth_equal('1.3 .to_r', [Rational(13,10)])
54
+ foorth_equal('2.5 .to_r', [Rational(5,2)])
55
+ foorth_equal('"5/2" .to_r', [Rational(5,2)])
56
+ foorth_equal('"apple" .to_r', [nil])
57
+
58
+ foorth_equal('7 .to_r!', [Rational(7,1)])
59
+ foorth_equal('7.0 .to_r!', [Rational(7,1)])
60
+ foorth_equal('1.3 .to_r!', [Rational(13,10)])
61
+ foorth_equal('2.5 .to_r!', [Rational(5,2)])
62
+ foorth_equal('"5/2" .to_r!', [Rational(5,2)])
63
+ foorth_raises('"apple" .to_r!')
64
+
65
+ foorth_equal('5 .to_x', [Complex(5,0)])
66
+ foorth_equal('5.2 .to_x', [Complex(5.2,0)])
67
+ foorth_equal('"5" .to_x', [Complex(5,0)])
68
+ foorth_equal('1+2i .to_x', [Complex(1,2)])
69
+ foorth_equal('"apple" .to_x', [nil])
70
+
71
+ foorth_equal('5 .to_x!', [Complex(5,0)])
72
+ foorth_equal('5.2 .to_x!', [Complex(5.2,0)])
73
+ foorth_equal('"5" .to_x!', [Complex(5,0)])
74
+ foorth_equal('1+2i .to_x!', [Complex(1,2)])
75
+ foorth_raises('"apple" .to_x!')
76
+
77
+ foorth_equal('5 .real', [5])
78
+ foorth_equal('5 .imaginary', [0])
79
+
80
+ foorth_equal('5+7i .real', [5])
81
+ foorth_equal('5+7i .imaginary', [7])
82
+
83
+ foorth_equal('3 .magnitude', [3])
84
+ foorth_equal('3 .angle .r2d', [0])
85
+ foorth_equal('-3 .angle .r2d', [180])
86
+
87
+ foorth_equal('3+4i .magnitude', [5])
88
+ foorth_equal('1+1i .angle .r2d', [45.0])
89
+
90
+ foorth_equal('42 .conjugate', [42])
91
+ foorth_equal('1+1i .conjugate', [Complex(1,-1)])
92
+
93
+ foorth_equal('1+1i .polar .r2d', [Math.sqrt(2.0), 45.0])
94
+ end
95
+
96
+ def test_some_computations
97
+ foorth_equal('5 3 +', [8])
98
+ foorth_equal('5 3 -', [2])
99
+ foorth_equal('5 3 *', [15])
100
+ foorth_equal('5 3 /', [1])
101
+ foorth_equal('5 3 mod', [2])
102
+ foorth_raises('1+1i 3 mod')
103
+ foorth_raises('3 1+1i mod')
104
+
105
+ foorth_equal('5 "3" +', [8])
106
+ foorth_equal('5 "3" -', [2])
107
+ foorth_equal('5 "3" *', [15])
108
+ foorth_equal('5 "3" /', [1])
109
+ foorth_equal('5 "3" mod', [2])
110
+
111
+ foorth_equal('5 neg', [-5])
112
+ foorth_equal('0 neg', [0])
113
+ foorth_equal('-5 neg', [5])
114
+
115
+ foorth_equal('5.0 neg', [-5.0])
116
+ foorth_equal('0.0 neg', [0.0])
117
+ foorth_equal('-5.0 neg', [5.0])
118
+
119
+ foorth_equal('5 3 <<', [40])
120
+ foorth_equal('40 3 >>', [5])
121
+
122
+ foorth_equal('2 10 **', [1024])
123
+ foorth_equal('2.0 .1/x', [0.5])
124
+
125
+ foorth_equal(' 2.0 .abs', [2.0])
126
+ foorth_equal('-2.0 .abs', [2.0])
127
+
128
+ foorth_equal(' 2.0 .ceil', [2])
129
+ foorth_equal(' 2.1 .ceil', [3])
130
+ foorth_equal(' 2.9 .ceil', [3])
131
+ foorth_raises('1+1i .ceil')
132
+
133
+ foorth_equal(' 2.0 .floor', [2])
134
+ foorth_equal(' 2.1 .floor', [2])
135
+ foorth_equal(' 2.9 .floor', [2])
136
+ foorth_raises('1+1i .floor')
137
+
138
+ foorth_equal(' 2.0 .round', [2])
139
+ foorth_equal(' 2.1 .round', [2])
140
+ foorth_equal(' 2.9 .round', [3])
141
+ foorth_raises('1+1i .round')
142
+
143
+ foorth_equal(' 1.5 .numerator', [3])
144
+ foorth_equal(' 1.5 .denominator', [2])
145
+
146
+ foorth_equal(' 3/2 .numerator', [3])
147
+ foorth_equal(' 3/2 .denominator', [2])
148
+
149
+ foorth_equal(' 0.1 .numerator', [1])
150
+ foorth_equal(' 0.1 .denominator', [10])
151
+
152
+ foorth_raises(' 1+1i .numerator')
153
+ foorth_raises(' 1+1i .denominator')
154
+ end
155
+
156
+ def test_some_bitwise_ops
157
+ foorth_equal("5 3 and", [1])
158
+ foorth_equal("5 3 or", [7])
159
+ foorth_equal("5 3 xor", [6])
160
+ foorth_equal("5 com", [-6])
161
+
162
+ foorth_equal("5 3 <<", [40])
163
+ foorth_equal("40 3 >>", [5])
164
+
165
+ end
166
+
167
+ def test_some_trig
168
+ foorth_equal("pi", [Math::PI])
169
+ foorth_equal("e", [Math::E])
170
+ foorth_equal("dpr", [XfOOrth::DegreesPerRadian])
171
+
172
+ foorth_equal("45 .d2r dup .sin dup * swap .cos dup * + ", [1.0])
173
+
174
+ foorth_equal("0 .tan ", [0.0])
175
+
176
+ foorth_equal("1 .asin .r2d", [90.0])
177
+ foorth_equal("1 .acos .r2d", [ 0.0])
178
+ foorth_equal("1 .atan .r2d", [45.0])
179
+ foorth_equal("1 1 .atan2 .r2d", [45.0])
180
+
181
+ foorth_raises('1+1i .sin')
182
+ foorth_raises('1+1i .cos')
183
+ foorth_raises('1+1i .tan')
184
+ foorth_raises('1+1i .asin')
185
+ foorth_raises('1+1i .acos')
186
+ foorth_raises('1+1i .atan')
187
+ foorth_raises('1+1i 3 .atan2')
188
+ foorth_raises('1 1+1i .atan2')
189
+ end
190
+
191
+ def test_some_exagerated_trig
192
+ foorth_equal("0 .sinh ", [0.0])
193
+ foorth_equal("0 .cosh ", [1.0])
194
+ foorth_equal("0 .tanh ", [0.0])
195
+
196
+ foorth_equal("0 .asinh ", [0.0])
197
+ foorth_equal("1 .acosh ", [0.0])
198
+ foorth_equal("0 .atanh ", [0.0])
199
+
200
+ foorth_raises('1+1i .sinh')
201
+ foorth_raises('1+1i .cosh')
202
+ foorth_raises('1+1i .tanh')
203
+ foorth_raises('1+1i .asinh')
204
+ foorth_raises('1+1i .acosh')
205
+ foorth_raises('1+1i .atanh')
206
+ end
207
+
208
+ def test_some_powers
209
+ foorth_equal("0 .e**", [1.0])
210
+ foorth_equal("1 .ln", [0.0])
211
+ foorth_equal("e .ln", [1.0])
212
+
213
+ foorth_equal("2 .10**", [100.0])
214
+ foorth_equal("100 .log10", [2.0])
215
+
216
+ foorth_equal("10 .2**", [1024.0])
217
+ foorth_equal("1024 .log2", [10.0])
218
+
219
+ foorth_equal("16 .sqr ", [256])
220
+ foorth_equal("16.0 .sqr ", [256.0])
221
+ foorth_equal("1+1i .sqr ", [Complex(0,2)])
222
+ foorth_equal("16 .cube", [4096])
223
+ foorth_equal("16.0 .cube", [4096.0])
224
+ foorth_equal("1+1i .cube", [Complex(-2,2)])
225
+
226
+ foorth_equal("1024 .sqrt", [32.0])
227
+ foorth_equal("0+1i .sqrt", [Complex(0.7071067811865476, 0.7071067811865475)])
228
+
229
+ foorth_equal("8 .cbrt", [ 2.0])
230
+ foorth_equal("64 .cbrt", [ 4.0])
231
+
232
+ foorth_equal("3 4 .hypot", [5.0])
233
+ end
234
+
235
+ def test_some_integer_ops
236
+ foorth_equal("100 64 .gcd", [4])
237
+ foorth_equal("100 64 .lcm", [1600])
238
+
239
+ foorth_equal("100 .even?", [true])
240
+ foorth_equal("101 .even?", [false])
241
+ foorth_equal("16666600 .even?", [true])
242
+ foorth_equal("16666601 .even?", [false])
243
+
244
+ foorth_equal("100 .odd?", [false])
245
+ foorth_equal("101 .odd?", [true])
246
+ foorth_equal("16666600 .odd?", [false])
247
+ foorth_equal("16666601 .odd?", [true])
248
+
249
+ end
250
+
251
+ def test_being_rational
252
+ foorth_equal("1/2 .split", [1, 2])
253
+
254
+ foorth_equal("1 2 rational", ['1/2'.to_r])
255
+ foorth_equal("3.1 4 rational", ['31/40'.to_r])
256
+ foorth_equal('"3.1" 4 rational', ['31/40'.to_r])
257
+ foorth_equal('pi 1 rational', ['245_850_922/78_256_779'.to_r])
258
+ foorth_equal('"apple" 4 rational', [nil])
259
+
260
+ foorth_equal("1 2 rational!", ['1/2'.to_r])
261
+ foorth_equal("3.1 4 rational!", ['31/40'.to_r])
262
+ foorth_equal('"3.1" 4 rational!', ['31/40'.to_r])
263
+ foorth_raises('"apple" 4 rational!')
264
+ foorth_raises('nil 4 rational!')
265
+ foorth_raises('false 4 rational!')
266
+ foorth_raises('true 4 rational!')
267
+ end
268
+
269
+ def test_that_we_do_not_rationalize_too_much
270
+ foorth_equal('0.01 pi .rationalize_to', ['22/7'.to_r])
271
+ foorth_equal('0.001 pi .rationalize_to', ['201/64'.to_r])
272
+ foorth_equal('0.0001 pi .rationalize_to', ['333/106'.to_r])
273
+ foorth_equal('0.00001 pi .rationalize_to', ['355/113'.to_r])
274
+ foorth_equal('0.000001 pi .rationalize_to', ['355/113'.to_r])
275
+ foorth_equal('0.0000001 pi .rationalize_to', ['75948/24175'.to_r])
276
+
277
+ foorth_equal('0.01 1234/55 .rationalize_to', ['157/7'.to_r])
278
+
279
+ foorth_raises('0.01 1+5i .rationalize_to')
280
+ foorth_raises('0.01 "apple" .rationalize_to')
281
+ end
282
+
283
+ def test_being_complex
284
+ foorth_equal("1 2 complex", [Complex(1,2)])
285
+ foorth_equal("1+2i .split", [1, 2])
286
+ end
287
+
288
+ def test_the_polar_vortex
289
+ foorth_equal("1 1 .c2p .r2d", [Math::sqrt(2.0), 45.0])
290
+ #foorth_equal("2.0 .sqrt 45 .d2r .p2c", [1.0, 1.0])
291
+ end
292
+
293
+ def test_short_cut_methods
294
+ foorth_equal('10 1+' , [11])
295
+ foorth_equal('10 1-' , [9])
296
+
297
+ foorth_equal('10 2+' , [12])
298
+ foorth_equal('10 2-' , [8])
299
+
300
+ foorth_equal('10 2*' , [20])
301
+ foorth_equal('10 2/' , [5])
302
+
303
+ foorth_equal('10.0 1+' , [11.0])
304
+ foorth_equal('10.0 1-' , [9.0])
305
+
306
+ foorth_equal('10.0 2+' , [12.0])
307
+ foorth_equal('10.0 2-' , [8.0])
308
+
309
+ foorth_equal('10.0 2*' , [20.0])
310
+ foorth_equal('10.0 2/' , [5.0])
311
+ end
312
+
313
+ def test_some_complex_conversions
314
+ foorth_equal('4 5 complex', [Complex(4,5)])
315
+ foorth_raises('"apple" 5 complex')
316
+ end
317
+
318
+ def test_some_rounding
319
+ foorth_equal('2 pi .round_to', [3.14])
320
+
321
+ foorth_raises('2 "apple" .round_to')
322
+ foorth_raises('"apple" pi .round_to')
323
+ end
324
+
325
+
326
+ end