-
Notifications
You must be signed in to change notification settings - Fork 2
/
Test5.java
551 lines (495 loc) · 17 KB
/
Test5.java
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
546
547
548
549
550
551
class Test5 extends Thread {
final static int DEFAULTFILES = 48;
final int files;
int fd;
final byte[] buf16 = new byte[16];
final byte[] buf32 = new byte[32];
final byte[] buf24 = new byte[24];
int size;
public Test5( String args[] ) {
files = Integer.parseInt( args[0] );
// SysLib.cout( "files = " + files + "\n" );
}
public Test5() {
files = DEFAULTFILES;
// SysLib.cout( "files = " + files + "\n" );
}
public void run( ) {
if ( test1( ) ) // format with specified # of files
SysLib.cout("Correct behavior of format......................2\n");
if ( test2( ) ) // open "css430" with "w+"
SysLib.cout("Correct behavior of open........................2\n");
if ( test3( ) ) // write buf[16]
SysLib.cout("Correct behavior of writing a few bytes.........2\n");
if ( test4( ) ) // close fd
SysLib.cout("Correct behavior of close.......................2\n");
if ( test5( ) ) // read buf[16] from "css430"
SysLib.cout("Correct behavior of reading a few bytes.........2\n");
if ( test6( ) ) // append buf[32] to "css430"
SysLib.cout("Correct behavior of appending a few bytes.......1\n");
if ( test7( ) ) // seek and read from "css430"
SysLib.cout("Correct behavior of seeking in a small file.....1\n");
if ( test8( ) ) // open "css430" with "w+"
SysLib.cout("Correct behavior of read/writing a small file.0.5\n");
test9( ); // open "bothell" with "w+"
if ( test10( ) ) // write buf[512 * 13]
SysLib.cout("Correct behavior of writing a lot of bytes....0.5\n");
test11( ); // close fd
if ( test12( ) ) // read buf[512 * 13] from "bothell"
SysLib.cout("Correct behavior of reading a lot of bytes....0.5\n");
if ( test13( ) ) // append buf[32] to "bothell"
SysLib.cout("Correct behavior of appending to a large file.0.5\n");
if ( test14( ) ) // seek and read from "bothell"
SysLib.cout("Correct behavior of seeking in a large file...0.5\n");
if ( test15( ) ) // open "bothell" with "w+"
SysLib.cout("Correct behavior of read/writing a large file.0.5\n");
if ( test16( ) ) // delete "css430"
SysLib.cout("Correct behavior of delete....................0.5\n");
if ( test17( ) ) // create "uwb0" - "uwb45" of buf[512 * 13]
SysLib.cout("Correct behavior of creating over 40 files ...0.5\n");
if ( test18( ) ) // "uwb1" read/written among Test5 and Test6
SysLib.cout("Correct behavior of two fds to the same file..0.5\n");
SysLib.cout( "Test completed\n" );
SysLib.exit( );
}
private boolean test1( ) {
//.............................................."
SysLib.cout( "1: format( " + files + " )..................." );
SysLib.format( files );
byte[] superblock = new byte[512];
SysLib.rawread( 0, superblock );
int totalBlocks = SysLib.bytes2int( superblock, 0 );
int inodeBlocks = SysLib.bytes2int( superblock, 4 );
int freeList = SysLib.bytes2int( superblock, 8 );
if ( totalBlocks != 1000 ) {
SysLib.cout( "totalBlocks = " + totalBlocks + " (wrong)\n" );
return false;
}
if ( inodeBlocks != files && inodeBlocks != files / 16 ) {
SysLib.cout( "inodeBlocks = " + inodeBlocks + " (wrong)\n" );
return false;
}
if ( freeList != 1 + files / 16 && freeList != 1 + files / 16 + 1 ) {
SysLib.cout( "freeList = " + freeList + " (wrong)\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test2( ) {
//.............................................."
SysLib.cout( "2: fd = open( \"css430\", \"w+\" )...." );
fd = SysLib.open( "css430", "w+" );
if ( fd != 3 ) {
SysLib.cout( "fd = " + fd + " (wrong)\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test3( ) {
//.............................................."
SysLib.cout( "3: size = write( fd, buf[16] )...." );
for ( byte i = 0; i < 16; i++ )
buf16[i] = i;
size = SysLib.write( fd, buf16 );
if ( size != 16 ) {
SysLib.cout( "size = " + size + " (wrong)\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test4( ) {
//.............................................."
SysLib.cout( "4: close( fd )...................." );
SysLib.close( fd );
size = SysLib.write( fd, buf16 );
if ( size > 0 ) {
SysLib.cout( "writable even after closing the file\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test5( ) {
//.............................................."
SysLib.cout( "5: reopen and read from \"css430\".." );
fd = SysLib.open( "css430", "r" );
byte[] tmpBuf = new byte[16];
size = SysLib.read( fd, tmpBuf );
if ( size != 16 ) {
SysLib.cout( "size = " + size + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 0; i < 16; i++ )
if ( tmpBuf[i] != buf16[i] ) {
SysLib.cout( "buf[" + i + "] = " + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test6( ) {
//.............................................."
SysLib.cout( "6: append buf[32] to \"css430\"....." );
for ( byte i = 0; i < 32; i++ )
buf32[i] = ( byte )( i + ( byte )16 );
fd = SysLib.open( "css430", "a" );
SysLib.write( fd, buf32 );
SysLib.close( fd );
fd = SysLib.open( "css430", "r" );
byte[] tmpBuf = new byte[48];
size = SysLib.read( fd, tmpBuf );
if ( size != 48 ) {
SysLib.cout( "size = " + size + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 0; i < 16; i++ )
if ( tmpBuf[i] != buf16[i] ) {
SysLib.cout( "buf[" + i + "] = " + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 16; i < 48; i++ )
if ( tmpBuf[i] != buf32[i - 16] ) {
SysLib.cout( "buf[" + i + "] = " + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test7( ) {
//.............................................."
SysLib.cout( "7: seek and read from \"css430\"...." );
fd = SysLib.open( "css430", "r" );
int position = SysLib.seek( fd, 10, 0 );
if ( position != 10 ) {
SysLib.cout( "seek(fd,10,0)=" + position + " (wrong)\n" );
SysLib.close( fd );
return false;
}
byte[] tmpBuf = new byte[2];
size = SysLib.read( fd, tmpBuf );
if ( tmpBuf[0] != ( byte )10 ) {
SysLib.cout( "seek(fd,10,0) contents " + tmpBuf[0] + "(wrong\n" );
SysLib.close( fd );
return false;
}
position = SysLib.seek( fd, 10, 0 );
position = SysLib.seek( fd, 10, 1 );
if ( position != 20 ) {
SysLib.cout( "seek(fd,10,1)=" + position + " (wrong)\n" );
SysLib.close( fd );
return false;
}
size = SysLib.read( fd, tmpBuf );
if ( tmpBuf[0] != ( byte )20 ) {
SysLib.cout( "seek(fd,10,1) contents " + tmpBuf[0] + "(wrong)\n" );
SysLib.close( fd );
return false;
}
position = SysLib.seek( fd, -2, 2 );
if ( position != 46 ) {
SysLib.cout( "seek(fd,-2,2)=" + position + "(wrong)\n" );
SysLib.close( fd );
return false;
}
size = SysLib.read( fd, tmpBuf );
if ( tmpBuf[0] != ( byte )46 ) {
SysLib.cout( "seek(fd,-2,2) contents " + tmpBuf[0] + "(wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test8( ) {
//.............................................."
SysLib.cout( "8: open \"css430\" with w+.........." );
for ( short i = 0; i < 24; i++ )
buf24[i] = ( byte )( 24 - i );
fd = SysLib.open( "css430", "w+" );
SysLib.seek( fd, 24, 0 );
SysLib.write( fd, buf24 );
SysLib.seek( fd, 0, 0 );
byte[] tmpBuf = new byte[48];
SysLib.read( fd, tmpBuf );
for ( byte i = 0; i < 16; i++ )
if ( tmpBuf[i] != buf16[i] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( byte i = 16; i < 24; i++ )
if ( tmpBuf[i] != buf32[i-16] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( byte i = 24; i < 48; i++ )
if ( tmpBuf[i] != buf24[i-24] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
//final byte[] buf8192 = new byte[8192];
final byte[] buf6656 = new byte[6656];
private boolean test9( ) {
//.............................................."
SysLib.cout( "9: fd = open( \"bothell\", \"w\" )...." );
fd = SysLib.open( "bothell", "w+" );
if ( fd != 3 ) {
SysLib.cout( "fd = " + fd + " (wrong)\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test10( ) {
//.............................................."
SysLib.cout( "10: size = write( fd, buf[6656] )." );
for ( int i = 0; i < 6656; i++ )
buf6656[i] = ( byte )( i % 256 );
size = SysLib.write( fd, buf6656 );
if ( size != 6656 ) {
SysLib.cout( "size = " + size + " (wrong)\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test11( ) {
//.............................................."
SysLib.cout( "11: close( fd )...................." );
SysLib.close( fd );
size = SysLib.write( fd, buf16 );
if ( size > 0 ) {
SysLib.cout( "writable even after closing the file\n" );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test12( ) {
//.............................................."
SysLib.cout( "12: reopen and read from \"bothell\"" );
fd = SysLib.open( "bothell", "r" );
byte[] tmpBuf = new byte[6656];
size = SysLib.read( fd, tmpBuf );
if ( size != 6656 ) {
SysLib.cout( "size = " + size + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 0; i < 6656; i++ )
if ( tmpBuf[i] != buf6656[i] ) {
SysLib.cout( "buf[" + i + "] = " + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test13( ) {
//.............................................."
SysLib.cout( "13: append buf[32] to \"bothell\"..." );
fd = SysLib.open( "bothell", "a" );
SysLib.write( fd, buf32 );
SysLib.close( fd );
fd = SysLib.open( "bothell", "r" );
byte[] tmpBuf = new byte[6688];
size = SysLib.read( fd, tmpBuf );
if ( size != 6688 ) {
SysLib.cout( "size = " + size + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 0; i < 6656; i++ ) {
if ( tmpBuf[i] != buf6656[i] ) {
SysLib.cout( "buf[" + i + "] = " + tmpBuf[i] + " buf6656 = " +
buf6656[i] + "\n" );
SysLib.close( fd );
return false;
}
}
for ( int i = 6656; i < 6688; i++ )
if ( tmpBuf[i] != buf32[i - 6656] ) {
SysLib.cout( "buf[" + i + "] = " + tmpBuf[i] + " buf32 = " +
buf32[i - 6656] + "\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test14( ) {
//.............................................."
SysLib.cout( "14: seek and read from \"bothell\"..." );
fd = SysLib.open( "bothell", "r" );
int position = SysLib.seek( fd, 512 * 11, 0 );
if ( position != 512 * 11 ) {
SysLib.cout( "seek(fd,512 * 11,0)=" + position + " (wrong)\n" );
SysLib.close( fd );
return false;
}
byte[] tmpBuf = new byte[2];
size = SysLib.read( fd, tmpBuf );
if ( tmpBuf[0] != ( byte )(512 * 11) ) {
SysLib.cout( "seek(fd,512*11,0) contents "+tmpBuf[0] + "(wrong)\n" );
SysLib.close( fd );
return false;
}
position = SysLib.seek( fd, 512 * 11, 0 );
position = SysLib.seek( fd, 512, 1 );
if ( position != 512 * 12 ) {
SysLib.cout( "seek(fd,512,1)=" + position + " (wrong)\n" );
SysLib.close( fd );
return false;
}
size = SysLib.read( fd, tmpBuf );
if ( tmpBuf[0] != ( byte )(512 * 12) ) {
SysLib.cout( "seek(fd,512,1) contents " + tmpBuf[0] + "(wrong)\n" );
SysLib.close( fd );
return false;
}
position = SysLib.seek( fd, -2, 2 );
if ( position != 512 * 13 + 32 - 2 ) {
SysLib.cout( "seek(fd,-2,2)=" + position + "(wrong)\n" );
SysLib.close( fd );
return false;
}
size = SysLib.read( fd, tmpBuf );
if ( tmpBuf[0] != ( byte )46 ) {
SysLib.cout( "seek(fd,-2,2) contents " + tmpBuf[0] + "(wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test15( ) {
//.............................................."
SysLib.cout( "15: open \"bothell\" with w+........." );
for ( short i = 0; i < 24; i++ )
buf24[i] = ( byte )( 24 - i );
fd = SysLib.open( "bothell", "w+" );
SysLib.seek( fd, 512 * 12 - 3, 0 );
SysLib.write( fd, buf24 );
SysLib.seek( fd, 0, 0 );
byte[] tmpBuf = new byte[6688];
SysLib.read( fd, tmpBuf );
for ( int i = 0; i < 512 * 12 - 3; i++ )
if ( tmpBuf[i] != buf6656[i] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 512 * 12 - 3; i < 512 * 12 - 3 + 24; i++ )
if ( tmpBuf[i] != buf24[i - ( 512 * 12 - 3)] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 512 * 12 -3 + 24; i < 6656; i++ )
if ( tmpBuf[i] != buf6656[i] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
for ( int i = 6656; i < 6688; i++ )
if ( tmpBuf[i] != buf32[i - 6656] ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " (wrong)\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test16( ) {
//.............................................."
SysLib.cout( "16: delete(\"css430\").............." );
fd = SysLib.open( "css430", "r" );
if ( fd == -1 ) {
SysLib.cout( "fd = " + fd + " (wrong)\n" );
return false;
}
SysLib.close( fd );
SysLib.delete( "css430" );
fd = SysLib.open( "css430", "r" );
if ( fd != -1 ) {
SysLib.cout( "fd = " + fd + " (wrong, css430 still exists!)\n" );
SysLib.close( fd );
return false;
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test17( ) {
//.............................................."
SysLib.cout( "17: create uwb0-29 of 512*13......" );
int fdes[] = new int[29];
for ( int i = 0; i < 29; i++ ) {
Integer suffix = new Integer( i );
String file = "uwb" + suffix.toString( );
fdes[i] = SysLib.open( file, "w+" );
if ( fdes[i] == -1 ) {
SysLib.cout( "file: " + file + " open failed\n" );
return false;
}
}
for ( int i = 0; i < 29; i++ ) {
if ( SysLib.write( fdes[i], buf6656 ) != 6656 ) {
SysLib.cout( "fd[" + fdes[i] + "] failed in writing\n" );
return false;
}
}
for ( int i = 0; i < 29; i++ ) {
if ( SysLib.close( fdes[i] ) == -1 ) {
SysLib.cout( "fd[" + fdes[i] + "] failed in closing\n" );
return false;
}
}
SysLib.cout( "successfully completed\n" );
return true;
}
private boolean test18( ) {
//.............................................."
SysLib.cout( "18: uwb0 read b/w Test5 & Test6...\n" );
fd = SysLib.open( "uwb0", "r" );
String[] cmd = new String[2];
cmd[0] = "Test6";
cmd[1] = String.format( "%d", fd );
SysLib.exec( cmd );
SysLib.join( );
SysLib.close( fd );
SysLib.cout( "Test6.java terminated\n" );
fd = SysLib.open( "uwb1", "r" );
byte[] tmpBuf = new byte[512];
SysLib.read( fd, tmpBuf );
for ( int i = 0; i < 512; i++ )
if ( tmpBuf[i] != (byte)100 ) {
SysLib.cout( "tmpBuf[" + i + "]=" + tmpBuf[i] + " should be 100"
+ "\n" );
SysLib.close( fd );
return false;
}
SysLib.close( fd );
return true;
}
}