-
Notifications
You must be signed in to change notification settings - Fork 11
/
unit_tests.inc
289 lines (242 loc) · 6 KB
/
unit_tests.inc
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
;--------------------------------------------------------
; unit_tests.inc
;
; Contains all macros used for unit testing.
; For sjasmplus.
;--------------------------------------------------------
; -----------------------------------------------------------
; Code for sjasmplus
MACRO UNITTEST_INITIALIZE
; Called to call each unit test.
@UNITTEST_TEST_WRAPPER:
di
ld sp,UNITTEST_STACK
@UNITTEST_CALL_ADDR:
call 0x0000 ; Will be changed by the z80 unit tests.
nop
@UNITTEST_TEST_READY_SUCCESS:
jr UNITTEST_TEST_READY_SUCCESS ; A breakpoint will be set here.
; Stack
@UNITTEST_STACK_BOTTOM:
defw 0
defs 2*50 ; Stack depth of 50 should be enough
@UNITTEST_STACK:
defw 0
; The unit test initialization. Provided by the user.
@UNITTEST_START:
; Disable interrupts
di
; Flow through.
; Directly after the UNITTEST_INITIALIZE macro the user
; should put his test initialization code.
; Note: the stack pointer does not need to be set up.
ENDM
; Macro that must be used at the end of the test case (instead of a ret).
MACRO TC_END
jp UNITTEST_TEST_READY_SUCCESS
ENDM
; Tests a memory address for a specific byte value.
MACRO TEST_MEMORY_BYTE addr?, value?
push hl
ld hl,addr?
ld l,(hl)
ld h,value?
nop ; ASSERTION L == H
pop hl
ENDM
; Tests a memory address for a specific word value.
; If the test fails the '(addr)' can be found in DE.
MACRO TEST_MEMORY_WORD addr?, value?
push hl, de
ld hl,addr?
ld e,(hl)
inc hl
ld d,(hl)
ld hl,value?
nop ; ASSERTION DE == HL
pop de, hl
ENDM
; Creates a failure when reached.
MACRO TEST_FAIL
nop ; ASSERTION false
ENDM
; Compares 2 memory areas containing null terminated strings.
; addr1 = the first string
; addr2 = the second string
MACRO TEST_STRING_PTR addr1?, addr2?
push af
push hl
push de
ld de,addr2?
ld hl,addr1?
.loop:
ld a,(de)
; Check for null termination
cp (hl)
jr nz,.not_equal
or a
jr z,.null_found
inc de
inc hl
jr .loop
.not_equal:
; Strings are not equal
pop de
pop hl
pop af
nop ; ASSERTION false
jr .end
.null_found:
pop de
pop hl
pop af
.end:
ENDM
; Tests a memory address for a specific string.
; addr = the tested string
; string = the compared string, e.g. "STRING"
; term0 = If 0 it is also tested that the string (addr) is null terminated
MACRO TEST_STRING addr?, string?, term0?
push af
push hl
push de
ld de,.comp_string
ld hl,addr?
.loop:
ld a,(de)
; Check for null termination
or a
jr z,.null_found
cp (hl)
inc de
inc hl
jr z,.loop
.not_equal:
; Strings are not equal
pop de
pop hl
pop af
nop ; ASSERTION false
jr .end
.comp_string:
defb string?, 0
.null_found:
; We are at the end of the compare string.
; Check if null termination should be compared with the tested string.
IF term0? == 0
cp (hl)
jr nz,.not_equal
ENDIF
pop de
pop hl
pop af
.end:
ENDM
; Tests a memory area on equality.
; addr1 = the tested memory area
; addr2 = the memory area to compare to
; count = the number of elements to compare
MACRO TEST_MEM_CMP addr1?, addr2?, count?
push af
push hl
push de
push bc
ld de,addr2?
ld hl,addr1?
ld bc,count?
.loop:
ld a,(de)
; Compare
cpi ; cp (hl++); dec bc
jr nz,.not_equal
inc de
jp pe,.loop
; Memory is equal
pop bc
pop de
pop hl
pop af
jr .end
.not_equal:
; Memory areas are not equal
pop bc
pop de
pop hl
pop af
nop ; ASSERTION false
jr .end
.end:
ENDM
; Initializes the registers with default values.
; Use before the tested function is called.
; After the call the TEST_UNCHANGED_XX tests can be used
; to check if the register was changed.
MACRO DEFAULT_REGS
ld a,0xAA
ld bc,0xBBCC
ld de,0xDDEE
ld hl,0x8899
ENDM
MACRO TEST_UNCHANGED_BC
nop ; ASSERTION BC == 0xBBCC
ENDM
MACRO TEST_UNCHANGED_DE
nop ; ASSERTION DE == 0xDDEE
ENDM
MACRO TEST_UNCHANGED_HL
nop ; ASSERTION HL == 0x8899
ENDM
MACRO TEST_UNCHANGED_BC_DE
TEST_UNCHANGED_BC
TEST_UNCHANGED_DE
ENDM
MACRO TEST_UNCHANGED_BC_DE_HL
TEST_UNCHANGED_BC_DE
TEST_UNCHANGED_HL
ENDM
MACRO TEST_UNCHANGED_A
nop ; TEST ASSERTION A == 0xAA
ENDM
MACRO TEST_UNCHANGED_B
nop ; TEST ASSERTION B == 0xBB
ENDM
MACRO TEST_UNCHANGED_C
nop ; TEST ASSERTION C == 0xCC
ENDM
MACRO TEST_UNCHANGED_D
nop ; TEST ASSERTION D == 0xDD
ENDM
MACRO TEST_UNCHANGED_E
nop ; TEST ASSERTION E == 0xEE
ENDM
MACRO TEST_UNCHANGED_H
nop ; TEST ASSERTION H == 0x88
ENDM
MACRO TEST_UNCHANGED_L
nop ; TEST ASSERTION L == 0x99
ENDM
; Macro to test flags for Z.
; Testcase fails for NZ.
MACRO TEST_FLAG_Z
nop ; TEST ASSERTION F & 0x40
ENDM
; Macro to test flags for NZ.
; Testcase fails for Z.
MACRO TEST_FLAG_NZ
nop ; TEST ASSERTION (F & 0x40) == 0
ENDM
; This macro uses all registers, also the shadow registers.
MACRO USE_ALL_REGS
ld a,0xCD
ld bc,0xBBCC
ld de,0xDDEE
ld hl,0xFF11
ld ix,0x7788
ld iy,0x99AA
exx
ld a,0xDC
ld bc,0x1122
ld de,0x3344
ld hl,0x5566
exx
ENDM