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

/doc/perturb/exceptiontest.fth

https://github.com/chitselb/pettil
Forth | 96 lines | 74 code | 22 blank | 0 comment | 5 complexity | 249f8ebe8a3358e2afd0e2c77a1051c5 MD5 | raw file
  1. \ To test the ANS Forth Exception word set and extension words
  2. \ This program was written by Gerry Jackson in 2006, 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.4 1 April 2012 Tests placed in the public domain.
  11. \ 0.3 6 March 2009 { and } replaced with T{ and }T
  12. \ 0.2 20 April 2007 ANS Forth words changed to upper case
  13. \ 0.1 Oct 2006 First version released
  14. \ ------------------------------------------------------------------------------
  15. \ The tests are based on John Hayes test program for the core word set
  16. \
  17. \ Words tested in this file are:
  18. \ CATCH THROW ABORT ABORT"
  19. \
  20. \ ------------------------------------------------------------------------------
  21. \ Assumptions and dependencies:
  22. \ - the forth system under test throws an exception with throw
  23. \ code -13 for a word not found by the text interpreter. The
  24. \ undefined word used is $$qweqweqwert$$, if this happens to be
  25. \ a valid word in your system change the definition of t7 below
  26. \ - tester.fr or ttester.fs has been loaded prior to this file
  27. \ - CASE, OF, ENDOF and ENDCASE from the core extension wordset
  28. \ are present and work correctly
  29. \ ------------------------------------------------------------------------------
  30. TESTING CATCH THROW
  31. DECIMAL
  32. : t1 9 ;
  33. : c1 1 2 3 ['] t1 CATCH ;
  34. T{ c1 -> 1 2 3 9 0 }T \ No THROW executed
  35. : t2 8 0 THROW ;
  36. : c2 1 2 ['] t2 CATCH ;
  37. T{ c2 -> 1 2 8 0 }T \ 0 THROW does nothing
  38. : t3 7 8 9 99 THROW ;
  39. : c3 1 2 ['] t3 CATCH ;
  40. T{ c3 -> 1 2 99 }T \ Restores stack to CATCH depth
  41. : t4 1- DUP 0> IF RECURSE ELSE 999 THROW -222 THEN ;
  42. : c4 3 4 5 10 ['] t4 CATCH -111 ;
  43. T{ c4 -> 3 4 5 0 999 -111 }T \ Test return stack unwinding
  44. : t5 2DROP 2DROP 9999 THROW ;
  45. : c5 1 2 3 4 ['] t5 CATCH \ Test depth restored correctly
  46. DEPTH >R DROP 2DROP 2DROP R> ; \ after stack has been emptied
  47. T{ c5 -> 5 }T
  48. \ ------------------------------------------------------------------------------
  49. TESTING ABORT ABORT"
  50. -1 CONSTANT exc_abort
  51. -2 CONSTANT exc_abort"
  52. -13 CONSTANT exc_undef
  53. : t6 ABORT ;
  54. \ The 77 in t10 is necessary for the second ABORT" test as the data stack
  55. \ is restored to a depth of 2 when THROW is executed. The 77 ensures the top
  56. \ of stack value is known for the results check
  57. : t10 77 SWAP ABORT" This should not be displayed" ;
  58. : c6 CATCH
  59. CASE exc_abort OF 11 ENDOF
  60. exc_abort" OF 12 ENDOF
  61. exc_undef OF 13 ENDOF
  62. ENDCASE
  63. ;
  64. T{ 1 2 ' t6 c6 -> 1 2 11 }T \ Test that ABORT is caught
  65. T{ 3 0 ' t10 c6 -> 3 77 }T \ ABORT" does nothing
  66. T{ 4 5 ' t10 c6 -> 4 77 12 }T \ ABORT" caught, no message
  67. \ ------------------------------------------------------------------------------
  68. TESTING a system generated exception
  69. : t7 S" 333 $$qweqweqwert$$ 334" EVALUATE 335 ;
  70. : t8 S" 222 t7 223" EVALUATE 224 ;
  71. : t9 S" 111 112 t8 113" EVALUATE 114 ;
  72. T{ 6 7 ' t9 c6 3 -> 6 7 13 3 }T \ Test unlinking of sources
  73. \ ------------------------------------------------------------------------------
  74. CR .( End of Exception word tests) CR