ctest_refobject.c 7.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2018,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "ctest_lib.h"

17
#include "../refobject.h"
18

19 20 21 22 23 24
static int return_zero(refobject_t self, const refobject_type_t *type, va_list ap)
{
    (void)self, (void)type, (void)ap;
    return 0;
}

25 26 27 28 29 30 31 32 33 34 35 36 37
static void test_ptr(void)
{
    refobject_t a;

    a = REFOBJECT_NULL;
    ctest_test("NULL is NULL", REFOBJECT_IS_NULL(a));

    if (!REFOBJECT_IS_NULL(a))
        ctest_bailed_out();
}

static void test_create_ref_unref(void)
{
38
    refobject_base_t *a;
39

40
    a = refobject_new(refobject_base_t);
41 42 43 44 45 46 47
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));

    ctest_test("referenced", refobject_ref(a) == 0);
    ctest_test("un-referenced (1 of 2)", refobject_unref(a) == 0);
    ctest_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
}

48 49 50 51 52
static void test_typename(void)
{
    refobject_base_t *a;
    const char *typename;

53
    a = refobject_new(refobject_base_t);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));

    typename = REFOBJECT_GET_TYPENAME(a);
    ctest_test("got typename", typename != NULL);
    ctest_test("typename matches", strcmp(typename, "refobject_base_t") == 0);

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

static void test_valid(void)
{
    refobject_base_t *a;

    typedef struct {
        refobject_base_t __base;
    } ctest_test_type_t;

71
    REFOBJECT_DEFINE_PRIVATE_TYPE(ctest_test_type_t);
72 73 74

    ctest_test("NULL is not valid", !REFOBJECT_IS_VALID(REFOBJECT_NULL, refobject_base_t));

75
    a = refobject_new(refobject_base_t);
76 77 78 79 80 81 82 83
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));

    ctest_test("is valid", REFOBJECT_IS_VALID(a, refobject_base_t));
    ctest_test("is valid as diffrent type", !REFOBJECT_IS_VALID(a, ctest_test_type_t));

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

84 85 86 87
static void test_sizes(void)
{
    refobject_t a;

88 89 90 91
    typedef struct {
        refobject_base_t __base;
        char padding[1024];
    } ctest_test_type_a_t;
92 93 94
    REFOBJECT_DEFINE_PRIVATE_TYPE(ctest_test_type_a_t,
            REFOBJECT_DEFINE_TYPE_NEW(return_zero)
            );
95 96 97 98 99

    typedef struct {
        refobject_base_t __base;
        char padding[131072];
    } ctest_test_type_b_t;
100 101 102
    REFOBJECT_DEFINE_PRIVATE_TYPE(ctest_test_type_b_t,
            REFOBJECT_DEFINE_TYPE_NEW(return_zero)
            );
103 104 105 106

    typedef struct {
        char padding[sizeof(refobject_base_t) - 1];
    } ctest_test_type_c_t;
107 108 109
    REFOBJECT_DEFINE_PRIVATE_TYPE(ctest_test_type_c_t,
            REFOBJECT_DEFINE_TYPE_NEW(return_zero)
            );
110 111 112 113

    typedef struct {
        char padding[0];
    } ctest_test_type_d_t;
114 115 116
    REFOBJECT_DEFINE_PRIVATE_TYPE(ctest_test_type_d_t,
            REFOBJECT_DEFINE_TYPE_NEW(return_zero)
            );
117

118
    a = REFOBJECT_FROM_TYPE(refobject_new(ctest_test_type_a_t));
119 120 121
    ctest_test("refobject created with size=sizeof(refobject_base_t) + 1024", !REFOBJECT_IS_NULL(a));
    ctest_test("un-referenced", refobject_unref(a) == 0);

122
    a = REFOBJECT_FROM_TYPE(refobject_new(ctest_test_type_b_t));
123 124 125
    ctest_test("refobject created with size=sizeof(refobject_base_t) + 131072", !REFOBJECT_IS_NULL(a));
    ctest_test("un-referenced", refobject_unref(a) == 0);

126
    a = REFOBJECT_FROM_TYPE(refobject_new(ctest_test_type_c_t));
127 128 129
    ctest_test("refobject created with size=sizeof(refobject_base_t) - 1", REFOBJECT_IS_NULL(a));
    if (!REFOBJECT_IS_NULL(a)) {
        ctest_test("un-referenced", refobject_unref(a) == 0);
130 131
    }

132
    a = REFOBJECT_FROM_TYPE(refobject_new(ctest_test_type_d_t));
133 134 135 136 137 138
    ctest_test("refobject created with size=0", REFOBJECT_IS_NULL(a));
    if (!REFOBJECT_IS_NULL(a)) {
        ctest_test("un-referenced", refobject_unref(a) == 0);
    }
}

139 140
static void test_name(void)
{
141
    refobject_base_t *a;
142 143 144
    const char *name = "test object name";
    const char *ret;

145
    a = refobject_new_ext(refobject_base_t, NULL, name, REFOBJECT_NULL);
146 147 148 149 150 151 152 153 154 155 156
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));

    ret = refobject_get_name(a);
    ctest_test("get name", ret != NULL);
    ctest_test("name match", strcmp(name, ret) == 0);

    ctest_test("un-referenced", refobject_unref(a) == 0);
}

static void test_userdata(void)
{
157
    refobject_base_t *a;
158 159 160 161
    int tmp = 0;
    void *userdata = &tmp;
    void *ret;

162
    a = refobject_new(refobject_base_t);
163 164 165 166 167 168 169 170 171 172 173 174 175
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));

    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == NULL);
    ctest_test("set userdata", refobject_set_userdata(a, userdata) == 0);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == userdata);
    ctest_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == NULL);

    ctest_test("un-referenced", refobject_unref(a) == 0);

176
    a = refobject_new_ext(refobject_base_t, userdata, NULL, REFOBJECT_NULL);
177 178 179 180 181 182 183 184 185 186 187
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == userdata);
    ctest_test("clearing userdata", refobject_set_userdata(a, NULL) == 0);
    ret = refobject_get_userdata(a);
    ctest_test("get userdata", ret == NULL);
    ctest_test("un-referenced", refobject_unref(a) == 0);
}

static void test_associated(void)
{
188
    refobject_base_t *a, *b;
189

190
    a = refobject_new(refobject_base_t);
191 192
    ctest_test("refobject created", !REFOBJECT_IS_NULL(a));

193
    b = refobject_new_ext(refobject_base_t, NULL, NULL, a);
194 195 196 197 198 199
    ctest_test("refobject created with associated", !REFOBJECT_IS_NULL(b));

    ctest_test("un-referenced (1 of 2)", refobject_unref(b) == 0);
    ctest_test("un-referenced (2 of 2)", refobject_unref(a) == 0);
}

200 201 202 203 204 205
static size_t test_freecb__called;
static void test_freecb__freecb(refobject_t self, void **userdata)
{
    test_freecb__called++;
}

206 207
static void test_freecb(void)
{
208 209 210 211 212 213
    typedef struct {
        refobject_base_t __base;
    } ctest_test_type_t;
    ctest_test_type_t *a;

    REFOBJECT_DEFINE_PRIVATE_TYPE(ctest_test_type_t,
214 215
            REFOBJECT_DEFINE_TYPE_FREE(test_freecb__freecb),
            REFOBJECT_DEFINE_TYPE_NEW(return_zero)
216
            );
217

218
    test_freecb__called = 0;
219
    a = refobject_new(ctest_test_type_t);
220 221
    ctest_test("refobject created", a != NULL);
    ctest_test("un-referenced", refobject_unref(REFOBJECT_FROM_TYPE(a)) == 0);
222
    ctest_test("freecb called", test_freecb__called == 1);
223

224
    test_freecb__called = 0;
225
    a = refobject_new(ctest_test_type_t);
226 227
    ctest_test("refobject created", a != NULL);
    ctest_test("referenced", refobject_ref(REFOBJECT_FROM_TYPE(a)) == 0);
228
    ctest_test("freecb uncalled", test_freecb__called == 0);
229
    ctest_test("un-referenced (1 of 2)", refobject_unref(REFOBJECT_FROM_TYPE(a)) == 0);
230
    ctest_test("freecb uncalled", test_freecb__called == 0);
231
    ctest_test("un-referenced (2 of 2)", refobject_unref(REFOBJECT_FROM_TYPE(a)) == 0);
232
    ctest_test("freecb called", test_freecb__called == 1);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

int main (void)
{
    ctest_init();

    test_ptr();

    if (ctest_bailed_out()) {
        ctest_fin();
        return 1;
    }

    test_create_ref_unref();

248 249 250
    test_typename();
    test_valid();

251 252
    test_sizes();

253 254 255 256 257 258 259 260 261
    test_name();
    test_userdata();
    test_associated();
    test_freecb();

    ctest_fin();

    return 0;
}