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

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

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

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

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

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

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

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

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


    return 0;
}

