test_set.c revision a74702c6
1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdio.h>
450aadb33SDmitri Tikhonov#include <stdlib.h>
550aadb33SDmitri Tikhonov#include <string.h>
650aadb33SDmitri Tikhonov
750aadb33SDmitri Tikhonov#include "lsquic_set.h"
850aadb33SDmitri Tikhonov
950aadb33SDmitri Tikhonovstatic void
1050aadb33SDmitri Tikhonovtest_lsquic_set32 (void)
1150aadb33SDmitri Tikhonov{
1250aadb33SDmitri Tikhonov    lsquic_set32_t set;
1350aadb33SDmitri Tikhonov    int i, s;
1450aadb33SDmitri Tikhonov
1550aadb33SDmitri Tikhonov    lsquic_set32_init(&set);
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonov    for (i = 2; i < 100; ++i)
1850aadb33SDmitri Tikhonov    {
1950aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set32_has(&set, i)));
2050aadb33SDmitri Tikhonov        s = lsquic_set32_add(&set, i);
2150aadb33SDmitri Tikhonov        assert(0 == s);
2250aadb33SDmitri Tikhonov    }
2350aadb33SDmitri Tikhonov
2450aadb33SDmitri Tikhonov    assert(("Value is not yet in the set", !lsquic_set32_has(&set, 0)));
2550aadb33SDmitri Tikhonov    s = lsquic_set32_add(&set, 0);
2650aadb33SDmitri Tikhonov    assert(0 == s);
2750aadb33SDmitri Tikhonov    assert(("Value is not yet in the set", !lsquic_set32_has(&set, 1)));
2850aadb33SDmitri Tikhonov    s = lsquic_set32_add(&set, 1);
2950aadb33SDmitri Tikhonov    assert(0 == s);
3050aadb33SDmitri Tikhonov
3150aadb33SDmitri Tikhonov    for (i = 0; i < 100; ++i)
3250aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set32_has(&set, i)));
3350aadb33SDmitri Tikhonov
3450aadb33SDmitri Tikhonov    for (i = 300; i > 200; --i)
3550aadb33SDmitri Tikhonov    {
3650aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set32_has(&set, i)));
3750aadb33SDmitri Tikhonov        s = lsquic_set32_add(&set, i);
3850aadb33SDmitri Tikhonov        assert(0 == s);
3950aadb33SDmitri Tikhonov    }
4050aadb33SDmitri Tikhonov
4150aadb33SDmitri Tikhonov    for (i = 300; i > 200; --i)
4250aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set32_has(&set, i)));
4350aadb33SDmitri Tikhonov
4450aadb33SDmitri Tikhonov    for (i = 100; i <= 200; ++i)
4550aadb33SDmitri Tikhonov        assert(("Value is not in the set", !lsquic_set32_has(&set, i)));
4650aadb33SDmitri Tikhonov
4750aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; i += 4)
4850aadb33SDmitri Tikhonov    {
4950aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set32_has(&set, i)));
5050aadb33SDmitri Tikhonov        lsquic_set32_add(&set, i);
5150aadb33SDmitri Tikhonov    }
5250aadb33SDmitri Tikhonov
5350aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; i += 4)
5450aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set32_has(&set, i)));
5550aadb33SDmitri Tikhonov
5650aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; i += 4)
5750aadb33SDmitri Tikhonov    {
5850aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set32_has(&set, i + 1)));
5950aadb33SDmitri Tikhonov        lsquic_set32_add(&set, i + 1);
6050aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set32_has(&set, i + 3)));
6150aadb33SDmitri Tikhonov        lsquic_set32_add(&set, i + 3);
6250aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set32_has(&set, i + 2)));
6350aadb33SDmitri Tikhonov        lsquic_set32_add(&set, i + 2);
6450aadb33SDmitri Tikhonov    }
6550aadb33SDmitri Tikhonov
6650aadb33SDmitri Tikhonov    for (i = 0; i < 100; ++i)
6750aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set32_has(&set, i)));
6850aadb33SDmitri Tikhonov    for (i = 100; i <= 200; ++i)
6950aadb33SDmitri Tikhonov        assert(("Value is not in the set", !lsquic_set32_has(&set, i)));
7050aadb33SDmitri Tikhonov    for (i = 201; i <= 300; ++i)
7150aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set32_has(&set, i)));
7250aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; ++i)
7350aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set32_has(&set, i)));
7450aadb33SDmitri Tikhonov
7550aadb33SDmitri Tikhonov    lsquic_set32_cleanup(&set);
7650aadb33SDmitri Tikhonov}
7750aadb33SDmitri Tikhonov
7850aadb33SDmitri Tikhonov
7950aadb33SDmitri Tikhonovstatic void
8050aadb33SDmitri Tikhonovtest_lsquic_set64 (void)
8150aadb33SDmitri Tikhonov{
8250aadb33SDmitri Tikhonov    lsquic_set64_t set;
8350aadb33SDmitri Tikhonov    int i;
8450aadb33SDmitri Tikhonov
8550aadb33SDmitri Tikhonov    lsquic_set64_init(&set);
8650aadb33SDmitri Tikhonov
8750aadb33SDmitri Tikhonov    for (i = 2; i < 100; ++i)
8850aadb33SDmitri Tikhonov    {
8950aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set64_has(&set, i)));
9050aadb33SDmitri Tikhonov        lsquic_set64_add(&set, i);
9150aadb33SDmitri Tikhonov    }
9250aadb33SDmitri Tikhonov
9350aadb33SDmitri Tikhonov    assert(("Value is not yet in the set", !lsquic_set64_has(&set, 0)));
9450aadb33SDmitri Tikhonov    lsquic_set64_add(&set, 0);
9550aadb33SDmitri Tikhonov    assert(("Value is not yet in the set", !lsquic_set64_has(&set, 1)));
9650aadb33SDmitri Tikhonov    lsquic_set64_add(&set, 1);
9750aadb33SDmitri Tikhonov
9850aadb33SDmitri Tikhonov    for (i = 0; i < 100; ++i)
9950aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set64_has(&set, i)));
10050aadb33SDmitri Tikhonov
10150aadb33SDmitri Tikhonov    for (i = 300; i > 200; --i)
10250aadb33SDmitri Tikhonov    {
10350aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set64_has(&set, i)));
10450aadb33SDmitri Tikhonov        lsquic_set64_add(&set, i);
10550aadb33SDmitri Tikhonov    }
10650aadb33SDmitri Tikhonov
10750aadb33SDmitri Tikhonov    for (i = 300; i > 200; --i)
10850aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set64_has(&set, i)));
10950aadb33SDmitri Tikhonov
11050aadb33SDmitri Tikhonov    for (i = 100; i <= 200; ++i)
11150aadb33SDmitri Tikhonov        assert(("Value is not in the set", !lsquic_set64_has(&set, i)));
11250aadb33SDmitri Tikhonov
11350aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; i += 4)
11450aadb33SDmitri Tikhonov    {
11550aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set64_has(&set, i)));
11650aadb33SDmitri Tikhonov        lsquic_set64_add(&set, i);
11750aadb33SDmitri Tikhonov    }
11850aadb33SDmitri Tikhonov
11950aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; i += 4)
12050aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set64_has(&set, i)));
12150aadb33SDmitri Tikhonov
12250aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; i += 4)
12350aadb33SDmitri Tikhonov    {
12450aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set64_has(&set, i + 1)));
12550aadb33SDmitri Tikhonov        lsquic_set64_add(&set, i + 1);
12650aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set64_has(&set, i + 3)));
12750aadb33SDmitri Tikhonov        lsquic_set64_add(&set, i + 3);
12850aadb33SDmitri Tikhonov        assert(("Value is not yet in the set", !lsquic_set64_has(&set, i + 2)));
12950aadb33SDmitri Tikhonov        lsquic_set64_add(&set, i + 2);
13050aadb33SDmitri Tikhonov    }
13150aadb33SDmitri Tikhonov
13250aadb33SDmitri Tikhonov    for (i = 0; i < 100; ++i)
13350aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set64_has(&set, i)));
13450aadb33SDmitri Tikhonov    for (i = 100; i <= 200; ++i)
13550aadb33SDmitri Tikhonov        assert(("Value is not in the set", !lsquic_set64_has(&set, i)));
13650aadb33SDmitri Tikhonov    for (i = 201; i <= 300; ++i)
13750aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set64_has(&set, i)));
13850aadb33SDmitri Tikhonov    for (i = 1000; i < 2000; ++i)
13950aadb33SDmitri Tikhonov        assert(("Value is in the set", lsquic_set64_has(&set, i)));
14050aadb33SDmitri Tikhonov
14150aadb33SDmitri Tikhonov    lsquic_set64_cleanup(&set);
14250aadb33SDmitri Tikhonov}
14350aadb33SDmitri Tikhonov
14450aadb33SDmitri Tikhonov
14550aadb33SDmitri Tikhonovint
14650aadb33SDmitri Tikhonovmain (void)
14750aadb33SDmitri Tikhonov{
14850aadb33SDmitri Tikhonov    test_lsquic_set32();
14950aadb33SDmitri Tikhonov    test_lsquic_set64();
15050aadb33SDmitri Tikhonov    return 0;
15150aadb33SDmitri Tikhonov}
152