PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/generic/forth/test.forth

https://gitlab.com/aguai/punyforth
Forth | 240 lines | 214 code | 26 blank | 0 comment | 15 complexity | b201b2ecd13b0a594ac2724aa48dd929 MD5 | raw file
  1. marker -tests
  2. : '.' [ char . ] literal ;
  3. : factorial ( n -- n! | err:1024 )
  4. dup 0< if
  5. drop 1024 throw
  6. then
  7. dup 0= if
  8. drop 1
  9. else
  10. dup 1= if
  11. drop 1
  12. else
  13. dup 1- factorial *
  14. then
  15. then ;
  16. : factorial2 ( n -- n! )
  17. 1 2 rot
  18. begin
  19. 2dup <=
  20. while
  21. -rot tuck
  22. * swap
  23. 1+ rot
  24. repeat
  25. 2drop ;
  26. : factorial3 ( n -- n! )
  27. case
  28. 0 of 1 endof
  29. 1 of 1 endof
  30. dup 1- factorial3 *
  31. endcase ;
  32. : 'F' [ char F ] literal ;
  33. 5 array test_numbers
  34. struct
  35. cell field: .width
  36. cell field: .height
  37. constant Rect
  38. : new-rect Rect create allot does> ;
  39. : area ( rect -- area ) dup .width @ swap .height @ * ;
  40. new-rect r1
  41. : nested-throw1 dup 1= if drop 10 throw then 2 = if 20 throw then 42 ;
  42. : nested-throw2
  43. ['] nested-throw1 catch dup 10 = if
  44. drop 30 throw
  45. else
  46. throw
  47. then ;
  48. : bench ( ntimes -- sec )
  49. time swap
  50. 0 do 10 factorial drop loop
  51. time swap - ;
  52. 0 variable! test_count
  53. variable test_var1 variable test_var2
  54. variable stored_dp
  55. dp stored_dp !
  56. marker -test-test
  57. : assert ( bool -- )
  58. test_count @ 1+ test_count ! '.' emit
  59. TRUE <> if 'F' emit test_count ? then ;
  60. : selftest ( -- )
  61. print "testing"
  62. depth 0= assert
  63. 0 test_count !
  64. 12 3 min 3 = assert
  65. -3 7 min -3 = assert
  66. -3 -7 min -7 = assert
  67. 132 33 max 132 = assert
  68. -33 77 max 77 = assert
  69. -389 -27 max -27 = assert
  70. 0 1- -1 = assert
  71. -10 1+ -9 = assert
  72. -10 4 < assert
  73. -10 -4 < assert
  74. 324 12 > assert
  75. -24 -212 > assert
  76. 24 -2 > assert
  77. 1 1- 0= assert
  78. -1 1+ 0= assert
  79. 15 0<> assert
  80. 0 0<> invert assert
  81. -3 0< TRUE = assert 3 0< FALSE = assert
  82. 3 0> TRUE = assert -3 0> FALSE = assert
  83. -12 2 / 6 + 0 = assert
  84. 1 2 tuck 2 = assert 1 = assert 2 = assert
  85. -123 abs 123 = assert 32 abs 32 = assert 0 abs 0 = assert
  86. -42 abs 42 abs = assert
  87. -12 -3 * 36 = assert -3 4 * -12 = assert 2 -4 * -8 = assert
  88. -12 -3 + -15 = assert -3 4 + 1 = assert 2 -4 + -2 = assert
  89. -12 -3 - -9 = assert -3 4 - -7 = assert 2 -4 - 6 = assert
  90. 12 -6 / -2 = assert -36 6 / -6 = assert -4 -2 / 2 = assert
  91. 0 1 lshift 0 = assert
  92. 1 1 lshift 2 = assert
  93. 3 4 lshift 48 = assert
  94. 3 0 lshift 3 = assert
  95. 0 1 rshift 0 = assert
  96. 2 1 rshift 1 = assert
  97. 128 3 rshift 16 = assert
  98. 4 4 >= assert 5 4 >= assert -4 -10 >= assert 4 5 >= invert assert
  99. 6 6 <= assert 3 9 <= assert -9 -5 <= assert 10 2 <= invert assert
  100. 12 3 /mod 4 = assert 0 = assert 12 4 / 3 = assert
  101. 13 5 /mod 2 = assert 3 = assert 14 6 % 2 = assert
  102. TRUE if TRUE assert else FALSE assert then
  103. FALSE if FALSE assert else TRUE assert then
  104. 2 TRUE if dup * then 4 = assert
  105. 2 FALSE if dup * then 2 = assert
  106. 10000 5 bounds 10000 = assert 10005 = assert
  107. 10000 5 bounds do i loop
  108. 10004 = assert 10003 = assert 10002 = assert 10001 = assert 10000 = assert depth 0= assert
  109. 0 11 1 do i + loop 55 = assert
  110. 0 11 1 do i + 1 +loop 55 = assert
  111. 0 50 0 do i + 5 +loop 225 = assert
  112. 15 0 do i 5 +loop 10 = assert 5 = assert 0 = assert depth 0= assert
  113. 3 0 do i 2 +loop 2 = assert 0 = assert depth 0= assert
  114. 1 0 do i 2 +loop 0 = assert depth 0= assert
  115. -5 0 do i -2 +loop -4 = assert -2 = assert 0 = assert depth 0= assert
  116. -1 0 do i -1 +loop -1 = assert 0 = assert depth 0= assert
  117. 0 0 do i -1 +loop 0 = assert depth 0= assert
  118. 0 8 2 do 9 3 do i j + + loop loop 360 = assert
  119. 9 factorial 362880 = assert
  120. 8 factorial 8 factorial2 = assert
  121. 9 factorial 9 factorial3 = assert
  122. 2 10 begin 1- swap 2 * swap dup 0= until drop 2048 = assert
  123. 1 0 or 1 = assert 0 1 or 1 = assert
  124. 1 1 or 1 = assert 0 0 or 0 = assert
  125. 1 0 and 0 = assert 0 1 and 0 = assert
  126. 1 1 and 1 = assert 0 0 and 0 = assert
  127. 1 0 xor 1 = assert 0 1 xor 1 = assert
  128. 1 1 xor 0 = assert 0 0 xor 0 = assert
  129. 10 2 < 3 1 > or if 1 else 0 then 1 = assert
  130. 3 10 < 3 11 > and if 1 else 0 then 0 = assert
  131. -98 45 < 33 11 > and if 1 else 0 then 1 = assert
  132. 5 0 do i i test_numbers ! loop
  133. 5 0 do i test_numbers @ i = assert loop
  134. 3 r1 .width ! 5 r1 .height !
  135. r1 area 15 = assert
  136. 12 test_var1 ! test_var1 @ 12 = assert
  137. 3 test_var1 +! test_var1 @ 15 = assert
  138. 1 case
  139. 1 of 10 endof
  140. 2 of 20 endof
  141. 3 of 30 endof
  142. endcase 10 = assert
  143. 2 case
  144. 1 of 10 endof
  145. 2 of 20 endof
  146. 3 of 30 endof
  147. endcase 20 = assert
  148. 3 case
  149. 1 of 10 endof
  150. 2 of 20 endof
  151. 3 of 30 endof
  152. endcase 30 = assert
  153. 1 case
  154. 1 of 2 endof
  155. 2 of 3 endof
  156. endcase 2 = assert
  157. sp@ test_var1 !
  158. -1 ['] factorial catch 1024 = assert
  159. 1 ['] nested-throw2 catch 30 = assert
  160. 2 ['] nested-throw2 catch 20 = assert
  161. 3 ['] nested-throw2 catch drop 42 = assert
  162. 3 nested-throw2 42 = assert
  163. sp@ test_var2 !
  164. test_var1 @ test_var2 @ = assert
  165. freemem 16 allot freemem - 16 = assert
  166. str "" strlen 0 = assert
  167. str "1" strlen 1 = assert
  168. str "12" strlen 2 = assert
  169. str "1234567" strlen 7 = assert
  170. str '""""' strlen 4 = assert
  171. str 'anystring'
  172. str ''
  173. str-starts-with TRUE = assert
  174. str ''
  175. str ''
  176. str-starts-with TRUE = assert
  177. str 'abc'
  178. str 'bc'
  179. str-starts-with FALSE = assert
  180. str 'abc'
  181. str 'ab'
  182. str-starts-with TRUE = assert
  183. str 'aabbc'
  184. str 'aabbc'
  185. str-starts-with TRUE = assert
  186. str 'aabbc'
  187. str 'aabbcc'
  188. str-starts-with FALSE = assert
  189. str 'abcxxxx'
  190. str 'abc'
  191. str-includes TRUE = assert
  192. str 'xxabcyy'
  193. str 'abc'
  194. str-includes TRUE = assert
  195. str 'xxabzyy'
  196. str 'abc'
  197. str-includes FALSE = assert
  198. str 'anystring'
  199. str ''
  200. str-includes TRUE = assert
  201. str 'xxx'
  202. str 'xxx'
  203. str-includes TRUE = assert
  204. str 'abcdef'
  205. str 'def'
  206. str-includes TRUE = assert
  207. str 'abcdef'
  208. str 'efg'
  209. str-includes FALSE = assert
  210. depth 0= assert
  211. -test-test dp stored_dp @ = assert
  212. print "OK " test_count ? cr ;
  213. ' selftest execute print "Punyforth ready" cr
  214. -tests