fOOrth 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rdoc_options +17 -0
  4. data/Gemfile +4 -0
  5. data/README.md +67 -0
  6. data/bin/fOOrth +8 -0
  7. data/demo.rb +24 -0
  8. data/fOOrth.gemspec +40 -0
  9. data/fOOrth.reek +109 -0
  10. data/integration/README.md +12 -0
  11. data/integration/_FILE_test.foorth +5 -0
  12. data/integration/array_lib_tests.rb +360 -0
  13. data/integration/class_lib_tests.rb +116 -0
  14. data/integration/clone_lib_tests.rb +108 -0
  15. data/integration/comparison_tests.rb +132 -0
  16. data/integration/compile_lib_tests.rb +190 -0
  17. data/integration/ctrl_struct_lib_tests.rb +80 -0
  18. data/integration/data_ref_lib_tests.rb +43 -0
  19. data/integration/exception_lib_tests.rb +86 -0
  20. data/integration/fiber_bundle_tests.rb +380 -0
  21. data/integration/hash_lib_tests.rb +120 -0
  22. data/integration/in_stream_test_1.txt +4 -0
  23. data/integration/load_test_one.foorth +6 -0
  24. data/integration/load_test_two.foorth +4 -0
  25. data/integration/numeric_lib_tests.rb +321 -0
  26. data/integration/object_lib_tests.rb +38 -0
  27. data/integration/procedure_lib_tests.rb +40 -0
  28. data/integration/queue_lib_tests.rb +66 -0
  29. data/integration/stack_lib_tests.rb +70 -0
  30. data/integration/standard_lib_tests.rb +208 -0
  31. data/integration/stdio_lib_tests.rb +52 -0
  32. data/integration/stream_lib_tests.rb +196 -0
  33. data/integration/string_lib_tests.rb +217 -0
  34. data/integration/support/foorth_testing.rb +135 -0
  35. data/integration/thread_lib_tests.rb +83 -0
  36. data/integration/time_lib_tests.rb +791 -0
  37. data/integration/vm_lib_tests.rb +38 -0
  38. data/lib/fOOrth.rb +57 -0
  39. data/lib/fOOrth/compiler.rb +78 -0
  40. data/lib/fOOrth/compiler/context.rb +49 -0
  41. data/lib/fOOrth/compiler/context/locals.rb +34 -0
  42. data/lib/fOOrth/compiler/context/map_name.rb +92 -0
  43. data/lib/fOOrth/compiler/context/tags.rb +48 -0
  44. data/lib/fOOrth/compiler/modes.rb +32 -0
  45. data/lib/fOOrth/compiler/modes/compiled.rb +41 -0
  46. data/lib/fOOrth/compiler/modes/deferred.rb +57 -0
  47. data/lib/fOOrth/compiler/modes/delayed.rb +40 -0
  48. data/lib/fOOrth/compiler/modes/nested.rb +34 -0
  49. data/lib/fOOrth/compiler/modes/suspend.rb +32 -0
  50. data/lib/fOOrth/compiler/parser.rb +26 -0
  51. data/lib/fOOrth/compiler/parser/get_string.rb +71 -0
  52. data/lib/fOOrth/compiler/parser/normal.rb +53 -0
  53. data/lib/fOOrth/compiler/parser/skip.rb +50 -0
  54. data/lib/fOOrth/compiler/parser/special.rb +42 -0
  55. data/lib/fOOrth/compiler/process.rb +47 -0
  56. data/lib/fOOrth/compiler/process/generate.rb +24 -0
  57. data/lib/fOOrth/compiler/process/get_token.rb +23 -0
  58. data/lib/fOOrth/compiler/process/procedure.rb +55 -0
  59. data/lib/fOOrth/compiler/process/string.rb +20 -0
  60. data/lib/fOOrth/compiler/source.rb +51 -0
  61. data/lib/fOOrth/compiler/source/console.rb +70 -0
  62. data/lib/fOOrth/compiler/source/file_source.rb +37 -0
  63. data/lib/fOOrth/compiler/source/read_point.rb +46 -0
  64. data/lib/fOOrth/compiler/source/string_source.rb +28 -0
  65. data/lib/fOOrth/compiler/token.rb +37 -0
  66. data/lib/fOOrth/compiler/word_specs.rb +178 -0
  67. data/lib/fOOrth/core.rb +27 -0
  68. data/lib/fOOrth/core/class.rb +116 -0
  69. data/lib/fOOrth/core/object.rb +78 -0
  70. data/lib/fOOrth/core/virtual_machine.rb +28 -0
  71. data/lib/fOOrth/debug.rb +13 -0
  72. data/lib/fOOrth/debug/context_dump.rb +31 -0
  73. data/lib/fOOrth/debug/dbg_puts.rb +17 -0
  74. data/lib/fOOrth/debug/display_abort.rb +37 -0
  75. data/lib/fOOrth/debug/vm_dump.rb +27 -0
  76. data/lib/fOOrth/initialize.rb +83 -0
  77. data/lib/fOOrth/interpreter.rb +24 -0
  78. data/lib/fOOrth/interpreter/add_to_hash.rb +17 -0
  79. data/lib/fOOrth/interpreter/data_stack.rb +125 -0
  80. data/lib/fOOrth/interpreter/do_loop.rb +55 -0
  81. data/lib/fOOrth/interpreter/squash.rb +25 -0
  82. data/lib/fOOrth/library.rb +38 -0
  83. data/lib/fOOrth/library/array_library.rb +577 -0
  84. data/lib/fOOrth/library/bundle_library.rb +112 -0
  85. data/lib/fOOrth/library/class_library.rb +90 -0
  86. data/lib/fOOrth/library/clone_library.rb +72 -0
  87. data/lib/fOOrth/library/command_library.rb +205 -0
  88. data/lib/fOOrth/library/compile_library.rb +181 -0
  89. data/lib/fOOrth/library/complex_library.rb +81 -0
  90. data/lib/fOOrth/library/ctrl_struct_library.rb +116 -0
  91. data/lib/fOOrth/library/data_ref_library.rb +100 -0
  92. data/lib/fOOrth/library/duration/arithmetic.rb +114 -0
  93. data/lib/fOOrth/library/duration/formatter.rb +152 -0
  94. data/lib/fOOrth/library/duration/intervals.rb +233 -0
  95. data/lib/fOOrth/library/duration/make.rb +75 -0
  96. data/lib/fOOrth/library/duration_library.rb +52 -0
  97. data/lib/fOOrth/library/fiber_library.rb +120 -0
  98. data/lib/fOOrth/library/hash_library.rb +203 -0
  99. data/lib/fOOrth/library/in_stream_library.rb +81 -0
  100. data/lib/fOOrth/library/integer_library.rb +104 -0
  101. data/lib/fOOrth/library/mutex_library.rb +31 -0
  102. data/lib/fOOrth/library/numeric_library.rb +380 -0
  103. data/lib/fOOrth/library/object_library.rb +80 -0
  104. data/lib/fOOrth/library/other_value_types_library.rb +96 -0
  105. data/lib/fOOrth/library/out_stream_library.rb +146 -0
  106. data/lib/fOOrth/library/procedure_library.rb +65 -0
  107. data/lib/fOOrth/library/queue_library.rb +47 -0
  108. data/lib/fOOrth/library/rational_library.rb +90 -0
  109. data/lib/fOOrth/library/stack_library.rb +56 -0
  110. data/lib/fOOrth/library/stdio_library.rb +56 -0
  111. data/lib/fOOrth/library/string_library.rb +285 -0
  112. data/lib/fOOrth/library/stubs.rb +76 -0
  113. data/lib/fOOrth/library/sync_bundle_library.rb +50 -0
  114. data/lib/fOOrth/library/thread_library.rb +73 -0
  115. data/lib/fOOrth/library/time_library.rb +302 -0
  116. data/lib/fOOrth/library/vm_library.rb +105 -0
  117. data/lib/fOOrth/main.rb +125 -0
  118. data/lib/fOOrth/monkey_patch.rb +14 -0
  119. data/lib/fOOrth/monkey_patch/complex.rb +30 -0
  120. data/lib/fOOrth/monkey_patch/exceptions.rb +154 -0
  121. data/lib/fOOrth/monkey_patch/false.rb +11 -0
  122. data/lib/fOOrth/monkey_patch/float.rb +22 -0
  123. data/lib/fOOrth/monkey_patch/integer.rb +22 -0
  124. data/lib/fOOrth/monkey_patch/nil.rb +11 -0
  125. data/lib/fOOrth/monkey_patch/numeric.rb +33 -0
  126. data/lib/fOOrth/monkey_patch/object.rb +43 -0
  127. data/lib/fOOrth/monkey_patch/rational.rb +31 -0
  128. data/lib/fOOrth/monkey_patch/string.rb +51 -0
  129. data/lib/fOOrth/symbol_map.rb +82 -0
  130. data/lib/fOOrth/version.rb +7 -0
  131. data/license.txt +21 -0
  132. data/rakefile.rb +65 -0
  133. data/reek.txt +1 -0
  134. data/sire.rb +132 -0
  135. data/t.txt +3 -0
  136. data/test.foorth +5 -0
  137. data/tests/compiler/context_tests.rb +180 -0
  138. data/tests/compiler/file_source_test_one.txt +1 -0
  139. data/tests/compiler/file_source_test_three.txt +3 -0
  140. data/tests/compiler/file_source_test_two.txt +3 -0
  141. data/tests/compiler/file_source_tests.rb +130 -0
  142. data/tests/compiler/mode_tests.rb +45 -0
  143. data/tests/compiler/parser_tests.rb +116 -0
  144. data/tests/compiler/spec_tests.rb +113 -0
  145. data/tests/compiler/string_source_tests.rb +128 -0
  146. data/tests/core_tests.rb +138 -0
  147. data/tests/interpreter/data_stack_tests.rb +119 -0
  148. data/tests/monkey_patch/coerce_test.rb +131 -0
  149. data/tests/monkey_patch/complex_test.rb +25 -0
  150. data/tests/monkey_patch/numeric_test.rb +62 -0
  151. data/tests/monkey_patch/object_test.rb +49 -0
  152. data/tests/monkey_patch/rational_test.rb +57 -0
  153. data/tests/monkey_patch/string_test.rb +53 -0
  154. data/tests/symbol_map_tests.rb +53 -0
  155. metadata +366 -0
@@ -0,0 +1,83 @@
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 fOOrth compile library.
10
+ class ThreadLibraryTester < Minitest::Test
11
+
12
+ include XfOOrthTestExtensions
13
+
14
+ #Track mini-test progress.
15
+ include MinitestVisible
16
+
17
+ def test_that_thread_classes_exist
18
+ foorth_equal('Thread', [Thread])
19
+ foorth_equal('Procedure', [Proc])
20
+ foorth_equal('Mutex', [Mutex])
21
+ end
22
+
23
+ def test_that_default_new_is_not_allowed
24
+ foorth_raises('Thread .new')
25
+ end
26
+
27
+ def test_the_current_thread
28
+ foorth_equal('Thread .current .name', ['Thread instance'])
29
+ foorth_equal('Thread .current .vm .name', ['VirtualMachine instance <Main>'])
30
+ end
31
+
32
+ def test_the_main_thread
33
+ foorth_equal('Thread .main .name', ['Thread instance'])
34
+ foorth_equal('Thread .main .vm .name', ['VirtualMachine instance <Main>'])
35
+ end
36
+
37
+ def test_thread_sleeping
38
+ start = Time.now
39
+ foorth_run('0.05 .sleep')
40
+ finish = Time.now
41
+ assert(finish-start > 0.025)
42
+ assert(finish-start < 0.1)
43
+ end
44
+
45
+ def test_creating_a_thread
46
+ foorth_equal('0 var$: $tcat1', [])
47
+ foorth_equal('"Test" Thread .new{{ 1 $tcat1 ! }} .join $tcat1 @', [1])
48
+ end
49
+
50
+ def test_joining_a_thread
51
+ foorth_output('{{ 0 5 do 0.01 .sleep i . loop }} .start .join ." done"', "01234 done")
52
+ end
53
+
54
+ def test_for_poking_with_a_stick
55
+ foorth_equal('{{ }} .start dup .join .alive?', [false])
56
+ foorth_equal('{{ 0.1 .sleep }} .start .alive?', [true])
57
+ end
58
+
59
+ def test_thread_status
60
+ foorth_equal('Thread .current .status', ['run'])
61
+ foorth_equal('{{ }} .start dup .join .status', ['dead'])
62
+ foorth_equal('{{ 0.1 .sleep }} .start .status', ['sleep'])
63
+ end
64
+
65
+ def test_named_threads
66
+ foorth_equal('"Fred" {{ }} .start_named dup .join .vm .vm_name', ["Fred"])
67
+ end
68
+
69
+ def test_some_mutex_stuff
70
+ foorth_run('Mutex .new val$: $tmtx')
71
+ foorth_run('$tmtx .lock $tmtx .unlock ')
72
+ foorth_equal('$tmtx .do{{ 3 4 + }}', [7])
73
+
74
+ foorth_run('"" val$: $tmtx_str')
75
+ foorth_equal('{{ $tmtx .do{{ 0 10 do $tmtx_str "@" << loop }} }} ' +
76
+ '.start drop $tmtx .do{{ $tmtx_str }} ', ["@"*10])
77
+
78
+ foorth_run('"" val$: $tmtx_str')
79
+ foorth_equal('{{ Mutex .do{{ 0 10 do $tmtx_str "@" << loop }} }} ' +
80
+ '.start drop Mutex .do{{ $tmtx_str }} ', ["@"*10])
81
+ end
82
+
83
+ end
@@ -0,0 +1,791 @@
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 fOOrth time and duration libraries.
10
+ class TimeLibraryTester < Minitest::Test
11
+
12
+ include XfOOrthTestExtensions
13
+
14
+ #Track mini-test progress.
15
+ include MinitestVisible
16
+
17
+ def test_that_the_time_classes_exists
18
+ foorth_equal('Time', [Time])
19
+ foorth_equal('Duration', [XfOOrth::Duration])
20
+ end
21
+
22
+ def test_that_new_not_allowed
23
+ foorth_raises('Time .new')
24
+ foorth_raises('Duration .new')
25
+ end
26
+
27
+ def test_some_time_duration_values
28
+ foorth_equal('now .class', [Time])
29
+ foorth_equal('local_offset', [Time.now.utc_offset])
30
+
31
+ foorth_equal('Duration .intervals',
32
+ [[31_556_952, 2_629_746, 86_400, 3_600, 60, 1]])
33
+
34
+ foorth_equal('Duration .labels',
35
+ [["years", "months", "days", "hours", "minutes", "seconds"]])
36
+
37
+ foorth_equal('a_year', [31_556_952])
38
+ foorth_equal('a_month', [ 2_629_746])
39
+ foorth_equal('a_day', [ 86_400])
40
+ foorth_equal('an_hour', [ 3_600])
41
+ foorth_equal('a_minute', [ 60])
42
+ foorth_equal('a_second', [ 1])
43
+
44
+ foorth_equal('a_year .class', [XfOOrth::Duration])
45
+ foorth_equal('a_month .class', [XfOOrth::Duration])
46
+ foorth_equal('a_day .class', [XfOOrth::Duration])
47
+ foorth_equal('an_hour .class', [XfOOrth::Duration])
48
+ foorth_equal('a_minute .class', [XfOOrth::Duration])
49
+ foorth_equal('a_second .class', [XfOOrth::Duration])
50
+
51
+ foorth_equal('a_year .years ', [1])
52
+ foorth_equal('a_year 3/2 * .years ', [1])
53
+ foorth_equal('a_year 2 * .years ', [2])
54
+
55
+ foorth_equal('a_year 3/2 * .years .class', [Fixnum])
56
+
57
+ foorth_equal('a_year .months ', [0])
58
+ foorth_equal('a_year 3/2 * .months ', [6])
59
+ foorth_equal('a_year 2 * .months ', [0])
60
+ foorth_equal('a_month .months ', [1])
61
+ foorth_equal('a_month 3/2 * .months ', [1])
62
+ foorth_equal('a_month 2 * .months ', [2])
63
+
64
+ foorth_equal('a_month 3/2 * .months .class', [Fixnum])
65
+
66
+ foorth_equal('a_year .days ', [0])
67
+ foorth_equal('a_year 3/2 * .days ', [0])
68
+ foorth_equal('a_year 2 * .days ', [0])
69
+ foorth_equal('a_month .days ', [0])
70
+ foorth_equal('a_month 3/2 * .days ', [15])
71
+ foorth_equal('a_month 2 * .days ', [0])
72
+ foorth_equal('a_day .days ', [1])
73
+ foorth_equal('a_day 3/2 * .days ', [1])
74
+ foorth_equal('a_day 2 * .days ', [2])
75
+
76
+ foorth_equal('a_day 3/2 * .days .class', [Fixnum])
77
+
78
+ foorth_equal('a_year .hours ', [0])
79
+ foorth_equal('a_year 3/2 * .hours ', [0])
80
+ foorth_equal('a_year 2 * .hours ', [0])
81
+ foorth_equal('a_month .hours ', [0])
82
+ foorth_equal('a_month 3/2 * .hours ', [5])
83
+ foorth_equal('a_month 2 * .hours ', [0])
84
+ foorth_equal('a_day .hours ', [0])
85
+ foorth_equal('a_day 3/2 * .hours ', [12])
86
+ foorth_equal('a_day 2 * .hours ', [0])
87
+ foorth_equal('an_hour .hours ', [1])
88
+ foorth_equal('an_hour 3/2 * .hours ', [1])
89
+ foorth_equal('an_hour 2 * .hours ', [2])
90
+
91
+ foorth_equal('an_hour 3/2 * .hours .class', [Fixnum])
92
+
93
+ foorth_equal('a_year .minutes ', [0])
94
+ foorth_equal('a_year 3/2 * .minutes ', [0])
95
+ foorth_equal('a_year 2 * .minutes ', [0])
96
+ foorth_equal('a_month .minutes ', [0])
97
+ foorth_equal('a_month 3/2 * .minutes ', [14])
98
+ foorth_equal('a_month 2 * .minutes ', [0])
99
+ foorth_equal('a_day .minutes ', [0])
100
+ foorth_equal('a_day 3/2 * .minutes ', [0])
101
+ foorth_equal('a_day 2 * .minutes ', [0])
102
+ foorth_equal('an_hour .minutes ', [0])
103
+ foorth_equal('an_hour 3/2 * .minutes ', [30])
104
+ foorth_equal('an_hour 2 * .minutes ', [0])
105
+ foorth_equal('a_minute .minutes ', [1])
106
+ foorth_equal('a_minute 3/2 * .minutes ', [1])
107
+ foorth_equal('a_minute 2 * .minutes ', [2])
108
+
109
+ foorth_equal('a_minute 3/2 * .minutes .class', [Fixnum])
110
+
111
+ foorth_equal('a_year .seconds ', [0])
112
+ foorth_equal('a_year 3/2 * .seconds ', [0])
113
+ foorth_equal('a_year 2 * .seconds ', [0])
114
+ foorth_equal('a_month .seconds ', [0])
115
+ foorth_equal('a_month 3/2 * .seconds ', [33])
116
+ foorth_equal('a_month 2 * .seconds ', [0])
117
+ foorth_equal('a_day .seconds ', [0])
118
+ foorth_equal('a_day 3/2 * .seconds ', [0])
119
+ foorth_equal('a_day 2 * .seconds ', [0])
120
+ foorth_equal('an_hour .seconds ', [0])
121
+ foorth_equal('an_hour 3/2 * .seconds ', [0])
122
+ foorth_equal('an_hour 2 * .seconds ', [0])
123
+ foorth_equal('a_minute .seconds ', [0])
124
+ foorth_equal('a_minute 3/2 * .seconds ', [30])
125
+ foorth_equal('a_minute 2 * .seconds ', [0])
126
+ foorth_equal('a_second .seconds ', [1])
127
+ foorth_equal('a_second 3/2 * .seconds ', [1.5])
128
+ foorth_equal('a_second 2 * .seconds ', [2])
129
+
130
+ foorth_equal('a_second 3/2 * .seconds .class', [Float])
131
+
132
+ foorth_equal('a_year .as_years ', [1])
133
+ foorth_equal('a_year 3/2 * .as_years ', [1.5])
134
+ foorth_equal('a_year 2 * .as_years ', [2])
135
+
136
+ foorth_equal('a_year .as_months ', [12])
137
+ foorth_equal('a_year 3/2 * .as_months ', [18])
138
+ foorth_equal('a_year 2 * .as_months ', [24])
139
+ foorth_equal('a_month .as_months ', [1])
140
+ foorth_equal('a_month 3/2 * .as_months ', [1.5])
141
+ foorth_equal('a_month 2 * .as_months ', [2])
142
+
143
+ foorth_equal('a_year .as_days ', [365.2425])
144
+ foorth_equal('a_year 3/2 * .as_days ', [1.5*365.2425])
145
+ foorth_equal('a_year 2 * .as_days ', [2*365.2425])
146
+ foorth_equal('a_month .as_days ', [365.2425/12])
147
+ foorth_equal('a_month 3/2 * .as_days ', [1.5*365.2425/12])
148
+ foorth_equal('a_month 2 * .as_days ', [365.2425/6])
149
+ foorth_equal('a_day .as_days ', [1])
150
+ foorth_equal('a_day 3/2 * .as_days ', [1.5])
151
+ foorth_equal('a_day 2 * .as_days ', [2])
152
+
153
+ foorth_equal('a_year .as_hours ', [8765.82])
154
+ foorth_equal('a_year 3/2 * .as_hours ', [13148.73])
155
+ foorth_equal('a_year 2 * .as_hours ', [17531.64])
156
+ foorth_equal('a_month .as_hours ', [730.485])
157
+ foorth_equal('a_month 3/2 * .as_hours ', [1095.7275])
158
+ foorth_equal('a_month 2 * .as_hours ', [1460.97])
159
+ foorth_equal('a_day .as_hours ', [24])
160
+ foorth_equal('a_day 3/2 * .as_hours ', [36])
161
+ foorth_equal('a_day 2 * .as_hours ', [48])
162
+ foorth_equal('an_hour .as_hours ', [1])
163
+ foorth_equal('an_hour 3/2 * .as_hours ', [1.5])
164
+ foorth_equal('an_hour 2 * .as_hours ', [2])
165
+
166
+ foorth_equal('a_year .as_minutes', [525949.2])
167
+ foorth_equal('a_year 3/2 * .as_minutes', [788923.8])
168
+ foorth_equal('a_year 2 * .as_minutes', [1051898.4])
169
+ foorth_equal('a_month .as_minutes', [43829.1])
170
+ foorth_equal('a_month 3/2 * .as_minutes', [65743.65])
171
+ foorth_equal('a_month 2 * .as_minutes', [87658.2])
172
+ foorth_equal('a_day .as_minutes', [1440])
173
+ foorth_equal('a_day 3/2 * .as_minutes', [2160])
174
+ foorth_equal('a_day 2 * .as_minutes', [2880])
175
+ foorth_equal('an_hour .as_minutes', [60])
176
+ foorth_equal('an_hour 3/2 * .as_minutes', [90])
177
+ foorth_equal('an_hour 2 * .as_minutes', [120])
178
+ foorth_equal('a_minute .as_minutes', [1])
179
+ foorth_equal('a_minute 3/2 * .as_minutes', [1.5])
180
+ foorth_equal('a_minute 2 * .as_minutes', [2])
181
+
182
+ foorth_equal('a_year .as_seconds', [31556952])
183
+ foorth_equal('a_year 3/2 * .as_seconds', [47335428])
184
+ foorth_equal('a_year 2 * .as_seconds', [63113904])
185
+ foorth_equal('a_month .as_seconds', [2629746])
186
+ foorth_equal('a_month 3/2 * .as_seconds', [3944619])
187
+ foorth_equal('a_month 2 * .as_seconds', [5259492])
188
+ foorth_equal('a_day .as_seconds', [86400])
189
+ foorth_equal('a_day 3/2 * .as_seconds', [129600])
190
+ foorth_equal('a_day 2 * .as_seconds', [172800])
191
+ foorth_equal('an_hour .as_seconds', [3600])
192
+ foorth_equal('an_hour 3/2 * .as_seconds', [5400])
193
+ foorth_equal('an_hour 2 * .as_seconds', [7200])
194
+ foorth_equal('a_minute .as_seconds', [60])
195
+ foorth_equal('a_minute 3/2 * .as_seconds', [90])
196
+ foorth_equal('a_minute 2 * .as_seconds', [120])
197
+ foorth_equal('a_second .as_seconds', [1])
198
+ foorth_equal('a_second 3/2 * .as_seconds', [1.5])
199
+ foorth_equal('a_second 2 * .as_seconds', [2])
200
+
201
+ end
202
+
203
+ def test_converting_to_time
204
+ foorth_equal('0 .to_t', [Time.at(0)])
205
+ foorth_equal('0 .to_t!', [Time.at(0)])
206
+ foorth_equal('infinity .to_t', [nil])
207
+ foorth_raises('infinity .to_t!')
208
+
209
+ foorth_raises('1+1i .to_t')
210
+ foorth_raises('1+1i .to_t!')
211
+
212
+ foorth_equal('"Oct 26 1985 1:22" .to_t', [Time.parse("Oct 26 1985 1:22")])
213
+ foorth_equal('"Oct 26 1985 1:22" .to_t!', [Time.parse("Oct 26 1985 1:22")])
214
+ foorth_equal('"apple" .to_t', [nil])
215
+ foorth_raises('"apple" .to_t!')
216
+
217
+ foorth_equal('0 .to_t .to_t', [Time.at(0)])
218
+ foorth_equal('0 .to_t .to_t!', [Time.at(0)])
219
+ end
220
+
221
+ def test_converting_to_duration
222
+ foorth_equal('0 .to_duration', [XfOOrth::Duration.new("0/1".to_r)])
223
+ foorth_equal('"apple" .to_duration', [nil])
224
+ foorth_equal('infinity .to_duration', [nil])
225
+ foorth_equal('1+2i .to_duration', [nil])
226
+
227
+ foorth_equal('0 .to_duration!', [XfOOrth::Duration.new("0/1".to_r)])
228
+ foorth_raises('"apple" .to_duration!')
229
+ foorth_raises('infinity .to_duration!')
230
+ foorth_raises('1+2i .to_duration!')
231
+
232
+ foorth_equal('[ 0 ] .to_duration', [XfOOrth::Duration.new( 0.to_r)])
233
+ foorth_equal('[ 1 ] .to_duration', [XfOOrth::Duration.new( 1.to_r)])
234
+ foorth_equal('[ 1 1 ] .to_duration', [XfOOrth::Duration.new( 61.to_r)])
235
+ foorth_equal('[ 1 1 1 ] .to_duration', [XfOOrth::Duration.new( 3661.to_r)])
236
+ foorth_equal('[ 1 1 1 1 ] .to_duration', [XfOOrth::Duration.new( 90061.to_r)])
237
+ foorth_equal('[ 1 1 1 1 1 ] .to_duration', [XfOOrth::Duration.new( 2719807.to_r)])
238
+ foorth_equal('[ 1 1 1 1 1 1 ] .to_duration', [XfOOrth::Duration.new(34276759.to_r)])
239
+
240
+ foorth_equal('[ 1 1 1 1 1 1 1 ] .to_duration', [nil])
241
+ foorth_equal('[ 1 "apple" 1 1 ] .to_duration', [nil])
242
+
243
+ foorth_equal('[ 0 ] .to_duration!', [XfOOrth::Duration.new( 0.to_r)])
244
+ foorth_equal('[ 1 ] .to_duration!', [XfOOrth::Duration.new( 1.to_r)])
245
+ foorth_equal('[ 1 1 ] .to_duration!', [XfOOrth::Duration.new( 61.to_r)])
246
+ foorth_equal('[ 1 1 1 ] .to_duration!', [XfOOrth::Duration.new( 3661.to_r)])
247
+ foorth_equal('[ 1 1 1 1 ] .to_duration!', [XfOOrth::Duration.new( 90061.to_r)])
248
+ foorth_equal('[ 1 1 1 1 1 ] .to_duration!', [XfOOrth::Duration.new( 2719807.to_r)])
249
+ foorth_equal('[ 1 1 1 1 1 1 ] .to_duration!', [XfOOrth::Duration.new(34276759.to_r)])
250
+
251
+ foorth_raises('[ 1 1 1 1 1 1 1 ] .to_duration!')
252
+ foorth_raises('[ 1 "apple" 1 1 ] .to_duration!')
253
+
254
+ foorth_equal('[ 2.5 ] .to_duration', [XfOOrth::Duration.new( (2.5).to_r)])
255
+ foorth_equal('[ 2.5 0 ] .to_duration', [XfOOrth::Duration.new( 150.to_r)])
256
+ foorth_equal('[ 2.5 0 0 ] .to_duration', [XfOOrth::Duration.new( 9000.to_r)])
257
+ foorth_equal('[ 2.5 0 0 0 ] .to_duration', [XfOOrth::Duration.new( 216000.to_r)])
258
+
259
+ end
260
+
261
+ def test_converting_from_a_duration
262
+ foorth_equal('5 .to_duration .to_r', ["5/1".to_r])
263
+ foorth_equal('5 .to_duration .to_f', [5.0])
264
+
265
+ foorth_equal('0.4 .to_duration .to_a', [[0, 0, 0, 0, 0, 0.4]])
266
+ foorth_equal('5.4 .to_duration .to_a', [[0, 0, 0, 0, 0, 5.4]])
267
+ foorth_equal('5 .to_duration .to_a', [[0, 0, 0, 0, 0, 5 ]])
268
+ foorth_equal('60 .to_duration .to_a', [[0, 0, 0, 0, 1, 0 ]])
269
+ foorth_equal('31556952 .to_duration .to_a', [[1, 0, 0, 0, 0, 0 ]])
270
+ foorth_equal('315569523/10 .to_duration .to_a', [[1, 0, 0, 0, 0, 0.3]])
271
+
272
+ end
273
+
274
+ def test_duration_comparisons
275
+ foorth_equal("0 .to_duration 0 .to_duration =", [true])
276
+ foorth_equal("1 .to_duration 0 .to_duration =", [false])
277
+ foorth_equal("0 .to_duration 1 .to_duration =", [false])
278
+
279
+ foorth_equal("0 .to_duration 0 =", [true])
280
+ foorth_equal("1 .to_duration 0 =", [false])
281
+ foorth_equal("0 .to_duration 1 =", [false])
282
+
283
+ foorth_equal("0 0 .to_duration =", [true])
284
+ foorth_equal("1 0 .to_duration =", [false])
285
+ foorth_equal("0 1 .to_duration =", [false])
286
+
287
+ foorth_equal('0 .to_duration "to" =', [false])
288
+ foorth_equal("0 .to_duration 1+2i =", [false])
289
+
290
+
291
+ foorth_equal("0 .to_duration! 0 .to_duration <>", [false])
292
+ foorth_equal("1 .to_duration 0 .to_duration <>", [true])
293
+ foorth_equal("0 .to_duration 1 .to_duration <>", [true])
294
+
295
+ foorth_equal("0 .to_duration 0 <>", [false])
296
+ foorth_equal("1 .to_duration 0 <>", [true])
297
+ foorth_equal("0 .to_duration 1 <>", [true])
298
+
299
+ foorth_equal("0 0 .to_duration <>", [false])
300
+ foorth_equal("1 0 .to_duration <>", [true])
301
+ foorth_equal("0 1 .to_duration <>", [true])
302
+
303
+ foorth_equal('0 .to_duration "to" <>', [true])
304
+ foorth_equal("0 .to_duration 1+2i <>", [true])
305
+
306
+
307
+ foorth_equal("0 .to_duration 0 .to_duration >", [false])
308
+ foorth_equal("1 .to_duration 0 .to_duration >", [true])
309
+ foorth_equal("0 .to_duration 1 .to_duration >", [false])
310
+
311
+ foorth_equal("0 .to_duration 0 >", [false])
312
+ foorth_equal("1 .to_duration 0 >", [true])
313
+ foorth_equal("0 .to_duration 1 >", [false])
314
+
315
+ foorth_equal("0 0 .to_duration >", [false])
316
+ foorth_equal("1 0 .to_duration >", [true])
317
+ foorth_equal("0 1 .to_duration >", [false])
318
+
319
+ foorth_raises("0 .to_duration 1+2i >")
320
+ foorth_raises('0 .to_duration "to" >')
321
+
322
+
323
+ foorth_equal("0 .to_duration 0 .to_duration >=", [true])
324
+ foorth_equal("1 .to_duration 0 .to_duration >=", [true])
325
+ foorth_equal("0 .to_duration 1 .to_duration >=", [false])
326
+
327
+ foorth_equal("0 .to_duration 0 >=", [true])
328
+ foorth_equal("1 .to_duration 0 >=", [true])
329
+ foorth_equal("0 .to_duration 1 >=", [false])
330
+
331
+ foorth_equal("0 0 .to_duration >=", [true])
332
+ foorth_equal("1 0 .to_duration >=", [true])
333
+ foorth_equal("0 1 .to_duration >=", [false])
334
+
335
+ foorth_raises("0 .to_duration 1+2i >=")
336
+ foorth_raises('0 .to_duration "to" >=')
337
+
338
+
339
+ foorth_equal("0 .to_duration 0 .to_duration <", [false])
340
+ foorth_equal("1 .to_duration 0 .to_duration <", [false])
341
+ foorth_equal("0 .to_duration 1 .to_duration <", [true])
342
+
343
+ foorth_equal("0 .to_duration 0 <", [false])
344
+ foorth_equal("1 .to_duration 0 <", [false])
345
+ foorth_equal("0 .to_duration 1 <", [true])
346
+
347
+ foorth_equal("0 0 .to_duration <", [false])
348
+ foorth_equal("1 0 .to_duration <", [false])
349
+ foorth_equal("0 1 .to_duration <", [true])
350
+
351
+ foorth_raises("0 .to_duration 1+2i <")
352
+ foorth_raises('0 .to_duration "to" <')
353
+
354
+
355
+ foorth_equal("0 .to_duration 0 .to_duration <=", [true])
356
+ foorth_equal("1 .to_duration 0 .to_duration <=", [false])
357
+ foorth_equal("0 .to_duration 1 .to_duration <=", [true])
358
+
359
+ foorth_equal("0 .to_duration 0 <=", [true])
360
+ foorth_equal("1 .to_duration 0 <=", [false])
361
+ foorth_equal("0 .to_duration 1 <=", [true])
362
+
363
+ foorth_equal("0 0 .to_duration <=", [true])
364
+ foorth_equal("1 0 .to_duration <=", [false])
365
+ foorth_equal("0 1 .to_duration <=", [true])
366
+
367
+ foorth_raises("0 .to_duration 1+2i <=")
368
+ foorth_raises('0 .to_duration "to" <=')
369
+
370
+
371
+ foorth_equal("0 .to_duration 0 .to_duration <=>", [0])
372
+ foorth_equal("1 .to_duration 0 .to_duration <=>", [1])
373
+ foorth_equal("0 .to_duration 1 .to_duration <=>", [-1])
374
+
375
+ foorth_equal("0 .to_duration 0 <=>", [0])
376
+ foorth_equal("1 .to_duration 0 <=>", [1])
377
+ foorth_equal("0 .to_duration 1 <=>", [-1])
378
+
379
+ foorth_equal("0 0 .to_duration <=>", [0])
380
+ foorth_equal("1 0 .to_duration <=>", [1])
381
+ foorth_equal("0 1 .to_duration <=>", [-1])
382
+
383
+ foorth_raises("0 .to_duration 1+2i <=>")
384
+ foorth_raises('0 .to_duration "to" <=>')
385
+ end
386
+
387
+ def test_some_duration_formatting
388
+ foorth_equal('4/3 .to_duration f"%r seconds" ', ["4/3 seconds"])
389
+ foorth_equal('4/3 .to_duration f"%8r seconds" ', [" 4/3 seconds"])
390
+ foorth_equal('4/3 .to_duration f"%-8r seconds" ', ["4/3 seconds"])
391
+ foorth_equal('44/3 .to_duration f"%4r seconds" ', ["44/3 seconds"])
392
+
393
+ foorth_equal('4/3 .to_duration f"%f seconds" ', ["1.333333 seconds"])
394
+ foorth_equal('4/3 .to_duration f"%8.2f seconds" ', [" 1.33 seconds"])
395
+ foorth_equal('4/3 .to_duration f"%-8.2f seconds"', ["1.33 seconds"])
396
+ foorth_equal('100.25 .to_duration f"%4.2f seconds" ', ["100.25 seconds"])
397
+
398
+ foorth_equal('0 .to_duration f"%y%$y" ', ["0 years"])
399
+ foorth_equal('a_year f"%y%$y" ', ["1 year"])
400
+ foorth_equal('a_year 5/2 * f"%3y%$y" ', [" 2 years"])
401
+ foorth_equal('a_year 5/2 * f"%-3y%$y" ', ["2 years"])
402
+
403
+ foorth_equal('0 .to_duration f"%?3y%?$y" ', [""])
404
+ foorth_equal('a_year f"%?3y%?$y" ', [" 1 year"])
405
+ foorth_equal('a_year 5/2 * f"%?3y%?$y" ', [" 2 years"])
406
+ foorth_equal('a_year 5/2 * f"%?-3y%?$y" ', ["2 years"])
407
+
408
+ foorth_equal('0 .to_duration f"%4.1Y%$Y" ', [" 0.0 years"])
409
+ foorth_equal('a_year f"%4.1Y%$Y" ', [" 1.0 year"])
410
+ foorth_equal('a_year 5/2 * f"%4.1Y%$Y" ', [" 2.5 years"])
411
+ foorth_equal('a_year 5/2 * f"%-4.1Y%$Y" ', ["2.5 years"])
412
+
413
+ foorth_equal('0 .to_duration f"%?4.1Y%?$Y" ', [""])
414
+ foorth_equal('a_year f"%?4.1Y%?$Y" ', [" 1.0 year"])
415
+ foorth_equal('a_year 5/2 * f"%?4.1Y%?$Y" ', [" 2.5 years"])
416
+ foorth_equal('a_year 5/2 * f"%?-4.1Y%?$Y" ', ["2.5 years"])
417
+
418
+ foorth_equal('0 .to_duration f"%o%$o" ', ["0 months"])
419
+ foorth_equal('a_year f"%o%$o" ', ["0 months"])
420
+ foorth_equal('a_year 5/2 * f"%3o%$o" ', [" 6 months"])
421
+ foorth_equal('a_year 5/2 * f"%-3o%$o" ', ["6 months"])
422
+ foorth_equal('a_month f"%-3o%$o" ', ["1 month"])
423
+ foorth_equal('a_month 5/2 * f"%-3o%$o" ', ["2 months"])
424
+
425
+ foorth_equal('0 .to_duration f"%?3o%?$o" ', [""])
426
+ foorth_equal('a_year f"%?3o%?$o" ', [""])
427
+ foorth_equal('a_year 5/2 * f"%?3o%?$o" ', [" 6 months"])
428
+ foorth_equal('a_year 5/2 * f"%?-3o%?$o" ', ["6 months"])
429
+ foorth_equal('a_month f"%?-3o%?$o" ', ["1 month"])
430
+ foorth_equal('a_month 5/2 * f"%?-3o%?$o" ', ["2 months"])
431
+
432
+ foorth_equal('0 .to_duration f"%4.1O%$O" ', [" 0.0 months"])
433
+ foorth_equal('a_year f"%4.1O%$O" ', ["12.0 months"])
434
+ foorth_equal('a_year 5/2 * f"%4.1O%$O" ', ["30.0 months"])
435
+ foorth_equal('a_year 5/2 * f"%-4.1O%$O" ', ["30.0 months"])
436
+ foorth_equal('a_month f"%-4.1O%$O" ', ["1.0 month"])
437
+ foorth_equal('a_month 5/2 * f"%-4.1O%$O" ', ["2.5 months"])
438
+
439
+ foorth_equal('0 .to_duration f"%?4.1O%?$O" ', [""])
440
+ foorth_equal('a_year f"%?4.1O%?$O" ', ["12.0 months"])
441
+ foorth_equal('a_year 5/2 * f"%?4.1O%?$O" ', ["30.0 months"])
442
+ foorth_equal('a_year 5/2 * f"%?-4.1O%?$O" ', ["30.0 months"])
443
+ foorth_equal('a_month f"%?-4.1O%?$O" ', ["1.0 month"])
444
+ foorth_equal('a_month 5/2 * f"%?-4.1O%?$O" ', ["2.5 months"])
445
+
446
+ foorth_equal('0 .to_duration f"%d%$d" ', ["0 days"])
447
+ foorth_equal('a_year f"%d%$d" ', ["0 days"])
448
+ foorth_equal('a_month f"%-3d%$d" ', ["0 days"])
449
+ foorth_equal('a_month 5/2 * f"%-3d%$d" ', ["15 days"])
450
+ foorth_equal('a_day f"%-3d%$d" ', ["1 day"])
451
+ foorth_equal('a_day 2 * f"%-3d%$d" ', ["2 days"])
452
+
453
+ foorth_equal('0 .to_duration f"%?d%?$d" ', [""])
454
+ foorth_equal('a_year f"%?d%?$d" ', [""])
455
+ foorth_equal('a_month f"%?-3d%?$d" ', [""])
456
+ foorth_equal('a_month 5/2 * f"%?-3d%?$d" ', ["15 days"])
457
+ foorth_equal('a_day f"%?-3d%?$d" ', ["1 day"])
458
+ foorth_equal('a_day 2 * f"%?-3d%?$d" ', ["2 days"])
459
+
460
+ foorth_equal('0 .to_duration f"%4.1D%$D" ', [" 0.0 days"])
461
+ foorth_equal('a_year f"%4.1D%$D" ', ["365.2 days"])
462
+ foorth_equal('a_month f"%-4.1D%$D" ', ["30.4 days"])
463
+ foorth_equal('a_month 5/2 * f"%-4.1D%$D" ', ["76.1 days"])
464
+ foorth_equal('a_day f"%-4.1D%$D" ', ["1.0 day"])
465
+ foorth_equal('a_day 5/2 * f"%-4.1D%$D" ', ["2.5 days"])
466
+
467
+ foorth_equal('0 .to_duration f"%?4.1D%?$D" ', [""])
468
+ foorth_equal('a_year f"%?4.1D%?$D" ', ["365.2 days"])
469
+ foorth_equal('a_month f"%?-4.1D%?$D" ', ["30.4 days"])
470
+ foorth_equal('a_month 5/2 * f"%?-4.1D%?$D" ', ["76.1 days"])
471
+ foorth_equal('a_day f"%?-4.1D%?$D" ', ["1.0 day"])
472
+ foorth_equal('a_day 5/2 * f"%?-4.1D%?$D" ', ["2.5 days"])
473
+
474
+ foorth_equal('0 .to_duration f"%h%$h" ', ["0 hours"])
475
+ foorth_equal('a_day f"%-3h%$h" ', ["0 hours"])
476
+ foorth_equal('a_day 5/2 * f"%-3h%$h" ', ["12 hours"])
477
+ foorth_equal('an_hour f"%-3h%$h" ', ["1 hour"])
478
+ foorth_equal('an_hour 5/2 * f"%-3h%$h" ', ["2 hours"])
479
+
480
+ foorth_equal('0 .to_duration f"%?h%?$h" ', [""])
481
+ foorth_equal('a_day f"%?-3h%?$h" ', [""])
482
+ foorth_equal('a_day 5/2 * f"%?-3h%?$h" ', ["12 hours"])
483
+ foorth_equal('an_hour f"%?-3h%?$h" ', ["1 hour"])
484
+ foorth_equal('an_hour 5/2 * f"%?-3h%?$h" ', ["2 hours"])
485
+
486
+ foorth_equal('0 .to_duration f"%4.1H%$H" ', [" 0.0 hours"])
487
+ foorth_equal('a_day f"%-4.1H%$H" ', ["24.0 hours"])
488
+ foorth_equal('a_day 5/2 * f"%-4.1H%$H" ', ["60.0 hours"])
489
+ foorth_equal('an_hour f"%-4.1H%$H" ', ["1.0 hour"])
490
+ foorth_equal('an_hour 5/2 * f"%-4.1H%$H" ', ["2.5 hours"])
491
+
492
+ foorth_equal('0 .to_duration f"%?4.1H%?$H" ', [""])
493
+ foorth_equal('a_day f"%?-4.1H%?$H" ', ["24.0 hours"])
494
+ foorth_equal('a_day 5/2 * f"%?-4.1H%?$H" ', ["60.0 hours"])
495
+ foorth_equal('an_hour f"%?-4.1H%?$H" ', ["1.0 hour"])
496
+ foorth_equal('an_hour 5/2 * f"%?-4.1H%?$H" ', ["2.5 hours"])
497
+
498
+ foorth_equal('0 .to_duration f"%m%$m" ', ["0 minutes"])
499
+ foorth_equal('an_hour f"%-3m%$m" ', ["0 minutes"])
500
+ foorth_equal('an_hour 5/2 * f"%-3m%$m" ', ["30 minutes"])
501
+ foorth_equal('a_minute f"%-3m%$m" ', ["1 minute"])
502
+ foorth_equal('a_minute 5/2 * f"%-3m%$m" ', ["2 minutes"])
503
+
504
+ foorth_equal('0 .to_duration f"%?m%?$m" ', [""])
505
+ foorth_equal('an_hour f"%?-3m%?$m" ', [""])
506
+ foorth_equal('an_hour 5/2 * f"%?-3m%?$m" ', ["30 minutes"])
507
+ foorth_equal('a_minute f"%?-3m%?$m" ', ["1 minute"])
508
+ foorth_equal('a_minute 5/2 * f"%?-3m%?$m" ', ["2 minutes"])
509
+
510
+ foorth_equal('0 .to_duration f"%4.1M%$M" ', [" 0.0 minutes"])
511
+ foorth_equal('an_hour f"%-4.1M%$M" ', ["60.0 minutes"])
512
+ foorth_equal('an_hour 5/2 * f"%-4.1M%$M" ', ["150.0 minutes"])
513
+ foorth_equal('a_minute f"%-4.1M%$M" ', ["1.0 minute"])
514
+ foorth_equal('a_minute 5/2 * f"%-4.1M%$M" ', ["2.5 minutes"])
515
+
516
+ foorth_equal('0 .to_duration f"%?4.1M%?$M" ', [""])
517
+ foorth_equal('an_hour f"%?-4.1M%?$M" ', ["60.0 minutes"])
518
+ foorth_equal('an_hour 5/2 * f"%?-4.1M%?$M" ', ["150.0 minutes"])
519
+ foorth_equal('a_minute f"%?-4.1M%?$M" ', ["1.0 minute"])
520
+ foorth_equal('a_minute 5/2 * f"%?-4.1M%?$M" ', ["2.5 minutes"])
521
+
522
+ foorth_equal('0 .to_duration f"%s%$s" ', ["0.000000 seconds"])
523
+ foorth_equal('a_minute f"%-3.0s%$s" ', ["0 seconds"])
524
+ foorth_equal('a_minute 5/2 * f"%-3.0s%$s" ', ["30 seconds"])
525
+ foorth_equal('a_second f"%-3.0s%$s" ', ["1 second"])
526
+ foorth_equal('a_second 5/2 * f"%-3.0s%$s" ', ["2 seconds"])
527
+
528
+ foorth_equal('0 .to_duration f"%?s%?$s" ', [""])
529
+ foorth_equal('a_minute f"%?-3s%?$s" ', [""])
530
+ foorth_equal('a_minute 5/2 * f"%?-3.0s%?$s" ', ["30 seconds"])
531
+ foorth_equal('a_second f"%?-3.0s%?$s" ', ["1 second"])
532
+ foorth_equal('a_second 5/2 * f"%?-3.0s%?$s" ', ["2 seconds"])
533
+
534
+ foorth_equal('0 .to_duration f"%4.1S%$S" ', [" 0.0 seconds"])
535
+ foorth_equal('a_minute f"%-4.1S%$S" ', ["60.0 seconds"])
536
+ foorth_equal('a_minute 5/2 * f"%-4.1S%$S" ', ["150.0 seconds"])
537
+ foorth_equal('a_second f"%-4.1S%$S" ', ["1.0 second"])
538
+ foorth_equal('a_second 5/2 * f"%-4.1S%$S" ', ["2.5 seconds"])
539
+
540
+ foorth_equal('0 .to_duration f"%?4.1S%?$S" ', [""])
541
+ foorth_equal('a_minute f"%?-4.1S%?$S" ', ["60.0 seconds"])
542
+ foorth_equal('a_minute 5/2 * f"%?-4.1S%?$S" ', ["150.0 seconds"])
543
+ foorth_equal('a_second f"%?-4.1S%?$S" ', ["1.0 second"])
544
+ foorth_equal('a_second 5/2 * f"%?-4.1S%?$S" ', ["2.5 seconds"])
545
+
546
+ foorth_equal('a_year 5/2 * f"%4.1B%$B" ', [" 2.5 years"])
547
+ foorth_equal('a_year f"%4.1B%$B" ', [" 1.0 year"])
548
+ foorth_equal('a_month 5/2 * f"%4.1B%$B" ', [" 2.5 months"])
549
+ foorth_equal('a_month f"%4.1B%$B" ', [" 1.0 month"])
550
+ foorth_equal('a_day 5/2 * f"%4.1B%$B" ', [" 2.5 days"])
551
+ foorth_equal('a_day f"%4.1B%$B" ', [" 1.0 day"])
552
+ foorth_equal('an_hour 5/2 * f"%4.1B%$B" ', [" 2.5 hours"])
553
+ foorth_equal('an_hour f"%4.1B%$B" ', [" 1.0 hour"])
554
+ foorth_equal('a_minute 5/2 * f"%4.1B%$B" ', [" 2.5 minutes"])
555
+ foorth_equal('a_minute f"%4.1B%$B" ', [" 1.0 minute"])
556
+ foorth_equal('a_second 5/2 * f"%4.1B%$B" ', [" 2.5 seconds"])
557
+ foorth_equal('a_second f"%4.1B%$B" ', [" 1.0 second"])
558
+ foorth_equal('0 .to_duration f"%4.1B%$B" ', [" 0.0 seconds"])
559
+
560
+ foorth_equal('a_second f"%?4.1B%?$B" ', [" 1.0 second"])
561
+ foorth_equal('0 .to_duration f"%?4.1B%?$B" ', [""])
562
+
563
+ end
564
+
565
+ def test_time_comparisons
566
+ foorth_equal('1434322206 .to_t 1434322206 .to_t > ', [false])
567
+ foorth_equal('1434322200 .to_t 1434322206 .to_t > ', [false])
568
+ foorth_equal('1434322206 .to_t 1434322200 .to_t > ', [true])
569
+
570
+ foorth_equal('1434322206 .to_t 1434322206 .to_t >= ', [true])
571
+ foorth_equal('1434322200 .to_t 1434322206 .to_t >= ', [false])
572
+ foorth_equal('1434322206 .to_t 1434322200 .to_t >= ', [true])
573
+
574
+ foorth_equal('1434322206 .to_t 1434322206 .to_t < ', [false])
575
+ foorth_equal('1434322200 .to_t 1434322206 .to_t < ', [true])
576
+ foorth_equal('1434322206 .to_t 1434322200 .to_t < ', [false])
577
+
578
+ foorth_equal('1434322206 .to_t 1434322206 .to_t <= ', [true])
579
+ foorth_equal('1434322200 .to_t 1434322206 .to_t <= ', [true])
580
+ foorth_equal('1434322206 .to_t 1434322200 .to_t <= ', [false])
581
+
582
+ foorth_equal('1434322206 .to_t 1434322206 .to_t = ', [true])
583
+ foorth_equal('1434322200 .to_t 1434322206 .to_t = ', [false])
584
+ foorth_equal('1434322206 .to_t 1434322200 .to_t = ', [false])
585
+
586
+ foorth_equal('1434322206 .to_t 1434322206 .to_t <> ', [false])
587
+ foorth_equal('1434322200 .to_t 1434322206 .to_t <> ', [true])
588
+ foorth_equal('1434322206 .to_t 1434322200 .to_t <> ', [true])
589
+
590
+ foorth_equal('1434322206 .to_t 1434322200 .to_t <=>', [1])
591
+ foorth_equal('1434322206 .to_t 1434322206 .to_t <=>', [0])
592
+ foorth_equal('1434322200 .to_t 1434322206 .to_t <=>', [-1])
593
+ end
594
+
595
+ def test_some_time_math
596
+ foorth_equal('1434322206 .to_t 100 + ', [Time.at(1434322206+100)])
597
+ foorth_equal('1434322206 .to_t 100 + .class', [Time])
598
+
599
+ foorth_equal('1434322206 .to_t a_minute + ', [Time.at(1434322206+60)])
600
+ foorth_equal('1434322206 .to_t a_minute + .class', [Time])
601
+
602
+ foorth_raises('1434322206 .to_t now + ')
603
+ foorth_raises('1434322206 .to_t "apple" + ')
604
+
605
+ foorth_equal('1434322206 .to_t 100 - ', [Time.at(1434322206-100)])
606
+ foorth_equal('1434322206 .to_t 100 - .class', [Time])
607
+
608
+ foorth_equal('1434322206 .to_t a_minute - ', [Time.at(1434322206-60)])
609
+ foorth_equal('1434322206 .to_t a_minute - .class', [Time])
610
+
611
+ foorth_equal('1434322206 .to_t 1434322206 .to_t - ', [XfOOrth::Duration.new(0.to_r)])
612
+ foorth_equal('1434322206 .to_t 1434322206 .to_t - .class', [XfOOrth::Duration])
613
+
614
+ foorth_raises('1434322206 .to_t "apple" - ')
615
+ end
616
+
617
+ def test_some_duration_math
618
+ foorth_equal('100 50 .to_duration + ', [150] )
619
+ foorth_equal('100 .to_duration 50 + ', [150] )
620
+ foorth_equal('100 .to_duration 50 .to_duration + ', [150] )
621
+
622
+ foorth_equal('100 50 .to_duration + .class ', [Fixnum] )
623
+ foorth_equal('100 .to_duration 50 + .class ', [XfOOrth::Duration] )
624
+ foorth_equal('100 .to_duration 50 .to_duration + .class ', [XfOOrth::Duration] )
625
+
626
+ foorth_equal('100 .to_duration dup 50 + distinct?', [true] )
627
+ foorth_equal('100 .to_duration dup 50 .to_duration + distinct?', [true] )
628
+
629
+
630
+ foorth_equal('100 50 .to_duration - ', [50] )
631
+ foorth_equal('100 .to_duration 50 - ', [50] )
632
+ foorth_equal('100 .to_duration 50 .to_duration - ', [50] )
633
+
634
+ foorth_equal('100 50 .to_duration - .class ', [Fixnum] )
635
+ foorth_equal('100 .to_duration 50 - .class ', [XfOOrth::Duration] )
636
+ foorth_equal('100 .to_duration 50 .to_duration - .class ', [XfOOrth::Duration] )
637
+
638
+ foorth_equal('100 .to_duration dup 50 - distinct?', [true] )
639
+ foorth_equal('100 .to_duration dup 50 .to_duration - distinct?', [true] )
640
+
641
+
642
+ foorth_equal('100 50 .to_duration * ', [5000] )
643
+ foorth_equal('100 .to_duration 50 * ', [5000] )
644
+ foorth_equal('100 .to_duration 50 .to_duration * ', [5000] )
645
+
646
+ foorth_equal('100 50 .to_duration * .class ', [Fixnum] )
647
+ foorth_equal('100 .to_duration 50 * .class ', [XfOOrth::Duration] )
648
+ foorth_equal('100 .to_duration 50 .to_duration * .class ', [XfOOrth::Duration] )
649
+
650
+ foorth_equal('100 .to_duration dup 50 * distinct?', [true] )
651
+ foorth_equal('100 .to_duration dup 50 .to_duration * distinct?', [true] )
652
+
653
+
654
+ foorth_equal('100 50 .to_duration / ', [2] )
655
+ foorth_equal('100 .to_duration 50 / ', [2] )
656
+ foorth_equal('100 .to_duration 50 .to_duration / ', [2] )
657
+
658
+ foorth_equal('100 50 .to_duration / .class ', [Fixnum] )
659
+ foorth_equal('100 .to_duration 50 / .class ', [XfOOrth::Duration] )
660
+ foorth_equal('100 .to_duration 50 .to_duration / .class ', [XfOOrth::Duration] )
661
+
662
+ foorth_equal('100 .to_duration dup 50 / distinct?', [true] )
663
+ foorth_equal('100 .to_duration dup 50 .to_duration / distinct?', [true] )
664
+
665
+ end
666
+
667
+ def test_some_time_to_string
668
+ foorth_equal('1434322206 .to_t .time_s ', [Time.at(1434322206).asctime])
669
+
670
+ foorth_equal('10 .to_duration .to_s', ["Duration instance <10.0 seconds>"])
671
+ end
672
+
673
+ def test_time_array_stuff
674
+ ofs = -14400
675
+
676
+ foorth_equal('1434322200 .to_t .to_a', [[2015, 6, 14, 18, 50, 0.0, ofs]])
677
+
678
+ foorth_equal('[ 2015 6 14 18 50 0.0 -14400 ] .to_t', [Time.at(1434322200)])
679
+ foorth_equal('[ 2015 6 14 18 50 0.0 ] .to_t', [Time.at(1434322200)])
680
+ foorth_equal('[ 2015 6 14 18 50 ] .to_t', [Time.at(1434322200)])
681
+ foorth_equal('[ 2015 6 14 18 ] .to_t', [Time.at(1434322200-(50*60))])
682
+ foorth_equal('[ 2015 6 14 ] .to_t', [Time.at(1434322200-((18*60 + 50)*60))])
683
+ foorth_equal('[ 2015 6 ] .to_t', [Time.at(1434322200-(((13*24 + 18)*60 + 50)*60))])
684
+ foorth_equal('[ 2015 ] .to_t', [Time.at(1434322200-((((151 + 13)*24 + 17)*60 + 50)*60))])
685
+
686
+ foorth_equal('[ 2015 15 14 18 50 0.0 -14400 ] .to_t', [nil])
687
+
688
+ foorth_equal('[ 2015 6 14 18 50 0.0 -14400 ] .to_t!', [Time.at(1434322200)])
689
+ foorth_raises('[ 2015 15 14 18 50 0.0 -14400 ] .to_t!')
690
+ end
691
+
692
+ def test_time_attributes
693
+ foorth_equal('1434322201.5 .to_t .year', [2015])
694
+ foorth_equal('1434322201.5 .to_t .month', [6])
695
+ foorth_equal('1434322201.5 .to_t .day', [14])
696
+
697
+ foorth_equal('1434322201.5 .to_t .hour', [18])
698
+ foorth_equal('1434322201.5 .to_t .minute', [50])
699
+ foorth_equal('1434322201.5 .to_t .second', [1])
700
+
701
+ foorth_equal('1434322201.5 .to_t .fraction', [0.5])
702
+ foorth_equal('1434322201.5 .to_t .sec_frac', [1.5])
703
+
704
+ foorth_equal('1434322201.5 .to_t .offset', [Time.at(1434322201.5).utc_offset])
705
+ end
706
+
707
+ def test_time_zone_control
708
+ ofs = -14400
709
+
710
+ foorth_equal('[ 2015 6 14 18 50 0.0 -14400 ] .to_t .utc?', [false])
711
+ foorth_equal('[ 2015 6 14 18 50 0.0 0 ] .to_t .utc?', [true])
712
+
713
+ foorth_equal('[ 2015 6 14 18 50 0.0 -14400 ] .to_t .as_utc',
714
+ [Time.at(1434322200).localtime(0)])
715
+
716
+ foorth_equal('[ 2015 6 14 18 50 0.0 0 ] .to_t .as_local',
717
+ [Time.at(1434322200+ofs)])
718
+
719
+ foorth_equal('3600 [ 2015 6 14 18 50 0.0 ] .to_t .as_zone',
720
+ [Time.at(1434322200).localtime(3600)])
721
+
722
+ end
723
+
724
+ def test_time_formating
725
+ foorth_equal('1434322201 .to_t f"%A %B %d at %I:%M %p"',
726
+ ["Sunday June 14 at 06:50 PM"])
727
+
728
+ foorth_equal('1434322201 .to_t f"%A %B %d, %r"',
729
+ ["Sunday June 14, 06:50:01 PM"])
730
+
731
+ foorth_equal('1434322201 .to_t f"%A %B %d, %T"',
732
+ ["Sunday June 14, 18:50:01"])
733
+
734
+ foorth_equal('1434322201 .to_t "%A %B %d at %I:%M %p" format',
735
+ ["Sunday June 14 at 06:50 PM"])
736
+
737
+ foorth_equal('1434322201 .to_t "%A %B %d, %r" format',
738
+ ["Sunday June 14, 06:50:01 PM"])
739
+
740
+ foorth_equal('1434322201 .to_t "%A %B %d, %T" format',
741
+ ["Sunday June 14, 18:50:01"])
742
+ end
743
+
744
+ def test_time_parsing
745
+ foorth_equal('"Sunday June 14, 2015 at 06:50 PM" Time p"%A %B %d, %Y at %I:%M %p"',
746
+ [Time.at(1434322200)])
747
+
748
+ foorth_equal('"Someday June 14 at 06:50 PM" Time p"%A %B %d at %I:%M %p"',
749
+ [nil])
750
+
751
+ foorth_equal('"Sunday June 14, 2015 at 06:50 PM" Time p!"%A %B %d, %Y at %I:%M %p"',
752
+ [Time.at(1434322200)])
753
+
754
+ foorth_raises('"Someday June 14 at 06:50 PM" Time p!"%A %B %d at %I:%M %p"')
755
+
756
+
757
+ foorth_equal('"Sunday June 14, 2015 at 06:50 PM" Time "%A %B %d, %Y at %I:%M %p" parse',
758
+ [Time.at(1434322200)])
759
+
760
+ foorth_equal('"Someday June 14 at 06:50 PM" Time "%A %B %d at %I:%M %p" parse',
761
+ [nil])
762
+
763
+ foorth_equal('"Sunday June 14, 2015 at 06:50 PM" Time "%A %B %d, %Y at %I:%M %p" parse!',
764
+ [Time.at(1434322200)])
765
+
766
+ foorth_raises('"Someday June 14 at 06:50 PM" Time "%A %B %d at %I:%M %p" parse!')
767
+ end
768
+
769
+ def test_some_duration_formatting_support
770
+ foorth_equal('0 .to_duration .largest_interval', [5])
771
+ foorth_equal('0.1 .to_duration .largest_interval', [5])
772
+ foorth_equal('1 .to_duration .largest_interval', [5])
773
+ foorth_equal('59 .to_duration .largest_interval', [5])
774
+
775
+ foorth_equal('60 .to_duration .largest_interval', [4])
776
+ foorth_equal('60.02 .to_duration .largest_interval', [4])
777
+ foorth_equal('120.02 .to_duration .largest_interval', [4])
778
+ foorth_equal('3120.02 .to_duration .largest_interval', [4])
779
+ foorth_equal('3599.99 .to_duration .largest_interval', [4])
780
+
781
+ foorth_equal('3600 .to_duration .largest_interval', [3])
782
+ foorth_equal('86399 .to_duration .largest_interval', [3])
783
+
784
+ foorth_equal('86400 .to_duration .largest_interval', [2])
785
+
786
+ foorth_equal('2629746 .to_duration .largest_interval', [1])
787
+
788
+ foorth_equal('31556952 .to_duration .largest_interval', [0])
789
+ end
790
+
791
+ end