File: error.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 (169 lines) | stat: -rw-r--r-- 6,813 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (C) 2018-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

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

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

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

#define _VECTOR(error,good) {(error), #error , (good)}
struct vector {
    igloo_error_t error;
    const char *string;
    bool good;
};

static void check_description(const igloo_error_desc_t * desc, igloo_error_t error)
{
    if (desc == NULL)
        return;
    igloo_tap_test_error("desc->error == error", error, desc->error);
    igloo_tap_test("desc->name != NULL", desc->name != NULL);
    igloo_tap_test("desc->uuid != NULL", desc->uuid != NULL);
    igloo_tap_test("desc->message != NULL", desc->message != NULL);

    if (desc->name) {
        const igloo_error_desc_t * desc_match = igloo_error_getbyname(desc->name);
        igloo_tap_test("igloo_error_getbyname(desc->name) != NULL", desc_match != NULL);
        igloo_tap_test_error("inverse error == forward error", error, desc_match->error);
    }
}

static void test_get_description(void)
{
    static const struct vector vector[] = {
        _VECTOR(igloo_ERROR_NONE, true),
        _VECTOR(igloo_ERROR_GENERIC, true),
        _VECTOR(igloo_ERROR_NOENT, true),
        _VECTOR(igloo_ERRORTESTVECTOR_NX, false)
    };
    size_t i;

    for (i = 0; i < (sizeof(vector)/sizeof(*vector)); i++) {
        const igloo_error_desc_t * desc = igloo_error_get_description(vector[i].error);
        char buffer[128];

        snprintf(buffer, sizeof(buffer), "igloo_error_get_description(%s) %c= NULL", vector[i].string, vector[i].good ? '!' : '=');

        if (vector[i].good) {
            igloo_tap_test(buffer, desc != NULL);
            check_description(desc, vector[i].error);
        } else {
            igloo_tap_test(buffer, desc == NULL);
        }
    }
}

static void test_getbyname(void)
{
    static const struct vector vector[] = {
        {igloo_ERROR_NONE, "NONE", true},
        {igloo_ERROR_NONE, "ERROR_NONE", false},
        {igloo_ERROR_NONE, "igloo_ERROR_NONE", false},
        {igloo_ERRORTESTVECTOR_NX, "NONEX", false}
    };
    size_t i;

    for (i = 0; i < (sizeof(vector)/sizeof(*vector)); i++) {
        const igloo_error_desc_t * desc = igloo_error_getbyname(vector[i].string);
        char buffer[128];

        snprintf(buffer, sizeof(buffer), "igloo_error_getbyname(\"%s\") %c= NULL", vector[i].string, vector[i].good ? '!' : '=');

        if (vector[i].good) {
            igloo_tap_test(buffer, desc != NULL);
            check_description(desc, vector[i].error);
        } else {
            igloo_tap_test(buffer, desc == NULL);
        }
    }
}


#ifdef HAVE_ERRNO_H
static void test_errno(void)
{
    igloo_error_t error;

    igloo_tap_test_error("igloo_error_by_domain value=ENOENT", igloo_ERROR_NOENT, igloo_error_by_domain(igloo_ERROR_DOMAIN_ERRNO, ENOENT, &error));
    igloo_tap_test_success("igloo_error_by_domain error", error);

    errno = ENOENT;
    igloo_tap_test_error("igloo_error_from_system errno=ENOENT", igloo_ERROR_NOENT, igloo_error_from_system(&error));
    igloo_tap_test_success("igloo_error_from_system error", error);

    igloo_tap_test_success("igloo_error_clear_system", igloo_error_clear_system());
    igloo_tap_test_success("igloo_error_from_system", igloo_error_from_system(&error));
    igloo_tap_test_success("igloo_error_from_system error", error);

    errno = ENOENT;
    igloo_tap_test_error("igloo_error_from_system errno=ENOENT (retry after clear)", igloo_ERROR_NOENT, igloo_error_from_system(&error));
    igloo_tap_test_success("igloo_error_from_system error", error);
}
#endif

static void test_ra_errors(void)
{
    igloo_error_t error;

    igloo_tap_test_error("igloo_error_by_domain value=-1", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, -1, &error));
    igloo_tap_test_success("igloo_error_by_domain error", error);

    igloo_tap_test_error("igloo_error_by_domain value=0", igloo_ERROR_NONE, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, 0, &error));
    igloo_tap_test_success("igloo_error_by_domain error", error);

    igloo_tap_test_error("igloo_error_by_domain value=-2", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, -2, &error));
    igloo_tap_test_error("igloo_error_by_domain error", igloo_ERROR_RANGE, error);

    igloo_tap_test_error("igloo_error_by_domain value=65536", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, 65536, &error));
    igloo_tap_test_error("igloo_error_by_domain error", igloo_ERROR_RANGE, error);

    igloo_tap_test_error("igloo_error_by_domain value=256", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, 256, &error));
    igloo_tap_test_error("igloo_error_by_domain error", igloo_ERROR_INVAL, error);
    igloo_tap_test_error("igloo_error_by_domain value=65535", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, 65535, &error));
    igloo_tap_test_error("igloo_error_by_domain error", igloo_ERROR_INVAL, error);
    igloo_tap_test_error("igloo_error_by_domain value=16777215", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, 16777215, &error));
    igloo_tap_test_error("igloo_error_by_domain error", igloo_ERROR_INVAL, error);
    igloo_tap_test_error("igloo_error_by_domain value=4294967295", igloo_ERROR_GENERIC, igloo_error_by_domain(igloo_ERROR_DOMAIN_RA_ERROR, 4294967295, &error));
    igloo_tap_test_error("igloo_error_by_domain error", igloo_ERROR_INVAL, error);
}

int main (void) {
    igloo_tap_init();
    igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
    igloo_tap_group_run("get description", test_get_description);
    igloo_tap_group_run("getbyname", test_getbyname);
#ifdef HAVE_ERRNO_H
    igloo_tap_group_run("errno", test_errno);
#endif
    igloo_tap_group_run("RoarAudio errors", test_ra_errors);
    igloo_tap_fin();

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