Sacado Package Browser (Single Doxygen Collection) Version of the Day
LogicalSparseUnitTests.cpp
Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #include "LogicalSparseUnitTests.hpp"
00033 
00034 CPPUNIT_TEST_SUITE_REGISTRATION( LogicalSparseOpsUnitTest );
00035 
00036 LogicalSparseOpsUnitTest::LogicalSparseOpsUnitTest() :
00037   urand(0.0, 1.0), n(5), tol_a(1.0e-15), tol_r(1.0e-14) {}
00038 
00039 LogicalSparseOpsUnitTest::LogicalSparseOpsUnitTest(int numComponents, 
00040                double absolute_tolerance, 
00041                double relative_tolerance) :
00042   urand(0.0, 1.0), 
00043   n(numComponents), 
00044   tol_a(absolute_tolerance), 
00045   tol_r(relative_tolerance) {}
00046 
00047 void LogicalSparseOpsUnitTest::setUp() {
00048   double val;
00049 
00050   val = urand.number();
00051   a_dfad = DFadType(n,val);
00052   a_ls = LSType(n,val);
00053   
00054   val = urand.number();
00055   b_dfad = DFadType(n,val);
00056   b_ls = LSType(n,val);
00057 
00058   val = urand.number();
00059   c_dfad = val;
00060   c_ls = val;
00061 
00062   for (int i=0; i<n; i++) {
00063     val = urand.number();
00064     a_dfad.fastAccessDx(i) = val;
00065     a_ls.fastAccessDx(i) = 1;
00066 
00067     val = urand.number();
00068     b_dfad.fastAccessDx(i) = val;
00069     b_ls.fastAccessDx(i) = 1;
00070   }
00071 }
00072 
00073 void LogicalSparseOpsUnitTest::tearDown() {}
00074 
00075 void LogicalSparseOpsUnitTest::compareFads(const DFadType& x_dfad,
00076              const LSType& x_ls) {
00077 
00078   // Compare sizes
00079   CPPUNIT_ASSERT(x_dfad.size() == x_ls.size());
00080   
00081   // Compare hasFastAccess
00082   CPPUNIT_ASSERT(x_dfad.hasFastAccess() == x_ls.hasFastAccess());
00083   
00084   // Compare values
00085   compareDoubles(x_dfad.val(), x_ls.val());
00086   
00087   for (int i=0; i<x_ls.size(); i++) {
00088     
00089     // Compare dx
00090     compareDx(x_dfad.dx(i), x_ls.dx(i));
00091     
00092     // Compare fastAccessDx
00093     compareDx(x_dfad.fastAccessDx(i), x_ls.fastAccessDx(i));
00094   }
00095 }
00096 
00097 void LogicalSparseOpsUnitTest::compareDoubles(double a, double b) {
00098   CPPUNIT_ASSERT( fabs(a-b) < tol_a + tol_r*fabs(a) );
00099 }
00100 
00101 void LogicalSparseOpsUnitTest::compareBools(bool a, bool b) {
00102   CPPUNIT_ASSERT( a == b );
00103 }
00104 
00105 void LogicalSparseOpsUnitTest::compareDx(double a, bool b) {
00106   CPPUNIT_ASSERT( (a && b) || !(a || b) );
00107 }
00108 
00109 void LogicalSparseOpsUnitTest::testMax() {
00110   double val;
00111 
00112   // LFAd, LFad
00113   LSType aa_ls = a_ls + 1.0;
00114   c_ls = max(aa_ls, a_ls);
00115   compareDoubles(c_ls.val(), aa_ls.val());
00116   for (int i=0; i<n; i++) {
00117     compareBools(c_ls.dx(i), aa_ls.dx(i));
00118     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00119   }
00120   c_ls = max(a_ls, aa_ls);
00121   compareDoubles(c_ls.val(), aa_ls.val());
00122   for (int i=0; i<n; i++) {
00123     compareBools(c_ls.dx(i), aa_ls.dx(i));
00124     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00125   }
00126 
00127   // Expr, LFad
00128   c_ls = max(a_ls+1.0, a_ls);
00129   compareDoubles(c_ls.val(), aa_ls.val());
00130   for (int i=0; i<n; i++) {
00131     compareBools(c_ls.dx(i), aa_ls.dx(i));
00132     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00133   }
00134   c_ls = max(a_ls, a_ls+1.0);
00135   compareDoubles(c_ls.val(), aa_ls.val());
00136   for (int i=0; i<n; i++) {
00137     compareBools(c_ls.dx(i), aa_ls.dx(i));
00138     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00139   }
00140 
00141   // Expr, Expr (same)
00142   c_ls = max(a_ls+1.0, a_ls+1.0);
00143   compareDoubles(c_ls.val(), aa_ls.val());
00144   for (int i=0; i<n; i++) {
00145     compareBools(c_ls.dx(i), aa_ls.dx(i));
00146     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00147   }
00148 
00149   // Expr, Expr (different)
00150   c_ls = max(a_ls+1.0, a_ls-1.0);
00151   compareDoubles(c_ls.val(), aa_ls.val());
00152   for (int i=0; i<n; i++) {
00153     compareBools(c_ls.dx(i), aa_ls.dx(i));
00154     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00155   }
00156   c_ls = max(a_ls-1.0, a_ls+1.0);
00157   compareDoubles(c_ls.val(), aa_ls.val());
00158   for (int i=0; i<n; i++) {
00159     compareBools(c_ls.dx(i), aa_ls.dx(i));
00160     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00161   }
00162   
00163   // LFad, const
00164   val = a_ls.val() + 1;
00165   c_ls = max(a_ls, val);
00166   compareDoubles(c_ls.val(), val);
00167   for (int i=0; i<n; i++)
00168     compareBools(c_ls.dx(i), 0);
00169   val = a_ls.val() - 1;
00170   c_ls = max(a_ls, val);
00171   compareDoubles(c_ls.val(), a_ls.val());
00172   for (int i=0; i<n; i++) {
00173     compareBools(c_ls.dx(i), a_ls.dx(i));
00174     compareBools(c_ls.fastAccessDx(i), a_ls.fastAccessDx(i));
00175   }
00176   val = b_ls.val() + 1;
00177   c_ls = max(val, b_ls);
00178   compareDoubles(c_ls.val(), val);
00179   for (int i=0; i<n; i++)
00180     compareBools(c_ls.dx(i), 0);
00181   val = b_ls.val() - 1;
00182   c_ls = max(val, b_ls);
00183   compareDoubles(c_ls.val(), b_ls.val());
00184   for (int i=0; i<n; i++) {
00185     compareBools(c_ls.dx(i), b_ls.dx(i));
00186     compareBools(c_ls.fastAccessDx(i), b_ls.fastAccessDx(i));
00187   }
00188 
00189   // Expr, const
00190   val = a_ls.val();
00191   c_ls = max(a_ls+1.0, val);
00192   compareDoubles(c_ls.val(), aa_ls.val());
00193   for (int i=0; i<n; i++) {
00194     compareBools(c_ls.dx(i), aa_ls.dx(i));
00195     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00196   }
00197   c_ls = max(val, a_ls+1.0);
00198   compareDoubles(c_ls.val(), aa_ls.val());
00199   for (int i=0; i<n; i++) {
00200     compareBools(c_ls.dx(i), aa_ls.dx(i));
00201     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00202   }
00203   
00204 }
00205 
00206 void LogicalSparseOpsUnitTest::testMin() {
00207   double val;
00208 
00209   // LFad, LFad
00210   LSType aa_ls = a_ls - 1.0;
00211   c_ls = min(aa_ls, a_ls);
00212   compareDoubles(c_ls.val(), aa_ls.val());
00213   for (int i=0; i<n; i++) {
00214     compareBools(c_ls.dx(i), aa_ls.dx(i));
00215     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00216   }
00217   c_ls = min(a_ls, aa_ls);
00218   compareDoubles(c_ls.val(), aa_ls.val());
00219   for (int i=0; i<n; i++) {
00220     compareBools(c_ls.dx(i), aa_ls.dx(i));
00221     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00222   }
00223 
00224   // Expr, LFad
00225   c_ls = min(a_ls-1.0, a_ls);
00226   compareDoubles(c_ls.val(), aa_ls.val());
00227   for (int i=0; i<n; i++) {
00228     compareBools(c_ls.dx(i), aa_ls.dx(i));
00229     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00230   }
00231   c_ls = min(a_ls, a_ls-1.0);
00232   compareDoubles(c_ls.val(), aa_ls.val());
00233   for (int i=0; i<n; i++) {
00234     compareBools(c_ls.dx(i), aa_ls.dx(i));
00235     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00236   }
00237 
00238   // Expr, Expr (same)
00239   c_ls = min(a_ls-1.0, a_ls-1.0);
00240   compareDoubles(c_ls.val(), aa_ls.val());
00241   for (int i=0; i<n; i++) {
00242     compareBools(c_ls.dx(i), aa_ls.dx(i));
00243     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00244   }
00245 
00246   // Expr, Expr (different)
00247   c_ls = min(a_ls+1.0, a_ls-1.0);
00248   compareDoubles(c_ls.val(), aa_ls.val());
00249   for (int i=0; i<n; i++) {
00250     compareBools(c_ls.dx(i), aa_ls.dx(i));
00251     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00252   }
00253   c_ls = min(a_ls-1.0, a_ls+1.0);
00254   compareDoubles(c_ls.val(), aa_ls.val());
00255   for (int i=0; i<n; i++) {
00256     compareBools(c_ls.dx(i), aa_ls.dx(i));
00257     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00258   }
00259 
00260   // LFad, const
00261   val = a_ls.val() - 1;
00262   c_ls = min(a_ls, val);
00263   compareDoubles(c_ls.val(), val);
00264   for (int i=0; i<n; i++)
00265     compareBools(c_ls.dx(i), 0);
00266   val = a_ls.val() + 1;
00267   c_ls = min(a_ls, val);
00268   compareDoubles(c_ls.val(), a_ls.val());
00269   for (int i=0; i<n; i++) {
00270     compareBools(c_ls.dx(i), a_ls.dx(i));
00271     compareBools(c_ls.fastAccessDx(i), a_ls.fastAccessDx(i));
00272   }
00273   val = b_ls.val() - 1;
00274   c_ls = min(val, b_ls);
00275   compareDoubles(c_ls.val(), val);
00276   for (int i=0; i<n; i++)
00277     compareBools(c_ls.dx(i), 0);
00278   val = b_ls.val() + 1;
00279   c_ls = min(val, b_ls);
00280   compareDoubles(c_ls.val(), b_ls.val());
00281   for (int i=0; i<n; i++) {
00282     compareBools(c_ls.dx(i), b_ls.dx(i));
00283     compareBools(c_ls.fastAccessDx(i), b_ls.fastAccessDx(i));
00284   }
00285 
00286   // Expr, const
00287   val = a_ls.val();
00288   c_ls = min(a_ls-1.0, val);
00289   compareDoubles(c_ls.val(), aa_ls.val());
00290   for (int i=0; i<n; i++) {
00291     compareBools(c_ls.dx(i), aa_ls.dx(i));
00292     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00293   }
00294   c_ls = min(val, a_ls-1.0);
00295   compareDoubles(c_ls.val(), aa_ls.val());
00296   for (int i=0; i<n; i++) {
00297     compareBools(c_ls.dx(i), aa_ls.dx(i));
00298     compareBools(c_ls.fastAccessDx(i), aa_ls.fastAccessDx(i));
00299   }
00300 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines