-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
executable file
·680 lines (529 loc) · 16.1 KB
/
main.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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#include "mavalloc.h"
#include "tinytest.h"
#include <stdio.h>
#include <string.h>
/*
*
* TEST CASE 1: Test init and a single allocation
*
*/
int test_case_1()
{
mavalloc_init( 65535, BEST_FIT );
char * ptr = ( char * ) mavalloc_alloc ( 65535 );
int size = mavalloc_size();
// If you failed here your allocation on line 13 failed
TINYTEST_ASSERT( ptr );
// If you failed here your linked list did not have a single node
// check your mavalloc_alloc or mavalloc_size
TINYTEST_EQUAL( size, 1);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 2: Test Coalescing Free Blocks
*
* This test case tests combining free block when the
* second free block is freed second
*
*/
int test_case_2()
{
mavalloc_init( 128000, BEST_FIT );
char * ptr1 = (char*)mavalloc_alloc( 65535 );
char * ptr2 = (char*)mavalloc_alloc( 65 );
// If you failed here your allocation on line 37 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 38 failed
TINYTEST_ASSERT( ptr2 );
//P: THE FREES AREN'T WORKING
mavalloc_free( ptr1 );
mavalloc_free( ptr2 );
int size = mavalloc_size();
// If you failed here your linked list did not have a single node
// check your mavalloc_alloc or mavalloc_size and make sure you
// are combining adjacent free nodes and counting your link list
// correctly
TINYTEST_EQUAL( size, 1 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 3: Test Coalescing Free Blocks
*
* This test case tests combining free block when the
* second free block is freed first
*
*/
int test_case_3()
{
mavalloc_init( 65600, BEST_FIT );
char * ptr1 = (char*)mavalloc_alloc( 65536 );
char * ptr2 = (char*)mavalloc_alloc( 64 );
// If you failed here your allocation on line 76 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 77 failed
TINYTEST_ASSERT( ptr2 );
mavalloc_free( ptr2 );
mavalloc_free( ptr1 );
int size = mavalloc_size();
// If you failed here your linked list did not have a single node
// check your mavalloc_alloc or mavalloc_size and make sure you
// are combining adjacent free nodes and counting your link list
// correctly
TINYTEST_EQUAL( size, 1 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 4: Test First Fit and splitting free blocks
*
*/
int test_case_4()
{
mavalloc_init( 65535, FIRST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
int size = mavalloc_size();
// If you failed here your allocation on line 110 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 111 failed
TINYTEST_ASSERT( ptr2 );
// If you failed here your linked list did not have three nodes
// check your mavalloc_alloc or mavalloc_size and make sure you
// are allocating and splitting nodes and counting your link list
// correctly
TINYTEST_EQUAL( size, 3);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 5: Test Next
*
*/
int test_case_5()
{
mavalloc_init( 4144, NEXT_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 1024 );
char * buf1 = ( char * ) mavalloc_alloc ( 4 );
char * ptr6 = ( char * ) mavalloc_alloc ( 16 );
char * buf2 = ( char * ) mavalloc_alloc ( 4 );
char * ptr2 = ( char * ) mavalloc_alloc ( 2048 );
char * buf3 = ( char * ) mavalloc_alloc ( 4 );
char * ptr7 = ( char * ) mavalloc_alloc ( 16 );
char * buf4 = ( char * ) mavalloc_alloc ( 4 );
char * ptr3 = ( char * ) mavalloc_alloc ( 1024 );
// If you failed here your allocation on line 141 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 145 failed
TINYTEST_ASSERT( ptr2 );
// If you failed here your allocation on line 149 failed
TINYTEST_ASSERT( ptr3 );
// If you failed here your allocation on line 143 failed
TINYTEST_ASSERT( ptr6 );
// If you failed here your allocation on line 147 failed
TINYTEST_ASSERT( ptr7 );
mavalloc_free( ptr1 );
mavalloc_free( ptr2 );
mavalloc_free( ptr3 );
char * ptr5 = ( char * ) mavalloc_alloc ( 2000 );
// If you failed here your allocation on line 170 failed
TINYTEST_ASSERT( ptr5 );
ptr5 = ptr5;
ptr7 = ptr7;
ptr6 = ptr6;
buf1 = buf1;
buf2 = buf2;
buf3 = buf3;
buf4 = buf4;
char * ptr4 = ( char * ) mavalloc_alloc ( 1000 );
// If you failed here your allocation on line 183 failed
TINYTEST_ASSERT( ptr4 );
// If you failed here your allocation on line 149 failed
TINYTEST_ASSERT( ptr3 );
// If you failed here your Next Fit algorithm is not choosing
// the correct hole
TINYTEST_EQUAL( ptr3, ptr4 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 6: Test Worst Fit
*
*/
int test_case_6()
{
mavalloc_init( 71608, WORST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 65535 );
char * buffer1 = ( char * ) mavalloc_alloc( 4 );
char * ptr4 = ( char * ) mavalloc_alloc ( 64 );
char * buffer2 = ( char * ) mavalloc_alloc( 4 );
char * ptr2 = ( char * ) mavalloc_alloc ( 6000 );
// If you failed here your allocation on line 206 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 210 failed
TINYTEST_ASSERT( ptr2 );
// If you failed here your allocation on line 208 failed
TINYTEST_ASSERT( ptr4 );
mavalloc_free( ptr1 );
mavalloc_free( ptr2 );
buffer1 = buffer1;
buffer2 = buffer2;
ptr4 = ptr4;
char * ptr3 = ( char * ) mavalloc_alloc ( 1000 );
// If you failed here then your worst fit picked the wrong node on line 228
TINYTEST_EQUAL( ptr1, ptr3 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 7: Test Best Fit
*
*/
int test_case_7()
{
mavalloc_init( 75000, BEST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 65535 );
char * buffer1 = ( char * ) mavalloc_alloc( 1 );
char * ptr4 = ( char * ) mavalloc_alloc ( 65 );
char * buffer2 = ( char * ) mavalloc_alloc( 1 );
char * ptr2 = ( char * ) mavalloc_alloc ( 1500 );
// If you failed here your allocation on line 244 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 248 failed
TINYTEST_ASSERT( ptr2 );
// If you failed here your allocation on line 246 failed
TINYTEST_ASSERT( ptr4 );
mavalloc_free( ptr1 );
mavalloc_free( ptr2 );
buffer1 = buffer1;
buffer2 = buffer2;
ptr4 = ptr4;
char * ptr3 = ( char * ) mavalloc_alloc ( 1000 );
// If you failed here your allocation on line 266 failed
TINYTEST_ASSERT( ptr3 );
// If you failed here your allocation on 266 picked the wrong node for Best Fit
TINYTEST_EQUAL( ptr2, ptr3 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 8: Test allocating more than we should
*
*/
int test_case_8()
{
mavalloc_init( 255, BEST_FIT );
char * ptr = ( char * ) mavalloc_alloc ( 1000 );
// If you failed here your Best Fit allocation line 285 didn't return NULL like
// it should have
TINYTEST_EQUAL( ptr, NULL );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 9: Test allocating more than we should
*
*/
int test_case_9()
{
mavalloc_init( 255, FIRST_FIT );
char * ptr = ( char * ) mavalloc_alloc ( 1000 );
// If you failed here your First Fit allocation on line 302 didn't return NULL like
// it should have
TINYTEST_EQUAL( ptr, NULL );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 10: Test allocating more than we should
*
*/
int test_case_10()
{
mavalloc_init( 255, WORST_FIT );
char * ptr = ( char * ) mavalloc_alloc ( 1000 );
// If you failed here your Worst Fit allocation on line 320 didn't return NULL like
// it should have
TINYTEST_EQUAL( ptr, NULL );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 11: Test Next Fit and splitting free blocks
*
*/
int test_case_11()
{
mavalloc_init( 4144, NEXT_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 1024 );
char * buf1 = ( char * ) mavalloc_alloc ( 4 );
char * ptr6 = ( char * ) mavalloc_alloc ( 16 );
char * buf2 = ( char * ) mavalloc_alloc ( 4 );
char * ptr2 = ( char * ) mavalloc_alloc ( 2048 );
char * buf3 = ( char * ) mavalloc_alloc ( 4 );
char * ptr7 = ( char * ) mavalloc_alloc ( 16 );
char * buf4 = ( char * ) mavalloc_alloc ( 4 );
char * ptr3 = ( char * ) mavalloc_alloc ( 1024 );
// if you failed here your allocation on line 339 failed
TINYTEST_ASSERT( ptr1 );
// if you failed here your allocation on line 343 failed
TINYTEST_ASSERT( ptr2 );
// if you failed here your allocation on line 347 failed
TINYTEST_ASSERT( ptr3 );
// if you failed here your allocation on line 341 failed
TINYTEST_ASSERT( ptr6 );
// if you failed here your allocation on line 345 failed
TINYTEST_ASSERT( ptr7 );
mavalloc_free( ptr1 );
mavalloc_free( ptr2 );
mavalloc_free( ptr3 );
char * ptr5 = ( char * ) mavalloc_alloc ( 2000 );
// if you failed here your allocation on line 368 failed
TINYTEST_ASSERT( ptr5 );
ptr5 = ptr5;
ptr7 = ptr7;
ptr6 = ptr6;
buf1 = buf1;
buf2 = buf2;
buf3 = buf3;
buf4 = buf4;
char * ptr4 = ( char * ) mavalloc_alloc ( 1000 );
// if you failed here your allocation on line 383 failed
TINYTEST_ASSERT( ptr4 );
// if you failed here your allocation on line 347 failed
TINYTEST_ASSERT( ptr3 );
// If you fail here then your Next Fit algorithm picked the wrong node
TINYTEST_EQUAL( ptr3, ptr4 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 12: Test destroy
*
*/
int test_case_12()
{
mavalloc_init( 65535, BEST_FIT );
mavalloc_destroy( );
int size = mavalloc_size();
// If you failed here then your destroy did not free
// your linked list
TINYTEST_EQUAL( size, 0);
return 1;
}
/*
*
* TEST CASE 13: Test Next Fit and splitting free blocks
*
*/
int test_case_13()
{
mavalloc_init( 65535, NEXT_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
// If you failed here then your allocation on line 422 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here then your allocation on line 423 failed
TINYTEST_ASSERT( ptr2 );
int size = mavalloc_size();
// If you failed here then your next fit is not splitting free blocks
// correctly
TINYTEST_EQUAL( size, 3);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 14: Test Best Fit and splitting free blocks
*
*/
int test_case_14()
{
mavalloc_init( 65535, BEST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
int size = mavalloc_size();
// If you failed here then your allocation on line 448 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here then your allocation on line 449 failed
TINYTEST_ASSERT( ptr2 );
// If you failed here then your best fit is not splitting free blocks
// correctly
TINYTEST_EQUAL( size, 3);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 15: Test Worst Fit and splitting free blocks
*
*/
int test_case_15()
{
mavalloc_init( 65535, WORST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
// If you fail here then your allocation on line 475 failed
TINYTEST_ASSERT( ptr1 );
// If you fail here then your allocation on line 476 failed
TINYTEST_ASSERT( ptr2 );
int size = mavalloc_size();
// If you failed here then your worst fit is not splitting free blocks
// correctly
TINYTEST_EQUAL( size, 3);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 16: Test Worst Fit and using the memory we were given
*
*/
int test_case_16()
{
mavalloc_init( 65535, WORST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
// If you fail here then your allocation on line 475 failed
TINYTEST_ASSERT( ptr1 );
// If you fail here then your allocation on line 476 failed
TINYTEST_ASSERT( ptr2 );
memcpy( ptr2, "THIS IS THE TEST STRING", 23);
int compare = memcmp( ptr2, "THIS IS THE TEST STRING", 23 );
TINYTEST_EQUAL( compare, 0);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 17: Test Next Fit and using the memory we were given
*
*/
int test_case_17()
{
mavalloc_init( 65535, NEXT_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
// If you fail here then your allocation on line 475 failed
TINYTEST_ASSERT( ptr1 );
// If you fail here then your allocation on line 476 failed
TINYTEST_ASSERT( ptr2 );
memcpy( ptr2, "THIS IS THE TEST STRING", 23);
int compare = memcmp( ptr2, "THIS IS THE TEST STRING", 23 );
TINYTEST_EQUAL( compare, 0);
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 18: Free then allocate and make sure we re-use the same block
*
*/
int test_case_18()
{
mavalloc_init( 1536, FIRST_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 1024 );
char * ptr2 = ( char * ) mavalloc_alloc ( 256 );
int size = mavalloc_size();
// If you failed here your allocation on line 558 failed
TINYTEST_ASSERT( ptr1 );
// If you failed here your allocation on line 559 failed
TINYTEST_ASSERT( ptr2 );
mavalloc_free( ptr2 );
char * ptr3 = ( char * ) mavalloc_alloc ( 256 );
// If you failed here your allocation on line 571 failed
TINYTEST_ASSERT( ptr3 );
// If you fail here then your first fit did not reuse the correct node
TINYTEST_EQUAL( ptr2, ptr3 );
mavalloc_destroy( );
return 1;
}
/*
*
* TEST CASE 19: Test if your code safeguards against calling mavalloc_alloc
* after destroy
*
*/
int test_case_19()
{
mavalloc_init( 65535, BEST_FIT );
char * ptr = ( char * ) mavalloc_alloc ( 65535 );
int size = mavalloc_size();
// If you failed here your allocation on line 13 failed
TINYTEST_ASSERT( ptr );
// If you failed here your linked list did not have a single node
// check your mavalloc_alloc or mavalloc_size
TINYTEST_EQUAL( size, 1);
mavalloc_destroy( );
char * ptr2 = ( char * ) mavalloc_alloc ( 65535 );
// You must return NULL on the alloc on line 605 since it occurs
// after the destroy
TINYTEST_EQUAL( ptr2, NULL);
return 1;
}
/*
*
* TEST CASE 20: Test Next Fit and looping around the end of the linked list
*
*/
int test_case_20()
{
mavalloc_init( 12000, NEXT_FIT );
char * ptr1 = ( char * ) mavalloc_alloc ( 10000 );
char * ptr2 = ( char * ) mavalloc_alloc ( 65 );
// If you fail here then your allocation on line 623 failed
TINYTEST_ASSERT( ptr1 );
// If you fail here then your allocation on line 624 failed
TINYTEST_ASSERT( ptr2 );
mavalloc_free( ptr1 );
char * ptr3 = ( char * ) mavalloc_alloc ( 10000 );
// Your next fit picked the wrong node if this line fails
TINYTEST_EQUAL( ptr1, ptr3 );
mavalloc_destroy( );
return 1;
}
int tinytest_setup(const char *pName)
{
fprintf( stderr, "tinytest_setup(%s)\n", pName);
return 0;
}
int tinytest_teardown(const char *pName)
{
fprintf( stderr, "tinytest_teardown(%s)\n", pName);
return 0;
}
TINYTEST_START_SUITE(MavAllocTestSuite);
TINYTEST_ADD_TEST(test_case_1,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_2,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_3,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_4,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_5,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_6,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_7,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_8,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_9,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_10,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_11,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_12,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_13,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_14,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_15,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_16,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_17,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_18,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_19,tinytest_setup,tinytest_teardown);
TINYTEST_ADD_TEST(test_case_20,tinytest_setup,tinytest_teardown);
TINYTEST_END_SUITE();
TINYTEST_MAIN_SINGLE_SUITE(MavAllocTestSuite);