PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/perturb/coreplustest.fth

https://github.com/chitselb/pettil
Forth | 198 lines | 136 code | 36 blank | 26 comment | 1 complexity | 6f647e0edc2c4fd85bdb1d544f5d874f MD5 | raw file
  1. \ Additional tests on the the ANS Forth Core word set
  2. \ This program was written by Gerry Jackson in 2007, with contributions from
  3. \ others where indicated, and is in the public domain - it can be distributed
  4. \ and/or modified in any way but please retain this notice.
  5. \ This program is distributed in the hope that it will be useful,
  6. \ but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. \ The tests are not claimed to be comprehensive or correct
  9. \ ------------------------------------------------------------------------------
  10. \ Version 0.10 3 August 2014 Test IMMEDIATE doesn't toggle an immediate flag
  11. \ 0.3 1 April 2012 Tests placed in the public domain.
  12. \ Testing multiple ELSE's.
  13. \ Further tests on DO +LOOPs.
  14. \ Ackermann function added to test RECURSE.
  15. \ >IN manipulation in interpreter mode
  16. \ Immediate CONSTANTs, VARIABLEs and CREATEd words tests.
  17. \ :NONAME with RECURSE moved to core extension tests.
  18. \ Parsing behaviour of S" ." and ( tested
  19. \ 0.2 6 March 2009 { and } replaced with T{ and }T
  20. \ Added extra RECURSE tests
  21. \ 0.1 20 April 2007 Created
  22. \ ------------------------------------------------------------------------------
  23. \ The tests are based on John Hayes test program for the core word set
  24. \
  25. \ This file provides some more tests on Core words where the original Hayes
  26. \ tests are thought to be incomplete
  27. \
  28. \ Words tested in this file are:
  29. \ DO +LOOP RECURSE ELSE >IN IMMEDIATE
  30. \ ------------------------------------------------------------------------------
  31. \ Assumptions and dependencies:
  32. \ - tester.fr or ttester.fs has been loaded prior to this file
  33. \ - core.fr has been loaded so that constants MAX-INT, MIN-INT and
  34. \ MAX-UINT are defined
  35. \ ------------------------------------------------------------------------------
  36. DECIMAL
  37. TESTING DO +LOOP with run-time increment, negative increment, infinite loop
  38. \ Contributed by Reinhold Straub
  39. VARIABLE iterations
  40. VARIABLE increment
  41. : gd7 ( limit start increment -- )
  42. increment !
  43. 0 iterations !
  44. DO
  45. 1 iterations +!
  46. I
  47. iterations @ 6 = IF LEAVE THEN
  48. increment @
  49. +LOOP iterations @
  50. ;
  51. T{ 4 4 -1 gd7 -> 4 1 }T
  52. T{ 1 4 -1 gd7 -> 4 3 2 1 4 }T
  53. T{ 4 1 -1 gd7 -> 1 0 -1 -2 -3 -4 6 }T
  54. T{ 4 1 0 gd7 -> 1 1 1 1 1 1 6 }T
  55. T{ 0 0 0 gd7 -> 0 0 0 0 0 0 6 }T
  56. T{ 1 4 0 gd7 -> 4 4 4 4 4 4 6 }T
  57. T{ 1 4 1 gd7 -> 4 5 6 7 8 9 6 }T
  58. T{ 4 1 1 gd7 -> 1 2 3 3 }T
  59. T{ 4 4 1 gd7 -> 4 5 6 7 8 9 6 }T
  60. T{ 2 -1 -1 gd7 -> -1 -2 -3 -4 -5 -6 6 }T
  61. T{ -1 2 -1 gd7 -> 2 1 0 -1 4 }T
  62. T{ 2 -1 0 gd7 -> -1 -1 -1 -1 -1 -1 6 }T
  63. T{ -1 2 0 gd7 -> 2 2 2 2 2 2 6 }T
  64. T{ -1 2 1 gd7 -> 2 3 4 5 6 7 6 }T
  65. T{ 2 -1 1 gd7 -> -1 0 1 3 }T
  66. T{ -20 30 -10 gd7 -> 30 20 10 0 -10 -20 6 }T
  67. T{ -20 31 -10 gd7 -> 31 21 11 1 -9 -19 6 }T
  68. T{ -20 29 -10 gd7 -> 29 19 9 -1 -11 5 }T
  69. \ ------------------------------------------------------------------------------
  70. TESTING DO +LOOP with large and small increments
  71. \ Contributed by Andrew Haley
  72. MAX-UINT 8 RSHIFT 1+ CONSTANT ustep
  73. ustep NEGATE CONSTANT -ustep
  74. MAX-INT 7 RSHIFT 1+ CONSTANT step
  75. step NEGATE CONSTANT -step
  76. VARIABLE bump
  77. T{ : gd8 bump ! DO 1+ bump @ +LOOP ; -> }T
  78. T{ 0 MAX-UINT 0 ustep gd8 -> 256 }T
  79. T{ 0 0 MAX-UINT -ustep gd8 -> 256 }T
  80. T{ 0 MAX-INT MIN-INT step gd8 -> 256 }T
  81. T{ 0 MIN-INT MAX-INT -step gd8 -> 256 }T
  82. \ Two's complement arithmetic, wraps around modulo wordsize
  83. \ Only tested if the Forth system does wrap around, use of conditional
  84. \ compilation deliberately avoided
  85. MAX-INT 1+ MIN-INT = CONSTANT +wrap?
  86. MIN-INT 1- MAX-INT = CONSTANT -wrap?
  87. MAX-UINT 1+ 0= CONSTANT +uwrap?
  88. 0 1- MAX-UINT = CONSTANT -uwrap?
  89. : gd9 ( n limit start step f result -- )
  90. >R IF gd8 ELSE 2DROP 2DROP R@ THEN -> R> }T
  91. ;
  92. T{ 0 0 0 ustep +uwrap? 256 gd9
  93. T{ 0 0 0 -ustep -uwrap? 1 gd9
  94. T{ 0 MIN-INT MAX-INT step +wrap? 1 gd9
  95. T{ 0 MAX-INT MIN-INT -step -wrap? 1 gd9
  96. \ ------------------------------------------------------------------------------
  97. TESTING DO +LOOP with maximum and minimum increments
  98. : (-mi) MAX-INT DUP NEGATE + 0= IF MAX-INT NEGATE ELSE -32767 THEN ;
  99. (-mi) CONSTANT -max-int
  100. T{ 0 1 0 MAX-INT gd8 -> 1 }T
  101. T{ 0 -max-int NEGATE -max-int OVER gd8 -> 2 }T
  102. T{ 0 MAX-INT 0 MAX-INT gd8 -> 1 }T
  103. T{ 0 MAX-INT 1 MAX-INT gd8 -> 1 }T
  104. T{ 0 MAX-INT -1 MAX-INT gd8 -> 2 }T
  105. T{ 0 MAX-INT dup 1- MAX-INT gd8 -> 1 }T
  106. T{ 0 MIN-INT 1+ 0 MIN-INT gd8 -> 1 }T
  107. T{ 0 MIN-INT 1+ -1 MIN-INT gd8 -> 1 }T
  108. T{ 0 MIN-INT 1+ 1 MIN-INT gd8 -> 2 }T
  109. T{ 0 MIN-INT 1+ DUP MIN-INT gd8 -> 1 }T
  110. \ ------------------------------------------------------------------------------
  111. TESTING multiple RECURSEs in one colon definition
  112. : ack ( m n -- u ) \ Ackermann function, from Rosetta Code
  113. OVER 0= IF NIP 1+ EXIT THEN \ ack(0, n) = n+1
  114. SWAP 1- SWAP ( -- m-1 n )
  115. DUP 0= IF 1+ RECURSE EXIT THEN \ ack(m, 0) = ack(m-1, 1)
  116. 1- OVER 1+ SWAP RECURSE RECURSE \ ack(m, n) = ack(m-1, ack(m,n-1))
  117. ;
  118. T{ 0 0 ack -> 1 }T
  119. T{ 3 0 ack -> 5 }T
  120. T{ 2 4 ack -> 11 }T
  121. \ ------------------------------------------------------------------------------
  122. TESTING multiple ELSE's in an IF statement
  123. \ Discussed on comp.lang.forth and accepted as valid ANS Forth
  124. : melse IF 1 ELSE 2 ELSE 3 ELSE 4 ELSE 5 THEN ;
  125. T{ 0 melse -> 2 4 }T
  126. T{ -1 melse -> 1 3 5 }T
  127. \ ------------------------------------------------------------------------------
  128. TESTING manipulation of >IN in interpreter mode
  129. T{ 123456 depth over 9 < 35 and + 3 + >in ! -> 123456 23456 3456 456 56 6 }T
  130. T{ 14145 8115 ?dup 0= 34 and >in +! tuck mod 14 >in ! GCD calculation -> 15 }T
  131. \ ------------------------------------------------------------------------------
  132. TESTING IMMEDIATE with CONSTANT VARIABLE and CREATE [ ... DOES> ]
  133. T{ 123 CONSTANT iw1 IMMEDIATE iw1 -> 123 }T
  134. T{ : iw2 iw1 LITERAL ; iw2 -> 123 }T
  135. T{ VARIABLE iw3 IMMEDIATE 234 iw3 ! iw3 @ -> 234 }T
  136. T{ : iw4 iw3 [ @ ] LITERAL ; iw4 -> 234 }T
  137. T{ :noname [ 345 ] iw3 [ ! ] ; DROP iw3 @ -> 345 }T
  138. T{ CREATE iw5 456 , IMMEDIATE -> }T
  139. T{ :noname iw5 [ @ iw3 ! ] ; DROP iw3 @ -> 456 }T
  140. T{ : iw6 CREATE , IMMEDIATE DOES> @ 1+ ; -> }T
  141. T{ 111 iw6 iw7 iw7 -> 112 }T
  142. T{ : iw8 iw7 LITERAL 1+ ; iw8 -> 113 }T
  143. T{ : iw9 CREATE , DOES> @ 2 + IMMEDIATE ; -> }T
  144. : find-iw bl word find nip ; ( -- 0 | 1 | -1 )
  145. T{ 222 iw9 iw10 find-iw iw10 -> -1 }T \ iw10 is not immediate
  146. T{ iw10 find-iw iw10 -> 224 1 }T \ iw10 becomes immediate
  147. \ ------------------------------------------------------------------------------
  148. TESTING that IMMEDIATE doesn't toggle a flag
  149. VARIABLE it1 0 it1 !
  150. : it2 1234 it1 ! ; IMMEDIATE IMMEDIATE
  151. T{ : it3 it2 ; it1 @ -> 1234 }T
  152. \ ------------------------------------------------------------------------------
  153. TESTING parsing behaviour of S" ." and (
  154. \ which should parse to just beyond the terminating character no space needed
  155. T{ S" A string"2DROP -> }T
  156. T{ ( A comment)1234 -> 1234 }T
  157. T{ : pb1 cr ." You should see 2345: "." 2345"( A comment) CR ; pb1 -> }T
  158. \ ------------------------------------------------------------------------------
  159. CR .( End of additional Core tests) CR