rc-stack-test.c 1.7 KB
Newer Older
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
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

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

int main(int argc, char *argv[])
{
    /* 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;

    struct rc_stack *s;
    rc_stack_create(&s);
    assert(s != NULL);

28 29 30 31 32 33 34 35 36 37
    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)
38 39 40 41

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

49
    ALLOC_ALL();
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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;
65
    rc_stack_gc(&lp, s);
66 67 68 69 70
    assert(1 == rc_stack_count(s));

    dat = rc_stack_pop(s);
    assert(c == dat);
    assert(0 == rc_stack_count(s));
71
    free(dat);
72 73

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

    return 0;
}

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