testing.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #include <ode/odeconfig.h>
  23. #include "config.h"
  24. #include <ode/misc.h>
  25. #include <ode/memory.h>
  26. #include "testing.h"
  27. #ifdef dDOUBLE
  28. static const dReal tol = 1.0e-9;
  29. #else
  30. static const dReal tol = 1.0e-5f;
  31. #endif
  32. // matrix header on the stack
  33. struct dMatrixComparison::dMatInfo {
  34. int n,m; // size of matrix
  35. char name[128]; // name of the matrix
  36. dReal *data; // matrix data
  37. int size; // size of `data'
  38. };
  39. dMatrixComparison::dMatrixComparison()
  40. {
  41. afterfirst = 0;
  42. index = 0;
  43. }
  44. dMatrixComparison::~dMatrixComparison()
  45. {
  46. reset();
  47. }
  48. dReal dMatrixComparison::nextMatrix (dReal *A, int n, int m, int lower_tri,
  49. const char *name, ...)
  50. {
  51. if (A==0 || n < 1 || m < 1 || name==0) dDebug (0,"bad args to nextMatrix");
  52. int num = n*dPAD(m);
  53. if (afterfirst==0) {
  54. dMatInfo *mi = (dMatInfo*) dAlloc (sizeof(dMatInfo));
  55. mi->n = n;
  56. mi->m = m;
  57. mi->size = num * sizeof(dReal);
  58. mi->data = (dReal*) dAlloc (mi->size);
  59. memcpy (mi->data,A,mi->size);
  60. va_list ap;
  61. va_start (ap,name);
  62. vsprintf (mi->name,name,ap);
  63. if (strlen(mi->name) >= sizeof (mi->name)) dDebug (0,"name too long");
  64. mat.push (mi);
  65. return 0;
  66. }
  67. else {
  68. if (lower_tri && n != m)
  69. dDebug (0,"dMatrixComparison, lower triangular matrix must be square");
  70. if (index >= mat.size()) dDebug (0,"dMatrixComparison, too many matrices");
  71. dMatInfo *mp = mat[index];
  72. index++;
  73. dMatInfo mi;
  74. va_list ap;
  75. va_start (ap,name);
  76. vsprintf (mi.name,name,ap);
  77. if (strlen(mi.name) >= sizeof (mi.name)) dDebug (0,"name too long");
  78. if (strcmp(mp->name,mi.name) != 0)
  79. dDebug (0,"dMatrixComparison, name mismatch (\"%s\" and \"%s\")",
  80. mp->name,mi.name);
  81. if (mp->n != n || mp->m != m)
  82. dDebug (0,"dMatrixComparison, size mismatch (%dx%d and %dx%d)",
  83. mp->n,mp->m,n,m);
  84. dReal maxdiff;
  85. if (lower_tri) {
  86. maxdiff = dMaxDifferenceLowerTriangle (A,mp->data,n);
  87. }
  88. else {
  89. maxdiff = dMaxDifference (A,mp->data,n,m);
  90. }
  91. if (maxdiff > tol)
  92. dDebug (0,"dMatrixComparison, matrix error (size=%dx%d, name=\"%s\", "
  93. "error=%.4e)",n,m,mi.name,maxdiff);
  94. return maxdiff;
  95. }
  96. }
  97. void dMatrixComparison::end()
  98. {
  99. if (mat.size() <= 0) dDebug (0,"no matrices in sequence");
  100. afterfirst = 1;
  101. index = 0;
  102. }
  103. void dMatrixComparison::reset()
  104. {
  105. for (int i=0; i<mat.size(); i++) {
  106. dFree (mat[i]->data,mat[i]->size);
  107. dFree (mat[i],sizeof(dMatInfo));
  108. }
  109. mat.setSize (0);
  110. afterfirst = 0;
  111. index = 0;
  112. }
  113. void dMatrixComparison::dump()
  114. {
  115. for (int i=0; i<mat.size(); i++)
  116. printf ("%d: %s (%dx%d)\n",i,mat[i]->name,mat[i]->n,mat[i]->m);
  117. }
  118. //****************************************************************************
  119. // unit test
  120. #include <setjmp.h>
  121. static jmp_buf jump_buffer;
  122. static void myDebug (int num, const char *msg, va_list ap)
  123. {
  124. // printf ("(Error %d: ",num);
  125. // vprintf (msg,ap);
  126. // printf (")\n");
  127. longjmp (jump_buffer,1);
  128. }
  129. extern "C" ODE_API void dTestMatrixComparison()
  130. {
  131. volatile int i;
  132. printf ("dTestMatrixComparison()\n");
  133. dMessageFunction *orig_debug = dGetDebugHandler();
  134. dMatrixComparison mc;
  135. dReal A[50*50];
  136. // make first sequence
  137. unsigned long seed = dRandGetSeed();
  138. for (i=1; i<49; i++) {
  139. dMakeRandomMatrix (A,i,i+1,1.0);
  140. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  141. }
  142. mc.end();
  143. //mc.dump();
  144. // test identical sequence
  145. dSetDebugHandler (&myDebug);
  146. dRandSetSeed (seed);
  147. if (setjmp (jump_buffer)) {
  148. printf ("\tFAILED (1)\n");
  149. }
  150. else {
  151. for (i=1; i<49; i++) {
  152. dMakeRandomMatrix (A,i,i+1,1.0);
  153. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  154. }
  155. mc.end();
  156. printf ("\tpassed (1)\n");
  157. }
  158. dSetDebugHandler (orig_debug);
  159. // test broken sequences (with matrix error)
  160. dRandSetSeed (seed);
  161. volatile int passcount = 0;
  162. for (i=1; i<49; i++) {
  163. if (setjmp (jump_buffer)) {
  164. passcount++;
  165. }
  166. else {
  167. dSetDebugHandler (&myDebug);
  168. dMakeRandomMatrix (A,i,i+1,1.0);
  169. A[(i-1)*dPAD(i+1)+i] += REAL(0.01);
  170. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  171. dSetDebugHandler (orig_debug);
  172. }
  173. }
  174. mc.end();
  175. printf ("\t%s (2)\n",(passcount == 48) ? "passed" : "FAILED");
  176. // test broken sequences (with name error)
  177. dRandSetSeed (seed);
  178. passcount = 0;
  179. for (i=1; i<49; i++) {
  180. if (setjmp (jump_buffer)) {
  181. passcount++;
  182. }
  183. else {
  184. dSetDebugHandler (&myDebug);
  185. dMakeRandomMatrix (A,i,i+1,1.0);
  186. mc.nextMatrix (A,i,i+1,0,"B%d",i);
  187. dSetDebugHandler (orig_debug);
  188. }
  189. }
  190. mc.end();
  191. printf ("\t%s (3)\n",(passcount == 48) ? "passed" : "FAILED");
  192. // test identical sequence again
  193. dSetDebugHandler (&myDebug);
  194. dRandSetSeed (seed);
  195. if (setjmp (jump_buffer)) {
  196. printf ("\tFAILED (4)\n");
  197. }
  198. else {
  199. for (i=1; i<49; i++) {
  200. dMakeRandomMatrix (A,i,i+1,1.0);
  201. mc.nextMatrix (A,i,i+1,0,"A%d",i);
  202. }
  203. mc.end();
  204. printf ("\tpassed (4)\n");
  205. }
  206. dSetDebugHandler (orig_debug);
  207. }