Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/src/test/test_Xinit.c: test LAGr_Init and LAGraph_Global
3 : //------------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2023 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //------------------------------------------------------------------------------
17 :
18 : #include "LAGraph_test.h"
19 : #include "LAGraphX.h"
20 : #include "LG_internal.h"
21 :
22 : // functions defined in LAGr_Init.c:
23 : LAGRAPH_PUBLIC void LG_set_LAGr_Init_has_been_called (bool setting) ;
24 : LAGRAPH_PUBLIC bool LG_get_LAGr_Init_has_been_called (void) ;
25 :
26 : //------------------------------------------------------------------------------
27 : // global variables
28 : //------------------------------------------------------------------------------
29 :
30 : char msg [LAGRAPH_MSG_LEN] ;
31 :
32 : //------------------------------------------------------------------------------
33 : // test_Xinit: test LAGr_Init
34 : //------------------------------------------------------------------------------
35 :
36 1 : void test_Xinit (void)
37 : {
38 :
39 1 : printf ("\nTesting LAGr_Init: with expected errors\n") ;
40 :
41 : #if LG_BRUTAL_TESTS
42 1 : printf ("with brutal tests\n") ;
43 : #else
44 : printf ("with NO brutal tests\n") ;
45 : #endif
46 :
47 1 : TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, NULL, NULL, NULL, NULL, msg)
48 : == GrB_NULL_POINTER) ;
49 1 : printf ("msg: [%s]\n", msg) ;
50 :
51 1 : TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, malloc, NULL, NULL, NULL, msg)
52 : == GrB_NULL_POINTER) ;
53 1 : printf ("msg: [%s]\n", msg) ;
54 :
55 1 : TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, NULL, NULL, NULL, free, msg)
56 : == GrB_NULL_POINTER) ;
57 1 : printf ("msg: [%s]\n", msg) ;
58 :
59 1 : OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;
60 1 : printf ("msg: [%s]\n", msg) ;
61 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
62 :
63 : // LAGr_Init cannot be called twice
64 1 : int status = LAGr_Init (GrB_NONBLOCKING,
65 : malloc, calloc, realloc, free, msg) ;
66 1 : TEST_CHECK (status != GrB_SUCCESS) ;
67 1 : printf ("msg: [%s]\n", msg) ;
68 :
69 1 : OK (LAGraph_Finalize (msg)) ;
70 :
71 : // the flag is still set after LAGraph_Finalize has been called,
72 : // per LAGraph policy
73 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
74 :
75 : // reset and try again
76 1 : LG_set_LAGr_Init_has_been_called (false) ;
77 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == false) ;
78 1 : OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;
79 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
80 1 : OK (LAGraph_Finalize (msg)) ;
81 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
82 1 : }
83 :
84 : //------------------------------------------------------------------------------
85 : // test_Xinit_brutal: test LAGr_Init with brutal memory debug
86 : //------------------------------------------------------------------------------
87 :
88 : #if LG_BRUTAL_TESTS
89 1 : void test_Xinit_brutal (void)
90 : {
91 : // no brutal memory failures, but test LG_brutal_malloc/calloc/realloc/free
92 1 : LG_brutal = -1 ;
93 1 : LG_nmalloc = 0 ;
94 1 : OK (LAGr_Init (GrB_NONBLOCKING,
95 : LG_brutal_malloc, LG_brutal_calloc, LG_brutal_realloc, LG_brutal_free,
96 : msg)) ;
97 :
98 1 : int32_t *p = LG_brutal_malloc (42 * sizeof (int32_t)) ;
99 1 : TEST_CHECK (p != NULL) ;
100 1 : LG_brutal_free (p) ;
101 1 : p = LG_brutal_calloc (42, sizeof (int32_t)) ;
102 43 : for (int k = 0 ; k < 42 ; k++)
103 : {
104 42 : TEST_CHECK (p [k] == 0) ;
105 : }
106 1 : p = LG_brutal_realloc (p, 99 * sizeof (int32_t)) ;
107 43 : for (int k = 0 ; k < 42 ; k++)
108 : {
109 42 : TEST_CHECK (p [k] == 0) ;
110 : }
111 1 : LG_brutal_free (p) ;
112 1 : p = LG_brutal_realloc (NULL, 4 * sizeof (int32_t)) ;
113 5 : for (int k = 0 ; k < 4 ; k++)
114 : {
115 4 : p [k] = k ;
116 : }
117 1 : LG_brutal_free (p) ;
118 :
119 1 : OK (LAGraph_Finalize (msg)) ;
120 1 : TEST_CHECK (LG_nmalloc == 0) ;
121 :
122 : // brutal tests: keep giving the method more malloc's until it succeeds
123 :
124 13 : for (int nbrutal = 0 ; nbrutal < 1000 ; nbrutal++)
125 : {
126 13 : LG_brutal = nbrutal ;
127 13 : GB_Global_GrB_init_called_set (false) ;
128 13 : GrB_Info info = GxB_init (GrB_NONBLOCKING, LG_brutal_malloc,
129 : LG_brutal_calloc, LG_brutal_realloc, LG_brutal_free) ;
130 13 : void *p = NULL, *pnew = NULL ;
131 13 : bool ok = false ;
132 13 : if (info == GrB_SUCCESS)
133 : {
134 3 : p = LG_brutal_realloc (NULL, 42) ;
135 3 : pnew = NULL ;
136 3 : ok = (p != NULL) ;
137 3 : if (ok)
138 : {
139 2 : pnew = LG_brutal_realloc (p, 107) ;
140 2 : ok = (pnew != NULL) ;
141 2 : LG_brutal_free (ok ? pnew : p) ;
142 : }
143 : }
144 13 : if (ok)
145 : {
146 1 : OK (GrB_finalize ( )) ;
147 1 : printf ("\nGxB_init, finally: %d %g\n", nbrutal,
148 : (double) LG_nmalloc) ;
149 1 : TEST_CHECK (LG_nmalloc == 0) ;
150 1 : break ;
151 : }
152 : }
153 :
154 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
155 :
156 56 : for (int nbrutal = 0 ; nbrutal < 1000 ; nbrutal++)
157 : {
158 56 : LG_brutal = nbrutal ;
159 : // reset both GraphBLAS and LAGraph
160 56 : GB_Global_GrB_init_called_set (false) ;
161 56 : LG_set_LAGr_Init_has_been_called (false) ;
162 56 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == false) ;
163 : // try to initialize GraphBLAS and LAGraph
164 56 : int result = LAGr_Init (GrB_NONBLOCKING,
165 : LG_brutal_malloc, LG_brutal_calloc,
166 : LG_brutal_realloc, LG_brutal_free, msg) ;
167 56 : if (result == 0)
168 : {
169 : // success
170 1 : OK (LAGraph_Finalize (msg)) ;
171 1 : printf ("LAGr_Init: finally: %d %g\n", nbrutal,
172 : (double) LG_nmalloc) ;
173 1 : TEST_CHECK (LG_nmalloc == 0) ;
174 1 : break ;
175 : }
176 : // failure: free anything partially allocated
177 55 : OK (LAGraph_Finalize (msg)) ;
178 : }
179 1 : }
180 : #endif
181 :
182 : //-----------------------------------------------------------------------------
183 : // TEST_LIST: the list of tasks for this entire test
184 : //-----------------------------------------------------------------------------
185 :
186 : TEST_LIST =
187 : {
188 : { "Xinit", test_Xinit },
189 : #if LG_BRUTAL_TESTS
190 : { "Xinit_brutal", test_Xinit_brutal },
191 : #endif
192 : { NULL, NULL }
193 : } ;
|