1 /*
2   Teem: Tools to process and visualize scientific data and images             .
3   Copyright (C) 2012, 2011, 2010, 2009  University of Chicago
4   Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5   Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6 
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public License
9   (LGPL) as published by the Free Software Foundation; either
10   version 2.1 of the License, or (at your option) any later version.
11   The terms of redistributing and/or modifying this software also
12   include exceptions to the LGPL that facilitate static linking.
13 
14   This library is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   Lesser General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public License
20   along with this library; if not, write to Free Software Foundation, Inc.,
21   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23 
24 
25 #include "teem/air.h"
26 
27 /*
28 ** to test:
29 
30 AIR_EXPORT FILE *airFopen(const char *name, FILE *std, const char *mode);
31 AIR_EXPORT FILE *airFclose(FILE *file);
32 AIR_EXPORT int airSinglePrintf(FILE *file, char *str, const char *fmt, ...);
33 AIR_EXPORT unsigned int airIndex(double min, double val, double max,
34                                  unsigned int N);
35 AIR_EXPORT unsigned int airIndexClamp(double min, double val, double max,
36                                       unsigned int N);
37 AIR_EXPORT airULLong airIndexULL(double min, double val, double max,
38                                  airULLong N);
39 AIR_EXPORT airULLong airIndexClampULL(double min, double val, double max,
40                                       airULLong N);
41 AIR_EXPORT char *airDoneStr(double start, double here, double end, char *str);
42 AIR_EXPORT void airBinaryPrintUInt(FILE *file, int digits, unsigned int N);
43 AIR_EXPORT int airILoad(void *v, int t);
44 AIR_EXPORT float airFLoad(void *v, int t);
45 AIR_EXPORT double airDLoad(void *v, int t);
46 AIR_EXPORT int airIStore(void *v, int t, int i);
47 AIR_EXPORT float airFStore(void *v, int t, float f);
48 AIR_EXPORT double airDStore(void *v, int t, double d);
49 AIR_EXPORT void airEqvAdd(airArray *eqvArr, unsigned int j, unsigned int k);
50 AIR_EXPORT unsigned int airEqvMap(airArray *eqvArr,
51                                   unsigned int *map, unsigned int len);
52 AIR_EXPORT unsigned int airEqvSettle(unsigned int *map, unsigned int len);
53 
54 */
55 
56 static size_t
multiply(size_t aa,size_t bb)57 multiply(size_t aa, size_t bb) {
58   return aa*bb;
59 }
60 
61 int
main(int argc,const char * argv[])62 main(int argc, const char *argv[]) {
63   const char *me;
64   void *ptr, *ptr2;
65 
66   AIR_UNUSED(argc);
67   me = argv[0];
68 
69   /* airNull */
70   {
71     ptr = airNull();
72     if (NULL != ptr) {
73       fprintf(stderr, "%s: airNull() returned %p not NULL\n", me, ptr);
74       exit(1);
75     }
76   }
77 
78   /* airSetNull */
79   {
80     ptr = AIR_CAST(void *, airNull);
81     if (NULL == ptr) {
82       fprintf(stderr, "%s: couldn't set a non-NULL pointer", me);
83       exit(1);
84     }
85     ptr2 = airSetNull(&ptr);
86     if (!(NULL == ptr && NULL == ptr2)) {
87       fprintf(stderr, "%s: airSetNull() didn't set (%p) or return (%p) NULL\n",
88               me, ptr, ptr2);
89       exit(1);
90     }
91   }
92 
93   /* AIR_CALLOC, airFree, airTime */
94   {
95     size_t big = 1024, times = 1, ii, jj;
96     double time0, dtime;
97     unsigned int *data, sum;
98     big = big*big*128; /* 128 megs */
99     time0 = airTime();
100     sum = 0;
101     for (ii=0; ii<times; ii++) {
102       /* will have a memory leak if airFree() didn't free() */
103       data = AIR_CALLOC(big, unsigned int);
104       for (jj=0; jj<big; jj++) {
105         sum += data[jj];
106       }
107       data = airFree(data);
108     }
109     if (sum) {
110       fprintf(stderr, "%s: AIR_CALLOC produced non-zero values\n", me);
111       exit(1);
112     }
113     if (!(NULL == data)) {
114       fprintf(stderr, "%s: airFree() returned %p not NULL\n",
115               me, AIR_VOIDP(data));
116       exit(1);
117     }
118     dtime = airTime() - time0;
119     if (!( dtime > 0 )) {
120       fprintf(stderr, "%s: airTime() => nonsense delta time %g\n", me, dtime);
121       exit(1);
122     }
123   }
124 
125   /* airStderr, airStdout, airStdin */
126   {
127     FILE *fret;
128     fret = airStderr();
129     if (stderr != fret) {
130       fprintf(stderr, "%s: airStderr() returned %p not stderr %p\n", me,
131               AIR_CAST(void *, fret), AIR_CAST(void *, stderr));
132       exit(1);
133     }
134     fret = airStdout();
135     if (stdout != fret) {
136       fprintf(stdout, "%s: airStdout() returned %p not stdout %p\n", me,
137               AIR_CAST(void *, fret), AIR_CAST(void *, stdout));
138       exit(1);
139     }
140     fret = airStdin();
141     if (stdin != fret) {
142       fprintf(stdin, "%s: airStdin() returned %p not stdin %p\n", me,
143               AIR_CAST(void *, fret), AIR_CAST(void *, stdin));
144       exit(1);
145     }
146   }
147 
148   /* airSprintSize_t, airSprintPtrdiff_t in pptest.c */
149 
150   /* airPrettySprintSize_t */
151   {
152     char prstmp[AIR_STRLEN_SMALL];
153     size_t vals[] = {0,                      /* 0 */
154                      800,                    /* 1 */
155                      1024,                   /* 2 = 2^10 */
156                      1024 + 1,               /* 3 */
157                      500*1024,               /* 4 */
158                      1024*1024,              /* 5 = 2^20 */
159                      1024*(1024 + 1),        /* 6 */
160                      500*1024*1024,          /* 7 */
161                      1024*1024*1024ul,       /* 8 = 2^30 */
162                      1024*1024*(1024ul + 1), /* 9 */
163                      500*1024ul,             /* 10 (will be multiplied below) */
164                      1024*1024ul,            /* 11 = 2^40 */
165                      1024*(1024ul + 1),      /* 12 */
166                      500*1024*1024,          /* 13 */
167                      1024*1024*1024ul,       /* 14 = 2^50 */
168                      1024*1024*(1024ul + 1), /* 15 */
169                      500*1024*1024,          /* 16 */
170                      1024*1024*1024ul,       /* 17 = 2^60 */
171                      1024*1024*(1024ul + 1), /* 18 */
172                      2*1024*1024ul,          /* 19 = 2^61 */
173                      16*1023*1024ul,         /* 20 */
174                      16*1024*1024ul,         /* 21 = 2^64 */
175                      0};
176     char *string[] = {
177       "0 bytes",    /* 0 */
178       "800 bytes",  /* 1 */
179       "1024 bytes", /* 2 */
180       "1.00098 KB", /* 3 */
181       "500 KB",     /* 4 */
182       "1024 KB",    /* 5 */
183       "1.00098 MB", /* 6 */
184       "500 MB",     /* 7 */
185       "1024 MB",    /* 8 */
186       "1.00098 GB", /* 9 */
187       "500 GB",     /* 10 */
188       "1024 GB",    /* 11 */
189       "1.00098 TB", /* 12 */
190       "500 TB",     /* 13 */
191       "1024 TB",    /* 14 */
192       "1.00098 PB", /* 15 */
193       "500 PB",     /* 16 */
194       "1024 PB",    /* 17 */
195       "1.00098 EB", /* 18 */
196       "2 EB",       /* 19 */
197       "15.9844 EB", /* 20 */
198       "blah",       /* 21 */
199     };
200     unsigned int ii;
201     /* hiding some multiplications in function calls,
202        to quiet compiler warnings.  Its ok if there is
203        wrap-around here because then we'll stop testing.
204        However, it would be better if we did NOT rely on
205        what is strictly speaking undefined behavior: the
206        overflow of *unsigned* integral quantities. */
207     vals[10] = multiply(multiply(vals[10], 1024), 1024);
208     vals[11] = multiply(multiply(vals[11], 1024), 1024);
209     vals[12] = multiply(multiply(vals[12], 1024), 1024);
210     vals[13] = multiply(multiply(vals[13], 1024), 1024);
211     vals[14] = multiply(multiply(vals[14], 1024), 1024);
212     vals[15] = multiply(multiply(vals[15], 1024), 1024);
213     vals[16] = multiply(multiply(multiply(vals[16], 1024), 1024), 1024);
214     vals[17] = multiply(multiply(multiply(vals[17], 1024), 1024), 1024);
215     vals[18] = multiply(multiply(multiply(vals[18], 1024), 1024), 1024);
216     vals[19] = multiply(multiply(multiply(multiply(vals[19], 1024), 1024), 1024), 1024);
217     vals[20] = multiply(multiply(multiply(multiply(vals[20], 1024), 1024), 1024), 1024);
218     vals[21] = multiply(multiply(multiply(multiply(vals[21], 1024), 1024), 1024), 1024);
219     for (ii=0; !ii || vals[ii] > vals[ii-1]; ii++) {
220       airPrettySprintSize_t(prstmp, vals[ii]);
221       if (strcmp(string[ii], prstmp)) {
222         fprintf(stderr, "%s: airPrettySprintSize_t made |%s| not |%s|\n",
223                 me, prstmp, string[ii]);
224         exit(1);
225       }
226       fprintf(stderr, "%u: %s\n", ii, prstmp);
227     }
228   }
229 
230 
231   exit(0);
232 }
233 
234 
235 
236