GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: Testing/nrrd/tload.c Lines: 41 57 71.9 %
Date: 2017-05-26 Branches: 17 32 53.1 %

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
** nrrdLoad
30
*/
31
32
int
33
main(int argc, const char **argv) {
34
  const char *me;
35
  Nrrd *nin;
36
  airArray *mop;
37
  char *fullname;
38
2
  int differ;
39
40
  AIR_UNUSED(argc);
41
1
  me = argv[0];
42
1
  mop = airMopNew();
43
44
1
  nin = nrrdNew();
45
1
  airMopAdd(mop, nin, (airMopper)nrrdNuke, airMopAlways);
46
1
  fullname = testDataPathPrefix("fmob-c4h.nrrd");
47
1
  airMopAdd(mop, fullname, airFree, airMopAlways);
48
1
  if (nrrdLoad(nin, fullname, NULL)) {
49
    char *err;
50
    airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
51
    fprintf(stderr, "%s: trouble reading data \"%s\":\n%s",
52
            me, fullname, err);
53
    airMopError(mop); return 1;
54
  }
55
56
  {
57
    Nrrd *ncopy;
58
1
    char *blah, *blah1L, explain[AIR_STRLEN_LARGE];
59
    size_t ii, blen;
60
1
    ncopy = nrrdNew();
61
1
    airMopAdd(mop, ncopy, (airMopper)nrrdNuke, airMopAlways);
62
    blen = AIR_STRLEN_HUGE*42;
63
1
    blah = AIR_CALLOC(blen, char);
64
1
    airMopAdd(mop, blah, airFree, airMopAlways);
65
86100
    for (ii=0; ii<blen-1; ii++) {
66
      /* NOTE: a more rigorous test would put things in here that can't
67
         be directly written to file, like \r \v \f, but fixing that bug
68
         is enough of a significant change to functionality that it should
69
         be discussed with users first */
70
43049
      blah[ii] = ("abcdefghijklmnopqrtsuvzwyz\n\"\\ "
71
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\"\\ ")
72
43049
        [airRandInt((26 + 4)*2)]; /* 4 other characters */
73
    }
74
1
    blah[ii] = '\0';
75
    /* NOTE: this blah1L is to overcome a long-stanging bug that \n's
76
       were permitted in labels and units, but can't be written.  Same
77
       as NOTE above.  New code in Teem
78
       (nrrd/keyvalue.c/_nrrdWriteEscaped()) now prevents generating
79
       broken NRRD files, but it means that the comparison test
80
       between in-memory vs the saved-and-read nrrd would fail, so we
81
       do the same transformation here to allow the comparison to
82
       work */
83
1
    blah1L = airOneLinify(airStrdup(blah));
84
1
    airMopAdd(mop, blah1L, airFree, airMopAlways);
85
1
    nrrdAxisInfoSet_va(nin, nrrdAxisInfoLabel,
86
                       "first axis label", "2nd axis label",
87
                       blah1L);
88
1
    nin->spaceUnits[0] = airOneLinify(airStrdup("\nsp\"\nu0\n"));
89
1
    nin->spaceUnits[1] = airStrdup("bob");
90
1
    nin->spaceUnits[2] = airStrdup(blah1L);  /* see NOTE above */
91
2
    if (nrrdCommentAdd(nin, "the first comment")
92
2
        || nrrdCommentAdd(nin, "very long comment follows")
93
2
        || nrrdCommentAdd(nin, blah)
94
2
        || nrrdKeyValueAdd(nin, "first key", "first value")
95
2
        || nrrdKeyValueAdd(nin, "2nd key", "2nd value")
96
2
        || nrrdKeyValueAdd(nin, "big key", blah)) {
97
      char *err;
98
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
99
      fprintf(stderr, "%s: trouble w/ comments or KVPs:\n%s",
100
              me, err);
101
      airMopError(mop); return 1;
102
    }
103
2
    if (nrrdCopy(ncopy, nin)
104
2
        || nrrdCompare(nin, ncopy, AIR_FALSE /* onlyData */,
105
1
                       0.0 /* epsilon */, &differ, explain)) {
106
      char *err;
107
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
108
      fprintf(stderr, "%s: trouble w/ copy or compare:\n%s\n", me, err);
109
      airMopError(mop); return 1;
110
    }
111
1
    if (differ) {
112
      fprintf(stderr, "%s: difference in in-memory copy: %s\n", me, explain);
113
      airMopError(mop); return 1;
114
    }
115
2
    if (nrrdSave("tloadTest.nrrd", nin, NULL)
116
2
        || nrrdLoad(ncopy, "tloadTest.nrrd", NULL)
117
2
        || nrrdCompare(nin, ncopy, AIR_FALSE /* onlyData */,
118
                       0.0 /* epsilon */, &differ, explain)) {
119
      char *err;
120
      airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways);
121
      fprintf(stderr, "%s: trouble w/ save, load, compare:\n%s\n", me, err);
122
      airMopError(mop); return 1;
123
    }
124
1
    if (differ) {
125
      fprintf(stderr, "%s: difference in on-disk copy: %s\n", me, explain);
126
      airMopError(mop); return 1;
127
    }
128
2
  }
129
130
1
  airMopOkay(mop);
131
1
  return 0;
132
1
}