Пример #1
0
void
test_create_lru_queue() {
  lru_queue_t *lru_queue = create_lru_queue();
  assert_is_not_null(lru_queue);
  assert_is_null(lru_queue->most_recent);
  assert_is_null(lru_queue->least_recent);
}
Пример #2
0
void
test_remove_entry() {
  lru_queue_t *lru_queue = create_lru_queue();

  cache_entry_t *c1 = malloc(calc_cache_entry_size(1, 1));
  assemble_cache_entry(c1, " ", 1, " ", 1);
  cache_entry_t *c2 = malloc(calc_cache_entry_size(1, 1));
  assemble_cache_entry(c2, " ", 1, " ", 1);
  cache_entry_t *c3 = malloc(calc_cache_entry_size(1, 1));
  assemble_cache_entry(c3, " ", 1, " ", 1);
  
  lru_add_new_entry(lru_queue, c1);
  lru_add_new_entry(lru_queue, c2);
  lru_add_new_entry(lru_queue, c3);
  assert_queue_equals(lru_queue, 3, c3, c2, c1);

  assert_true(lru_remove_entry(lru_queue, c2));
  assert_queue_equals(lru_queue, 2, c3, c1);

  assert_true(lru_remove_entry(lru_queue, c1));
  assert_queue_equals(lru_queue, 1, c3);

  assert_true(lru_remove_entry(lru_queue, c3));
  assert_is_null(lru_queue->most_recent);
  assert_is_null(lru_queue->least_recent);

  assert_false(lru_remove_entry(lru_queue, c3));
}
Пример #3
0
void
test_very_short_lru_queue_mark_as_used() {
  lru_queue_t *lru_queue = create_lru_queue();

  cache_entry_t *c1 = malloc(calc_cache_entry_size(1, 1));
  assemble_cache_entry(c1, " ", 1, " ", 1);
  cache_entry_t *c2 = malloc(calc_cache_entry_size(1, 1));
  assemble_cache_entry(c2, " ", 1, " ", 1);

  lru_add_new_entry(lru_queue, c1);
  assert_true(lru_bump_used_entry(lru_queue, c1));
  assert_equals(lru_queue->most_recent, c1);
  assert_equals(lru_queue->least_recent, c1);
  assert_is_null(c1->lru_more_recent);
  assert_is_null(c1->lru_less_recent);

  lru_add_new_entry(lru_queue, c2);
  assert_queue_equals(lru_queue, 2, c2, c1);

  assert_true(lru_bump_used_entry(lru_queue, c1));
  assert_queue_equals(lru_queue, 2, c1, c2);

  assert_true(lru_bump_used_entry(lru_queue, c1));
  assert_queue_equals(lru_queue, 2, c1, c2);
}
Пример #4
0
void testAlarmWithoutInsertingAnythingGetNextOfHead(void) {
    event_bin_t* head = alarm_queue->head;
    if(!assert_not_null(head)) return;

    event_bin_t* after_head = head->next;
    if(!assert_is_null(after_head)) return;
}
Пример #5
0
void replace_when_string_null(void)
{
    char *string = NULL;
    char *sub = "hello";
    char *new = "goodbye";

    char *result = str_replace(string, sub, new);

    assert_is_null(result);
}
Пример #6
0
/*
 * A utility function to make it easier to test the order of the lru queue.
 * It must take at least 2 parameters.
 */
void
assert_queue_equals(lru_queue_t *lru_queue, int num, ...) {
  cache_entry_t **expected = malloc(sizeof(cache_entry_t *) * num);
  va_list args;
  va_start(args, num);
  for (int i = 0; i < num; i++) {
    expected[i] = va_arg(args, cache_entry_t *);
  }
  va_end(args);

  if (num == 1) {
    assert_equals(expected[0], lru_queue->most_recent);
    assert_equals(expected[0], lru_queue->least_recent);
    assert_is_null(lru_queue->most_recent->lru_more_recent);
    assert_is_null(lru_queue->most_recent->lru_less_recent);
    return;
  }

  cache_entry_t *q_pos = lru_queue->most_recent;
  for (int i = 0; i < num; i++) {
    assert_equals(expected[i], q_pos);
    if (i == 0) {
      assert_is_null(q_pos->lru_more_recent);
      assert_equals(expected[i+1], q_pos->lru_less_recent);
    } else if (i == num - 1) {
      assert_is_null(q_pos->lru_less_recent);
      assert_equals(expected[i-1], q_pos->lru_more_recent);
    } else {
      assert_equals(expected[i-1], q_pos->lru_more_recent);
      assert_equals(expected[i+1], q_pos->lru_less_recent);
    }
    q_pos = q_pos->lru_less_recent;
  }
  assert_equals(expected[0], lru_queue->most_recent);
  assert_equals(expected[num-1], lru_queue->least_recent);

  free(expected);
}
Пример #7
0
void
test_assert_is_null() {
  assert_is_null(NULL);
}