int main()
{
    int sizeOfRegion = 4096;

    int val = psumeminit(0, sizeOfRegion);
    printf("psumeminit called, val: %d\n", val);

    printf("sizeof node_t %d\n", sizeof(node_t));
    printf("sizeof header_t %d\n", sizeof(header_t));

    void *ptr1, *ptr2, *ptr3;

    printf("before first malloc\n");
    psumemdump();

    // alloc close to meminit limit
    ptr1 = psumalloc(sizeOfRegion-sizeof(header_t));
    assert(ptr1 != NULL);

    printf("after malloc\n");
    psumemdump();

    psufree(ptr1);

    printf("after free\n");
    psumemdump();

    //int res = psufree(ptr1);
    //assert(res == -1);

    return 0;
}
int main(){
    FILE* f = fopen ("test_output6.txt", "w");
    int size;
    int sizeOfRegion = 1 << 20;// 1MB
    int a = psumeminit(1, sizeOfRegion);
    if (a == -1){
        fprintf(f, "Initialization failed!\n");
    }
    
    void* pointer_array[16][NUM];
    for (int i = 0; i < 16; i++){
        for (int j = 0; j < NUM; j++){
            pointer_array[i][j] = NULL;
        }
    }

    for (int i = 0; i < 16; i++){
        for (int j = 0; j < NUM; ++j)
        {
            size = rand()%248 + 8;
            test* a = (test*)psumalloc(size);
            write_test(a);
            read_test(a, f);
            if (a == NULL){
                fprintf(f, "No. %d: No extra space for allocation in memory!\n", j);
            }
            else{
                pointer_array[i][j] = a;
                fprintf(f, "NO.%d chunk has been allocated, the size is %d bytes\n", j, size);
            }
        }
    }
    
    int half = 16;
    while (half != 0){
        half /= 2;
        int bound;
        if (half){
            bound = half * 2;
        }
        else{
            bound = 1;
        }
        for (int i = half; i < bound ; i++){
            for (int j = 0; j < NUM; j++)
            {
                int a = psufree(pointer_array[i][j]);
                if (a == 0){
                    fprintf(f, "No.%d chunk has been freed. \n", j);
                }
                else{
                    fprintf(f, "Can not free No.%d chunk. \n", j);
                }
            }
        }
    }
    fclose(f);
    return 0;
}
int main(){
	FILE* f = fopen ("output_files/large_symmetric_worst_output.txt", "w");
	int size;
	int sizeOfRegion = 1 << 20;// 1MB
	int malloccounter = 0;
	int freecounter = 0;
	int a = psumeminit(1, sizeOfRegion);
	if (a == -1){
		fprintf(f, "Initialization failed!\n");
	}
	for (int i = 0; i < 10; i++){
		void* pointer_array[NUM];
		for (int i = 0; i < NUM; ++i){
			pointer_array[i] = NULL;
		}

		for (int i = 0; i < NUM; ++i)
		{
			if (i % 2 == 0){
				size = 64;
			}
			else{
				size = 64 * 1024;
			}
            void* a = psumalloc(size);
			if (a == NULL){
				malloccounter++;
				fprintf(f, "No extra space for allocation in memory!\n");
			}
			else{
				pointer_array[i] = a;
				fprintf(f, "NO.%d chunk has been allocated. the size is %d bytes\n", i, size);
			}
		}

		for (int i = 0; i < NUM; ++i)
		{
			int a = psufree(pointer_array[i]);
			if (a == 0){
				fprintf(f, "No.%d chunk has been freed. \n", i);
			}
			else{
				freecounter++;
				fprintf(f, "Can not free No.%d chunk. \n", i);
			}
		}
	}
	fclose(f);
	return 0;
}
int main(){
    FILE* f = fopen ("test_output8.txt", "w");
    int size;
    int sizeOfRegion = 1 << 20;// 1MB
    int a = psumeminit(1, sizeOfRegion);
    if (a == -1){
        fprintf(f, "Initialization failed!\n");
    }
    for (int i = 0; i < 10; i++){
        void* pointer_array[NUM];
        for (int i = 0; i < NUM; ++i){
            pointer_array[i] = NULL;
        }
        for (int i = 0; i < NUM; ++i)
        {
            size = rand()%248 + 8;
            test* a = (test*)psumalloc(size);
            write_test(a);
            read_test(a, f);
            if (a == NULL){
                fprintf(f, "No.%d : No extra space for allocation in memory!\n", i);
            }
            else{
                pointer_array[i] = a;
                fprintf(f, "NO.%d chunk has been allocated. the size is %d bytes \n", i, size);
            }
        }
        
        for (int i = 0; i < NUM; ++i)
        {
            int a = psufree(pointer_array[i]);
            if (a == 0){
                fprintf(f, "No.%d chunk has been freed. \n", i);
            }
            else{
                fprintf(f, "Can not free No.%d chunk. \n", i);
            }
        }
    }
    fclose(f);
    return 0;
}
int main(){
    struct timeval start;
    struct timeval finish;
	FILE* f = fopen ("test_output3.txt", "w");
	int size;
	int sizeOfRegion = 1 << 20;// 1MB
	int a = psumeminit(0, sizeOfRegion);
	if (a == -1){
		fprintf(f, "Initialization failed!\n");
	}
    void* sample_malloc[20];
    void* sample_free[20];
    for (int i = 0; i < 20; i++){
		void* pointer_array[NUM];
        for (int i = 0; i < NUM; i++){
            pointer_array[i] = NULL;
        }
        int duration_malloc = 0;
		for (int i = 0; i < NUM; ++i)
		{
			if (i % 2 == 0){
				size = 64;
			}
			else{
				size = 64 * 1024;
			}
            gettimeofday(&start,NULL);
			test* a = (test*)psumalloc(size);
            gettimeofday(&finish,NULL);
            
             duration_malloc += (finish.tv_sec*1000000 + finish.tv_usec)- (start.tv_sec*1000000 + start.tv_usec);
            
            write_test(a);
            read_test(a, f);
			if (a == NULL){
				fprintf(f, "No extra space for allocation in memory!\n");
			}
			else{
				pointer_array[i] = a;
				fprintf(f, "NO.%d chunk has been allocated. the size is %d bytes\n", i, size);
			}
        }sample_malloc[i] = duration_malloc;

        int duration_free = 0;
		for (int i = 0; i < NUM; ++i)
		{
			gettimeofday(&start,NULL);
            int a = psufree(pointer_array[i]);
            gettimeofday(&finish,NULL);
			if (a == 0){
				fprintf(f, "No.%d chunk has been freed. \n", i);
			}
			else{
				fprintf(f, "Can not free No.%d chunk. \n", i);
			}
		}sample_free[i] = duration_free;
    }
    qsort((void *)sample_malloc, 20, sizeof(sample_malloc[0]), (int (*)(const void*, const void*))strcmp );
    qsort((void *)sample_free, 20, sizeof(sample_free[0]), (int (*)(const void*, const void*))strcmp );
    printf("\n25th percentile psumalloc=%d",sample_malloc[4]);
    printf("\n75th percentile psumalloc=%d",sample_malloc[14]);
    printf("\nAverage psumalloc=%d\n",sample_malloc[9]);
    printf("\n25th percentile psufree=%d",sample_free[4]);
    printf("\n75th percentile psufree=%d",sample_free[14]);
    printf("\nAverage psufree=%d\n",sample_free[9]);
	fclose(f);
	return 0;
}