GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: Testing/nrrd/trand.c Lines: 51 71 71.8 %
Date: 2017-05-26 Branches: 20 32 62.5 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 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
#include "teem/nrrd.h"
25
#include <testDataPath.h>
26
27
/*
28
** Tests:
29
** airSrandMT
30
** airNormalRand
31
** nrrdNew
32
** nrrdAlloc_va
33
** nrrdHisto
34
** nrrdHistoDraw
35
** nrrdSave (to .pgm file)
36
** nrrdNuke
37
*/
38
39
#define BINS 1000
40
#define HGHT 1000
41
42
int
43
main(int argc, const char *argv[]) {
44
  const char *me;
45
  size_t vi, ii, qvalLen;
46
2
  Nrrd *nval, *nhist, *nimg, *nread, *ncorr, *ninmem[3];
47
1
  double aa, bb, *val;
48
  airArray *mop;
49
1
  char *name, explain[AIR_STRLEN_LARGE];
50
#define VALS 0
51
#define HIST 1
52
#define IMAG 2
53
  /* PGM image since this Teem build might not support PNG */
54
  static const char * const mine[3] = { "vals.nrrd",
55
                    "histo.nrrd",
56
                    "histo.pgm" };
57
  static const char * const corr[3] = { "test/trandvals.nrrd",
58
                    "test/trandhisto.nrrd",
59
                    "test/trandhisto.pgm"};
60
  static const char * const what[3] = { "value",
61
                    "histogram",
62
                    "histogram image" };
63
1
  int differ, wi;
64
65
  AIR_UNUSED(argc);
66
1
  me = argv[0];
67
1
  mop = airMopNew();
68
69
  qvalLen = 10*BINS;
70
1
  nrrdAlloc_va(nval=nrrdNew(), nrrdTypeDouble, 1, 4*qvalLen);
71
1
  airMopAdd(mop, nval, (airMopper)nrrdNuke, airMopAlways);
72
1
  val = AIR_CAST(double*, nval->data);
73
74
1
  nhist=nrrdNew();
75
1
  airMopAdd(mop, nhist, (airMopper)nrrdNuke, airMopAlways);
76
1
  nimg=nrrdNew();
77
1
  airMopAdd(mop, nimg, (airMopper)nrrdNuke, airMopAlways);
78
1
  nread = nrrdNew();
79
1
  airMopAdd(mop, nread, (airMopper)nrrdNuke, airMopAlways);
80
1
  ncorr = nrrdNew();
81
1
  airMopAdd(mop, ncorr, (airMopper)nrrdNuke, airMopAlways);
82
83
1
  airSrandMT(999);
84
  vi = 0;
85
  /* without first casting to float, the platform-dependent
86
     differences in the values from airNormalRand() would
87
     lead to testing errors, e.g.:
88
     correct (test/trandvals.nrrd) and generated values differ:
89
     valA[0]=0.36654774192269141 < valB[0]=0.36654774192269146 by 5.55112e-17
90
     Would be nice to figure out exactly what the origin of that is ... */
91
20002
  for (ii=0; ii<qvalLen; ii++) {
92
10000
    airNormalRand(&aa, NULL);
93
10000
    val[vi++] = AIR_CAST(float, aa);
94
  }
95
20002
  for (ii=0; ii<qvalLen; ii++) {
96
10000
    airNormalRand(NULL, &bb);
97
10000
    val[vi++] = AIR_CAST(float, bb);
98
  }
99
20002
  for (ii=0; ii<qvalLen; ii++) {
100
10000
    airNormalRand(&aa, &bb);
101
10000
    val[vi++] = AIR_CAST(float, aa);
102
10000
    val[vi++] = AIR_CAST(float, bb);
103
  }
104
105
2
  if (nrrdSave(mine[VALS], nval, NULL)
106
2
      || nrrdHisto(nhist, nval, NULL, NULL, BINS, nrrdTypeInt)
107
2
      || nrrdSave(mine[HIST], nhist, NULL)
108
2
      || nrrdHistoDraw(nimg, nhist, HGHT, AIR_TRUE, 0.0)
109
2
      || nrrdSave(mine[IMAG], nimg, NULL)) {
110
    char *err;
111
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
112
    fprintf(stderr, "%s: trouble:\n%s", me, err);
113
    airMopError(mop); return 1;
114
  }
115
116
1
  ninmem[VALS] = nval;
117
1
  ninmem[HIST] = nhist;
118
1
  ninmem[IMAG] = nimg;
119
8
  for (wi=0; wi<3; wi++) {
120
3
    name = testDataPathPrefix(corr[wi]);
121
3
    airMopAdd(mop, name, airFree, airMopAlways);
122
6
    if (nrrdLoad(ncorr, name, NULL)
123
6
        || nrrdLoad(nread, mine[wi], NULL)) {
124
      char *err;
125
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
126
      fprintf(stderr, "%s: trouble reading %s:\n%s", me, err, what[wi]);
127
      airMopError(mop); return 1;
128
    }
129

6
    if (nrrdCompare(ninmem[wi], nread, AIR_FALSE /* onlyData */,
130
3
                    0.0 /* epsilon */, &differ, explain)) {
131
      char *err;
132
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
133
      fprintf(stderr, "%s: trouble comparing in-mem and from-disk %s:\n%s",
134
              me, what[wi], err);
135
      airMopError(mop); return 1;
136
    }
137
3
    if (differ) {
138
      fprintf(stderr, "%s: in-mem and from-disk (%s) %ss differ: %s\n",
139
              me, mine[wi], what[wi], explain);
140
      airMopError(mop); return 1;
141
    } else {
142
3
      printf("%s: good: in-mem and from-disk %ss same\n", me, what[wi]);
143
    }
144
3
    if (nrrdCompare(ncorr, nread, AIR_FALSE /* onlyData */,
145
                    0.0 /* epsilon */, &differ, explain)) {
146
      char *err;
147
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
148
      fprintf(stderr, "%s: trouble comparing correct and generated %s:\n%s",
149
              me, what[wi], err);
150
      airMopError(mop); return 1;
151
    }
152
3
    if (differ) {
153
      fprintf(stderr, "%s: correct (%s) and generated %ss differ: %s\n",
154
              me, corr[wi], what[wi], explain);
155
      airMopError(mop); return 1;
156
    } else {
157
3
      printf("%s: good: correct and generated %ss same\n", me, what[wi]);
158
    }
159
  }
160
161
1
  airMopOkay(mop);
162
1
  return 0;
163
1
}