rc-stack-test.c 1.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

#include <assert.h>
#include <ross.h>
#include "codes/rc-stack.h"

Jonathan Jenkins's avatar
Jonathan Jenkins committed
11
int main()
12 13 14 15 16 17 18 19 20 21 22 23
{
    /* mock up a dummy lp for testing */
    tw_lp lp;
    tw_kp kp;
    tw_pe pe;
    memset(&lp, 0, sizeof(lp));
    memset(&kp, 0, sizeof(kp));
    memset(&pe, 0, sizeof(pe));

    lp.pe = &pe;
    lp.kp = &kp;

24 25
    g_tw_synchronization_protocol = OPTIMISTIC;

26 27 28 29
    struct rc_stack *s;
    rc_stack_create(&s);
    assert(s != NULL);

30 31 32 33 34 35 36 37 38 39
    int *a, *b, *c;
#define ALLOC_ALL() \
    do { \
        a = malloc(sizeof(*a)); \
        b = malloc(sizeof(*b)); \
        c = malloc(sizeof(*c)); \
        *a = 1; \
        *b = 2; \
        *c = 3; \
    } while (0)
40 41 42 43

#define PUSH_ALL() \
    do { \
        kp.last_time = 1.0; \
44
        rc_stack_push(&lp, a, free, s); \
45
        kp.last_time = 2.0; \
46
        rc_stack_push(&lp, b, free, s); \
47
        kp.last_time = 3.0; \
48
        rc_stack_push(&lp, c, free, s); \
49 50
    } while (0)

51
    ALLOC_ALL();
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    PUSH_ALL();

    void *dat;
    assert(3 == rc_stack_count(s));
    dat = rc_stack_pop(s);
    assert(c == dat);
    dat = rc_stack_pop(s);
    assert(b == dat);
    dat = rc_stack_pop(s);
    assert(a == dat);
    assert(0 == rc_stack_count(s));

    PUSH_ALL();
    /* garbage collect the first two (NOT freeing the pointers first) */
    pe.GVT = 2.5;
67
    rc_stack_gc(&lp, s);
68 69 70 71 72
    assert(1 == rc_stack_count(s));

    dat = rc_stack_pop(s);
    assert(c == dat);
    assert(0 == rc_stack_count(s));
73
    free(dat);
74 75

    /* destroy everything */
76
    ALLOC_ALL();
77
    PUSH_ALL();
78
    rc_stack_destroy(s);
79 80 81 82 83 84 85 86 87 88 89 90

    return 0;
}

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */