示例#1
0
static void test_delete()
{
    int i;
    int64_t start_time;
    int64_t end_time;
    void *value;

    start_time = get_current_time_ms();
    for (i=0; i<COUNT; i++) {
        assert(skiplist_delete(&sl, numbers + i) == 0);
    }
    assert(instance_count == 0);

    end_time = get_current_time_ms();
    printf("delete time used: %"PRId64" ms\n", end_time - start_time);

    start_time = get_current_time_ms();
    for (i=0; i<COUNT; i++) {
        value = skiplist_find(&sl, numbers + i);
        assert(value == NULL);
    }
    end_time = get_current_time_ms();
    printf("find after delete time used: %"PRId64" ms\n", end_time - start_time);

    i = 0;
    skiplist_iterator(&sl, &iterator);
    while ((value=skiplist_next(&iterator)) != NULL) {
        i++;
    }
    assert(i==0);
}
示例#2
0
int main(int argc, char *argv[]) {
  srand(clock());
  SkipList_t *ptrSl = skiplist_alloc(10,1);
  int64_t i;
  for (i = 1; i <= 10; i++) {
    skiplist_insert(ptrSl, i, (void*)(i*i));
    SlKey_t slKey;
    int64_t cValue;
    if (skiplist_index(ptrSl, (i+1)/2-1, &slKey, (void**)(&cValue))) {
      assert(slKey == (i+1)/2);
    } else {
      assert(0);
    }
  }
  for (i = 0; i < 10; i++) {
    SlKey_t slKey;
    int64_t cValue;
    if (skiplist_index(ptrSl, i, &slKey, (void**)(&cValue))) {
      assert(slKey == i+1);
    } else {
      assert(0);
    }
  }
  int32_t ixRm = 5;
  assert(skiplist_delete(ptrSl, ixRm));
  for (i = 1; i <= 10; i++) {
    int64_t cValue = (int64_t)skiplist_find(ptrSl, i);
    if (i != ixRm) {
      assert(cValue == i*i);
    } else {
      assert(cValue == 0);
    }
  }
  for (i = 1; i <= 10; i+=2) {
    int32_t fSuccess = skiplist_delete(ptrSl,i);
    if (i != ixRm) {
      assert(fSuccess);
    } else {
      assert(!fSuccess);
    }
  }
  skiplist_free(ptrSl);
  return 1;
}
示例#3
0
static int test_insert()
{
    int i;
    int result;
    int64_t start_time;
    int64_t end_time;
    void *value;

    instance_count = 0;
    start_time = get_current_time_ms();
    for (i=0; i<COUNT; i++) {
        if ((result=skiplist_insert(&sl, numbers + i)) != 0) {
            return result;
        }
        instance_count++;
    }
    assert(instance_count == COUNT);

    end_time = get_current_time_ms();
    printf("insert time used: %"PRId64" ms\n", end_time - start_time);

    start_time = get_current_time_ms();
    for (i=0; i<COUNT; i++) {
        value = skiplist_find(&sl, numbers + i);
        assert(value != NULL && *((int *)value) == numbers[i]);
    }
    end_time = get_current_time_ms();
    printf("find time used: %"PRId64" ms\n", end_time - start_time);

    start_time = get_current_time_ms();
    i = 0;
    skiplist_iterator(&sl, &iterator);
    while ((value=skiplist_next(&iterator)) != NULL) {
        i++;
        assert(i == *((int *)value));
    }
    assert(i==COUNT);

    end_time = get_current_time_ms();
    printf("iterator time used: %"PRId64" ms\n", end_time - start_time);
    return 0;
}
示例#4
0
static int test_stable_sort()
{
#define RECORDS 32
    int i;
    int result;
    int index1;
    int index2;
    int delete_count;
    int total_delete_count;
    Skiplist sl;
    SkiplistIterator iterator;
    Record records[RECORDS];
    Record *record;
    Record target;
    void *value;

    instance_count = 0;
    result = skiplist_init_ex(&sl, 12, compare_record,
            free_test_func, 128, skiplist_type);
    if (result != 0) {
        return result;
    }

    for (i=0; i<RECORDS; i++) {
        records[i].line = i + 1;
        records[i].key = i + 1;
    }

    for (i=0; i<RECORDS/4; i++) {
        index1 = (RECORDS - 1) * (int64_t)rand() / (int64_t)RAND_MAX;
        index2 = RECORDS - 1 - index1;
        if (index1 != index2) {
            records[index1].key = records[index2].key;
        }
    }

    for (i=0; i<RECORDS; i++) {
        if ((result=skiplist_insert(&sl, records + i)) != 0) {
            return result;
        }
        instance_count++;
    }
    assert(instance_count == RECORDS);

    for (i=0; i<RECORDS; i++) {
        value = skiplist_find(&sl, records + i);
        assert(value != NULL && ((Record *)value)->key == records[i].key);
    }

    i = 0;
    skiplist_iterator(&sl, &iterator);
    while ((value=skiplist_next(&iterator)) != NULL) {
        i++;
        record = (Record *)value;
        printf("%d => #%d\n", record->key, record->line);
    }
    assert(i==RECORDS);

    target.key = 10;
    target.line = 0;
    if (skiplist_find_all(&sl, &target, &iterator) == 0) {
        printf("found key: %d\n", target.key);
    }
    i = 0;
    while ((value=skiplist_next(&iterator)) != NULL) {
        i++;
        record = (Record *)value;
        printf("%d => #%d\n", record->key, record->line);
    }
    printf("found record count: %d\n", i);

    total_delete_count = 0;
    for (i=0; i<RECORDS; i++) {
        if ((result=skiplist_delete_all(&sl, records + i,
                        &delete_count)) == 0)
        {
            total_delete_count += delete_count;
        }
        assert((result == 0 && delete_count > 0) ||
                (result != 0 && delete_count == 0));
    }
    assert(total_delete_count == RECORDS);
    assert(instance_count == 0);

    i = 0;
    skiplist_iterator(&sl, &iterator);
    while ((value=skiplist_next(&iterator)) != NULL) {
        i++;
    }
    assert(i == 0);

    skiplist_destroy(&sl);
    assert(instance_count == 0);

    return 0;
}