1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  * FILE
16  * ttsafe.c - HDF5 threadsafe testing framework main file.
17  *
18  * REMARKS
19  * General test wrapper for HDF5 library thread safety test programs
20  *
21  * DESIGN
22  * Each test function should be implemented as function having no
23  * parameters and returning void (i.e. no return value).  They should be put
24  * into the list of InitTest() calls in main() below.  Functions which depend
25  * on other functionality should be placed below the InitTest() call for the
26  * base functionality testing.
27  * Each test module should include ttsafe.h and define a unique set of
28  * names for test files they create.
29  *
30  */
31 
32 /* ANY new test needs to have a prototype in ttsafe.h */
33 #include "ttsafe.h"
34 
35 
36 #define MAX_NUM_NAME 1000
37 #define NAME_OFFSET 6         /* offset for "name<num>" */
38 
39 /* pre-condition: num must be a non-negative number */
40 H5_ATTR_PURE static unsigned
num_digits(int num)41 num_digits(int num)
42 {
43     unsigned u;
44 
45     if(num == 0)
46         return 1;
47 
48     for(u = 0; num > 0; u++)
49         num = num / 10;
50 
51     return u;
52 }
53 
54 /* Test the H5is_library_threadsafe() function */
55 void
tts_is_threadsafe(void)56 tts_is_threadsafe(void)
57 {
58     hbool_t is_ts;
59     hbool_t should_be;
60 
61 #ifdef H5_HAVE_THREADSAFE
62     is_ts = FALSE;
63     should_be = TRUE;
64 #else /* H5_HAVE_THREADSAFE */
65     is_ts = TRUE;
66     should_be = FALSE;
67 #endif /* H5_HAVE_THREADSAFE */
68 
69     if(H5is_library_threadsafe(&is_ts) != SUCCEED)
70             TestErrPrintf("H5_is_library_threadsafe() call failed - test failed\n");
71 
72     if(is_ts != should_be)
73             TestErrPrintf("Thread-safety value incorrect - test failed\n");
74 
75     return;
76 }
77 
78 /* Routine to generate attribute names for numeric values */
gen_name(int value)79 char *gen_name(int value)
80 {
81     char *temp;
82     unsigned length;
83     int i;
84 
85     length = num_digits(MAX_NUM_NAME - 1);
86     temp = (char *)HDmalloc(NAME_OFFSET + length + 1);
87     temp = HDstrcpy(temp, "attrib");
88     temp[NAME_OFFSET + length] = '\0';
89 
90     for (i = (int)(length - 1); i >= 0; i--) {
91         temp[NAME_OFFSET + i] = (char)((int)'0' + value % 10);
92         value = value / 10;
93     }
94 
95     return temp;
96 }
97 
main(int argc,char * argv[])98 int main(int argc, char *argv[])
99 {
100 
101     /* Initialize testing framework */
102     TestInit(argv[0], NULL, NULL);
103 
104     /* Tests are generally arranged from least to most complexity... */
105     AddTest("is_threadsafe", tts_is_threadsafe, NULL, "library threadsafe status", NULL);
106 #ifdef H5_HAVE_THREADSAFE
107     AddTest("dcreate", tts_dcreate, cleanup_dcreate, "multi-dataset creation", NULL);
108     AddTest("error", tts_error, cleanup_error, "per-thread error stacks", NULL);
109 #ifdef H5_HAVE_PTHREAD_H
110     /* Thread cancellability only supported with pthreads ... */
111     AddTest("cancel", tts_cancel, cleanup_cancel, "thread cancellation safety test", NULL);
112 #endif /* H5_HAVE_PTHREAD_H */
113     AddTest("acreate", tts_acreate, cleanup_acreate, "multi-attribute creation", NULL);
114 
115 #else /* H5_HAVE_THREADSAFE */
116 
117     HDprintf("Most thread-safety tests skipped because THREADSAFE not enabled\n");
118 
119 #endif /* H5_HAVE_THREADSAFE */
120 
121     /* Display testing information */
122     TestInfo(argv[0]);
123 
124     /* Parse command line arguments */
125     TestParseCmdLine(argc,argv);
126 
127     /* Perform requested testing */
128     PerformTests();
129 
130     /* Display test summary, if requested */
131     if (GetTestSummary())
132         TestSummary();
133 
134     /* Clean up test files, if allowed */
135     if (GetTestCleanup() && !getenv("HDF5_NOCLEANUP"))
136         TestCleanup();
137 
138     /* Release test infrastructure */
139     TestShutdown();
140 
141     return GetTestNumErrs();
142 
143 } /* end main() */
144 
145