File: ro.c

package info (click to toggle)
libigloo 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,968 kB
  • sloc: ansic: 5,476; sh: 4,996; makefile: 62
file content (142 lines) | stat: -rw-r--r-- 5,729 bytes parent folder | download | duplicates (3)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Copyright (C) 2021       Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <igloo/tap.h>
#include <igloo/igloo.h>
#include <igloo/error.h>
#include <igloo/ro.h>

static igloo_ro_t g_instance;

static void group_create_object(void)
{
    igloo_ro_stub_t *stub;
    igloo_ro_tiny_t *tiny;
    igloo_ro_full_t *full; 

    igloo_tap_test_success("igloo_ro_new igloo_ro_stub_t", igloo_ro_new(&stub, igloo_ro_stub_t, g_instance));
    igloo_tap_dump_ro_variable(stub);
    igloo_tap_test_success("unref", igloo_ro_unref(&stub));

    igloo_tap_test_success("igloo_ro_new igloo_ro_tiny_t", igloo_ro_new(&tiny, igloo_ro_tiny_t, g_instance));
    igloo_tap_dump_ro_variable(tiny);
    igloo_tap_test_success("unref", igloo_ro_unref(&tiny));

    igloo_tap_test_success("igloo_ro_new igloo_ro_full_t", igloo_ro_new(&full, igloo_ro_full_t, g_instance));
    igloo_tap_dump_ro_variable(full);
    igloo_tap_test_success("unref", igloo_ro_unref(&full));
}

static void group_refcounting(void)
{
    igloo_ro_tiny_t *tiny;
    igloo_ro_tiny_t *extra_ref;

    igloo_tap_test_success("igloo_ro_new igloo_ro_tiny_t", igloo_ro_new(&tiny, igloo_ro_tiny_t, g_instance));
    igloo_tap_dump_ro_variable(tiny);
    igloo_tap_test_success("igloo_ro_ref", igloo_ro_ref(tiny, &extra_ref, igloo_ro_tiny_t));
    igloo_tap_dump_ro_variable(extra_ref);
    igloo_tap_test_success("igloo_ro_unref extra", igloo_ro_unref(&extra_ref));
    igloo_tap_dump_ro_variable(tiny);
    igloo_tap_test_success("igloo_ro_ref", igloo_ro_ref(tiny, &extra_ref, igloo_ro_tiny_t));
    igloo_tap_test_success("unref", igloo_ro_unref(&tiny));
    igloo_tap_dump_ro_variable(extra_ref);
    igloo_tap_test_success("igloo_ro_unref extra", igloo_ro_unref(&extra_ref));
}

static void test_stringify_real(igloo_ro_stub_t *stub, igloo_ro_sy_t flags, const char *flags_str, igloo_error_t expected)
{
    char buf[64];
    char *res = NULL;
    igloo_error_t error = igloo_ro_stringify(stub, &res, flags);

    snprintf(buf, sizeof(buf), "stub=%p, flags=0x%x (%s)", stub, (unsigned int)flags, flags_str);
    igloo_tap_diagnostic(buf);
    igloo_tap_diagnostic(res);

    igloo_tap_test_error("correct error", expected, error);

    if (expected == igloo_ERROR_NONE) {
        igloo_tap_test("result is non-NULL", res != NULL);
    } else {
        igloo_tap_test("result is NULL", res == NULL);
    }

    free(res);
}
#define test_stringify(stub, flags, expected) test_stringify_real((stub), (flags), (# flags), (expected))

static void group_stringify(void)
{
    char buffer[64];
    igloo_ro_stub_t *stub_strong;
    igloo_ro_stub_t *stub_weak;
    igloo_ro_stub_t *invalid = (void*)&(buffer[1]);

    memset(buffer, 0, sizeof(buffer));

    igloo_tap_test_success("igloo_ro_new igloo_ro_stub_t", igloo_ro_new(&stub_strong, igloo_ro_stub_t, g_instance));
    igloo_tap_dump_ro_variable(stub_strong);
    test_stringify(stub_strong, igloo_RO_SY_DEFAULT, igloo_ERROR_NONE);
    test_stringify(stub_strong, igloo_RO_SY_OBJECT, igloo_ERROR_NONE);
    test_stringify(stub_strong, igloo_RO_SY_CONTENT, igloo_ERROR_NOSYS);
    test_stringify(stub_strong, igloo_RO_SY_DEBUG, igloo_ERROR_NOSYS);
    igloo_tap_test_success("weak_ref", igloo_ro_weak_ref(stub_strong, &stub_weak, igloo_ro_stub_t));
    igloo_tap_test_success("unref", igloo_ro_unref(&stub_strong));
    igloo_tap_dump_ro_variable(stub_strong);
    igloo_tap_dump_ro_variable(stub_weak);
    test_stringify(stub_weak, igloo_RO_SY_DEFAULT, igloo_ERROR_NONE);
    test_stringify(stub_weak, igloo_RO_SY_OBJECT, igloo_ERROR_NONE);
    test_stringify(stub_weak, igloo_RO_SY_CONTENT, igloo_ERROR_GONE);
    test_stringify(stub_weak, igloo_RO_SY_DEBUG, igloo_ERROR_GONE);
    igloo_tap_test_success("weak_unref", igloo_ro_weak_unref(&stub_weak));
    igloo_tap_dump_ro_variable(stub_weak);

    test_stringify(invalid, igloo_RO_SY_DEFAULT, igloo_ERROR_NONE);
    test_stringify(invalid, igloo_RO_SY_OBJECT, igloo_ERROR_NONE);
    test_stringify(invalid, igloo_RO_SY_CONTENT, igloo_ERROR_FAULT);
    test_stringify(invalid, igloo_RO_SY_DEBUG, igloo_ERROR_FAULT);

    test_stringify(NULL, igloo_RO_SY_DEFAULT, igloo_ERROR_NONE);
    test_stringify(NULL, igloo_RO_SY_OBJECT, igloo_ERROR_NONE);
    test_stringify(NULL, igloo_RO_SY_CONTENT, igloo_ERROR_FAULT);
    test_stringify(NULL, igloo_RO_SY_DEBUG, igloo_ERROR_FAULT);
}

int main(void)
{

    igloo_tap_init();
    igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
    igloo_tap_test_success("igloo_initialize", igloo_initialize(&g_instance));
    igloo_tap_group_run("create_object", group_create_object);
    igloo_tap_group_run("refcounting", group_refcounting);
    igloo_tap_group_run("stringify", group_stringify);
    igloo_tap_test_success("unref instance", igloo_ro_unref(&g_instance));
    igloo_tap_fin();

    return EXIT_FAILURE; // return failure as we should never reach this point!
}