PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/coreexttest.fth

https://gitlab.com/BGCX261/zmforth-hg-to-git
Forth | 247 lines | 190 code | 57 blank | 0 comment | 3 complexity | b3fb55d417991cd906e0977dbf65a096 MD5 | raw file
Possible License(s): GPL-3.0
  1. \ To test some of the ANS Forth Core Ext word set, version 0.1
  2. \ Copyright (C) Gerry Jackson 2006, 2007
  3. \ This program is free software; you can redistribute it and/or
  4. \ modify it any way.
  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.3 6 March 2009 { and } replaced with T{ and }T
  11. \ CONVERT test now independent of cell size
  12. \ 0.2 20 April 2007 ANS Forth words changed to upper case
  13. \ Tests qd3 to qd6 by Reinhold Straub
  14. \ 0.1 Oct 2006 First version released
  15. \ --------------------------------------------------------------------
  16. \ This is only a partial test of the core extension words.
  17. \ The tests are based on John Hayes test program for the core word set
  18. \ Words tested in this file are:
  19. \ TRUE FALSE :NONAME ?DO VALUE TO CASE OF ENDOF ENDCASE
  20. \ C" CONVERT COMPILE, [COMPILE] SAVE-INPUT RESTORE-INPUT
  21. \ NIP TUCK ROLL WITHIN
  22. \ --------------------------------------------------------------------
  23. \ Assumptions:
  24. \ - tester.fr has been included prior to this file
  25. \ - core words to have been tested
  26. \ --------------------------------------------------------------------
  27. Testing Core Extension words
  28. DECIMAL
  29. 0 INVERT 1 RSHIFT CONSTANT max-int \ 01...1
  30. Testing TRUE FALSE
  31. T{ TRUE -> 0 INVERT }T
  32. T{ FALSE -> 0 }T
  33. \ --------------------------------------------------------------------
  34. Testing :NONAME
  35. VARIABLE nn1
  36. VARIABLE nn2
  37. :NONAME 1234 ; nn1 !
  38. :NONAME 9876 ; nn2 !
  39. T{ nn1 @ EXECUTE -> 1234 }T
  40. T{ nn2 @ EXECUTE -> 9876 }T
  41. \ --------------------------------------------------------------------
  42. Testing ?DO
  43. : qd ?DO I LOOP ;
  44. T{ 789 789 qd -> }T
  45. T{ -9876 -9876 qd -> }T
  46. T{ 5 0 qd -> 0 1 2 3 4 }T
  47. : qd1 ?DO I 10 +LOOP ;
  48. T{ 50 1 qd1 -> 1 11 21 31 41 }T
  49. T{ 50 0 qd1 -> 0 10 20 30 40 }T
  50. : qd2 ?DO I 3 > if LEAVE else I then LOOP ;
  51. T{ 5 -1 qd2 -> -1 0 1 2 3 }T
  52. : qd3 ?DO I 1 +LOOP ;
  53. T{ 4 4 qd3 -> }T
  54. T{ 4 1 qd3 -> 1 2 3 }T
  55. T{ 2 -1 qd3 -> -1 0 1 }T
  56. : qd4 ?DO I -1 +LOOP ;
  57. T{ 4 4 qd4 -> }T
  58. T{ 1 4 qd4 -> 4 3 2 1 }T
  59. T{ -1 2 qd4 -> 2 1 0 -1 }T
  60. : qd5 ?DO I -10 +LOOP ;
  61. T{ 1 50 qd5 -> 50 40 30 20 10 }T
  62. T{ 0 50 qd5 -> 50 40 30 20 10 0 }T
  63. T{ -25 10 qd5 -> 10 0 -10 -20 }T
  64. VARIABLE iterations
  65. VARIABLE increment
  66. : qd6 ( limit start increment -- )
  67. increment !
  68. 0 iterations !
  69. ?DO
  70. 1 iterations +!
  71. I
  72. iterations @ 6 = IF LEAVE THEN
  73. increment @
  74. +LOOP iterations @
  75. ;
  76. T{ 4 4 -1 qd6 -> 0 }T
  77. T{ 1 4 -1 qd6 -> 4 3 2 1 4 }T
  78. T{ 4 1 -1 qd6 -> 1 0 -1 -2 -3 -4 6 }T
  79. T{ 4 1 0 qd6 -> 1 1 1 1 1 1 6 }T
  80. T{ 0 0 0 qd6 -> 0 }T
  81. T{ 1 4 0 qd6 -> 4 4 4 4 4 4 6 }T
  82. T{ 1 4 1 qd6 -> 4 5 6 7 8 9 6 }T
  83. T{ 4 1 1 qd6 -> 1 2 3 3 }T
  84. T{ 4 4 1 qd6 -> 0 }T
  85. T{ 2 -1 -1 qd6 -> -1 -2 -3 -4 -5 -6 6 }T
  86. T{ -1 2 -1 qd6 -> 2 1 0 -1 4 }T
  87. T{ 2 -1 0 qd6 -> -1 -1 -1 -1 -1 -1 6 }T
  88. T{ -1 2 0 qd6 -> 2 2 2 2 2 2 6 }T
  89. T{ -1 2 1 qd6 -> 2 3 4 5 6 7 6 }T
  90. T{ 2 -1 1 qd6 -> -1 0 1 3 }T
  91. \ --------------------------------------------------------------------
  92. Testing VALUE TO
  93. T{ 111 VALUE v1 -999 VALUE v2 -> }T
  94. T{ v1 -> 111 }T
  95. T{ v2 -> -999 }T
  96. T{ 222 TO v1 -> }T
  97. T{ v1 -> 222 }T
  98. T{ : vd1 v1 ; -> }T
  99. T{ vd1 -> 222 }T
  100. T{ : vd2 TO v2 ; -> }T
  101. T{ v2 -> -999 }T
  102. T{ -333 vd2 -> }T
  103. T{ v2 -> -333 }T
  104. T{ v1 -> 222 }T
  105. \ --------------------------------------------------------------------
  106. Testing CASE OF ENDOF ENDCASE
  107. : cs1 CASE 1 OF 111 ENDOF
  108. 2 OF 222 ENDOF
  109. 3 OF 333 ENDOF
  110. >R 999 R>
  111. ENDCASE
  112. ;
  113. T{ 1 cs1 -> 111 }T
  114. T{ 2 cs1 -> 222 }T
  115. T{ 3 cs1 -> 333 }T
  116. T{ 4 cs1 -> 999 }T
  117. : cs2 >R CASE -1 OF CASE R@ 1 OF 100 ENDOF
  118. 2 OF 200 ENDOF
  119. >R -300 R>
  120. ENDCASE
  121. ENDOF
  122. -2 OF CASE R@ 1 OF -99 ENDOF
  123. >R -199 R>
  124. ENDCASE
  125. ENDOF
  126. >R 299 R>
  127. ENDCASE R> DROP
  128. ;
  129. T{ -1 1 cs2 -> 100 }T
  130. T{ -1 2 cs2 -> 200 }T
  131. T{ -1 3 cs2 -> -300 }T
  132. T{ -2 1 cs2 -> -99 }T
  133. T{ -2 2 cs2 -> -199 }T
  134. T{ 0 2 cs2 -> 299 }T
  135. \ --------------------------------------------------------------------
  136. Testing C" CONVERT
  137. T{ : cq1 C" 123" ; -> }T
  138. T{ cq1 COUNT EVALUATE -> 123 }T
  139. T{ : cq2 C" " ; -> }T
  140. T{ cq2 COUNT EVALUATE -> }T
  141. \ Create two large integers, small enough to not cause overflow
  142. max-int 3 / CONSTANT cvi1
  143. max-int 5 / CONSTANT cvi2
  144. \ Create a string of the form "(n1digits.n2digits)"
  145. : 2n>str ( +n1 +n2 -- caddr u )
  146. <# [CHAR] ) HOLD S>D #S 2DROP ( -- +n1 )
  147. [CHAR] . HOLD S>D #S
  148. [CHAR] ( HOLD #> ( -- caddr1 u )
  149. HERE SWAP 2DUP 2>R CHARS DUP ALLOT MOVE 2R>
  150. ;
  151. cvi1 cvi2 2n>str CONSTANT cv$len CONSTANT cv$ad
  152. T{ 0 0 cv$ad CONVERT C@ -> cvi1 S>D CHAR . }T
  153. T{ 0 0 cv$ad CONVERT CONVERT C@
  154. -> 0 0 cv$ad CHAR+ cv$len 1- >NUMBER
  155. 1- SWAP CHAR+ SWAP >NUMBER 2DROP CHAR ) }T
  156. \ --------------------------------------------------------------------
  157. Testing COMPILE, [COMPILE]
  158. :NONAME DUP + ; CONSTANT dup+
  159. T{ : q dup+ COMPILE, ; -> }T
  160. T{ : as [ q ] ; -> }T
  161. T{ 123 as -> 246 }T
  162. T{ : [c1] [COMPILE] DUP ; IMMEDIATE -> }T
  163. T{ 123 [c1] -> 123 123 }T \ With default compilation semantics
  164. T{ : [c2] [COMPILE] [c1] ; -> }T
  165. T{ 234 [c2] -> 234 234 }T \ With an immediate word
  166. T{ : [cif] [COMPILE] IF ; IMMEDIATE -> }T
  167. T{ : [c3] [cif] 111 ELSE 222 THEN ; -> }T \ With special compilation semantics
  168. T{ -1 [c3] -> 111 }T
  169. T{ 0 [c3] -> 222 }T
  170. \ --------------------------------------------------------------------
  171. Testing NIP TUCK ROLL
  172. T{ 1 2 3 NIP -> 1 3 }T
  173. T{ 1 2 3 TUCK -> 1 3 2 3 }T
  174. T{ 1 2 3 4 0 ROLL -> 1 2 3 4 }T
  175. T{ 1 2 3 4 1 ROLL -> 1 2 4 3 }T
  176. T{ 1 2 3 4 2 ROLL -> 1 3 4 2 }T
  177. T{ 1 2 3 4 3 ROLL -> 2 3 4 1 }T
  178. \ --------------------------------------------------------------------
  179. Testing WITHIN
  180. T{ 0 0 1 WITHIN -> <true> }T
  181. T{ 1 0 1 WITHIN -> <false> }T
  182. T{ -1 -1 1 WITHIN -> <true> }T
  183. T{ 0 -1 1 WITHIN -> <true> }T
  184. T{ 1 -1 1 WITHIN -> <false> }T
  185. T{ MID-UINT MID-UINT MID-UINT+1 WITHIN -> <true> }T
  186. T{ MID-UINT+1 MID-UINT MID-UINT+1 WITHIN -> <false> }T
  187. T{ MID-UINT MID-UINT+1 MAX-UINT WITHIN -> <false> }T
  188. T{ MID-UINT+1 MID-UINT+1 MAX-UINT WITHIN -> <true> }T
  189. \ --------------------------------------------------------------------
  190. CR .( End of Core Extension word tests) CR