summaryrefslogtreecommitdiffhomepage
path: root/tests/test_new_queue.c
blob: a7fdb723a9984a94e9f4ffbc0b807cf2733718b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stc/crandom.h>
#include <stc/forward.h>
#include <stdio.h>

forward_cqueue(pnt, struct Point);

struct Point { int x, y; } typedef Point;
int point_compare(const Point* a, const Point* b) {
    int c = c_default_compare(&a->x, &b->x);
    return c ? c : c_default_compare(&a->y, &b->y);
}
#define F_tag pnt
#define i_val Point
#define i_cmp point_compare
#include <stc/cqueue.h>

#define i_val int
#include <stc/cqueue.h>
#include <time.h>

int main() {
    int n = 60000000;
    stc64_t rng = stc64_init(time(NULL));
    stc64_uniform_t dist = stc64_uniform_init(0, n);

    c_auto (cqueue_int, Q)
    {
        // Push eight million random numbers onto the queue.
        for (int i=0; i<n; ++i)
            cqueue_int_push(&Q, stc64_uniform(&rng, &dist));

        // Push or pop on the queue ten million times
        printf("befor: size %zu, capacity %zu\n", cqueue_int_size(Q), cqueue_int_capacity(Q));
        for (int i=n; i>0; --i) {
            int r = stc64_uniform(&rng, &dist);
            if (r & 1)
                cqueue_int_push(&Q, r);
            else
                cqueue_int_pop(&Q);
        }
        printf("after: size %zu, capacity %zu\n", cqueue_int_size(Q), cqueue_int_capacity(Q));
    }
}