libatomprobe
Library for Atom Probe Tomography (APT) computation
unittests.cpp
Go to the documentation of this file.
1 /* unittests.cpp : runs all internal unint tests from the library
2  * Copyright (C) 2014 Daniel Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <iostream>
18 #include <cassert>
19 
20 #include "atomprobe/atomprobe.h"
21 
22 #ifndef ASSERT
23  #define ASSERT(f) assert( (f) );
24 #endif
25 
26 #ifdef TEST
27 #undef TEST
28 #define TEST(f) { if(!(f)) return false; }
29 #endif
30 
31 using namespace std;
32 using namespace AtomProbe;
33 
34 vector<string> rangePaths()
35 {
36  //TODO: Replace with proper system to obtain
37  // the actual range path, relative to this folder
38  // e.g. via a glob
39  vector<string> paths;
40  paths.push_back("samples/ranges/test10.rng");
41  paths.push_back("samples/ranges/test11.rng");
42  paths.push_back("samples/ranges/test12.rng");
43  paths.push_back("samples/ranges/test1.env");
44  paths.push_back("samples/ranges/test1.rng");
45  paths.push_back("samples/ranges/test1.rrng");
46  paths.push_back("samples/ranges/test2.rng");
47  paths.push_back("samples/ranges/test2.rrng");
48  paths.push_back("samples/ranges/test3.rng");
49  paths.push_back("samples/ranges/test3.rrng");
50  paths.push_back("samples/ranges/test4.rng");
51  paths.push_back("samples/ranges/test4.rrng");
52  paths.push_back("samples/ranges/test5.rng");
53  paths.push_back("samples/ranges/test5.rrng");
54  paths.push_back("samples/ranges/test6.rrng");
55  paths.push_back("samples/ranges/test7.rng");
56  paths.push_back("samples/ranges/test8.rng");
57  paths.push_back("samples/ranges/test9.rng");
58 
59  return paths;
60 }
61 
62 #ifdef DEBUG
63 void gsl_catch_errors(const char *reason, const char *file, int line, int gsl_errno)
64 {
65  cerr << "Failed because :" << reason << " in file " << file << " line" << line << " error number :" << gsl_errno << endl;
66  ASSERT(false);
67 }
68 #endif
69 
70 bool runTests()
71 {
72 #ifdef DEBUG
73  //Disable the GSL error handler.
74  //It uses abort() which destroys the stack trace.
75  // painful for debugging
76  gsl_set_error_handler(gsl_catch_errors);
77 
78  bool haveAbundance=false;
79  AbundanceData abundance;
80  if(!abundance.open("naturalAbundance.xml"))
81  haveAbundance=true;
82 
83  cerr << "Testing overlap...";
84  TEST(testDeconvolve());
85  cerr << "\tDone" << endl;
86 
87  cerr << "Testing rotation...";
88  TEST(testRotationAlgorithms());
89  cerr << "\tDone" << endl;
90 
91 //Qhull crashes under cygwin
92 #ifndef __CYGWIN__
93  cerr << "Testing Convex Hull...";
94  TEST(testConvexHull());
95  cerr << "\tDone" << endl;
96 #endif
97  cerr << "Testing progress bar...";
98  TEST(testProgressBar()); //No need for "Done",its built into progress bar.
99 
100  cerr << "KD tree...";
101  TEST(K3DTreeExact::runUnitTests());
102  TEST(K3DTreeApprox::test());
103  cerr << "\tDone" << endl;
104 
105  cerr << "Mesh tests...";
106  TEST(meshTests());
107  cerr << "\tDone" << endl;
108 
109  cerr << "Testing sampling...";
110  TEST(testSampling())
111  cerr << "\tDone" << endl;
112 
113  cerr << "Isotope tests..." ;
114  TEST(AbundanceData::runUnitTests())
115  cerr << "\tDone" << endl;
116 
117  cerr << "Knapsack tests..." ;
118  TEST(MassTool::runUnitTests())
119  cerr << "\tDone" << endl;
120 
121  cerr << "Overlap tests..." ;
122  TEST(testOverlapDetect());
123  cerr << "\tDone" << endl;
124 
125  cerr << "Projection tests..." ;
126  TEST(testProjection());
127  cerr << "\tDone" << endl;
128 
129  cerr << "Miller index tests...";
130  TEST(millerTest());
131  cerr << "\tDone" << endl;
132 
133  cerr << "Reconstruction tests...";
135  cerr << "\tDone" << endl;
136 
137  cerr << "Isotope Filter tests...";
138  TEST(AtomProbe::isotopeFilterTests());
139  cerr << "\tDone" << endl;
140 
141  vector<string> rangeFilePaths;
142  rangeFilePaths =rangePaths();
143 
144  cerr << "Range tests...";
145  for(auto ui=0; ui < rangeFilePaths.size();ui++)
146  {
147  RangeFile rng;
148  TEST(rng.open(rangeFilePaths[ui].c_str()));
149 
150  TEST(rng.isSelfConsistent());
151  }
152  TEST(RangeFile::test());
153  TEST(MultiRange::test());
154  //To test the splitting code n
155  if(haveAbundance)
156  {
157  cerr << "(+Multi-split)...";
158  TEST(MultiRange::testSplit(abundance));
159  }
160  cerr << "\tDone" << endl;
161 
162  cerr << "Range check tests...";
163  TEST(AtomProbe::testRangeChecking());
164  cerr << "\tDone" << endl;
165 
166  cerr << "Generation tests....";
167  TEST(AtomProbe::runGenerateTests());
168  cerr << "\tDone" << endl;
169 
170  cerr << "Mass<>mol conversion...";
171  TEST(AtomProbe::testMassMolConversion());
172  cerr << "\tDone" << endl;
173 
174  cerr << "Composition tests....";
175  TEST(AtomProbe::testComposition());
176  cerr << "\tDone" << endl;
177 
178  cerr << "Statistics tests....";
179  TEST(AtomProbe::runConfidenceTests());
180  cerr << "\tDone" << endl;
181 
182  cerr << "Spectrum tests....";
183  TEST(AtomProbe::testBackgroundFitMaths());
184  cerr << "\tDone" << endl;
185  cerr << "Voxel tests....";
186  TEST(AtomProbe::runVoxelTests());
187  cerr << "\tDone" << endl;
188 
189  cerr << "Isosurface tests...";
190  TEST(AtomProbe::testIsoSurface());
191  cerr << "\tDone" << endl;
192 
193  cerr << "Misc. Maths tests....";
194  TEST(AtomProbe::runMiscMathsTests());
195  TEST(AtomProbe::testPoint3D());
196  TEST(AtomProbe::testIonHit());
197  cerr << "\tDone" << endl;
198 
199  if(haveAbundance)
200  {
201  cerr << "Ion Adjacency tests...";
202  TEST(AtomProbe::runMiscMathsTests());
203  cerr << "\tDone" << endl;
204  }
205  else
206  cerr << "Skipping Ion Adjacency ; no abundance" << endl;
207 
208 #else
209  cerr << "Tests not enabled in release mode. Please recompile in debug mode" << endl;
210 #endif
211  return true;
212 }
213 
214 int main()
215 {
216  cerr << "Running test program " << endl;
217  if(!runTests())
218  {
219  cerr << "unit tests failed" << endl;
220  return 1;
221  }
222 
223  return 0;
224 }
#define ASSERT(f)
Definition: unittests.cpp:23
vector< string > rangePaths()
Definition: unittests.cpp:34
STL namespace.
int main()
Definition: unittests.cpp:214
#define TEST(f, g)
Definition: aptAssert.h:49
bool isSelfConsistent() const
Performs checks for self consistency.
Definition: ranges.cpp:2450
bool reconstructTest()
size_t open(const char *file, bool strict=false)
Attempt to open the abundance data file, return 0 on success, nonzero on failure. ...
Definition: abundance.cpp:79
Class to load abundance information for natural isotopes.
Definition: abundance.h:54
bool open(const char *rangeFile)
Open a specified range file - returns true on success.
Definition: ranges.cpp:678
Data storage and retrieval class for various range files.
Definition: ranges.h:95
bool runTests()
Definition: unittests.cpp:70