PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/toolstest.fth

https://gitlab.com/BGCX261/zmforth-hg-to-git
Forth | 161 lines | 122 code | 39 blank | 0 comment | 1 complexity | 3531d7b93f2dc2574fcd207fcb723a9d MD5 | raw file
Possible License(s): GPL-3.0
  1. \ To test some of the ANS Forth Programming Tools and extension wordset
  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.4 6 March 2009 ENDIF changed to THEN. {...} changed to T{...}T
  11. \ 0.3 20 April 2007 ANS Forth words changed to upper case
  12. \ 0.2 30 Oct 2006 updated following GForth test to avoid
  13. \ changing stack depth during a colon definition
  14. \ 0.1 Oct 2006 First version released
  15. \ ------------------------------------------------------------------------------
  16. \ The tests are based on John Hayes test program
  17. \ Words tested in this file are:
  18. \ AHEAD [IF] [ELSE] [THEN] CS-PICK CS-ROLL
  19. \
  20. \ Words not tested:
  21. \ .S ? DUMP SEE WORDS
  22. \ ;CODE ASSEMBLER BYE CODE EDITOR FORGET STATE
  23. \ ------------------------------------------------------------------------------
  24. \ Assumptions and dependencies:
  25. \ - ttester.fs has been loaded prior to this file
  26. \ ------------------------------------------------------------------------------
  27. DECIMAL
  28. 0 INVERT CONSTANT <true>
  29. 0 CONSTANT <false>
  30. \ ------------------------------------------------------------------------------
  31. Testing AHEAD
  32. T{ : pt1 AHEAD 1111 2222 THEN 3333 ; -> }T
  33. T{ pt1 -> 3333 }T
  34. \ ------------------------------------------------------------------------------
  35. Testing [IF] [ELSE] [THEN]
  36. T{ <true> [IF] 111 [ELSE] 222 [THEN] -> 111 }T
  37. T{ <false> [IF] 111 [ELSE] 222 [THEN] -> 222 }T
  38. T{ <true> [IF] 1 \ Code spread over more than 1 line
  39. 2
  40. [ELSE]
  41. 3
  42. 4
  43. [THEN] -> 1 2 }T
  44. T{ <false> [IF]
  45. 1 2
  46. [ELSE]
  47. 3 4
  48. [THEN] -> 3 4 }T
  49. T{ <true> [IF] 1 <true> [IF] 2 [ELSE] 3 [THEN] [ELSE] 4 [THEN] -> 1 2 }T
  50. T{ <false> [IF] 1 <true> [IF] 2 [ELSE] 3 [THEN] [ELSE] 4 [THEN] -> 4 }T
  51. T{ <true> [IF] 1 <false> [IF] 2 [ELSE] 3 [THEN] [ELSE] 4 [THEN] -> 1 3 }T
  52. T{ <false> [IF] 1 <false> [IF] 2 [ELSE] 3 [THEN] [ELSE] 4 [THEN] -> 4 }T
  53. \ ------------------------------------------------------------------------------
  54. Testing immediacy of [IF] [ELSE] [THEN]
  55. T{ : pt2 [ 0 ] [IF] 1111 [ELSE] 2222 [THEN] ; pt2 -> 2222 }T
  56. T{ : pt3 [ -1 ] [IF] 3333 [ELSE] 4444 [THEN] ; pt3 -> 3333 }T
  57. \ ------------------------------------------------------------------------------
  58. Testing CS-PICK and CS-ROLL
  59. \ Test pt5 based on example in ANS document p 176.
  60. : ?repeat
  61. 0 CS-PICK POSTPONE UNTIL
  62. ; IMMEDIATE
  63. VARIABLE pt4
  64. : <= > 0= ;
  65. T{ : pt5 ( n1 -- )
  66. pt4 !
  67. BEGIN
  68. -1 pt4 +!
  69. pt4 @ 4 <= ?repeat \ Back to BEGIN if false
  70. 111
  71. pt4 @ 3 <= ?repeat
  72. 222
  73. pt4 @ 2 <= ?repeat
  74. 333
  75. pt4 @ 1 =
  76. UNTIL
  77. ; -> }T
  78. T{ 6 pt5 -> 111 111 222 111 222 333 111 222 333 }T
  79. T{ : ?DONE POSTPONE IF 1 CS-ROLL ; IMMEDIATE -> }T \ Same as WHILE
  80. T{ : pt6
  81. >R
  82. BEGIN
  83. R@
  84. ?DONE
  85. R@
  86. R> 1- >R
  87. REPEAT
  88. R> DROP
  89. ; -> }T
  90. T{ 5 pt6 -> 5 4 3 2 1 }T
  91. : mix_up 2 CS-ROLL ; IMMEDIATE \ cs-rot
  92. : pt7 ( f3 f2 f1 -- ? )
  93. IF 1111 ROT ROT ( -- 1111 f3 f2 ) ( cs: -- orig1 )
  94. IF 2222 SWAP ( -- 1111 2222 f3 ) ( cs: -- orig1 orig2 )
  95. IF ( cs: -- orig1 orig2 orig3 )
  96. 3333 mix_up ( -- 1111 2222 3333 ) ( cs: -- orig2 orig3 orig1 )
  97. THEN ( cs: -- orig2 orig3 )
  98. 4444 \ Hence failure of first IF comes here and falls through
  99. THEN ( cs: -- orig2 )
  100. 5555 \ Failure of 3rd IF comes here
  101. THEN ( cs: -- )
  102. 6666 \ Failure of 2nd IF comes here
  103. ;
  104. T{ -1 -1 -1 pt7 -> 1111 2222 3333 4444 5555 6666 }T
  105. T{ 0 -1 -1 pt7 -> 1111 2222 5555 6666 }T
  106. T{ 0 0 -1 pt7 -> 1111 0 6666 }T
  107. T{ 0 0 0 pt7 -> 0 0 4444 5555 6666 }T
  108. : [1cs-roll] 1 CS-ROLL ; IMMEDIATE
  109. T{ : pt8
  110. >r
  111. AHEAD 111
  112. BEGIN 222
  113. [1cs-roll]
  114. THEN
  115. 333
  116. R> 1- >R
  117. R@ 0<
  118. UNTIL
  119. R> DROP
  120. ; -> }T
  121. T{ 1 pt8 -> 333 222 333 }T
  122. CR .( End of Programming Tools word tests) CR