aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/common/lib/forth2012/tester/memorytest.fth
blob: 1967fc3fdc1ea552e2590e8922e50a0f68eb9f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
\ To test the ANS Forth Memory-Allocation word set

\ This program was written by Gerry Jackson in 2006, with contributions from
\ others where indicated, and is in the public domain - it can be distributed
\ and/or modified in any way but please retain this notice.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

\ The tests are not claimed to be comprehensive or correct 

\ ------------------------------------------------------------------------------
\ Version 0.7 1 April 2012  Tests placed in the public domain.
\         0.6 30 January 2011 CHECKMEM modified to work with ttester.fs
\         0.5 30 November 2009 <false> replaced with FALSE
\         0.4 9 March 2009 Aligned test improved and data space pointer tested
\         0.3 6 March 2009 { and } replaced with T{ and }T
\         0.2 20 April 2007  ANS Forth words changed to upper case
\         0.1 October 2006 First version released

\ ------------------------------------------------------------------------------
\ The tests are based on John Hayes test program for the core word set
\ and requires those files to have been loaded

\ Words tested in this file are:
\     ALLOCATE FREE RESIZE
\     
\ ------------------------------------------------------------------------------
\ Assumptions and dependencies:
\     - that 'addr -1 ALLOCATE' and 'addr -1 RESIZE' will return an error
\     - tester.fr or ttester.fs has been loaded prior to this file
\     - testing FREE failing is not done as it is likely to crash the
\       system
\ ------------------------------------------------------------------------------

TESTING Memory-Allocation word set

DECIMAL

\ ------------------------------------------------------------------------------
TESTING ALLOCATE FREE RESIZE

VARIABLE addr1
VARIABLE datsp

HERE datsp !
T{ 100 ALLOCATE SWAP addr1 ! -> 0 }T
T{ addr1 @ ALIGNED -> addr1 @ }T   \ Test address is aligned
T{ HERE -> datsp @ }T            \ Check data space pointer is unchanged
T{ addr1 @ FREE -> 0 }T

T{ 99 ALLOCATE SWAP addr1 ! -> 0 }T
T{ addr1 @ ALIGNED -> addr1 @ }T
T{ addr1 @ FREE -> 0 }T

T{ 50 ALLOCATE SWAP addr1 ! -> 0 }T

: writemem 0 DO I 1+ OVER C! 1+ LOOP DROP ;	( ad n -- )

\ checkmem is defined this way to maintain compatibility with both
\ tester.fr and ttester.fs which differ in their definitions of T{

: checkmem  ( ad n --- )
   0
   DO
      >R
      T{ R@ C@ -> R> I 1+ SWAP >R }T
      R> 1+
   LOOP
   DROP
;

addr1 @ 50 writemem addr1 @ 50 checkmem

T{ addr1 @ 28 RESIZE SWAP addr1 ! -> 0 }T
addr1 @ 28 checkmem

T{ addr1 @ 200 RESIZE SWAP addr1 ! -> 0 }T
addr1 @ 28 checkmem

\ ------------------------------------------------------------------------------
TESTING failure of RESIZE and ALLOCATE (unlikely to be enough memory)

T{ addr1 @ -1 RESIZE 0= -> addr1 @ FALSE }T

T{ addr1 @ FREE -> 0 }T

T{ -1 ALLOCATE SWAP DROP 0= -> FALSE }T		\ Memory allocate failed

\ ------------------------------------------------------------------------------

CR .( End of Memory-Allocation word tests) CR