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 :
21 : // functions defined in LAGr_Init.c:
22 : LAGRAPH_PUBLIC void LG_set_LAGr_Init_has_been_called (bool setting) ;
23 : LAGRAPH_PUBLIC bool LG_get_LAGr_Init_has_been_called (void) ;
24 :
25 : //------------------------------------------------------------------------------
26 : // global variables
27 : //------------------------------------------------------------------------------
28 :
29 : char msg [LAGRAPH_MSG_LEN] ;
30 :
31 : //------------------------------------------------------------------------------
32 : // test_Xinit: test LAGr_Init
33 : //------------------------------------------------------------------------------
34 :
35 1 : void test_Xinit (void)
36 : {
37 :
38 1 : printf ("\nTesting LAGr_Init: with expected errors\n") ;
39 :
40 1 : TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, NULL, NULL, NULL, NULL, msg)
41 : == GrB_NULL_POINTER) ;
42 1 : printf ("msg: [%s]\n", msg) ;
43 :
44 1 : TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, malloc, NULL, NULL, NULL, msg)
45 : == GrB_NULL_POINTER) ;
46 1 : printf ("msg: [%s]\n", msg) ;
47 :
48 1 : TEST_CHECK (LAGr_Init (GrB_NONBLOCKING, NULL, NULL, NULL, free, msg)
49 : == GrB_NULL_POINTER) ;
50 1 : printf ("msg: [%s]\n", msg) ;
51 :
52 1 : OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;
53 1 : printf ("msg: [%s]\n", msg) ;
54 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
55 :
56 : // LAGr_Init cannot be called twice
57 1 : int status = LAGr_Init (GrB_NONBLOCKING,
58 : malloc, calloc, realloc, free, msg) ;
59 1 : TEST_CHECK (status != GrB_SUCCESS) ;
60 1 : printf ("msg: [%s]\n", msg) ;
61 :
62 1 : OK (LAGraph_Finalize (msg)) ;
63 :
64 : // the flag is still set after LAGraph_Finalize has been called,
65 : // per LAGraph policy
66 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
67 :
68 : // reset and try again
69 1 : LG_set_LAGr_Init_has_been_called (false) ;
70 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == false) ;
71 1 : OK (LAGr_Init (GrB_NONBLOCKING, malloc, calloc, realloc, free, msg)) ;
72 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
73 1 : OK (LAGraph_Finalize (msg)) ;
74 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
75 1 : }
76 :
77 : //------------------------------------------------------------------------------
78 : // test_Xinit_brutal: test LAGr_Init with brutal memory debug
79 : //------------------------------------------------------------------------------
80 :
81 : #if LAGRAPH_SUITESPARSE
82 1 : void test_Xinit_brutal (void)
83 : {
84 : // no brutal memory failures, but test LG_brutal_malloc/calloc/realloc/free
85 1 : LG_brutal = -1 ;
86 1 : LG_nmalloc = 0 ;
87 1 : OK (LAGr_Init (GrB_NONBLOCKING,
88 : LG_brutal_malloc, LG_brutal_calloc, LG_brutal_realloc, LG_brutal_free,
89 : msg)) ;
90 :
91 1 : int32_t *p = LG_brutal_malloc (42 * sizeof (int32_t)) ;
92 1 : TEST_CHECK (p != NULL) ;
93 1 : LG_brutal_free (p) ;
94 1 : p = LG_brutal_calloc (42, sizeof (int32_t)) ;
95 43 : for (int k = 0 ; k < 42 ; k++)
96 : {
97 42 : TEST_CHECK (p [k] == 0) ;
98 : }
99 1 : p = LG_brutal_realloc (p, 99 * sizeof (int32_t)) ;
100 43 : for (int k = 0 ; k < 42 ; k++)
101 : {
102 42 : TEST_CHECK (p [k] == 0) ;
103 : }
104 1 : LG_brutal_free (p) ;
105 1 : p = LG_brutal_realloc (NULL, 4 * sizeof (int32_t)) ;
106 5 : for (int k = 0 ; k < 4 ; k++)
107 : {
108 4 : p [k] = k ;
109 : }
110 1 : LG_brutal_free (p) ;
111 :
112 1 : OK (LAGraph_Finalize (msg)) ;
113 1 : TEST_CHECK (LG_nmalloc == 0) ;
114 :
115 : // brutal tests: keep giving the method more malloc's until it succeeds
116 :
117 13 : for (int nbrutal = 0 ; nbrutal < 1000 ; nbrutal++)
118 : {
119 13 : LG_brutal = nbrutal ;
120 13 : GB_Global_GrB_init_called_set (false) ;
121 13 : GrB_Info info = GxB_init (GrB_NONBLOCKING, LG_brutal_malloc,
122 : LG_brutal_calloc, LG_brutal_realloc, LG_brutal_free) ;
123 13 : void *p = NULL, *pnew = NULL ;
124 13 : bool ok = false ;
125 13 : if (info == GrB_SUCCESS)
126 : {
127 3 : p = LG_brutal_realloc (NULL, 42) ;
128 3 : pnew = NULL ;
129 3 : ok = (p != NULL) ;
130 3 : if (ok)
131 : {
132 2 : pnew = LG_brutal_realloc (p, 107) ;
133 2 : ok = (pnew != NULL) ;
134 2 : LG_brutal_free (ok ? pnew : p) ;
135 : }
136 : }
137 13 : if (ok)
138 : {
139 1 : OK (GrB_finalize ( )) ;
140 1 : printf ("\nGxB_init, finally: %d %g\n", nbrutal,
141 : (double) LG_nmalloc) ;
142 1 : TEST_CHECK (LG_nmalloc == 0) ;
143 1 : break ;
144 : }
145 : }
146 :
147 1 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == true) ;
148 :
149 52 : for (int nbrutal = 0 ; nbrutal < 1000 ; nbrutal++)
150 : {
151 52 : LG_brutal = nbrutal ;
152 : // reset both GraphBLAS and LAGraph
153 52 : GB_Global_GrB_init_called_set (false) ;
154 52 : LG_set_LAGr_Init_has_been_called (false) ;
155 52 : TEST_CHECK (LG_get_LAGr_Init_has_been_called ( ) == false) ;
156 : // try to initialize GraphBLAS and LAGraph
157 52 : int result = LAGr_Init (GrB_NONBLOCKING,
158 : LG_brutal_malloc, LG_brutal_calloc,
159 : LG_brutal_realloc, LG_brutal_free, msg) ;
160 52 : if (result == 0)
161 : {
162 : // success
163 1 : OK (LAGraph_Finalize (msg)) ;
164 1 : printf ("LAGr_Init: finally: %d %g\n", nbrutal,
165 : (double) LG_nmalloc) ;
166 1 : TEST_CHECK (LG_nmalloc == 0) ;
167 1 : break ;
168 : }
169 : // failure: free anything partially allocated
170 51 : OK (LAGraph_Finalize (msg)) ;
171 : }
172 1 : }
173 : #endif
174 :
175 : //-----------------------------------------------------------------------------
176 : // TEST_LIST: the list of tasks for this entire test
177 : //-----------------------------------------------------------------------------
178 :
179 : TEST_LIST =
180 : {
181 : { "Xinit", test_Xinit },
182 : #if LAGRAPH_SUITESPARSE
183 : { "Xinit_brutal", test_Xinit_brutal },
184 : #endif
185 : { NULL, NULL }
186 : } ;
|