1 /* ========================================================================== */
2 /* === RBio/Tcov/RBtest.c: C test =========================================== */
3 /* ========================================================================== */
4 
5 /* Copyright 2009-2013, Timothy A. Davis, All Rights Reserved.
6    Refer to RBio/Doc/license.txt for the RBio license. */
7 
8 #include "RBio.h"
9 #define Long SuiteSparse_long
10 #define ID "%ld"
11 
12 #define SLEN 4096
13 
main(int argc,char ** argv)14 int main (int argc, char **argv)
15 {
16     double xr, xz, xmin, xmax ;
17     double *Ax, *Az ;
18     Long nrow, ncol, mkind, skind, *Ap, *Ai, i, *Zp, *Zi, asize, mkind2, skind2,
19         znz, j, p, status, njumbled, nzeros, build_upper, zero_handling, fem,
20         xsize, nelnz, nnz, kk, anz ;
21     int ok ;
22     char title [73], key [9], mtype [4], mtype2 [4], *filename, s [100], *As ;
23 
24     /* initialize the memory allocation functions */
25     SuiteSparse_start ( ) ;
26 
27     /* test arg-handling for RB functions */
28 
29     status = RBread (NULL, 0, 0, NULL, NULL, NULL,
30         NULL, NULL, NULL, NULL, NULL, NULL,
31         NULL, NULL, NULL, NULL, NULL, NULL) ;
32     if (status != RBIO_ARG_ERROR)
33     {
34         printf ("RBtest failure (1)!\n") ;
35         return (1) ;
36     }
37 
38     status = RBreadraw (NULL, NULL, NULL, NULL, NULL, NULL, NULL,
39         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ;
40     if (status != RBIO_ARG_ERROR)
41     {
42         printf ("RBtest failure (2)\n") ;
43         return (1) ;
44     }
45 
46     status = RBread ("no file", build_upper, zero_handling, title, key, mtype,
47         &nrow, &ncol, &mkind, &skind, &asize, &znz,
48         &Ap, &Ai, &Ax, &Az, &Zp, &Zi) ;
49     if (status != RBIO_FILE_IOERROR)
50     {
51         printf ("RBtest failure (3)\n") ;
52         return (1) ;
53     }
54 
55     status = RBreadraw ("no file", title, key, mtype, &nrow, &ncol, &nnz,
56         &nelnz, &mkind, &skind, &fem, &xsize, &Ap, &Ai, &Ax) ;
57     if (status != RBIO_FILE_IOERROR)
58     {
59         printf ("RBtest failure (4)\n") ;
60         return (1) ;
61     }
62 
63     status = RBwrite (NULL, NULL, NULL, 1, 1, NULL, NULL, NULL, NULL,
64         NULL, NULL, 0, NULL) ;
65     if (status != RBIO_ARG_ERROR)
66     {
67         printf ("RBtest failure (5)\n") ;
68         return (1) ;
69     }
70 
71     status = RBkind (1, 1, NULL, NULL, NULL, NULL, 0, NULL, NULL,
72         NULL, NULL, NULL, NULL) ;
73     if (status != RBIO_ARG_ERROR)
74     {
75         printf ("RBtest failure (6)\n") ;
76         return (1) ;
77     }
78 
79     status = RBread ("matrices/m4.rb", 1, 2, title, key, mtype,
80         &nrow, &ncol, &mkind, &skind, &asize, &znz,
81         &Ap, &Ai, &Ax, NULL, &Zp, &Zi) ;
82     if (status != 0)
83     {
84         SuiteSparse_free (Ap) ;
85         SuiteSparse_free (Ai) ;
86         SuiteSparse_free (Ax) ;
87         SuiteSparse_free (Zp) ;
88         SuiteSparse_free (Zi) ;
89         printf ("RBread test failure (7) "ID"\n", status) ;
90         return (1) ;
91     }
92 
93     /* mangle the matrix */
94     Ap [0] = 1 ;
95     status = RBwrite ("temp.rb", title, key, nrow, ncol, Ap, Ai, Ax,
96         NULL, Zp, Zi, mkind, mtype2) ;
97     if (status == RBIO_OK)
98     {
99         printf ("RBtest failure (8)\n") ;
100         return (1) ;
101     }
102 
103     Ap [0] = 0 ;
104     Zp [0] = 1 ;
105     status = RBwrite ("temp.rb", title, key, nrow, ncol, Ap, Ai, Ax,
106         NULL, Zp, Zi, mkind, mtype2) ;
107     if (status == RBIO_OK)
108     {
109         printf ("RBtest failure (9)\n") ;
110         return (1) ;
111     }
112     Zp [0] = 0 ;
113 
114     /* valid matrix */
115     status = RBwrite ("temp.rb", title, key, nrow, ncol, Ap, Ai, Ax,
116         NULL, Zp, Zi, mkind, NULL) ;
117     if (status != RBIO_OK)
118     {
119         printf ("RBtest failure (10) "ID"\n", status) ;
120         return (1) ;
121     }
122 
123     /* valid matrix, different integer formats */
124     Ax [0] = 1e5 ;
125     for (kk = 0 ; kk < 10 ; kk++)
126     {
127         sprintf (s, "temp_" ID ".rb", kk) ;
128         status = RBwrite (s, title, key, nrow, ncol, Ap, Ai, Ax,
129             NULL, Zp, Zi, mkind, NULL) ;
130         if (status != RBIO_OK)
131         {
132             printf ("RBtest failure (11) "ID" "ID"\n", status, kk) ;
133             return (1) ;
134         }
135         Ax [0] *= 10 ;
136     }
137 
138     /* valid matrix, but with a very large integer */
139     Ax [0] = 1e9 + 1 ;
140     status = RBwrite ("temp1.rb", title, key, nrow, ncol, Ap, Ai, Ax,
141         NULL, Zp, Zi, mkind, NULL) ;
142     if (status != RBIO_OK)
143     {
144         printf ("RBtest failure (12) "ID"\n", status) ;
145         return (1) ;
146     }
147 
148     /* valid matrix, but with a very large real */
149     Ax [0] = 1e100 ;
150     status = RBwrite ("temp2.rb", title, key, nrow, ncol, Ap, Ai, Ax,
151         NULL, Zp, Zi, mkind, NULL) ;
152     if (status != RBIO_OK)
153     {
154         printf ("RBtest failure (13) "ID"\n", status) ;
155         return (1) ;
156     }
157 
158     /* valid matrix, but with a Z that overlaps A */
159     Ax [0] = 1 ;
160     Zi [0] = 1 ;
161     status = RBwrite ("temp3.rb", title, key, nrow, ncol, Ap, Ai, Ax,
162         NULL, Zp, Zi, mkind, NULL) ;
163     if (status != RBIO_OK)
164     {
165         printf ("RBtest failure (14) "ID"\n", status) ;
166         return (1) ;
167     }
168 
169     /* invalid file name */
170     status = RBwrite ("gunk/gunk.rb", title, key, nrow, ncol, Ap, Ai, Ax,
171         NULL, Zp, Zi, mkind, NULL) ;
172     if (status != RBIO_FILE_IOERROR)
173     {
174         printf ("RBtest failure (12) "ID"\n", status) ;
175         return (1) ;
176     }
177 
178     /* cannot write a matrix with no entries */
179     for (j = 0 ; j <= ncol ; j++)
180     {
181         Ap [j] = 0 ;
182         Zp [j] = 0 ;
183     }
184     status = RBwrite ("temp.rb", title, key, nrow, ncol, Ap, Ai, Ax,
185         NULL, Zp, Zi, mkind, NULL) ;
186     if (status != RBIO_DIM_INVALID)
187     {
188         printf ("RBtest failure (13) "ID"\n", status) ;
189         return (1) ;
190     }
191 
192     SuiteSparse_free (Ap) ;
193     SuiteSparse_free (Ai) ;
194     SuiteSparse_free (Ax) ;
195     SuiteSparse_free (Zp) ;
196     SuiteSparse_free (Zi) ;
197 
198     /* re-read a valid matrix */
199     status = RBread ("matrices/m4.rb", 1, 0, title, key, mtype,
200         &nrow, &ncol, &mkind, &skind, &asize, &znz,
201         &Ap, &Ai, &Ax, NULL, NULL, NULL) ;
202     if (status != 0)
203     {
204         SuiteSparse_free (Ap) ;
205         SuiteSparse_free (Ai) ;
206         SuiteSparse_free (Ax) ;
207         SuiteSparse_free (Zp) ;
208         SuiteSparse_free (Zi) ;
209         printf ("RBread test failure (14) "ID"\n", status) ;
210         return (1) ;
211     }
212 
213     status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
214         &njumbled, &nzeros) ;
215     if (status != RBIO_OK)
216     {
217         printf ("RBread test failure (15) "ID"\n", status) ;
218     }
219 
220     status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, -1,
221         &njumbled, &nzeros) ;
222     if (status != RBIO_MKIND_INVALID)
223     {
224         printf ("RBread test failure (16) "ID"\n", status) ;
225     }
226 
227     status = RBok (-1, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
228         &njumbled, &nzeros) ;
229     if (status != RBIO_DIM_INVALID)
230     {
231         printf ("RBread test failure (17) "ID"\n", status) ;
232     }
233 
234     i = Ap [1] ;
235     Ap [1] = 999999 ;
236     status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
237         &njumbled, &nzeros) ;
238     if (status != RBIO_CP_INVALID)
239     {
240         printf ("RBread test failure (18) "ID"\n", status) ;
241     }
242     Ap [1] = i ;
243 
244     status = RBok (nrow, ncol, asize, Ap, NULL, Ax, Az, NULL, mkind,
245         &njumbled, &nzeros) ;
246     if (status != RBIO_ROW_INVALID)
247     {
248         printf ("RBread test failure (19) "ID"\n", status) ;
249     }
250 
251     i = Ai [0] ;
252     Ai [0] = 999999 ;
253     status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
254         &njumbled, &nzeros) ;
255     if (status != RBIO_ROW_INVALID)
256     {
257         printf ("RBread test failure (20) "ID"\n", status) ;
258     }
259     Ai [0] = i ;
260 
261     i = Ai [1] ;
262     Ai [1] = Ai [0] ;
263     status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, NULL, mkind,
264         &njumbled, &nzeros) ;
265     if (status != RBIO_JUMBLED)
266     {
267         printf ("RBread test failure (21) "ID"\n", status) ;
268     }
269     Ai [1] = i ;
270 
271     ok = 1 ;
272     As = (char *) SuiteSparse_malloc (asize, sizeof (char)) ;
273     for (i = 0 ; i < asize ; i++) As [i] = 1 ;
274     status = RBok (nrow, ncol, asize, Ap, Ai, Ax, Az, As, 1,
275         &njumbled, &nzeros) ;
276     if (status != RBIO_OK)
277     {
278         printf ("RBread test failure (22) "ID"\n", status) ;
279     }
280     SuiteSparse_free (As) ;
281 
282     printf ("RBtest OK\n") ;
283     SuiteSparse_finish ( ) ;
284     return (0) ;
285 }
286