aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/common/lib/forth2012/tester/doubletest.fth
blob: 523b110a457bfce08e5e9461b4f733b6f84b6a04 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
\ To test the ANS Forth Double-Number word set and double number extensions

\ This program was written by Gerry Jackson in 2006, with contributions from
\ others where indicated, and is in the public domain - it can be distributed
\ and/or modified in any way but please retain this notice.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

\ The tests are not claimed to be comprehensive or correct 
\ ------------------------------------------------------------------------------
\ Version 0.6   1 April 2012 Tests placed in the public domain.
\               Immediate 2CONSTANTs and 2VARIABLEs tested
\         0.5   20 November 2009 Various constants renamed to avoid
\               redefinition warnings. <true> and <false> replaced
\               with TRUE and FALSE
\         0.4   6 March 2009 { and } replaced with T{ and }T
\               Tests rewritten to be independent of word size and
\               tests re-ordered
\         0.3   20 April 2007 ANS Forth words changed to upper case
\         0.2   30 Oct 2006 Updated following GForth test to include
\               various constants from core.fr
\         0.1   Oct 2006 First version released
\ ------------------------------------------------------------------------------
\ The tests are based on John Hayes test program for the core word set

\ Words tested in this file are:
\     2CONSTANT 2LITERAL 2VARIABLE D+ D- D. D.R D0< D0= D2* D2/
\     D< D= D>S DABS DMAX DMIN DNEGATE M*/ M+ 2ROT DU<
\ Also tests the interpreter and compiler reading a double number
\ ------------------------------------------------------------------------------
\ Assumptions and dependencies:
\     - tester.fr or ttester.fs has been included prior to this file
\     - core words and core extension words have been tested
\ ------------------------------------------------------------------------------
\ Constant definitions

DECIMAL
0 INVERT        CONSTANT 1sd
1sd 1 RSHIFT    CONSTANT max-intd   \ 01...1
max-intd INVERT CONSTANT min-intd   \ 10...0
max-intd 2/     CONSTANT hi-int     \ 001...1
min-intd 2/     CONSTANT lo-int     \ 110...1

\ ------------------------------------------------------------------------------
TESTING interpreter and compiler reading a double number

T{ 1. -> 1 0 }T
T{ -2. -> -2 -1 }T
T{ : rdl1 3. ; rdl1 -> 3 0 }T
T{ : rdl2 -4. ; rdl2 -> -4 -1 }T

\ ------------------------------------------------------------------------------
TESTING 2CONSTANT

T{ 1 2 2CONSTANT 2c1 -> }T
T{ 2c1 -> 1 2 }T
T{ : cd1 2c1 ; -> }T
T{ cd1 -> 1 2 }T
T{ : cd2 2CONSTANT ; -> }T
T{ -1 -2 cd2 2c2 -> }T
T{ 2c2 -> -1 -2 }T
T{ 4 5 2CONSTANT 2c3 IMMEDIATE 2c3 -> 4 5 }T
T{ : cd6 2c3 2LITERAL ; cd6 -> 4 5 }T

\ ------------------------------------------------------------------------------
\ Some 2CONSTANTs for the following tests

1sd max-intd 2CONSTANT max-2int  \ 01...1
0   min-intd 2CONSTANT min-2int  \ 10...0
max-2int 2/  2CONSTANT hi-2int   \ 001...1
min-2int 2/  2CONSTANT lo-2int   \ 110...0

\ ------------------------------------------------------------------------------
TESTING DNEGATE

T{ 0. DNEGATE -> 0. }T
T{ 1. DNEGATE -> -1. }T
T{ -1. DNEGATE -> 1. }T
T{ max-2int DNEGATE -> min-2int SWAP 1+ SWAP }T
T{ min-2int SWAP 1+ SWAP DNEGATE -> max-2int }T

\ ------------------------------------------------------------------------------
TESTING D+ with small integers

T{  0.  5. D+ ->  5. }T
T{ -5.  0. D+ -> -5. }T
T{  1.  2. D+ ->  3. }T
T{  1. -2. D+ -> -1. }T
T{ -1.  2. D+ ->  1. }T
T{ -1. -2. D+ -> -3. }T
T{ -1.  1. D+ ->  0. }T

TESTING D+ with mid range integers

T{  0  0  0  5 D+ ->  0  5 }T
T{ -1  5  0  0 D+ -> -1  5 }T
T{  0  0  0 -5 D+ ->  0 -5 }T
T{  0 -5 -1  0 D+ -> -1 -5 }T
T{  0  1  0  2 D+ ->  0  3 }T
T{ -1  1  0 -2 D+ -> -1 -1 }T
T{  0 -1  0  2 D+ ->  0  1 }T
T{  0 -1 -1 -2 D+ -> -1 -3 }T
T{ -1 -1  0  1 D+ -> -1  0 }T
T{ min-intd 0 2DUP D+ -> 0 1 }T
T{ min-intd S>D min-intd 0 D+ -> 0 0 }T

TESTING D+ with large double integers

T{ hi-2int 1. D+ -> 0 hi-int 1+ }T
T{ hi-2int 2DUP D+ -> 1sd 1- max-intd }T
T{ max-2int min-2int D+ -> -1. }T
T{ max-2int lo-2int D+ -> hi-2int }T
T{ hi-2int min-2int D+ 1. D+ -> lo-2int }T
T{ lo-2int 2DUP D+ -> min-2int }T

\ ------------------------------------------------------------------------------
TESTING D- with small integers

T{  0.  5. D- -> -5. }T
T{  5.  0. D- ->  5. }T
T{  0. -5. D- ->  5. }T
T{  1.  2. D- -> -1. }T
T{  1. -2. D- ->  3. }T
T{ -1.  2. D- -> -3. }T
T{ -1. -2. D- ->  1. }T
T{ -1. -1. D- ->  0. }T

TESTING D- with mid-range integers

T{  0  0  0  5 D- ->  0 -5 }T
T{ -1  5  0  0 D- -> -1  5 }T
T{  0  0 -1 -5 D- ->  1  4 }T
T{  0 -5  0  0 D- ->  0 -5 }T
T{ -1  1  0  2 D- -> -1 -1 }T
T{  0  1 -1 -2 D- ->  1  2 }T
T{  0 -1  0  2 D- ->  0 -3 }T
T{  0 -1  0 -2 D- ->  0  1 }T
T{  0  0  0  1 D- ->  0 -1 }T
T{ min-intd 0 2DUP D- -> 0. }T
T{ min-intd S>D max-intd 0 D- -> 1 1sd }T

TESTING D- with large integers

T{ max-2int max-2int D- -> 0. }T
T{ min-2int min-2int D- -> 0. }T
T{ max-2int hi-2int  D- -> lo-2int DNEGATE }T
T{ hi-2int  lo-2int  D- -> max-2int }T
T{ lo-2int  hi-2int  D- -> min-2int 1. D+ }T
T{ min-2int min-2int D- -> 0. }T
T{ min-2int lo-2int  D- -> lo-2int }T

\ ------------------------------------------------------------------------------
TESTING D0< D0=

T{ 0. D0< -> FALSE }T
T{ 1. D0< -> FALSE }T
T{ min-intd 0 D0< -> FALSE }T
T{ 0 max-intd D0< -> FALSE }T
T{ max-2int  D0< -> FALSE }T
T{ -1. D0< -> TRUE }T
T{ min-2int D0< -> TRUE }T

T{ 1. D0= -> FALSE }T
T{ min-intd 0 D0= -> FALSE }T
T{ max-2int  D0= -> FALSE }T
T{ -1 max-intd D0= -> FALSE }T
T{ 0. D0= -> TRUE }T
T{ -1. D0= -> FALSE }T
T{ 0 min-intd D0= -> FALSE }T

\ ------------------------------------------------------------------------------
TESTING D2* D2/

T{ 0. D2* -> 0. D2* }T
T{ min-intd 0 D2* -> 0 1 }T
T{ hi-2int D2* -> max-2int 1. D- }T
T{ lo-2int D2* -> min-2int }T

T{ 0. D2/ -> 0. }T
T{ 1. D2/ -> 0. }T
T{ 0 1 D2/ -> min-intd 0 }T
T{ max-2int D2/ -> hi-2int }T
T{ -1. D2/ -> -1. }T
T{ min-2int D2/ -> lo-2int }T

\ ------------------------------------------------------------------------------
TESTING D< D=

T{  0.  1. D< -> TRUE  }T
T{  0.  0. D< -> FALSE }T
T{  1.  0. D< -> FALSE }T
T{ -1.  1. D< -> TRUE  }T
T{ -1.  0. D< -> TRUE  }T
T{ -2. -1. D< -> TRUE  }T
T{ -1. -2. D< -> FALSE }T
T{ -1. max-2int D< -> TRUE }T
T{ min-2int max-2int D< -> TRUE }T
T{ max-2int -1. D< -> FALSE }T
T{ max-2int min-2int D< -> FALSE }T
T{ max-2int 2DUP -1. D+ D< -> FALSE }T
T{ min-2int 2DUP  1. D+ D< -> TRUE  }T

T{ -1. -1. D= -> TRUE  }T
T{ -1.  0. D= -> FALSE }T
T{ -1.  1. D= -> FALSE }T
T{  0. -1. D= -> FALSE }T
T{  0.  0. D= -> TRUE  }T
T{  0.  1. D= -> FALSE }T
T{  1. -1. D= -> FALSE }T
T{  1.  0. D= -> FALSE }T
T{  1.  1. D= -> TRUE  }T

T{ 0 -1 0 -1 D= -> TRUE  }T
T{ 0 -1 0  0 D= -> FALSE }T
T{ 0 -1 0  1 D= -> FALSE }T
T{ 0  0 0 -1 D= -> FALSE }T
T{ 0  0 0  0 D= -> TRUE  }T
T{ 0  0 0  1 D= -> FALSE }T
T{ 0  1 0 -1 D= -> FALSE }T
T{ 0  1 0  0 D= -> FALSE }T
T{ 0  1 0  1 D= -> TRUE  }T

T{ max-2int min-2int D= -> FALSE }T
T{ max-2int 0. D= -> FALSE }T
T{ max-2int max-2int D= -> TRUE }T
T{ max-2int hi-2int  D= -> FALSE }T
T{ max-2int min-2int D= -> FALSE }T
T{ min-2int min-2int D= -> TRUE }T
T{ min-2int lo-2int  D=  -> FALSE }T
T{ min-2int max-2int D= -> FALSE }T

\ ------------------------------------------------------------------------------
TESTING 2LITERAL 2VARIABLE

T{ : cd3 [ max-2int ] 2LITERAL ; -> }T
T{ cd3 -> max-2int }T
T{ 2VARIABLE 2v1 -> }T
T{ 0. 2v1 2! -> }T
T{ 2v1 2@ -> 0. }T
T{ -1 -2 2v1 2! -> }T
T{ 2v1 2@ -> -1 -2 }T
T{ : cd4 2VARIABLE ; -> }T
T{ cd4 2v2 -> }T
T{ : cd5 2v2 2! ; -> }T
T{ -2 -1 cd5 -> }T
T{ 2v2 2@ -> -2 -1 }T
T{ 2VARIABLE 2v3 IMMEDIATE 5 6 2v3 2! -> }T
T{ 2v3 2@ -> 5 6 }T
T{ : cd7 2v3 [ 2@ ] 2LITERAL ; cd7 -> 5 6 }T
T{ : cd8 [ 6 7 ] 2v3 [ 2! ] ; 2v3 2@ -> 6 7 }T

\ ------------------------------------------------------------------------------
TESTING DMAX DMIN

T{  1.  2. DMAX -> 2. }T
T{  1.  0. DMAX -> 1. }T
T{  1. -1. DMAX -> 1. }T
T{  1.  1. DMAX -> 1. }T
T{  0.  1. DMAX -> 1. }T
T{  0. -1. DMAX -> 0. }T
T{ -1.  1. DMAX -> 1. }T
T{ -1. -2. DMAX -> -1. }T

T{ max-2int hi-2int  DMAX -> max-2int }T
T{ max-2int min-2int DMAX -> max-2int }T
T{ min-2int max-2int DMAX -> max-2int }T
T{ min-2int lo-2int  DMAX -> lo-2int  }T

T{ max-2int  1. DMAX -> max-2int }T
T{ max-2int -1. DMAX -> max-2int }T
T{ min-2int  1. DMAX ->  1. }T
T{ min-2int -1. DMAX -> -1. }T


T{  1.  2. DMIN ->  1. }T
T{  1.  0. DMIN ->  0. }T
T{  1. -1. DMIN -> -1. }T
T{  1.  1. DMIN ->  1. }T
T{  0.  1. DMIN ->  0. }T
T{  0. -1. DMIN -> -1. }T
T{ -1.  1. DMIN -> -1. }T
T{ -1. -2. DMIN -> -2. }T

T{ max-2int hi-2int  DMIN -> hi-2int  }T
T{ max-2int min-2int DMIN -> min-2int }T
T{ min-2int max-2int DMIN -> min-2int }T
T{ min-2int lo-2int  DMIN -> min-2int }T

T{ max-2int  1. DMIN ->  1. }T
T{ max-2int -1. DMIN -> -1. }T
T{ min-2int  1. DMIN -> min-2int }T
T{ min-2int -1. DMIN -> min-2int }T

\ ------------------------------------------------------------------------------
TESTING D>S DABS

T{  1234  0 D>S ->  1234 }T
T{ -1234 -1 D>S -> -1234 }T
T{ max-intd  0 D>S -> max-intd }T
T{ min-intd -1 D>S -> min-intd }T

T{  1. DABS -> 1. }T
T{ -1. DABS -> 1. }T
T{ max-2int DABS -> max-2int }T
T{ min-2int 1. D+ DABS -> max-2int }T

\ ------------------------------------------------------------------------------
TESTING M+ M*/

T{ hi-2int   1 M+ -> hi-2int   1. D+ }T
T{ max-2int -1 M+ -> max-2int -1. D+ }T
T{ min-2int  1 M+ -> min-2int  1. D+ }T
T{ lo-2int  -1 M+ -> lo-2int  -1. D+ }T

\ To correct the result if the division is floored, only used when
\ necessary i.e. negative quotient and remainder <> 0

: ?floored [ -3 2 / -2 = ] LITERAL IF 1. D- THEN ;

T{  5.  7 11 M*/ ->  3. }T
T{  5. -7 11 M*/ -> -3. ?floored }T    \ floored -4.
T{ -5.  7 11 M*/ -> -3. ?floored }T    \ floored -4.
T{ -5. -7 11 M*/ ->  3. }T
T{ max-2int  8 16 M*/ -> hi-2int }T
T{ max-2int -8 16 M*/ -> hi-2int DNEGATE ?floored }T  \ floored subtract 1
T{ min-2int  8 16 M*/ -> lo-2int }T
T{ min-2int -8 16 M*/ -> lo-2int DNEGATE }T
T{ max-2int max-intd max-intd M*/ -> max-2int }T
T{ max-2int max-intd 2/ max-intd M*/ -> max-intd 1- hi-2int NIP }T
T{ min-2int lo-2int NIP DUP NEGATE M*/ -> min-2int }T
T{ min-2int lo-2int NIP 1- max-intd M*/ -> min-intd 3 + hi-2int NIP 2 + }T
T{ max-2int lo-2int NIP DUP NEGATE M*/ -> max-2int DNEGATE }T
T{ min-2int max-intd DUP M*/ -> min-2int }T

\ ------------------------------------------------------------------------------
TESTING D. D.R

\ Create some large double numbers
max-2int 71 73 M*/ 2CONSTANT dbl1
min-2int 73 79 M*/ 2CONSTANT dbl2

: d>ascii  ( d -- caddr u )
   DUP >R <# DABS #S R> SIGN #>    ( -- caddr1 u )
   HERE SWAP 2DUP 2>R CHARS DUP ALLOT MOVE 2R>
;

dbl1 d>ascii 2CONSTANT "dbl1"
dbl2 d>ascii 2CONSTANT "dbl2"

: DoubleOutput
   CR ." You should see lines duplicated:" CR
   5 SPACES "dbl1" TYPE CR
   5 SPACES dbl1 D. CR
   8 SPACES "dbl1" DUP >R TYPE CR
   5 SPACES dbl1 R> 3 + D.R CR
   5 SPACES "dbl2" TYPE CR
   5 SPACES dbl2 D. CR
   10 SPACES "dbl2" DUP >R TYPE CR
   5 SPACES dbl2 R> 5 + D.R CR
;

T{ DoubleOutput -> }T

\ ------------------------------------------------------------------------------
TESTING 2ROT DU< (Double Number extension words)

T{ 1. 2. 3. 2ROT -> 2. 3. 1. }T
T{ max-2int min-2int 1. 2ROT -> min-2int 1. max-2int }T

T{  1.  1. DU< -> FALSE }T
T{  1. -1. DU< -> TRUE  }T
T{ -1.  1. DU< -> FALSE }T
T{ -1. -2. DU< -> FALSE }T

T{ max-2int hi-2int  DU< -> FALSE }T
T{ hi-2int  max-2int DU< -> TRUE  }T
T{ max-2int min-2int DU< -> TRUE }T
T{ min-2int max-2int DU< -> FALSE }T
T{ min-2int lo-2int  DU< -> TRUE }T

\ ------------------------------------------------------------------------------

CR .( End of Double-Number word tests) CR