1 /* ========================================================================== */
2 /* === Tcov/null ============================================================ */
3 /* ========================================================================== */
4 
5 /* -----------------------------------------------------------------------------
6  * CHOLMOD/Tcov Module.  Copyright (C) 2005-2006, Timothy A. Davis
7  * http://www.suitesparse.com
8  * -------------------------------------------------------------------------- */
9 
10 /* Test CHOLMOD with NULL pointers, and other error cases. */
11 
12 #include "cm.h"
13 
14 
15 /* ========================================================================== */
16 /* === my_hander2 =========================================================== */
17 /* ========================================================================== */
18 
my_handler2(int status,const char * file,int line,const char * msg)19 void my_handler2 (int status, const char *file, int line, const char *msg)
20 {
21     printf ("This ERROR is expected: file %s line %d\n%d: %s\n",
22 	    file, line, status, msg) ;
23 }
24 
25 
26 /* ========================================================================== */
27 /* === null_test ============================================================ */
28 /* ========================================================================== */
29 
30 /* This routine is not called during memory testing */
31 
null_test(cholmod_common * cn)32 void null_test (cholmod_common *cn)
33 {
34     cholmod_sparse *A = NULL, *F = NULL, *C = NULL, *R = NULL, *B = NULL ;
35     cholmod_factor *L = NULL ;
36     cholmod_triplet *T = NULL ;
37     cholmod_dense *X = NULL, *DeltaB = NULL, *S = NULL, *Y = NULL, *E = NULL ;
38     void *p = NULL, *ii = NULL, *jj = NULL, *xx = NULL, *zz = NULL ;
39     Int *Perm = NULL, *fset = NULL, *Parent = NULL, *Post = NULL,
40 	*RowCount = NULL, *ColCount = NULL, *First = NULL, *Level = NULL,
41 	*UserPerm = NULL, *colmark = NULL, *Constraints = NULL,
42 	*r = NULL, *c = NULL, *Set = NULL ;
43     char *name = NULL ;
44     double alpha [2], beta [2], bk [2], yk [2], rcond ;
45     double dj = 1, nm = 0, tol = 0 ;
46     int ok, stype = 0, xtype = 0, sorted = 0, packed = 0, nint = 0, update = 0,
47 	postorder = 0, pack = 0, values = 0, mode = 0, sys = 0, norm = 0,
48 	to_xtype = 0, to_ll = 0, to_super = 0, to_packed = 0, to_monotonic = 0,
49 	scale = 0, transpose = 0, option = 0, ordering = 0, prefer = 0,
50 	mtype = 0, asym = 0 ;
51     SuiteSparse_long lr = 0, k1 = 0, k2 = 0 ;
52     size_t j = 0, need = 0, n = 0, mr = 0, nrow = 0, ncol = 0, iworksize = 0,
53 	newsize = 0, fsize = 0, d = 0, nzmax = 0, nnew = 0, size = 0,
54 	nold = 0, xwork = 0, kstart = 0, kend = 0, nr = 0, nc = 0, len = 0,
55 	krow = 0, k = 0 ;
56 
57 #ifndef NPARTITION
58     Int *Anw = NULL, *Aew = NULL, *Partition = NULL, *CParent = NULL,
59         *Cmember = NULL ;
60     Int compress = 0 ;
61 #endif
62 
63 #ifndef NCAMD
64     Int *Cmem2 = NULL ;
65 #endif
66 
67     /* ---------------------------------------------------------------------- */
68     /* Core */
69     /* ---------------------------------------------------------------------- */
70 
71     if (cn == NULL)
72     {
73 	ok = CHOLMOD(start)(cn) ;				NOT (ok) ;
74     }
75     ok = CHOLMOD(finish)(cn) ;					NOT (ok) ;
76     ok = CHOLMOD(defaults)(cn) ;				NOT (ok) ;
77     mr = CHOLMOD(maxrank)(n, cn) ;				NOT (mr>0) ;
78     ok = CHOLMOD(allocate_work)(nrow, iworksize, xwork, cn) ;	NOT (ok) ;
79     ok = CHOLMOD(free_work)(cn) ;				NOT (ok) ;
80     lr = CHOLMOD(clear_flag)(cn) ;				NOT (lr>=0) ;
81 
82     dj = CHOLMOD(dbound)(dj, cn) ;				OK (dj==0) ;
83     ok = CHOLMOD(error)(CHOLMOD_INVALID, __FILE__, __LINE__, "oops", cn) ;
84 								NOT (ok) ;
85     A = CHOLMOD(allocate_sparse)(nrow, ncol, nzmax, sorted,
86 	packed, stype, xtype, cn) ;				NOP (A) ;
87     ok = CHOLMOD(free_sparse)(&A, cn) ;				NOT (ok) ;
88     ok = CHOLMOD(reallocate_sparse)(newsize, A, cn) ;		NOT (ok) ;
89     lr = CHOLMOD(nnz)(A, cn) ;					NOT (lr>=0) ;
90     A  = CHOLMOD(speye)(nrow, ncol, xtype, cn) ;		NOP (A) ;
91     A  = CHOLMOD(spzeros)(nrow, ncol, 0, xtype, cn) ;		NOP (A) ;
92     A  = CHOLMOD(ptranspose)(A, values, Perm, fset, fsize, cn);	NOP (A) ;
93     A  = CHOLMOD(transpose)(A, values, cn) ;			NOP (A) ;
94     ok = CHOLMOD(transpose_unsym)(A, values, Perm, fset, fsize, F, cn) ;
95     NOT (ok) ;
96     ok = CHOLMOD(transpose_sym)(A, values, Perm, F, cn) ;	NOT (ok) ;
97     ok = CHOLMOD(sort)(A, cn) ;					NOT (ok) ;
98     A  = CHOLMOD(copy_sparse)(A, cn) ;				NOP (A) ;
99     C  = CHOLMOD(aat)(A, fset, fsize, mode, cn) ;		NOP (C) ;
100 
101     L  = CHOLMOD(allocate_factor)(n, cn) ;			NOP (L) ;
102     ok = CHOLMOD(free_factor)(&L, cn) ;				NOT (ok) ;
103     ok = CHOLMOD(reallocate_factor)(newsize, L, cn) ;		NOT (ok) ;
104     ok = CHOLMOD(change_factor)(0, 0, 0, 0, 0, L, cn) ;		NOT (ok) ;
105     ok = CHOLMOD(pack_factor)(L, cn) ;				NOT (ok) ;
106     ok = CHOLMOD(change_factor)(to_xtype, to_ll, to_super,
107 	to_packed, to_monotonic, L, cn) ;			NOT (ok) ;
108     ok = CHOLMOD(reallocate_column)(j, need, L, cn) ;		NOT (ok) ;
109     A  = CHOLMOD(factor_to_sparse)(L, cn) ;			NOP (A) ;
110     L  = CHOLMOD(copy_factor)(L, cn) ;				NOP (L) ;
111 
112     X  = CHOLMOD(allocate_dense)(nrow, ncol, d, xtype, cn) ;	NOP (X) ;
113     X  = CHOLMOD(zeros)(nrow, ncol, xtype, cn) ;		NOP (X) ;
114     X  = CHOLMOD(ones)(nrow, ncol, xtype, cn) ;			NOP (X) ;
115     X  = CHOLMOD(eye)(nrow, ncol, xtype, cn) ;			NOP (X) ;
116     ok = CHOLMOD(free_dense)(&X, cn) ;				NOT (ok) ;
117     X  = CHOLMOD(sparse_to_dense)(A, cn) ;			NOP (X) ;
118     A  = CHOLMOD(dense_to_sparse)(X, values, cn) ;		NOP (A) ;
119     Y  = CHOLMOD(copy_dense)(X, cn) ;				NOP (X) ;
120     ok = CHOLMOD(copy_dense2)(X, Y, cn) ;			NOT (ok) ;
121 
122     T  = CHOLMOD(allocate_triplet)(nrow, ncol, nzmax,
123 	    stype, xtype, cn) ;					NOP (T) ;
124     ok = CHOLMOD(free_triplet)(&T, cn) ;			NOT (ok) ;
125     T  = CHOLMOD(sparse_to_triplet)(A, cn) ;			NOP (T) ;
126     A  = CHOLMOD(triplet_to_sparse)(T, 0, cn) ;			NOP (A) ;
127     T  = CHOLMOD(copy_triplet)(T, cn) ;				NOP (T) ;
128     ok = CHOLMOD(reallocate_triplet)(nzmax, T, cn) ;		NOT (ok) ;
129 
130     lr = CHOLMOD(postorder)(Parent, nrow, NULL, Post, cn) ;	NOT (lr>=0) ;
131     p  = CHOLMOD(malloc)(n, size, cn) ;				NOP (p) ;
132     p  = CHOLMOD(calloc)(n, size, cn) ;				NOP (p) ;
133     p  = CHOLMOD(free)(n, size, p, cn) ;			NOP (p) ;
134     p  = CHOLMOD(realloc)(nnew, size, p, &n, cn) ;		NOP (p) ;
135     ok = CHOLMOD(realloc_multiple)(nnew, nint, xtype,
136 	    &ii, &jj, &xx, &zz, &nold, cn) ;			NOT (ok) ;
137 
138     C = CHOLMOD(band)(A, k1, k2, mode, cn) ;			NOP (C) ;
139     ok = CHOLMOD(band_inplace)(k1, k2, mode, A, cn) ;		NOT (ok) ;
140 
141     ok = CHOLMOD(factor_xtype)(CHOLMOD_REAL, L, cn) ;		NOT (ok) ;
142     ok = CHOLMOD(sparse_xtype)(CHOLMOD_REAL, A, cn) ;		NOT (ok) ;
143     ok = CHOLMOD(dense_xtype)(CHOLMOD_REAL, X, cn) ;		NOT (ok) ;
144     ok = CHOLMOD(triplet_xtype)(CHOLMOD_REAL, T, cn) ;		NOT (ok) ;
145 
146     /* ---------------------------------------------------------------------- */
147     /* Cholesky */
148     /* ---------------------------------------------------------------------- */
149 
150     L  = CHOLMOD(analyze)(A, cn) ;				NOP (L) ;
151     L  = CHOLMOD(analyze_p)(A, UserPerm, fset, fsize, cn) ;	NOP (L) ;
152     ok = CHOLMOD(factorize)(A, L, cn) ;				NOT (ok) ;
153     ok = CHOLMOD(factorize_p)(A, beta, fset, fsize, L, cn) ;	NOT (ok) ;
154     rcond = CHOLMOD(rcond)(L, cn) ;				NOT (rcond>=0) ;
155     X = CHOLMOD(solve)(sys, L, Y, cn) ;				NOP (X) ;
156     C = CHOLMOD(spsolve)(sys, L, B, cn) ;			NOP (C) ;
157     ok = CHOLMOD(etree)(A, Parent, cn) ;			NOT (ok) ;
158     ok = CHOLMOD(rowcolcounts)(A, fset, fsize, Parent, Post,
159 	RowCount, ColCount, First, Level, cn) ;			NOT (ok) ;
160     ok = CHOLMOD(amd)(A, fset, fsize, Perm, cn) ;		NOT (ok) ;
161     ok = CHOLMOD(camd)(A, fset, fsize, Constraints, Perm, cn) ;	NOT (ok) ;
162     ok = CHOLMOD(colamd)(A, fset, fsize, postorder, Perm, cn) ;	NOT (ok) ;
163     ok = CHOLMOD(rowfac)(A, F, beta, kstart, kend, L, cn) ;	NOT (ok) ;
164     ok = CHOLMOD(row_subtree)(A, F, krow, Parent, R, cn) ;	NOT (ok) ;
165     ok = CHOLMOD(row_lsubtree)(A, c, 0, krow, L, R, cn) ;	NOT (ok) ;
166     ok = CHOLMOD(resymbol)(A, fset, fsize, pack, L, cn) ;	NOT (ok) ;
167     ok = CHOLMOD(resymbol_noperm)(A, fset, fsize, pack, L, cn) ;NOT (ok) ;
168     ok = CHOLMOD(analyze_ordering)(A, ordering, Perm, fset,
169 	fsize, Parent, Post, ColCount, First, Level, cn) ;	NOT (ok) ;
170 
171     /* ---------------------------------------------------------------------- */
172     /* Modify */
173     /* ---------------------------------------------------------------------- */
174 
175     ok = CHOLMOD(updown)(update, C, L, cn) ;			NOT (ok) ;
176     ok = CHOLMOD(updown_solve)(update, C, L, X, DeltaB, cn) ;	NOT (ok) ;
177     ok = CHOLMOD(updown_mark)(update, C, colmark, L, X, DeltaB,
178 	    cn) ;					NOT (ok) ;
179     ok = CHOLMOD(rowadd)(k, R, L, cn) ;				NOT (ok) ;
180     ok = CHOLMOD(rowadd_solve)(k, R, bk, L, X, DeltaB, cn) ;	NOT (ok) ;
181     ok = CHOLMOD(rowadd_mark)(k, R, bk, colmark, L, X, DeltaB,
182 	    cn) ;					NOT (ok) ;
183     ok = CHOLMOD(rowdel)(k, R, L, cn) ;				NOT (ok) ;
184     ok = CHOLMOD(rowdel_solve)(k, R, yk, L, X, DeltaB, cn) ;	NOT (ok) ;
185     ok = CHOLMOD(rowdel_mark)(k, R, yk, colmark, L, X, DeltaB,
186 	    cn) ;					NOT (ok) ;
187 
188     /* ---------------------------------------------------------------------- */
189     /* MatrixOps */
190     /* ---------------------------------------------------------------------- */
191 
192     C = CHOLMOD(add)(A, B, alpha, beta, values, sorted, cn) ;	NOP (C) ;
193 
194     C = CHOLMOD(copy)(A, stype, mode, cn) ;			NOP (C) ;
195     ok = CHOLMOD(drop)(tol, A, cn) ;				NOT (ok) ;
196     nm = CHOLMOD(norm_dense)(X, norm, cn) ;			NOT (nm>=0) ;
197     nm = CHOLMOD(norm_sparse)(A, norm, cn) ;			NOT (nm>=0) ;
198     C = CHOLMOD(horzcat)(A, B, values, cn) ;			NOP (C) ;
199     ok = CHOLMOD(scale)(S, scale, A, cn) ;			NOT (ok) ;
200     ok = CHOLMOD(sdmult)(A, transpose, alpha, beta, X, Y, cn) ;	NOT (ok) ;
201     C = CHOLMOD(ssmult)(A, B, stype, values, sorted, cn) ;	NOP (C) ;
202     C = CHOLMOD(submatrix)(A, r, nr, c, nc, values, sorted,
203 	    cn) ;						NOP (C) ;
204     C = CHOLMOD(vertcat)(A, B, values, cn) ;			NOP (C) ;
205     asym = CHOLMOD(symmetry)(A, option, NULL, NULL, NULL, NULL,
206 	    cn) ;						NOT(asym>=0) ;
207 
208     /* ---------------------------------------------------------------------- */
209     /* Supernodal */
210     /* ---------------------------------------------------------------------- */
211 
212     ok = CHOLMOD(super_symbolic)(A, F, Parent, L, cn) ;	NOT (ok) ;
213     ok = CHOLMOD(super_numeric)(A, F, beta, L, cn) ;		NOT (ok) ;
214     ok = CHOLMOD(super_lsolve)(L, X, E, cn) ;			NOT (ok) ;
215     ok = CHOLMOD(super_ltsolve)(L, X, E, cn) ;			NOT (ok) ;
216 
217     /* ---------------------------------------------------------------------- */
218     /* Check */
219     /* ---------------------------------------------------------------------- */
220 
221     ok = CHOLMOD(check_common)(cn) ;				NOT (ok) ;
222     ok = CHOLMOD(print_common)(name, cn) ;			NOT (ok) ;
223 
224     ok = CHOLMOD(check_sparse)(A, cn) ;				NOT (ok) ;
225     ok = CHOLMOD(print_sparse)(A, name, cn) ;			NOT (ok) ;
226     ok = CHOLMOD(check_dense)(X, cn) ;				NOT (ok) ;
227     ok = CHOLMOD(print_dense)(X, name, cn) ;			NOT (ok) ;
228     ok = CHOLMOD(check_factor)(L, cn) ;				NOT (ok) ;
229     ok = CHOLMOD(print_factor)(L, name, cn) ;			NOT (ok) ;
230     ok = CHOLMOD(check_triplet)(T, cn) ;			NOT (ok) ;
231     ok = CHOLMOD(print_triplet)(T, name, cn) ;			NOT (ok) ;
232     ok = CHOLMOD(check_subset)(Set, len, n, cn) ;		NOT (ok) ;
233     ok = CHOLMOD(print_subset)(Set, len, n, name, cn) ;		NOT (ok) ;
234     ok = CHOLMOD(check_perm)(Perm, n, n, cn) ;			NOT (ok) ;
235     ok = CHOLMOD(print_perm)(Perm, n, n, name, cn) ;		NOT (ok) ;
236     ok = CHOLMOD(check_parent)(Parent, n, cn) ;			NOT (ok) ;
237     ok = CHOLMOD(print_parent)(Parent, n, name, cn) ;		NOT (ok) ;
238 
239     A = CHOLMOD(read_sparse)(NULL, cn) ;			NOP (A) ;
240     p = CHOLMOD(read_matrix)(NULL, prefer, &mtype, cn) ;	NOP (p) ;
241     X = CHOLMOD(read_dense)(NULL, cn) ;				NOP (X) ;
242     T = CHOLMOD(read_triplet)(NULL, cn) ;			NOP (T) ;
243 
244     asym = CHOLMOD(write_dense) (NULL, NULL, NULL, cn) ;	NOT (asym>=0) ;
245     asym = CHOLMOD(write_dense) ((FILE *) 1, NULL, NULL, cn) ;	NOT (asym>=0) ;
246 
247     asym = CHOLMOD(write_sparse)(NULL, NULL, NULL, NULL, cn) ;	NOT (asym>=0) ;
248     asym = CHOLMOD(write_sparse)((FILE *) 1, NULL, NULL, NULL,
249 	    cn) ;						NOT (asym>=0) ;
250 
251     /* ---------------------------------------------------------------------- */
252     /* Partition */
253     /* ---------------------------------------------------------------------- */
254 
255 #ifndef NPARTITION
256     lr = CHOLMOD(nested_dissection)(A, fset, fsize, Perm,
257 	    CParent, Cmember, cn) ;				NOT (lr >= 0) ;
258     lr = CHOLMOD(collapse_septree) (n, n, 1., 4,
259 	    CParent, Cmember, cn) ;				NOT (lr >= 0) ;
260     ok = CHOLMOD(metis)(A, fset, fsize, postorder, Perm, cn) ;	NOT (ok) ;
261     lr = CHOLMOD(bisect)(A, fset, fsize, compress,
262 	    Partition, cn) ;					NOT (lr >= 0) ;
263     lr = CHOLMOD(metis_bisector)(A, Anw, Aew, Partition, cn) ;	NOT (lr >= 0) ;
264 #endif
265 
266 #ifndef NCAMD
267     ok = CHOLMOD(ccolamd)(A, fset, fsize, Cmem2, Perm, cn) ;	NOT (ok) ;
268     ok = CHOLMOD(csymamd)(A, Cmem2, Perm, cn) ;		        NOT (ok) ;
269 #endif
270 
271 }
272 
273 /* ========================================================================== */
274 /* === null_test2 =========================================================== */
275 /* ========================================================================== */
276 
null_test2(void)277 void null_test2 (void)
278 {
279     cholmod_dense *X, *Xbad = NULL ;
280     cholmod_sparse *Sbad = NULL, *A ;
281     int ok ;
282 
283     /* ---------------------------------------------------------------------- */
284     /* Test Core Common */
285     /* ---------------------------------------------------------------------- */
286 
287     ok = CHOLMOD(allocate_work)(Size_max, 1, 1, cm) ;		NOT (ok) ;
288     ok = CHOLMOD(allocate_work)(1, Size_max, 1, cm) ;		NOT (ok) ;
289     ok = CHOLMOD(allocate_work)(1, 1, Size_max, cm) ;		NOT (ok) ;
290 
291     /* free a NULL pointer */
292     CHOLMOD(free)(42, sizeof (char), NULL, cm) ;
293     cm->print = 5 ; CHOLMOD(print_common)("cm", cm) ; cm->print = 3 ;
294 
295     cm->maxrank = 3 ;
296     cm->maxrank = CHOLMOD(maxrank)(5, cm) ; OK (cm->maxrank == 4) ;
297     cm->maxrank = 1 ;
298     cm->maxrank = CHOLMOD(maxrank)(5, cm) ; OK (cm->maxrank == 2) ;
299     cm->maxrank = 8 ;
300 
301     /* test the error handler */
302     cm->error_handler = my_handler2 ;
303     CHOLMOD(drop)(0., NULL, cm) ;
304     cm->error_handler = NULL ;
305 
306     /* ---------------------------------------------------------------------- */
307     /* dense */
308     /* ---------------------------------------------------------------------- */
309 
310     X = CHOLMOD(allocate_dense)(5, 4, 1, CHOLMOD_REAL, cm) ;	    NOP (X) ;
311     X = CHOLMOD(allocate_dense)(1, Int_max, 1, CHOLMOD_REAL, cm) ;  NOP (X) ;
312     X = CHOLMOD(allocate_dense)(1, 1, 1, -1, cm) ;		    NOP (X) ;
313     CHOLMOD(free_dense)(&X, cm) ;
314 
315     /* free a NULL dense matrix */
316     ok = CHOLMOD(free_dense)(&X, cm) ;				    OK (ok) ;
317     ok = CHOLMOD(free_dense)(NULL, cm) ;			    OK (ok) ;
318 
319     /* make an invalid sparse matrix */
320     Sbad = CHOLMOD(speye)(2, 3, CHOLMOD_REAL, cm) ;		    OKP (Sbad) ;
321     Sbad->stype = 1 ;
322     ok = CHOLMOD(check_sparse)(Sbad, cm) ;			    NOT (ok) ;
323     X = CHOLMOD(sparse_to_dense)(Sbad, cm) ;			    NOP (X) ;
324     ok = CHOLMOD(free_sparse)(&Sbad, cm) ;			    OK (ok) ;
325 
326     /* make an invalid dense matrix */
327     Xbad = CHOLMOD(eye)(4, 4, CHOLMOD_REAL, cm) ;		    OKP (Xbad) ;
328     Xbad->d = 1 ;
329     ok = CHOLMOD(check_dense)(Xbad, cm) ;			    NOT (ok) ;
330     A = CHOLMOD(dense_to_sparse)(Xbad, TRUE, cm) ;
331     ok = CHOLMOD(free_dense)(&Xbad, cm) ;			    OK (ok) ;
332     CHOLMOD(print_common)("cm", cm) ;
333     cm->print = 5 ; CHOLMOD(print_sparse)(A, "Bad A", cm) ; cm->print = 3 ;
334     NOP (A) ;
335 
336     /* ---------------------------------------------------------------------- */
337     /* sparse */
338     /* ---------------------------------------------------------------------- */
339 
340     /* free a NULL sparse matrix */
341     ok = CHOLMOD(free_sparse)(&A, cm) ;				    OK (ok) ;
342     ok = CHOLMOD(free_sparse)(NULL, cm) ;			    OK (ok) ;
343     A = CHOLMOD(copy_sparse)(NULL, cm) ;			    NOP (A) ;
344 
345     /* ---------------------------------------------------------------------- */
346     /* error tests done */
347     /* ---------------------------------------------------------------------- */
348 
349     printf ("------------------ null tests done\n") ;
350 }
351