test_lsquic_hash.c revision fb3e20e0
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdint.h>
450aadb33SDmitri Tikhonov#include <stdio.h>
550aadb33SDmitri Tikhonov#include <stdlib.h>
650aadb33SDmitri Tikhonov#include <string.h>
750aadb33SDmitri Tikhonov#include <sys/queue.h>
8461e84d8SAmol Deshpande#ifndef WIN32
950aadb33SDmitri Tikhonov#include <unistd.h>
10461e84d8SAmol Deshpande#endif
1150aadb33SDmitri Tikhonov
1250aadb33SDmitri Tikhonov#include "lsquic_hash.h"
1350aadb33SDmitri Tikhonov
1450aadb33SDmitri Tikhonov
1550aadb33SDmitri Tikhonovstruct widget
1650aadb33SDmitri Tikhonov{
1750aadb33SDmitri Tikhonov    unsigned long   key;
185392f7a3SLiteSpeed Tech    struct lsquic_hash_elem hash_el;
1950aadb33SDmitri Tikhonov    char            data[30];
2050aadb33SDmitri Tikhonov};
2150aadb33SDmitri Tikhonov
2250aadb33SDmitri Tikhonov
2350aadb33SDmitri Tikhonovint
2450aadb33SDmitri Tikhonovmain (int argc, char **argv)
2550aadb33SDmitri Tikhonov{
2650aadb33SDmitri Tikhonov    struct lsquic_hash *hash;
2750aadb33SDmitri Tikhonov    struct lsquic_hash_elem *el;
2850aadb33SDmitri Tikhonov    unsigned n, nelems;
2950aadb33SDmitri Tikhonov    struct widget *widgets, *widget;
3050aadb33SDmitri Tikhonov
3150aadb33SDmitri Tikhonov    hash = lsquic_hash_create();
3250aadb33SDmitri Tikhonov
3350aadb33SDmitri Tikhonov    if (argc > 1)
3450aadb33SDmitri Tikhonov        nelems = atoi(argv[1]);
3550aadb33SDmitri Tikhonov    else
3650aadb33SDmitri Tikhonov        nelems = 1000000;
3750aadb33SDmitri Tikhonov
38fb3e20e0SDmitri Tikhonov    widgets = calloc(nelems, sizeof(widgets[0]));
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonov    for (n = 0; n < nelems; ++n)
4150aadb33SDmitri Tikhonov    {
4250aadb33SDmitri Tikhonov        widget = &widgets[n];
4350aadb33SDmitri Tikhonov        widget->key = n;    /* This will be used for verification later the test */
4450aadb33SDmitri Tikhonov        sprintf(widget->data, "%lu", widget->key);
4550aadb33SDmitri Tikhonov        el = lsquic_hash_find(hash, &widget->key, sizeof(widget->key));
4650aadb33SDmitri Tikhonov        assert(!el);
475392f7a3SLiteSpeed Tech        el = lsquic_hash_insert(hash, &widget->key, sizeof(widget->key), widget, &widget->hash_el);
4850aadb33SDmitri Tikhonov        assert(el);
4950aadb33SDmitri Tikhonov    }
5050aadb33SDmitri Tikhonov
5150aadb33SDmitri Tikhonov    assert(nelems == lsquic_hash_count(hash));
5250aadb33SDmitri Tikhonov
5350aadb33SDmitri Tikhonov    for (n = 0, el = lsquic_hash_first(hash); el; ++n, el = lsquic_hash_next(hash))
5450aadb33SDmitri Tikhonov    {
5550aadb33SDmitri Tikhonov        widget = lsquic_hashelem_getdata(el);
5650aadb33SDmitri Tikhonov        assert(widget >= widgets);
5750aadb33SDmitri Tikhonov        assert(widget < widgets + nelems);
5850aadb33SDmitri Tikhonov    }
5950aadb33SDmitri Tikhonov    assert(n == nelems);
6050aadb33SDmitri Tikhonov
6150aadb33SDmitri Tikhonov    for (n = 0; n < nelems; ++n)
6250aadb33SDmitri Tikhonov    {
6350aadb33SDmitri Tikhonov        unsigned long key = n;
6450aadb33SDmitri Tikhonov        el = lsquic_hash_find(hash, &key, sizeof(key));
6550aadb33SDmitri Tikhonov        assert(el);
6650aadb33SDmitri Tikhonov        widget = lsquic_hashelem_getdata(el);
6750aadb33SDmitri Tikhonov        assert(widget->key == key);
6850aadb33SDmitri Tikhonov        lsquic_hash_erase(hash, el);
6950aadb33SDmitri Tikhonov        el = lsquic_hash_find(hash, &key, sizeof(key));
7050aadb33SDmitri Tikhonov        assert(!el);
7150aadb33SDmitri Tikhonov    }
7250aadb33SDmitri Tikhonov
7350aadb33SDmitri Tikhonov    assert(0 == lsquic_hash_count(hash));
7450aadb33SDmitri Tikhonov
7550aadb33SDmitri Tikhonov    lsquic_hash_destroy(hash);
7650aadb33SDmitri Tikhonov    free(widgets);
7750aadb33SDmitri Tikhonov
7850aadb33SDmitri Tikhonov    exit(0);
7950aadb33SDmitri Tikhonov}
80