PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/exceptiontest.fth

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