PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/memorytest.fth

https://gitlab.com/BGCX261/zmforth-hg-to-git
Forth | 92 lines | 60 code | 32 blank | 0 comment | 1 complexity | 6b99ee5cb4c6fae1508c55a9a8e78103 MD5 | raw file
Possible License(s): GPL-3.0
  1. \ To test the ANS Forth Memory-Allocation word set
  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 October 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. \ ALLOCATE FREE RESIZE
  18. \
  19. \ ------------------------------------------------------------------------------
  20. \ Assumptions and dependencies:
  21. \ - that 'addr -1 ALLOCATE' and 'addr -1 RESIZE' will return an error
  22. \ - tester.fr has been loaded prior to this file
  23. \ - testing FREE failing is not done as it is likely to crash the
  24. \ system
  25. \ ------------------------------------------------------------------------------
  26. Testing Memory-Allocation word set
  27. DECIMAL
  28. 0 CONSTANT <false>
  29. \ ------------------------------------------------------------------------------
  30. Testing ALLOCATE FREE RESIZE
  31. VARIABLE addr
  32. T{ 100 ALLOCATE SWAP addr ! -> 0 }T
  33. T{ addr @ 1 CELLS 1- and -> 0 }T \ Test address is aligned
  34. T{ addr @ FREE -> 0 }T
  35. T{ 99 ALLOCATE SWAP addr ! -> 0 }T
  36. T{ addr @ 1 CELLS 1- AND -> 0 }T \ Test address is aligned
  37. T{ addr @ FREE -> 0 }T
  38. T{ 50 ALLOCATE SWAP addr ! -> 0 }T
  39. : writemem 0 DO I 1+ OVER C! 1+ LOOP DROP ; ( ad n -- )
  40. : checkmem ( ad n --- )
  41. 0
  42. DO
  43. DUP C@ SWAP >R
  44. T{ -> R> I 1+ SWAP >R }T \ Luckily tester checks can be compiled
  45. R> 1+
  46. LOOP
  47. DROP
  48. ;
  49. addr @ 50 writemem addr @ 50 checkmem
  50. T{ addr @ 28 RESIZE SWAP addr ! -> 0 }T
  51. addr @ 28 checkmem
  52. T{ addr @ 200 RESIZE SWAP addr ! -> 0 }T
  53. T{ addr @ 28 checkmem
  54. \ ------------------------------------------------------------------------------
  55. Testing failure of RESIZE and ALLOCATE (unlikely to be enough memory)
  56. T{ addr @ -1 RESIZE 0= -> addr @ <false> }T
  57. T{ addr @ FREE -> 0 }T
  58. T{ -1 ALLOCATE SWAP DROP 0= -> <false> }T \ Memory allocate failed
  59. \ ------------------------------------------------------------------------------
  60. CR .( End of Memory-Allocation word tests) CR