-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RT_read_modify_write_tests.spin2
545 lines (451 loc) · 20.9 KB
/
RT_read_modify_write_tests.spin2
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'' =================================================================================================
''
'' File....... RT_read_modify_write_tests.spin2
'' Purpose.... This object exercises the read/modify/write capabilites of the flash filesystem
'' Author..... Stephen M Moraco
'' -- see below for terms of use
'' Started.... DEC 2023
'' Updated.... 18 DEC 2023
''
'' =================================================================================================
CON
_CLKFREQ = 320_000_000
{
' debug via serial to RPi (using RPi gateway pins)
' USES LED1 (56) and LED2 (57) pins
DEBUG_PIN_TX = 57 ' GRY GPIO 10
DEBUG_PIN_RX = 56 ' WHT GPIO 8 (gnd is BLK GPIO 6)
DEBUG_WINDOWS_OFF = -1 ' no debug windows showing
DEBUG_BAUD = 2_000_000 ' comms at 2Mbit
'}
OBJ
flash : "flash_fs"
utils : "RT_utilities"
CON
RECORD_LEN = 384 ' just for fun, tiny but even number of longs
RECORD_DATA_LEN = RECORD_LEN - 12
DAT
testfile BYTE "rw4blocks.bin", 0
testfile2 BYTE "rw2blocks.bin", 0
testfile3 BYTE "rw3blocks.bin", 0
testfile4 BYTE "rwxblocks.bin", 0
record
recordMARK LONG $adbeedfe ' visual start of record marker (FEED BEED in memory)
recordID LONG 0
recordData LONG 0[RECORD_DATA_LEN/4]
recordCRC LONG 0
recordEnd
writeLength LONG 0
recordsThisTest LONG 0
PUB go() | status, handle, testValue, expectedValue, bytePattern, endPosn, recordOffset, expectedFileBlocks, expectedFileSize
status := flash.format() ' Comment out this line to not erase all files
if status < 0
utils.showError(@"format()", status)
return
status := flash.mount()
if status < 0
utils.showError(@"mount()", status)
return
utils.ShowStats() ' should have empty filesystem
utils.showFiles()
utils.showChipAndDriverVersion()
' test specs:
' ----------------------------------------
' TEST #1 R+ (file exists)
utils.startTestGroup(@"TEST #1 R+ (file exists) - replace three")
recordsThisTest := 40
expectedFileSize := recordsThisTest * RECORD_LEN
expectedFileBlocks := utils.blockCountForFileSize(expectedFileSize)
' open for modify replace record that spans blocks
testWriteRecords(@testfile, recordsThisTest, 0, 0)
' verify that file is correct
testVerifyRecords(@testfile, recordsThisTest)
' replace 1st record
handle := testReplaceWithinFile(@testfile, recordsThisTest)
' seek away from location so block is written
recordOffset := (recordsThisTest - 3) * RECORD_LEN
seekAwayFromWrite(handle, recordOffset)
' ensure close does NOT write again
' close file (effect should be nothing since earlier seek caused update
utils.startTest(@"Close written file (no further block updates)")
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
' get single file stats and verify against expectation
utils.startTest(@"Ensure expected file stats")
utils.evaluateFileStats(@testfile, expectedFileBlocks, expectedFileSize)
' re-open for read and verify all records
testVerifyRecordsAfterReplace(@testfile, recordsThisTest, 1, 4)
' ----------------------------------------
' TEST #2 R+ (file exists)
utils.startTestGroup(@"TEST #2 R+ (file exists) - replace span")
recordsThisTest := 20
expectedFileSize := recordsThisTest * RECORD_LEN
expectedFileBlocks := utils.blockCountForFileSize(expectedFileSize)
' create file open/create/close
testWriteRecords(@testfile2, recordsThisTest, 1, 4)
' verify that file is correct
testVerifyRecords(@testfile2, recordsThisTest)
' open for modify
' replace record that spans blocks
handle := testReplaceRecordWithinFile(@testfile2, recordsThisTest, 4)
' close file ensure that change gets written
utils.startTest(@"Close written file (should write last updated block!)")
' close file (effect should be nothing since earlier seek caused update
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
' get single file stats and verify against expectation
utils.startTest(@"Ensure expected file stats")
utils.evaluateFileStats(@testfile2, expectedFileBlocks, expectedFileSize)
' re-open for read and verify all records
testVerifyRecordsAfterReplace(@testfile2, recordsThisTest, 2, 6)
' ----------------------------------------
' TEST #3 R+ (file exists)
utils.startTestGroup(@"TEST #3 R+ (file exists) - replace span, last block")
recordsThisTest := 21
expectedFileSize := recordsThisTest * RECORD_LEN
expectedFileBlocks := utils.blockCountForFileSize(expectedFileSize)
' create file open/create/close
testWriteRecords(@testfile3, recordsThisTest, 2, 6)
' verify that file is correct
testVerifyRecords(@testfile3, recordsThisTest)
' open for modify
' replace record that spans blocks (last record in file spans blocks)
handle := testReplaceRecordWithinFile(@testfile3, recordsThisTest, 8)
' seek away from location so block is written
recordOffset := 0
seekAwayFromWrite(handle, recordOffset)
' close file (effect should be nothing since earlier seek caused update
utils.startTest(@"Close written file (no further block updates)")
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
' get single file stats and verify against expectation
utils.startTest(@"Ensure expected file stats")
utils.evaluateFileStats(@testfile3, expectedFileBlocks, expectedFileSize)
' re-open for read and verify all records
testVerifyRecordsAfterReplace(@testfile3, recordsThisTest, 3, 9)
utils.ShowStats()
utils.showFiles()
' ------------------------------------------
' TEST #4 W+ (new file over the top of old)
utils.startTestGroup(@"TEST #4 W+ (file exists) - replace earliest span")
recordsThisTest := 32
expectedFileSize := recordsThisTest * RECORD_LEN
expectedFileBlocks := utils.blockCountForFileSize(expectedFileSize)
' open for modify replace record that spans blocks
testWriteRecords(@testfile2, recordsThisTest, 3 - 1, 9 - 2) ' overwrite replaces existing 2-block file
' verify that file is correct
testVerifyRecords(@testfile2, recordsThisTest)
' replace 1st record
handle := testReplaceWithinFile(@testfile2, recordsThisTest)
' close file ensure that change gets written
utils.startTest(@"Close written file (should write last updated block!)")
' close file (effect should be nothing since earlier seek caused update
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
' get single file stats and verify against expectation
utils.startTest(@"Ensure expected file stats")
utils.evaluateFileStats(@testfile2, expectedFileBlocks, expectedFileSize)
' re-open for read and verify all records
testVerifyRecordsAfterReplace(@testfile2, recordsThisTest, 3, 11)
utils.ShowStats()
utils.showFiles()
utils.ShowTestEndCounts()
debug("* Test Complete")
repeat
PRI seekAwayFromWrite(handle, seekOffset) | status, endPosn
' seek to another location causing record to be written
utils.startTest(@"seek away from write location (should update block)")
utils.setCheckCountPerTest(2)
endPosn := flash.seek(handle, seekOffset, flash.SK_FILE_START)
utils.evaluateSubValue(endPosn, @"seek()", seekOffset)
status := flash.error()
utils.evaluateSubStatus(status, @"seek()", flash.SUCCESS)
PRI buildRecord(recordNbr) | testValue
' fill in buffer with well formatted buffer
testValue := $40 + recordNbr ' A, B, C, etc.
recordId := recordNbr
bytefill(@recordData, testValue, RECORD_DATA_LEN)
recordCRC := utils.record_crc(@record, RECORD_LEN)
PRI replaceRecord(handle, rcdNumber) | endPosn, recordOffset, status
'debug("* write rcd #", udec_(recordNbr), " of 10")
'utils.dbgMemDump(@"Record", @record, RECORD_LEN)
utils.setCheckCountPerTest(4)
recordOffset := (rcdNumber - 1) * RECORD_LEN
setMsgRcdNbr(rcdNumber)
endPosn := flash.seek(handle, recordOffset, flash.SK_FILE_START)
utils.evaluateSubValue(endPosn, @seekMsg, recordOffset)
status := flash.error()
utils.evaluateSubStatus(status, @seekMsg, flash.SUCCESS)
writeLength := flash.write(handle, @record, RECORD_LEN)
utils.evaluateSubValue(writeLength, @writeMsg, RECORD_LEN)
status := flash.error()
utils.evaluateSubStatus(status, @writeMsg, flash.SUCCESS)
' dump commit chain
utils.showPendingCommitChain(handle)
debug(" ")
DAT ' verifyMessages
readMsg BYTE "Read() Rcd #"
readNbr BYTE "xx",0
writeMsg BYTE "Write() Rcd #"
writeNbr BYTE "xx",0
seekMsg BYTE "Seek() Rcd #"
seekNbr BYTE "xx",0
idMsg BYTE "recordID Rcd #"
idNbr BYTE "xx",0
crcMsg BYTE "recordCRC Rcd #"
crcNbr BYTE "xx",0
PRI setMsgRcdNbr(rcdNumber) | units, tens
tens := (rcdNumber / 10) + $30
units := (rcdNumber +// 10) + $30
if(tens == $30)
' no leading zero
BYTE[@readNbr][0] := units
BYTE[@readNbr][1] := 0
BYTE[@writeNbr][0] := units
BYTE[@writeNbr][1] := 0
BYTE[@seekNbr][0] := units
BYTE[@seekNbr][1] := 0
BYTE[@idNbr][0] := units
BYTE[@idNbr][1] := 0
BYTE[@crcNbr][0] := units
BYTE[@crcNbr][1] := 0
else
BYTE[@readNbr][0] := tens
BYTE[@readNbr][1] := units
BYTE[@writeNbr][0] := tens
BYTE[@writeNbr][1] := units
BYTE[@seekNbr][0] := tens
BYTE[@seekNbr][1] := units
BYTE[@idNbr][0] := tens
BYTE[@idNbr][1] := units
BYTE[@crcNbr][0] := tens
BYTE[@crcNbr][1] := units
PRI dumpBlocksAtSplice(pFilename, rcdNbr, totalRecordsWritten, pRcdBuffer, rcdSize) | fileOffset, p_buffer, p_buffer2, blkAddr, blkAddr2
fileOffset := (rcdNbr - 1) * rcdSize
p_buffer, p_buffer2, blkAddr, blkAddr2 := flash.TEST_get4kBlockAndNextAtOffset(pFilename, fileOffset)
if p_buffer
' dump record
utils.dbgMemDump(@"Record HEAD", pRcdBuffer, 128)
utils.dbgMemDump(@"Record TAIL", @BYTE[pRcdBuffer][rcdSize-128], 128)
debug(" ")
' dump first block tail
debug("/* [", uhex_word_(blkAddr),"]")
utils.dbgMemDump(@"lead-buffer TAIL", @BYTE[p_buffer][4096-256], 256)
' dump second block head
if p_buffer2
debug(" ")
debug("/* [", uhex_word_(blkAddr2),"]")
utils.dbgMemDump(@"trail-buffer HEAD", p_buffer2, 256)
else
debug("- no following block")
else
debug("- dumpBlocksAtSplice() FILE NOT FOUND [", zstr_(pFilename), "]")
PRI testWriteRecords(pFilename, recordCount, initialFiles, initialBlocks) | status, handle, testValue, expectedValue, recordNbr, wrLength, rdLength, pBlock, expectedFileSize, expectedFileBlocks, calcCRC, sizeUnused, allocatedFileSize
' ----------------------------------
' TEST
' open file for write
debug(" ")
debug(" ---------- Write Records ------------ ")
' ensure returns SUCCESS
utils.startTest(@"Open File for write")
handle := flash.open(pFilename,"w")
utils.evaluateResultNegError(handle, flash.SUCCESS)
utils.startTest(@"Write Records")
utils.setCheckCountPerTest(2)
status := flash.SUCCESS
repeat recordNbr from 1 to recordCount
buildRecord(recordNbr)
setMsgRcdNbr(recordNbr)
'debug("* write rcd #", udec_(recordNbr), " of 10")
'utils.dbgMemDump(@"Record", @record, RECORD_LEN)
writeLength := flash.write(handle, @record, RECORD_LEN)
utils.evaluateSubValue(writeLength, @writeMsg, RECORD_LEN)
status := flash.error()
utils.evaluateSubStatus(status, @writeMsg, flash.SUCCESS)
if status <> flash.SUCCESS
quit ' abort on first failure
utils.startTest(@"ensure no errors on Record writes")
utils.evaluateResultNegError(status, flash.SUCCESS)
' dump commit chain
utils.showPendingCommitChain(handle)
' close file
utils.startTest(@"Close written file")
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
expectedFileSize := recordCount * RECORD_LEN
expectedFileBlocks := utils.blockCountForFileSize(expectedFileSize)
allocatedFileSize := utils.bytesAllocatedInBlocks(expectedFileBlocks)
' get filesystem stats and verify against expectation
utils.startTest(@"Ensure expected file system changes")
utils.evaluateFSStats(@"post file write", initialFiles + 1, initialBlocks + expectedFileBlocks)
' get single file stats and verify against expectation
utils.startTest(@"Ensure expected file stats")
utils.evaluateFileStats(pFilename, expectedFileBlocks, expectedFileSize)
utils.startTest(@"Ensure expected unused bytes stats")
sizeUnused := flash.file_size_unused(pFilename)
utils.evaluateSingleValue(sizeUnused, @"file_size_unused()", allocatedFileSize - expectedFileSize)
utils.startTest(@"Ensure no error on size unused call")
status := flash.error()
utils.evaluateResultNegError(status, flash.SUCCESS)
PRI testVerifyRecords(pFilename, recordCount) | status, handle, testValue, expectedValue, recordNbr, wrLength, rdLength, pBlock, expectedFileSize, expectedFileBlocks, calcCRC, sizeUnused, allocatedFileSize
debug(" ")
debug(" ------------ Verify Original Records ---------------- ")
' open file for read
utils.startTest(@"Open File for read")
handle := flash.open(pFilename,"r")
utils.evaluateResultNegError(handle, flash.SUCCESS)
' seek to various locations and read longs validating for correct value
utils.startTest(@"read expected RECORDs")
utils.setCheckCountPerTest(3)
repeat recordNbr from 1 to recordCount
setMsgRcdNbr(recordNbr)
rdLength := flash.read(handle, @record, RECORD_LEN)
if rdLength <> RECORD_LEN
status := flash.error()
quit
' ensure length is same as expected
utils.evaluateSubValue(rdLength, @readMsg, RECORD_LEN)
' ensure record ID is same as expected
utils.evaluateSubValue(recordId, @idMsg, recordNbr)
' ensure record CRC is same as expected
calcCRC := utils.record_crc(@record, RECORD_LEN)
utils.evaluateSubValue(calcCRC, @crcMsg, recordCRC)
' IF WE HAVE a CRC error then show buffers!
if calcCRC <> recordCRC
dumpBlocksAtSplice(pFilename, recordNbr, recordCount, @record, RECORD_LEN)
utils.startTest(@"ensure no errors on reads")
utils.evaluateResultNegError(status, flash.SUCCESS)
' read past end of file to verify read returns error
utils.startTest(@"BAD: read one more Record than written")
utils.setCheckCountPerTest(2)
setMsgRcdNbr(999)
rdLength := flash.read(handle, @record, RECORD_LEN)
utils.evaluateSubValue(rdLength, @readMsg, 0)
status := flash.error()
utils.evaluateSubStatus(status, @readMsg, flash.E_END_OF_FILE)
' close file
utils.startTest(@"Close read file")
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
PRI testReplaceRecordWithinFile(pFilename, maxRecords, recordNumber) : handle | status, newRecordNbr, recordOffset
debug(" ")
debug(" ----------- Overwrite single Record ----------- ")
' open file for modify writes
utils.startTest(@"Open File for modify write")
handle := flash.open(pFilename, flash.FILEMODE_READ_EXTENDED)
status := flash.error()
utils.evaluateResultNegError(status, flash.SUCCESS)
if status <> flash.SUCCESS
handle := flash.E_NO_HANDLE
debug("* ERROR filed to open file for modify")
return
' replace first, random middle and last records
' - FIRST
utils.startTest(@"Write Record")
newRecordNbr := recordNumber + maxRecords
buildRecord(newRecordNbr)
replaceRecord(handle, recordNumber)
status := flash.error()
utils.startTest(@"ensure no errors on Record writes")
utils.evaluateResultNegError(status, flash.SUCCESS)
' dump commit chain
utils.showPendingCommitChain(handle)
PRI testReplaceWithinFile(pFilename, maxRecords) : handle | status, recordNbr, recordOffset, newRecordNbr
debug(" ")
debug(" ----------- Overwrite 1st/mid/last Records ----------- ")
' open file for modify writes
utils.startTest(@"Open File for modify write")
handle := flash.open(pFilename, flash.FILEMODE_READ_EXTENDED)
status := flash.error()
utils.evaluateResultNegError(status, flash.SUCCESS)
if status <> flash.SUCCESS
handle := flash.E_NO_HANDLE
debug("* ERROR filed to open file for modify")
return
' replace first, random middle and last records
' - FIRST
utils.startTest(@"Write 1st Record")
recordNbr := 1
newRecordNbr := recordNbr + maxRecords
buildRecord(newRecordNbr)
replaceRecord(handle, recordNbr)
' - MID-somewhere
utils.startTest(@"Write Mid Record")
recordNbr := maxRecords / 2
newRecordNbr := recordNbr + maxRecords
buildRecord(newRecordNbr)
replaceRecord(handle, recordNbr)
' - LAST
utils.startTest(@"Write LAST Record")
recordNbr := maxRecords
newRecordNbr := recordNbr + maxRecords
buildRecord(newRecordNbr)
replaceRecord(handle, recordNbr)
status := flash.error()
utils.startTest(@"ensure no errors on Record writes")
utils.evaluateResultNegError(status, flash.SUCCESS)
' dump commit chain
utils.showPendingCommitChain(handle)
PRI testVerifyRecordsAfterReplace(pFilename, recordCount, initialFiles, initialBlocks) | handle, recordNbr, rdLength, status, calcCRC, testRecordNbr, expectedFileSize, expectedFileBlocks
debug(" ")
debug(" ------------ Verify Replaced Records ------------- ")
' open file for read
utils.startTest(@"Open File for read")
handle := flash.open(pFilename,"r")
utils.evaluateResultNegError(handle, flash.SUCCESS)
' seek to various locations and read longs validating for correct value
utils.startTest(@"read expected RECORDs")
utils.setCheckCountPerTest(3)
repeat recordNbr from 1 to recordCount
setMsgRcdNbr(recordNbr)
rdLength := flash.read(handle, @record, RECORD_LEN)
if rdLength <> RECORD_LEN
status := flash.error()
quit
' ensure length is same as expected
utils.evaluateSubValue(rdLength, @readMsg, RECORD_LEN)
' ensure record ID is same as expected
testRecordNbr := (recordId <> recordNbr) ? recordNbr + recordCount : recordNbr
utils.evaluateSubValue(recordId, @idMsg, testRecordNbr)
' ensure record CRC is same as expected
calcCRC := utils.record_crc(@record, RECORD_LEN)
utils.evaluateSubValue(calcCRC, @crcMsg, recordCRC)
' IF WE HAVE a CRC error then show buffers!
if calcCRC <> recordCRC
dumpBlocksAtSplice(pFilename, recordNbr, recordCount, @record, RECORD_LEN)
utils.startTest(@"ensure no errors on reads")
utils.evaluateResultNegError(status, flash.SUCCESS)
' close file
utils.startTest(@"Close read file")
status := flash.close(handle)
utils.evaluateResultNegError(status, flash.SUCCESS)
' get filesystem stats and verify against expectation
utils.startTest(@"Ensure expected file system changes")
utils.evaluateFSStats(@"post file write", initialFiles, initialBlocks)
expectedFileSize := recordCount * RECORD_LEN
expectedFileBlocks := utils.blockCountForFileSize(expectedFileSize)
' get single file stats and verify against expectation
utils.startTest(@"Ensure expected file stats")
utils.evaluateFileStats(pFilename, expectedFileBlocks, expectedFileSize)
CON { license }
{{
=================================================================================================
Terms of Use: MIT License
Copyright (c) 2023 Iron Sheep Productions, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=================================================================================================
}}