GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: Testing/nrrd/tbspec.c Lines: 59 75 78.7 %
Date: 2017-05-26 Branches: 9 16 56.2 %

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
26
/*
27
** Tests:
28
** nrrdBoundarySpecNew
29
** nrrdBoundarySpecNix
30
** nrrdBoundarySpecParse
31
** nrrdBoundarySpecSprint
32
** nrrdBoundarySpecCheck
33
** nrrdHestBoundarySpec
34
*/
35
36
void
37
checkFailOrDie(const NrrdBoundarySpec *bspec, airArray *mop) {
38
  static const char me[]="checkFailOrDie";
39
  char *err;
40
41
4
  if (!nrrdBoundarySpecCheck(bspec)) {
42
    /* what, no error? */
43
    fprintf(stderr, "%s: did not get expected check failure!\n", me);
44
    airMopError(mop);
45
    exit(1);
46
  }
47
  /* good, there's an error */
48
2
  err = biffGetDone(NRRD);
49
2
  airMopAdd(mop, err, airFree, airMopAlways);
50
2
  fprintf(stderr, "(expecting error): %s", err);
51
  return;
52
2
}
53
54
void
55
parseFailOrDie(NrrdBoundarySpec *bspec, const char *str, airArray *mop) {
56
  static const char me[]="parseFailOrDie";
57
  char *err;
58
59
10
  if (!nrrdBoundarySpecParse(bspec, str)) {
60
    /* what, no error? */
61
    fprintf(stderr, "%s: did not get expected parse fail on \"%s\"\n", me, str);
62
    airMopError(mop);
63
    exit(1);
64
  }
65
  /* good, there's an error */
66
5
  err = biffGetDone(NRRD);
67
5
  airMopAdd(mop, err, airFree, airMopAlways);
68
5
  fprintf(stderr, "(expecting error): %s", err);
69
  return;
70
5
}
71
72
/*
73
** yes, the equality check should really be on boundary specs,
74
** not on the strings, but that isn't implemented yet
75
*/
76
void
77
psLoopOrDie(NrrdBoundarySpec *bsp, const char *str,
78
                     airArray *mop) {
79
  static const char me[]="psLoopOrDie";
80
  char *err;
81
12
  char buff[AIR_STRLEN_LARGE];
82
83
12
  if (nrrdBoundarySpecParse(bsp, str)
84
12
      || nrrdBoundarySpecSprint(buff, bsp)) {
85
    err = biffGetDone(NRRD);
86
    airMopAdd(mop, err, airFree, airMopAlways);
87
    fprintf(stderr, "%s: error: %s", me, err);
88
    airMopError(mop);
89
    exit(1);
90
  }
91

12
  if (strcmp(str, buff)) {
92
6
    fprintf(stderr, "%s: parse->sprint->\"%s\" != given \"%s\"\n", me,
93
            buff, str);
94
    airMopError(mop);
95
    exit(1);
96
  }
97
6
  fprintf(stderr, "(looped okay): %s\n", str);
98
  return;
99
6
}
100
101
static const char *tbspecInfo =
102
  "for testing handling of boundary specifications";
103
104
int
105
main(int argc, const char *argv[]) {
106
  /* stock variables */
107
  const char *me;
108
2
  hestOpt *hopt=NULL;
109
  hestParm *hparm;
110
  airArray *mop;
111
  /* variables specific to this program */
112
  char *err;
113
1
  NrrdBoundarySpec *bspec, **bsv;
114
1
  unsigned int bsNum, bsIdx;
115
1
  char buff[AIR_STRLEN_LARGE];
116
117
1
  me = argv[0];
118
1
  mop = airMopNew();
119
1
  hparm = hestParmNew();
120
1
  airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways);
121
1
  hestOptAdd(&hopt, "bs", "bspec0", airTypeOther, 1, -1, &bsv, NULL,
122
1
             "bspecs", &bsNum, NULL, nrrdHestBoundarySpec);
123
1
  hestParseOrDie(hopt, argc-1, argv+1, hparm, me, tbspecInfo,
124
                 AIR_TRUE, AIR_TRUE, AIR_TRUE);
125
1
  airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways);
126
1
  airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways);
127
128
1
  printf("command line options:\n");
129
8
  for (bsIdx=0; bsIdx<bsNum; bsIdx++) {
130
3
    if (nrrdBoundarySpecSprint(buff, bsv[bsIdx])) {
131
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
132
      fprintf(stderr, "%s: problem with bsv[%u]:%s", me, bsIdx, err);
133
      airMopError(mop); return 1;
134
    }
135
3
    printf("%s: bspec[%u] = %s\n", me, bsIdx, buff);
136
  }
137
138
1
  bspec = nrrdBoundarySpecNew();
139
1
  airMopAdd(mop, bspec, (airMopper)nrrdBoundarySpecNix, airMopAlways);
140
141
1
  fprintf(stderr, "%s: bogus value\n", me);
142
1
  bspec->boundary = -10;
143
1
  checkFailOrDie(bspec, mop);
144
145
1
  fprintf(stderr, "%s: pad with bogus padValue\n", me);
146
1
  bspec->boundary = nrrdBoundaryPad;
147
1
  bspec->padValue = AIR_POS_INF;
148
1
  checkFailOrDie(bspec, mop);
149
150
1
  fprintf(stderr, "%s: checking parse failures\n", me);
151
1
  parseFailOrDie(bspec, "bingo", mop);
152
1
  parseFailOrDie(bspec, "wrap:10", mop);
153
1
  parseFailOrDie(bspec, "pad", mop);
154
1
  parseFailOrDie(bspec, "pad:nan", mop);
155
1
  parseFailOrDie(bspec, "pad:bob", mop);
156
157
1
  fprintf(stderr, "%s: checking string->bpsec->string\n", me);
158
1
  psLoopOrDie(bspec, airEnumStr(nrrdBoundary, nrrdBoundaryBleed), mop);
159
1
  psLoopOrDie(bspec, airEnumStr(nrrdBoundary, nrrdBoundaryWrap), mop);
160
1
  psLoopOrDie(bspec, airEnumStr(nrrdBoundary, nrrdBoundaryWeight), mop);
161
1
  psLoopOrDie(bspec, airEnumStr(nrrdBoundary, nrrdBoundaryMirror), mop);
162
1
  psLoopOrDie(bspec, "pad:0", mop);
163
1
  psLoopOrDie(bspec, "pad:3", mop);
164
165
1
  airMopOkay(mop);
166
1
  return 0;
167
1
}