Пример #1
0
void test_min_heap (void) {
  BinomialHeap* heap;
  int* val;
  int i;

  heap = binomial_heap_new (BINOMIAL_HEAP_TYPE_MIN, int_compare);

  /* Push a load of values onto the heap */

  for (i = 0; i < NUM_TEST_VALUES; ++i) {
    test_array[i] = i;
    assert (binomial_heap_insert (heap, &test_array[i]) != 0);
    }

  /* Pop values off the heap and check they are in order */

  i = -1;

  while (binomial_heap_num_entries (heap) > 0) {
    val = (int*) binomial_heap_pop (heap);

    assert (*val == i + 1);
    i = *val;
    }

  /* Test pop on an empty heap */

  val = (int*) binomial_heap_pop (heap);
  assert (val == NULL);

  binomial_heap_free (heap);
  }
Пример #2
0
static void test_insert_out_of_memory (void) {
  BinomialHeap* heap;
  int i;

  /* There are various memory allocations performed during the insert;
   * probe at different limit levels to catch them all. */

  for (i = 0; i < 6; ++i) {
    heap = generate_heap();

    /* Insert should fail */

    alloc_test_set_limit (i);
    test_array[TEST_VALUE] = TEST_VALUE;
    assert (binomial_heap_insert (heap,
                                  &test_array[TEST_VALUE]) == 0);
    alloc_test_set_limit (-1);

    /* Check that the heap is unharmed */

    verify_heap (heap);

    binomial_heap_free (heap);
    }
  }
Пример #3
0
int main()
{
  int key;
  int i;
  HeapNode *Heap = NULL;
  HeapNode *tmp;
  HeapNode *tmpChild;

  srand(time(NULL));

  for (i = 0; i < 100; i++) {
    key = rand() % 10000 + 1;
    printf("%d\n", key);
    binomial_heap_insert(&Heap, key, NULL);
  }

  printf("%s: %d\n", "The minimum key is", binomial_heap_minimum(Heap)->key);

  while ( (tmp=binomial_heap_extract_min(&Heap)) != NULL ) {
    printf("%d ", tmp->key);
    free(tmp);
  }
  printf("\n");

  return 0;
}
Пример #4
0
void test_binomial_heap_insert (void) {
  BinomialHeap* heap;
  int i;

  heap = binomial_heap_new (BINOMIAL_HEAP_TYPE_MIN, int_compare);

  for (i = 0; i < NUM_TEST_VALUES; ++i) {
    test_array[i] = i;
    assert (binomial_heap_insert (heap, &test_array[i]) != 0);
    }

  assert (binomial_heap_num_entries (heap) == NUM_TEST_VALUES);

  /* Test for out of memory */

  alloc_test_set_limit (0);
  assert (binomial_heap_insert (heap, &i) == 0);

  binomial_heap_free (heap);
  }
Пример #5
0
static BinomialHeap* generate_heap (void) {
  BinomialHeap* heap;
  int i;

  heap = binomial_heap_new (BINOMIAL_HEAP_TYPE_MIN, int_compare);

  /* Push a load of values onto the heap */

  for (i = 0; i < NUM_TEST_VALUES; ++i) {
    test_array[i] = i;

    if (i != TEST_VALUE) {
      assert (binomial_heap_insert (heap, &test_array[i]) != 0);
      }
    }

  return heap;
  }