unittest.h

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2001-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Mon Sep 10 18:57:59 2001
00010 // commit: $Id: unittest.h 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/rutz/unittest.h $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_RUTZ_UNITTEST_H_UTC20050626084020_DEFINED
00035 #define GROOVX_RUTZ_UNITTEST_H_UTC20050626084020_DEFINED
00036 
00037 #include "rutz/error.h"
00038 
00039 #include "rutz/fileposition.h"
00040 #include "rutz/sfmt.h"
00041 
00042 #include <cmath>
00043 
00044 #include "rutz/debug.h"
00045 GVX_DBG_REGISTER
00046 
00047 namespace rutz
00048 {
00049   inline void test_require(bool expr,
00050                            const char* expr_string,
00051                            const rutz::file_pos& pos)
00052   {
00053     dbg_print(3, expr_string); dbg_eval_nl(3, expr);
00054     if (!expr)
00055       throw rutz::error(rutz::sfmt("%s:%d:\n\texpected: %s\n",
00056                                    pos.m_file_name,
00057                                    pos.m_line_no,
00058                                    expr_string), pos);
00059   }
00060 
00061   template <class T, class U>
00062   inline void test_require_eq(const T& expr1,
00063                               const U& expr2,
00064                               const char* expr_string1,
00065                               const char* expr_string2,
00066                               const rutz::file_pos& pos)
00067   {
00068     dbg_print(3, expr_string1); dbg_eval_nl(3, expr1);
00069     dbg_print(3, expr_string2); dbg_eval_nl(3, expr2);
00070     if (!(expr1 == expr2))
00071       {
00072         const rutz::fstring msg =
00073           rutz::sfmt("%s:%d: failed test:\n"
00074                      "\texpected %s (lhs) == %s (rhs)\n"
00075                      "\tgot: (lhs) %s == %s\n"
00076                      "\t     (rhs) %s == %s\n",
00077                      pos.m_file_name, pos.m_line_no,
00078                      expr_string1, expr_string2,
00079                      expr_string1, rutz::sconvert(expr1).c_str(),
00080                      expr_string2, rutz::sconvert(expr2).c_str());
00081         throw rutz::error(msg, pos);
00082       }
00083   }
00084 
00085   const double APPROX_TOL = 1e-40;
00086 
00087   inline bool approx_eq(double a, double b, double tol = APPROX_TOL)
00088   {
00089     return fabs(a-b) < fabs(tol);
00090   }
00091 
00092   inline void test_require_approx_eq(double expr1,
00093                                      double expr2,
00094                                      double tol,
00095                                      const char* expr_string1,
00096                                      const char* expr_string2,
00097                                      const rutz::file_pos& pos)
00098   {
00099     dbg_print(3, expr_string1); dbg_eval_nl(3, expr1);
00100     dbg_print(3, expr_string2); dbg_eval_nl(3, expr2);
00101     dbg_eval_nl(3, tol);
00102     if (!approx_eq(expr1, expr2, tol))
00103       {
00104         const rutz::fstring msg =
00105           rutz::sfmt("%s:%d: failed test:\n"
00106                      "\texpected %s (lhs) ~= %s (rhs)\n"
00107                      "\tgot: (lhs) %s == %g\n"
00108                      "\t     (rhs) %s == %g\n"
00109                      "\tallowable tolerance was: %g\n"
00110                      "\tactual difference was:   %g\n",
00111                      pos.m_file_name, pos.m_line_no,
00112                      expr_string1, expr_string2,
00113                      expr_string1, expr1,
00114                      expr_string2, expr2,
00115                      tol,
00116                      expr1-expr2);
00117         throw rutz::error(msg, pos);
00118       }
00119   }
00120 }
00121 
00122 #define TEST_REQUIRE(expr) \
00123   rutz::test_require(bool(expr), #expr, SRC_POS)
00124 
00125 #define TEST_REQUIRE_EQ(expr1, expr2) \
00126   rutz::test_require_eq(expr1, expr2, \
00127                         #expr1, #expr2, SRC_POS)
00128 
00129 #define TEST_REQUIRE_APPROX(expr1, expr2, tol) \
00130   rutz::test_require_approx_eq(expr1, expr2, tol, \
00131                                #expr1, #expr2, SRC_POS)
00132 
00133 #define DEF_TEST(pkg, func) pkg->def(#func, "", &func, SRC_POS)
00134 
00135 static const char __attribute__((used)) vcid_groovx_rutz_unittest_h_utc20050626084020[] = "$Id: unittest.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00136 #endif // !GROOVX_RUTZ_UNITTEST_H_UTC20050626084020_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:41 2008 by Doxygen version 1.5.5.