-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmemorytest.fth
127 lines (94 loc) · 4.15 KB
/
memorytest.fth
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
\ 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.11 25 April 2015 Now checks memory region is unchanged following a
\ RESIZE. @ and ! in allocated memory.
\ 0.8 10 January 2013, Added CHARS and CHAR+ where necessary to correct
\ the assumption that 1 CHARS = 1
\ 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
\ Words tested in this file are:
\ ALLOCATE FREE RESIZE
\
\ ------------------------------------------------------------------------------
\ Assumptions, dependencies and notes:
\ - tester.fr (or ttester.fs), errorreport.fth and utilities.fth have been
\ included prior to this file
\ - the Core word set is available and tested
\ - that 'addr -1 ALLOCATE' and 'addr -1 RESIZE' will return an error
\ - 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 CHARS ALLOCATE SWAP ADDR1 ! -> 0 }T
: WRITEMEM 0 DO I 1+ OVER C! CHAR+ 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> CHAR+
LOOP
DROP
;
ADDR1 @ 50 WRITEMEM ADDR1 @ 50 CHECKMEM
T{ ADDR1 @ 28 CHARS RESIZE SWAP ADDR1 ! -> 0 }T
ADDR1 @ 28 CHECKMEM
T{ ADDR1 @ 200 CHARS RESIZE SWAP ADDR1 ! -> 0 }T
ADDR1 @ 28 CHECKMEM
\ ------------------------------------------------------------------------------
TESTING failure of RESIZE and ALLOCATE (unlikely to be enough memory)
\ This test relies on the previous test having passed
VARIABLE RESIZE-OK
T{ ADDR1 @ -1 CHARS RESIZE 0= DUP RESIZE-OK ! -> ADDR1 @ FALSE }T
\ Check unRESIZEd allocation is unchanged following RESIZE failure
: MEM? RESIZE-OK @ 0= IF ADDR1 @ 28 CHECKMEM THEN ; \ Avoid using [IF]
MEM?
T{ ADDR1 @ FREE -> 0 }T \ Tidy up
T{ -1 ALLOCATE SWAP DROP 0= -> FALSE }T \ Memory allocate failed
\ ------------------------------------------------------------------------------
TESTING @ and ! work in ALLOCATEd memory (provided by Peter Knaggs)
: WRITE-CELL-MEM ( ADDR N -- )
1+ 1 DO I OVER ! CELL+ LOOP DROP
;
: CHECK-CELL-MEM ( ADDR N -- )
1+ 1 DO
I SWAP >R >R
T{ R> ( I ) -> R@ ( ADDR ) @ }T
R> CELL+
LOOP DROP
;
\ Cell based access to the heap
T{ 50 CELLS ALLOCATE SWAP ADDR1 ! -> 0 }T
ADDR1 @ 50 WRITE-CELL-MEM
ADDR1 @ 50 CHECK-CELL-MEM
\ ------------------------------------------------------------------------------
MEMORY-ERRORS SET-ERROR-COUNT
CR .( End of Memory-Allocation word tests) CR