summaryrefslogtreecommitdiff
path: root/cesar/lib/test/restrack/src/test_restrack.c
blob: e2ce11bb4de79acfd867b67837cb55f549fbbbd0 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_restrack.c
 * \brief   Test resources tracker.
 * \ingroup test
 */
#include "common/std.h"

#include "lib/restrack.h"
#include "lib/blk.h"
#include "lib/test.h"

void
restrack_alone_test_case (test_t t)
{
    test_case_begin (t, "alone");
    test_begin (t, "basic")
    {
        int a, b, c, i;
        restrack_create (NULL, &a, __func__, __LINE__, 1);
        restrack_create (NULL, &b, __func__, __LINE__, 1);
        restrack_create (NULL, &c, __func__, __LINE__, 1);
        for (i = 0; i < 5; i++)
        {
            restrack_update (NULL, &a, __func__, __LINE__, 1);
            restrack_update (NULL, &b, __func__, __LINE__, 1);
            restrack_update (NULL, &b, __func__, __LINE__, 2);
            restrack_update (NULL, &b, __func__, __LINE__, -1);
        }
        test_fail_unless (!restrack_check ());
        for (i = 0; i < 5; i++)
        {
            restrack_update (NULL, &a, __func__, __LINE__, -1);
            restrack_update (NULL, &b, __func__, __LINE__, -2);
        }
        restrack_destroy (NULL, &a, __func__, __LINE__, -1);
        restrack_destroy (NULL, &b, __func__, __LINE__, -1);
        restrack_destroy (NULL, &c, __func__, __LINE__, -1);
        test_fail_unless (restrack_check ());
    } test_end;
}

void
restrack_blk_test_case (test_t t)
{
    test_case_begin (t, "blk");
    test_begin (t, "basic")
    {
        void *a, *b;
        blk_t *f, *l;
        int i;
        a = blk_alloc ();
        b = blk_alloc ();
        f = blk_alloc_desc_range (5, &l);
        for (i = 0; i < 5; i++)
        {
            blk_addref (a);
            blk_addref (b);
            blk_addref (b);
            blk_release (b);
            blk_addref_desc_range (f, l);
            blk_addref_desc (f);
            blk_addref_desc (l);
            blk_release_desc_range (f, l);
        }
        test_fail_unless (!restrack_check ());
        blk_release_desc_range (f, l);
        test_fail_unless (!restrack_check ());
        for (i = 0; i < 5; i++)
        {
            blk_release (b);
            blk_release (a);
            blk_release_desc (f);
            blk_release_desc (l);
        }
        blk_release (b);
        blk_release (a);
        test_fail_unless (restrack_check ());
    } test_end;
}

void
restrack_test_suite (test_t t)
{
    test_suite_begin (t, "restrack");
    restrack_alone_test_case (t);
    restrack_blk_test_case (t);
}

int
main (int argc, char **argv)
{
    test_t t;
    test_init (t, argc, argv);
    restrack_test_suite (t);
    test_result (t);
    return test_nb_failed (t) == 0 ? 0 : 1;
}