/* Checks for a scenario where only FIRST FIT fails to allocate memory  
   and other policies work well.
*/

#include <stdio.h>
#include <unistd.h>
#include "mem.h"

int main()
{
    int err, i, j;
    void *p[8];
    int sizes[8] = { 88, 488, 288, 488, 388, 1216, 88, 956};
    void **ptrs[3];

    err = Mem_Init(4096, P_FIRSTFIT);
    if(err == -1)
        return -1;

    for(i=0; i<8; i++) {
        p[i] = Mem_Alloc(sizes[i]);
        if(p[i] == NULL)
             return -1;
    }
//    Mem_Dump();
    
    ptrs[0] = &p[2];
    ptrs[1] = &p[4];
    ptrs[2] = &p[6];

    for(i=0; i<3; i++) {
       err = Mem_Free(*ptrs[i]);
       if(err == -1)
             return -1;
    }
//    Mem_Dump();

    p[0]= Mem_Alloc(60);
    if(p[0] == NULL)
         return -1;

    p[2]= Mem_Alloc(320);
    if(p[2] == NULL)
         return -1;

    //This should not work for FIRST FIT and should work for others   
    p[4]= Mem_Alloc(260);
    if(p[4] != NULL)
         return -1;

    return 0;
}

