summaryrefslogtreecommitdiffstats
path: root/test/app_ref.c
blob: 729862ee39fb69b280cebe2a1dbf48090965c8e0 (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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose: Tests closing the library after reference counts have been
 *          manipulated.
 */
#include "h5test.h"

#define APPREF_DSET  "test_dset"
#define APPREF_ATTR  "test_attr"
#define APPREF_GROUP "test_grp"

#define ERR_WIDTH 40 /* Width of output for the SIGABRT handler */
#define MAX_NINC  16 /* Maximum increments of a reference count */

/* Macro to increment the reference count on id a random number of times (from
 * 1 to MAX_NINC).  Assumes integers i and ninc are in scope. */
#define RAND_INC(id)                                                                                         \
    do {                                                                                                     \
        ninc = (HDrand() % MAX_NINC) + 1;                                                                    \
                                                                                                             \
        for (i = 0; i < ninc; i++)                                                                           \
            if (H5Iinc_ref(ids[id]) != i + 2)                                                                \
                TEST_ERROR;                                                                                  \
                                                                                                             \
        rc[id] = ninc + 1;                                                                                   \
    } while (0)

typedef enum {
    T_FILE,
    T_PLIST,
    T_PCLASS,
    T_TYPE,
    T_SPACE,
    T_DSET,
    T_ATTR,
    T_GROUP,
    T_ECLASS,
    T_EMSG,
    T_ESTACK,
    T_NUMCLASSES
} id_class_t;

static const char *FILENAME[] = {"app_ref", NULL};

static const char *IDNAME[T_NUMCLASSES] = {"File",        "Property List", "Property Class", "Datatype",
                                           "Dataspace",   "Dataset",       "Attribute",      "Group",
                                           "Error Class", "Error Message", "Error Stack"};

static int rc[T_NUMCLASSES];

void Abrt_Handler(int sig);

/* Handler for SIGABRT - prints the reference count on each id */
void
Abrt_Handler(int H5_ATTR_UNUSED sig)
{
    int i, n;

    const char *string = " ID reference count: ";
    for (i = 0; i < T_NUMCLASSES; i++) {
        fprintf(stderr, "%s%s", IDNAME[i], string);
        n = (int)(strlen(IDNAME[i]) + strlen(string));
        fprintf(stderr, "%*d\n", (n < ERR_WIDTH) ? (ERR_WIDTH - n) : 0, rc[i]);
    }
}

/* Main test routine */
int
main(void)
{
    const char *env_h5_drvr; /* File Driver value from environment */
    hid_t       ids[T_NUMCLASSES];
    hid_t       fapl; /* File Access Property List */
    int         ninc;
    int         i;
    char        filename[1024];

    h5_reset();
    h5_fixname(FILENAME[0], H5P_DEFAULT, filename, sizeof filename);

    HDsrand((unsigned)HDtime(NULL));

    TESTING("library shutdown with reference count > 1");

    /* Get the VFD to use */
    env_h5_drvr = getenv(HDF5_DRIVER);
    if (env_h5_drvr == NULL)
        env_h5_drvr = "nomatch";

    /* Don't run this test with the multi/split VFD. A bug in library shutdown
     * ordering causes problems with the multi VFD when IDs are left dangling.
     */
    if (!strcmp(env_h5_drvr, "multi") || !strcmp(env_h5_drvr, "split")) {
        puts("\n -- SKIPPED for incompatible VFD --");
        return 0;
    }

    /* Create the file */
    if ((ids[T_FILE] = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    RAND_INC(T_FILE);

    /* Create the property list */
    if ((ids[T_PLIST] = H5Pcreate(H5P_DATASET_CREATE)) < 0)
        TEST_ERROR;

    RAND_INC(T_PLIST);

    /* Create a property class */
    if ((ids[T_PCLASS] = H5Pcreate_class(H5P_DATASET_CREATE, "foo", NULL, NULL, NULL, NULL, NULL, NULL)) < 0)
        TEST_ERROR;

    RAND_INC(T_PCLASS);

    /* Create a datatype */
    if ((ids[T_TYPE] = H5Tcreate(H5T_OPAQUE, (size_t)16)) < 0)
        TEST_ERROR;

    RAND_INC(T_TYPE);

    /* Create a dataspace */
    if ((ids[T_SPACE] = H5Screate(H5S_SCALAR)) < 0)
        TEST_ERROR;

    RAND_INC(T_SPACE);

    /* Create a dataset */
    if ((ids[T_DSET] = H5Dcreate2(ids[T_FILE], APPREF_DSET, H5T_NATIVE_INT, ids[T_SPACE], H5P_DEFAULT,
                                  H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    RAND_INC(T_DSET);

    /* Create an attribute */
    if ((ids[T_ATTR] = H5Acreate2(ids[T_DSET], APPREF_ATTR, H5T_NATIVE_INT, ids[T_SPACE], H5P_DEFAULT,
                                  H5P_DEFAULT)) < 0)
        TEST_ERROR;

    RAND_INC(T_ATTR);

    /* Create a group */
    if ((ids[T_GROUP] = H5Gcreate2(ids[T_FILE], APPREF_GROUP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    RAND_INC(T_GROUP);

    /* Create an error class */
    if ((ids[T_ECLASS] = H5Eregister_class("foo", "bar", "baz")) < 0)
        TEST_ERROR;

    RAND_INC(T_ECLASS);

    /* Create an error message */
    if ((ids[T_EMSG] = H5Ecreate_msg(ids[T_ECLASS], H5E_MAJOR, "mumble")) < 0)
        TEST_ERROR;

    RAND_INC(T_EMSG);

    /* Create an error stack */
    if ((ids[T_ESTACK] = H5Eget_current_stack()) < 0)
        TEST_ERROR;

    RAND_INC(T_ESTACK);

    HDsignal(SIGABRT, &Abrt_Handler);

    if (H5close() < 0)
        TEST_ERROR;

    PASSED();

    /* Restore the default error handler (set in h5_reset()) */
    h5_restore_err();

    /* Clean up any file(s) created */
    h5_reset();
    fapl = H5Pcreate(H5P_FILE_ACCESS);
    h5_cleanup(FILENAME, fapl);

    return EXIT_SUCCESS;

error:

    puts("***** APPLICATION REFERENCE COUNT TESTS FAILED *****");

    return EXIT_FAILURE;
}