-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_file_write_chunks.c
401 lines (340 loc) · 13.4 KB
/
test_file_write_chunks.c
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
#include <check.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h> // for unlink()
#endif
#include "adflib.h"
//#include "adf_util.h"
#include "test_util.h"
typedef struct test_data_s {
struct AdfDevice * device;
// struct AdfVolume * vol;
char * adfname;
char * volname;
uint8_t fstype; // 0 - OFS, 1 - FFS
unsigned nVolumeBlocks;
AdfFileMode openMode;
unsigned char * buffer;
unsigned bufsize;
unsigned chunksize;
} test_data_t;
void setup ( test_data_t * const tdata );
void teardown ( test_data_t * const tdata );
START_TEST ( test_check_framework )
{
ck_assert ( 1 );
}
END_TEST
void test_file_write ( test_data_t * const tdata )
{
struct AdfDevice * const device = tdata->device;
ck_assert_ptr_nonnull ( device );
///
/// mount the test volume
///
// mount the test volume
struct AdfVolume * vol = // tdata->vol =
adfVolMount ( tdata->device, 0, ADF_ACCESS_MODE_READWRITE );
ck_assert_ptr_nonnull ( vol );
// check it is an empty floppy disk
unsigned free_blocks_before = adfCountFreeBlocks ( vol );
ck_assert_int_eq ( tdata->nVolumeBlocks, free_blocks_before );
int nentries = adfDirCountEntries ( vol, vol->curDirPtr );
ck_assert_int_eq ( 0, nentries );
///
/// create a new file
///
char filename[] = "testfile.tmp";
struct AdfFile * file = adfFileOpen ( vol, filename, tdata->openMode );
ck_assert_ptr_nonnull ( file );
adfFileClose ( file );
// reset volume state (remount)
adfVolUnMount ( vol );
vol = // tdata->vol =
adfVolMount ( tdata->device, 0, ADF_ACCESS_MODE_READWRITE );
// verify free blocks
const unsigned file_blocks_used_by_empty_file = 1;
ck_assert_int_eq ( free_blocks_before - file_blocks_used_by_empty_file,
adfCountFreeBlocks ( vol ) );
// verify the number of entries
ck_assert_int_eq ( 1, adfDirCountEntries ( vol, vol->curDirPtr ) );
// verify file information (meta-data)
file = adfFileOpen ( vol, filename, ADF_FILE_MODE_READ );
ck_assert_uint_eq ( 0, file->fileHdr->byteSize );
ck_assert_uint_eq ( 0, file->pos );
ck_assert_int_eq ( 0, file->posInExtBlk );
//ck_assert_int_eq ( 0, file->posInDataBlk );
ck_assert_int_eq ( 0, file->nDataBlock );
ck_assert_int_eq ( adfEndOfFile ( file ), true );
adfFileClose ( file );
// the same when open for appending
/* file = adfFileOpen ( vol, filename, "a" );
ck_assert_uint_eq ( 0, file->fileHdr->byteSize );
ck_assert_uint_eq ( 0, file->pos );
ck_assert_int_eq ( 0, file->posInExtBlk );
//ck_assert_int_eq ( 0, file->posInDataBlk );
ck_assert_int_eq ( 0, file->nDataBlock );
ck_assert_int_eq ( adfEndOfFile ( file ), true );
adfFileClose ( file );
*/
// the same when open for writing
file = adfFileOpen ( vol, filename, ADF_FILE_MODE_WRITE );
ck_assert_uint_eq ( 0, file->fileHdr->byteSize );
ck_assert_uint_eq ( 0, file->pos );
ck_assert_int_eq ( 0, file->posInExtBlk );
//ck_assert_int_eq ( 0, file->posInDataBlk );
ck_assert_int_eq ( 0, file->nDataBlock );
ck_assert_int_eq ( adfEndOfFile ( file ), true );
adfFileClose ( file );
///
/// test writing data to the created above, empty file
///
// open for writing
file = adfFileOpen ( vol, filename, ADF_FILE_MODE_WRITE );
ck_assert_uint_eq ( 0, file->fileHdr->byteSize );
ck_assert_int_eq ( file->fileHdr->firstData, 0 );
ck_assert_uint_eq ( 0, file->pos );
ck_assert_int_eq ( 0, file->posInExtBlk );
//ck_assert_int_eq ( 0, file->posInDataBlk );
ck_assert_int_eq ( 0, file->nDataBlock );
ck_assert_int_eq ( adfEndOfFile ( file ), true );
// write data buffer to the file
const unsigned bufsize = tdata->bufsize;
const unsigned char * const buffer = tdata->buffer;
const unsigned chunksize = tdata->chunksize;
ck_assert_uint_gt ( bufsize, chunksize );
// writing the file in chunks, result should be the same as:
// int bytes_written = adfFileWrite ( file, bufsize, buffer );
// ck_assert_int_eq ( bufsize, bytes_written );
const unsigned nchunks = bufsize / chunksize +
( bufsize % chunksize > 0 ? 1 : 0 );
ck_assert_uint_gt ( nchunks, 0 );
for ( unsigned i = 0 ; i < nchunks ; ++i ) {
const unsigned offset = i * chunksize;
const uint8_t * const chunk = &buffer[ offset ];
unsigned const wsize = ( i == nchunks - 1 ) ? // the last chunk
//bufsize - ( nchunks - 1 ) * chunksize + bufsize % chunksize :
( bufsize % chunksize == 0 ? chunksize : // the last chunk is chunksize
bufsize % chunksize ) : // or what is left
chunksize; // not the last chunk (-> chunksize)
/*if ( offset < 488 && offset + wsize >= 488 ) {
printf ("wrinting chunk %u (nchunks %u), chunksize %u, wsize %u, offset %u, bufsize %u\n",
chunk_i, nchunks, chunksize, wsize, offset, bufsize );
fflush (stdout);
}*/
//ADF_RETCODE rc = adfFileSeek ( file, offset );
//ck_assert_int_eq ( rc, ADF_RC_OK );
unsigned bytes_written = adfFileWrite ( file, wsize, chunk );
ck_assert_uint_eq ( wsize, bytes_written );
}
ck_assert_uint_eq ( bufsize, file->fileHdr->byteSize );
ck_assert_int_gt ( file->fileHdr->firstData, 0 );
ck_assert_uint_eq ( bufsize, file->pos );
//ck_assert_int_eq ( 0, file->posInExtBlk );
//ck_assert_int_eq ( datablock2posInExtBlk (
// filesize2datablocks ( bufsize, vol->datablockSize ) ),
// file->posInExtBlk );
unsigned expected_posInExtBlk = datablock2posInExtBlk (
filesize2datablocks ( bufsize, vol->datablockSize ) );
//int expected_posInExtBlk = datablock2posInExtBlk (
// pos2datablockIndex ( bufsize, vol->datablockSize ) );
ck_assert_msg ( expected_posInExtBlk == file->posInExtBlk ||
( file->posInExtBlk == 72 && expected_posInExtBlk == 0 ),
"file->posInExtBlk %d != %d, bufsize %u",
file->posInExtBlk, expected_posInExtBlk, tdata->bufsize );
//ck_assert_int_eq ( 1, file->posInDataBlk );
ck_assert_int_eq ( ( ( bufsize - 1 ) / vol->datablockSize ) + 1, file->nDataBlock );
ck_assert_int_eq ( adfEndOfFile ( file ), true );
adfFileClose ( file );
// reset volume state (remount)
adfVolUnMount ( vol );
vol = //tdata->vol =
adfVolMount ( tdata->device, 0, ADF_ACCESS_MODE_READWRITE );
// verify free blocks
//ck_assert_int_eq ( free_blocks_before - file_blocks_used_by_empty_file - 1,
// adfCountFreeBlocks ( vol ) );
//int expected_free_blocks = free_blocks_before - file_blocks_used_by_empty_file - 1;
unsigned expected_free_blocks =
free_blocks_before - filesize2blocks ( bufsize, vol->datablockSize );
unsigned free_blocks = adfCountFreeBlocks ( vol );
ck_assert_msg ( free_blocks == expected_free_blocks,
"Free blocks incorrect: %d (should be %d), bufsize %d",
free_blocks, expected_free_blocks, bufsize );
// verify the number of entries
ck_assert_int_eq ( 1, adfDirCountEntries ( vol, vol->curDirPtr ) );
// verify file information (meta-data)
file = adfFileOpen ( vol, filename, ADF_FILE_MODE_READ );
ck_assert_uint_eq ( bufsize, file->fileHdr->byteSize );
ck_assert_int_gt ( file->fileHdr->firstData, 0 );
ck_assert_uint_eq ( 0, file->pos );
ck_assert_int_eq ( 0, file->posInExtBlk );
//ck_assert_int_eq ( 0, file->posInDataBlk );
ck_assert_int_eq ( 1, file->nDataBlock );
ck_assert_int_eq ( adfEndOfFile ( file ), false );
unsigned char * rbuf = malloc ( bufsize );
ck_assert_ptr_nonnull ( rbuf );
unsigned bytes_read = adfFileRead ( file, bufsize, rbuf );
free ( rbuf );
rbuf = NULL;
ck_assert_int_eq ( bufsize, bytes_read );
ck_assert_uint_eq ( bufsize, file->pos );
//ck_assert_int_eq ( 0, file->posInExtBlk ); // TODO
//ck_assert_int_eq ( 1, file->posInDataBlk );
unsigned expected_nDataBlock =
//( ( bufsize - 1 ) / vol->datablockSize ) + 1;
//filesize2datablocks ( bufsize, vol->datablockSize ) + 1;
pos2datablockIndex ( bufsize - 1, vol->datablockSize ) + 1;
//ck_assert_int_eq ( expected_nDataBlock, file->nDataBlock );
ck_assert_msg ( file->nDataBlock == expected_nDataBlock,
"file->nDataBlock %d == expected %d, bufsize %u",
file->nDataBlock, expected_nDataBlock, bufsize );
ck_assert_int_eq ( adfEndOfFile ( file ), true );
adfFileClose ( file );
ck_assert_msg ( verify_file_data ( vol, filename, buffer, bufsize, 10 ) == 0,
"Data verification failed for bufsize %u (0x%x)", bufsize, bufsize );
//printf ( "File meta-data verification for bufsize %u (0x%x):\n", bufsize, bufsize );
ck_assert_msg ( validate_file_metadata ( vol, filename, 10 ) == 0,
"File meta-data verification failed for bufsize %u (0x%x)", bufsize, bufsize );
// umount volume
adfVolUnMount ( vol );
}
static const unsigned buflen[] = {
1, 2, 256,
487, 488, 489,
511, 512, 513,
970, 974, 975, 976, 977, 978,
1022, 1023, 1024, 1025,
2047, 2048, 2049, 2050,
4095, 4096, 4097,
10000, 20000, 35000, 35130,
35136,
35137, // min. file requiring an ext. block (OFS)
35138,
36000, 36864,
36865, // min. file requiring an ext. block (FFS)
37000, 37380, 40000, 50000,
60000, 69784, 69785, 69796, 69800, 70000,
100000, 200000, 512000, 800000
};
static const unsigned buflensize = sizeof ( buflen ) / sizeof (int);
static const unsigned chunklen[] = {
//1, 2,
3,
// 4, 5, 6,
7,
//8, 9, 10,
//15, 16, 32, 64,
256,
487, 488, 489,
511, 512, 513,
970, 974, 975, 976, 977, 978,
1022, 1023, 1024, 1025,
2047, 2048, 2049, 2050,
4095, 4096
};
static const unsigned chunklensize = sizeof ( chunklen ) / sizeof (unsigned);
START_TEST ( test_file_write_ofs )
{
test_data_t test_data = {
.adfname = "test_file_write_chunks_ofs.adf",
.volname = "Test_file_write_chunks_ofs",
.fstype = 0, // OFS
.openMode = ADF_FILE_MODE_WRITE,
.nVolumeBlocks = 1756
};
for ( unsigned i = 0 ; i < buflensize ; ++i ) {
test_data.bufsize = buflen[i];
for ( unsigned j = 0 ; j < chunklensize ; ++j ) {
if ( chunklen[j] >= test_data.bufsize )
break;
test_data.chunksize = chunklen[j];
setup ( &test_data );
test_file_write ( &test_data );
teardown ( &test_data );
}
}
}
END_TEST
START_TEST ( test_file_write_ffs )
{
test_data_t test_data = {
.adfname = "test_file_write_chunks_ffs.adf",
.volname = "Test_file_write_chunks_ffs",
.fstype = 1, // FFS
.openMode = ADF_FILE_MODE_WRITE,
.nVolumeBlocks = 1756
};
for ( unsigned i = 0 ; i < buflensize ; ++i ) {
test_data.bufsize = buflen[i];
for ( unsigned j = 0 ; j < chunklensize ; ++j ) {
if ( chunklen[j] >= test_data.bufsize )
break;
test_data.chunksize = chunklen[j];
setup ( &test_data );
test_file_write ( &test_data );
teardown ( &test_data );
}
}
}
END_TEST
Suite * adflib_suite ( void )
{
Suite * s = suite_create ( "adflib" );
TCase * tc = tcase_create ( "check framework" );
tcase_add_test ( tc, test_check_framework );
suite_add_tcase ( s, tc );
tc = tcase_create ( "adflib test_file_write_ofs" );
tcase_add_test ( tc, test_file_write_ofs );
tcase_set_timeout ( tc, 120 );
suite_add_tcase ( s, tc );
tc = tcase_create ( "adflib test_file_write_ffs" );
//tcase_add_checked_fixture ( tc, setup_ffs, teardown_ffs );
tcase_add_test ( tc, test_file_write_ffs );
tcase_set_timeout ( tc, 120 );
suite_add_tcase ( s, tc );
return s;
}
int main ( void )
{
Suite * s = adflib_suite();
SRunner * sr = srunner_create ( s );
adfEnvInitDefault();
srunner_run_all ( sr, CK_VERBOSE ); //CK_NORMAL );
adfEnvCleanUp();
int number_failed = srunner_ntests_failed ( sr );
srunner_free ( sr );
return ( number_failed == 0 ) ?
EXIT_SUCCESS :
EXIT_FAILURE;
}
void setup ( test_data_t * const tdata )
{
tdata->device = adfDevCreate ( "ramdisk", tdata->adfname, 80, 2, 11 );
if ( ! tdata->device ) {
//return;
exit(1);
}
if ( adfCreateFlop ( tdata->device, tdata->volname, tdata->fstype ) != ADF_RC_OK ) {
fprintf ( stderr, "adfCreateFlop error creating volume: %s\n",
tdata->volname );
exit(1);
}
//tdata->vol = adfVolMount ( tdata->device, 0, ADF_ACCESS_MODE_READWRITE );
//if ( ! tdata->vol )
// return;
// exit(1);
tdata->buffer = malloc ( tdata->bufsize );
if ( ! tdata->buffer )
exit(1);
//pattern_AMIGAMIG ( tdata->buffer, tdata->bufsize );
pattern_random ( tdata->buffer, tdata->bufsize );
}
void teardown ( test_data_t * const tdata )
{
free ( tdata->buffer );
tdata->buffer = NULL;
//adfVolUnMount ( tdata->vol );
adfDevUnMount ( tdata->device );
adfDevClose ( tdata->device );
//unlink ( tdata->adfname );
}