#include "best_fit_allocator.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  /* small pool for clarity */
  best_fit_allocator_t *pool = bf_allocator_create(1024);
  if (!pool) {
    fprintf(stderr, "Failed to create allocator\n");
    return 1;
  }

  /* Step 1: allocate three blocks */
  void *b1 = bf_allocator_alloc(pool, 100);
  void *b2 = bf_allocator_alloc(pool, 200);
  void *b3 = bf_allocator_alloc(pool, 300);
  printf("After initial allocations (100,200,300):\n");
  bf_allocator_print(pool);

  /* Step 2: free the first two */
  bf_allocator_free(pool, b1);
  bf_allocator_free(pool, b2);
  printf("\nAfter freeing 100- and 200-byte blocks:\n");
  bf_allocator_print(pool);

  /* Step 3: allocate 150 bytes—should go into the 200-byte hole */
  void *b4 = bf_allocator_alloc(pool, 150);
  printf("\nRequesting 150 bytes: (best-fit should pick the 200-byte block)\n");
  bf_allocator_print(pool);

  /* Verify that b4 lies inside the old b2 region */
  size_t diff = (uintptr_t)b4 - (uintptr_t)b2;
  printf("\nb2 was at %p, b4 is at %p, diff = %zu bytes\n", b2, b4, diff);

  bf_allocator_destroy(pool);
}
