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

/tests/filetest.fth

https://gitlab.com/BGCX261/zmforth-hg-to-git
Forth | 205 lines | 145 code | 50 blank | 10 comment | 5 complexity | 93f62764b9db539bbe4beef138adce17 MD5 | raw file
Possible License(s): GPL-3.0
  1. \ To test the ANS File Access 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.4 22 March 2009 { and } replaced with T{ and }T
  11. \ 0.3 20 April 2007 ANS Forth words changed to upper case
  12. \ Removed directory test from the filenames
  13. \ 0.2 30 Oct 2006 updated following GForth tests to remove
  14. \ system dependency on file size, to allow for file
  15. \ buffering and to allow for PAD moving around
  16. \ 0.1 Oct 2006 First version released
  17. \ ------------------------------------------------------------------------------
  18. \ The tests are based on John Hayes test program for the core word set
  19. \ and requires those files to have been loaded
  20. \ Words tested in this file are:
  21. \ ( BIN CLOSE-FILE CREATE-FILE DELETE-FILE FILE-POSITION FILE-SIZE
  22. \ OPEN-FILE R/O R/W READ-FILE READ-LINE REPOSITION-FILE RESIZE-FILE
  23. \ S" SOURCE-ID W/O WRITE-FILE WRITE-LINE
  24. \ FILE-STATUS FLUSH-FILE RENAME-FILE
  25. \ Words not tested:
  26. \ REFILL INCLUDED INCLUDE-FILE (as these will likely have been
  27. \ tested in the execution of the test files)
  28. \ ------------------------------------------------------------------------------
  29. \ Assumptions, dependencies and notes:
  30. \ - tester.fr has been loaded prior to this file
  31. \ - These tests create files in the current directory, if all goes
  32. \ well these will be deleted. If something fails they may not be
  33. \ deleted. If this is a problem ensure you set a suitable
  34. \ directory before running this test. There is no ANS standard
  35. \ way of doing this. Also be aware of the file names used below
  36. \ which are: fatest1.txt, fatest2.txt and fatest3.txt
  37. \ ------------------------------------------------------------------------------
  38. Testing File Access word set
  39. DECIMAL
  40. 0 CONSTANT <false>
  41. 0 INVERT CONSTANT <true>
  42. \ ------------------------------------------------------------------------------
  43. Testing CREATE-FILE CLOSE-FILE
  44. : fn1 S" fatest1.txt" ;
  45. VARIABLE fid1
  46. T{ fn1 R/W CREATE-FILE SWAP fid1 ! -> 0 }T
  47. T{ fid1 @ CLOSE-FILE -> 0 }T
  48. \ ------------------------------------------------------------------------------
  49. Testing OPEN-FILE W/O WRITE-LINE
  50. : line1 S" Line 1" ;
  51. T{ fn1 W/O OPEN-FILE SWAP fid1 ! -> 0 }T
  52. T{ line1 FID1 @ WRITE-LINE -> 0 }T
  53. T{ fid1 @ CLOSE-FILE -> 0 }T
  54. \ ------------------------------------------------------------------------------
  55. Testing R/O FILE-POSITION (simple) READ-LINE
  56. 200 CONSTANT bsize
  57. CREATE buf bsize ALLOT
  58. VARIABLE #chars
  59. T{ fn1 R/O OPEN-FILE SWAP fid1 ! -> 0 }T
  60. T{ fid1 @ FILE-POSITION -> 0. 0 }T
  61. T{ buf 100 fid1 @ READ-LINE ROT DUP #chars ! -> <true> 0 line1 SWAP DROP }T
  62. T{ buf #chars @ line1 COMPARE -> 0 }T
  63. T{ fid1 @ CLOSE-FILE -> 0 }T
  64. \ ------------------------------------------------------------------------------
  65. Testing R/W WRITE-FILE REPOSITION-FILE READ-FILE FILE-POSITION S"
  66. : line2 S" Line 2 blah blah blah" ;
  67. : rl1 buf 100 fid1 @ READ-LINE ;
  68. 2VARIABLE fp
  69. T{ fn1 R/W OPEN-FILE SWAP fid1 ! -> 0 }T
  70. T{ fid1 @ FILE-SIZE DROP fid1 @ REPOSITION-FILE -> 0 }T
  71. T{ fid1 @ FILE-SIZE -> fid1 @ FILE-POSITION }T
  72. T{ line2 fid1 @ WRITE-FILE -> 0 }T
  73. T{ 10. fid1 @ REPOSITION-FILE -> 0 }T
  74. T{ fid1 @ FILE-POSITION -> 10. 0 }T
  75. T{ 0. fid1 @ REPOSITION-FILE -> 0 }T
  76. T{ rl1 -> line1 SWAP DROP <true> 0 }T
  77. T{ rl1 ROT DUP #chars ! -> <true> 0 line2 SWAP DROP }T
  78. T{ buf #chars @ line2 COMPARE -> 0 }T
  79. T{ rl1 -> 0 <false> 0 }T
  80. T{ fid1 @ FILE-POSITION ROT ROT fp 2! -> 0 }T
  81. T{ fp 2@ fid1 @ FILE-SIZE DROP D= -> <true> }T
  82. T{ s" " fid1 @ WRITE-LINE -> 0 }T
  83. T{ s" " fid1 @ WRITE-LINE -> 0 }T
  84. T{ fp 2@ fid1 @ REPOSITION-FILE -> 0 }T
  85. T{ rl1 -> 0 <true> 0 }T
  86. T{ rl1 -> 0 <true> 0 }T
  87. T{ rl1 -> 0 <false> 0 }T
  88. T{ fid1 @ CLOSE-FILE -> 0 }T
  89. \ ------------------------------------------------------------------------------
  90. Testing BIN READ-FILE FILE-SIZE
  91. : cbuf buf bsize 0 FILL ;
  92. : fn2 S" fatest2.txt" ;
  93. VARIABLE fid2
  94. : setpad PAD 50 0 DO I OVER C! CHAR+ LOOP DROP ;
  95. setpad \ If anything else is defined setpad must be called again
  96. \ as pad may move
  97. T{ fn2 R/W BIN CREATE-FILE SWAP fid2 ! -> 0 }T
  98. T{ PAD 50 fid2 @ WRITE-FILE FID2 @ FLUSH-FILE -> 0 0 }T
  99. T{ fid2 @ FILE-SIZE -> 50. 0 }T
  100. T{ 0. fid2 @ REPOSITION-FILE -> 0 }T
  101. T{ cbuf buf 29 fid2 @ READ-FILE -> 29 0 }T
  102. T{ PAD 29 buf 29 COMPARE -> 0 }T
  103. T{ PAD 30 buf 30 COMPARE -> 1 }T
  104. T{ cbuf buf 29 fid2 @ READ-FILE -> 21 0 }T
  105. T{ PAD 29 + 21 buf 21 COMPARE -> 0 }T
  106. T{ fid2 @ FILE-SIZE DROP fid2 @ FILE-POSITION DROP D= -> <true> }T
  107. T{ buf 10 fid2 @ READ-FILE -> 0 0 }T
  108. T{ fid2 @ CLOSE-FILE -> 0 }T
  109. \ ------------------------------------------------------------------------------
  110. Testing RESIZE-FILE
  111. T{ fn2 R/W BIN OPEN-FILE SWAP fid2 ! -> 0 }T
  112. T{ 37. fid2 @ RESIZE-FILE -> 0 }T
  113. T{ fid2 @ FILE-SIZE -> 37. 0 }T
  114. T{ 0. fid2 @ REPOSITION-FILE -> 0 }T
  115. T{ cbuf buf 100 fid2 @ READ-FILE -> 37 0 }T
  116. T{ PAD 37 buf 37 COMPARE -> 0 }T
  117. T{ PAD 38 buf 38 COMPARE -> 1 }T
  118. T{ 500. fid2 @ RESIZE-FILE -> 0 }T
  119. T{ fid2 @ FILE-SIZE -> 500. 0 }T
  120. T{ 0. fid2 @ REPOSITION-FILE -> 0 }T
  121. T{ cbuf buf 100 fid2 @ READ-FILE -> 100 0 }T
  122. T{ PAD 37 buf 37 COMPARE -> 0 }T
  123. T{ fid2 @ CLOSE-FILE -> 0 }T
  124. \ ------------------------------------------------------------------------------
  125. Testing DELETE-FILE
  126. T{ fn2 DELETE-FILE -> 0 }T
  127. T{ fn2 R/W BIN OPEN-FILE SWAP DROP 0= -> <false> }T
  128. T{ fn2 DELETE-FILE 0= -> <false> }T
  129. \ ------------------------------------------------------------------------------
  130. Testing multi-line ( comments
  131. T{ ( 1 2 3
  132. 4 5 6
  133. 7 8 9 ) 11 22 33 -> 11 22 33 }T
  134. \ ------------------------------------------------------------------------------
  135. Testing SOURCE-ID (can only test it does not return 0 or -1)
  136. T{ SOURCE-ID DUP -1 = SWAP 0= OR -> <false> }T
  137. \ ------------------------------------------------------------------------------
  138. Testing RENAME-FILE FILE-STATUS FLUSH-FILE
  139. : fn3 S" fatest3.txt" ;
  140. : >end fid1 @ FILE-SIZE DROP fid1 @ REPOSITION-FILE ;
  141. T{ fn3 DELETE-FILE DROP -> }T
  142. T{ fn1 fn3 RENAME-FILE 0= -> <true> }T
  143. T{ fn1 FILE-STATUS SWAP DROP 0= -> <false> }T
  144. T{ fn3 FILE-STATUS SWAP DROP 0= -> <true> }T \ Return value is undefined
  145. T{ fn3 R/W OPEN-FILE SWAP fid1 ! -> 0 }T
  146. T{ >end -> 0 }T
  147. T{ S" Final line" fid1 @ WRITE-LINE -> 0 }T
  148. T{ fid1 @ FLUSH-FILE -> 0 }T \ Can only test FLUSH-FILE doesn't fail
  149. T{ fid1 @ CLOSE-FILE -> 0 }T
  150. \ Tidy the test folder
  151. T{ fn3 DELETE-FILE drop -> }T
  152. \ ------------------------------------------------------------------------------
  153. CR .( End of File-Access word tests) CR