1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2006 Sandia National Laboratories.  Developed at the
    University of Wisconsin--Madison under SNL contract number
    624796.  The U.S. Government and the University of Wisconsin
    retain certain rights to this software.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    (2006) [email protected]

  ***************************************************************** */

/** \file StdDevTemplateTest.cpp
 *  \brief Unit tests for StdDevTemplate and VarianceTemplate
 *  \author Jason Kraftcheck
 */

#include "Mesquite.hpp"
#include "StdDevTemplate.hpp"
#include "VarianceTemplate.hpp"
#include "MsqError.hpp"
#include "PatchData.hpp"
#include "ObjectiveFunctionTests.hpp"
#include "MsqHessian.hpp"

using namespace MBMesquite;
using namespace std;

const double EPSILON = 1e-4;

class StdDevTemplateTest : public CppUnit::TestFixture, public ObjectiveFunctionTests
{
  private:
    CPPUNIT_TEST_SUITE( StdDevTemplateTest );

    CPPUNIT_TEST( test_eval_calc );
    CPPUNIT_TEST( test_eval_accum );
    CPPUNIT_TEST( test_eval_save );
    CPPUNIT_TEST( test_eval_update );
    CPPUNIT_TEST( test_eval_temp );

    CPPUNIT_TEST( test_eval_calc_sqr );
    CPPUNIT_TEST( test_eval_accum_sqr );
    CPPUNIT_TEST( test_eval_save_sqr );
    CPPUNIT_TEST( test_eval_update_sqr );
    CPPUNIT_TEST( test_eval_temp_sqr );

    CPPUNIT_TEST( test_grad_calc );
    CPPUNIT_TEST( test_grad_save );
    CPPUNIT_TEST( test_grad_update );
    CPPUNIT_TEST( test_grad_temp );

    CPPUNIT_TEST( test_grad_calc_sqr );
    CPPUNIT_TEST( test_grad_save_sqr );
    CPPUNIT_TEST( test_grad_update_sqr );
    CPPUNIT_TEST( test_grad_temp_sqr );

    CPPUNIT_TEST( test_diag_calc );
    CPPUNIT_TEST( test_diag_save );
    CPPUNIT_TEST( test_diag_update );
    CPPUNIT_TEST( test_diag_temp );

    CPPUNIT_TEST( test_diag_calc_sqr );
    CPPUNIT_TEST( test_diag_save_sqr );
    CPPUNIT_TEST( test_diag_update_sqr );
    CPPUNIT_TEST( test_diag_temp_sqr );

    CPPUNIT_TEST( test_hessian_fails );
    CPPUNIT_TEST( test_hessian_fails_sqr );

    CPPUNIT_TEST( test_failed_metric_in_eval );
    CPPUNIT_TEST( test_failed_metric_in_grad );
    CPPUNIT_TEST( test_failed_metric_in_eval_sqr );
    CPPUNIT_TEST( test_failed_metric_in_grad_sqr );

    CPPUNIT_TEST( test_false_metric_in_eval );
    CPPUNIT_TEST( test_false_metric_in_grad );
    CPPUNIT_TEST( test_false_metric_in_eval_sqr );
    CPPUNIT_TEST( test_false_metric_in_grad_sqr );

    CPPUNIT_TEST( test_evaluate );
    CPPUNIT_TEST( test_evaluate_sqr );

    CPPUNIT_TEST( test_numerical_gradient );
    CPPUNIT_TEST( test_numerical_gradient_sqr );

    CPPUNIT_TEST( test_diagonal_gradient );
    CPPUNIT_TEST( test_diagonal_gradient_sqr );

    CPPUNIT_TEST( test_hessian_diag );
    CPPUNIT_TEST( test_hessian_diag_sqr );

    CPPUNIT_TEST( test_clone );
    CPPUNIT_TEST( test_clone_sqr );

    CPPUNIT_TEST( test_eval_negate );
    CPPUNIT_TEST( test_eval_negate_sqr );
    CPPUNIT_TEST( test_grad_negate );
    CPPUNIT_TEST( test_grad_negate_sqr );
    CPPUNIT_TEST( test_diag_negate );
    CPPUNIT_TEST( test_diag_negate_sqr );

    CPPUNIT_TEST_SUITE_END();

  public:
    void test_eval_calc()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::CALCULATE, EVAL, &of );
    }
    void test_eval_accum()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::ACCUMULATE, EVAL, &of );
    }
    void test_eval_save()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::SAVE, EVAL, &of );
    }
    void test_eval_update()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::UPDATE, EVAL, &of );
    }
    void test_eval_temp()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::TEMPORARY, EVAL, &of );
    }

    void test_eval_calc_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::CALCULATE, EVAL, &of );
    }
    void test_eval_accum_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::ACCUMULATE, EVAL, &of );
    }
    void test_eval_save_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::SAVE, EVAL, &of );
    }
    void test_eval_update_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::UPDATE, EVAL, &of );
    }
    void test_eval_temp_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::TEMPORARY, EVAL, &of );
    }

    void test_grad_calc()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::CALCULATE, GRAD, &of );
    }
    void test_grad_accum()<--- The function 'test_grad_accum' is never used.
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::ACCUMULATE, GRAD, &of );
    }
    void test_grad_save()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::SAVE, GRAD, &of );
    }
    void test_grad_update()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::UPDATE, GRAD, &of );
    }
    void test_grad_temp()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::TEMPORARY, GRAD, &of );
    }

    void test_grad_calc_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::CALCULATE, GRAD, &of );
    }
    void test_grad_accum_sqr()<--- The function 'test_grad_accum_sqr' is never used.
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::ACCUMULATE, GRAD, &of );
    }
    void test_grad_save_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::SAVE, GRAD, &of );
    }
    void test_grad_update_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::UPDATE, GRAD, &of );
    }
    void test_grad_temp_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::TEMPORARY, GRAD, &of );
    }

    void test_diag_calc()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::CALCULATE, DIAG, &of );
    }
    void test_diag_accum()<--- The function 'test_diag_accum' is never used.
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::ACCUMULATE, DIAG, &of );
    }
    void test_diag_save()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::SAVE, DIAG, &of );
    }
    void test_diag_update()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::UPDATE, DIAG, &of );
    }
    void test_diag_temp()
    {
        StdDevTemplate of( NULL );
        test_eval_type( ObjectiveFunction::TEMPORARY, DIAG, &of );
    }

    void test_diag_calc_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::CALCULATE, DIAG, &of );
    }
    void test_diag_accum_sqr()<--- The function 'test_diag_accum_sqr' is never used.
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::ACCUMULATE, DIAG, &of );
    }
    void test_diag_save_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::SAVE, DIAG, &of );
    }
    void test_diag_update_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::UPDATE, DIAG, &of );
    }
    void test_diag_temp_sqr()
    {
        VarianceTemplate of( NULL );
        test_eval_type( ObjectiveFunction::TEMPORARY, DIAG, &of );
    }

    void test_evaluate();
    void test_evaluate_sqr();

    void test_numerical_gradient()
    {
        StdDevTemplate of( NULL );
        compare_numerical_gradient( &of );
    }
    void test_numerical_gradient_sqr()
    {
        VarianceTemplate of( NULL );
        compare_numerical_gradient( &of );
    }

    void test_diagonal_gradient()
    {
        StdDevTemplate of( NULL );
        compare_diagonal_gradient( &of );
    }
    void test_diagonal_gradient_sqr()
    {
        VarianceTemplate of( NULL );
        compare_diagonal_gradient( &of );
    }

    void test_hessian_diag()
    {
        StdDevTemplate of( NULL );
        compare_numerical_hessian_diagonal( &of );
    }
    void test_hessian_diag_sqr()
    {
        VarianceTemplate of( NULL );
        compare_numerical_hessian_diagonal( &of );
    }

    void test_hessian_fails();
    void test_hessian_fails_sqr();

    void test_failed_metric_in_eval()
    {
        StdDevTemplate of( NULL );
        test_handles_qm_error( EVAL, &of );
    }
    void test_failed_metric_in_grad()
    {
        StdDevTemplate of( NULL );
        test_handles_qm_error( GRAD, &of );
    }
    void test_failed_metric_in_eval_sqr()
    {
        VarianceTemplate of( NULL );
        test_handles_qm_error( EVAL, &of );
    }
    void test_failed_metric_in_grad_sqr()
    {
        VarianceTemplate of( NULL );
        test_handles_qm_error( GRAD, &of );
    }

    void test_false_metric_in_eval()
    {
        StdDevTemplate of( NULL );
        test_handles_invalid_qm( EVAL, &of );
    }
    void test_false_metric_in_grad()
    {
        StdDevTemplate of( NULL );
        test_handles_invalid_qm( GRAD, &of );
    }
    void test_false_metric_in_eval_sqr()
    {
        VarianceTemplate of( NULL );
        test_handles_invalid_qm( EVAL, &of );
    }
    void test_false_metric_in_grad_sqr()
    {
        VarianceTemplate of( NULL );
        test_handles_invalid_qm( GRAD, &of );
    }

    void test_clone()
    {
        StdDevTemplate of( NULL );
        ObjectiveFunctionTests::test_clone( &of );
    }
    void test_clone_sqr()
    {
        VarianceTemplate of( NULL );
        ObjectiveFunctionTests::test_clone( &of );
    }

    void test_eval_negate()
    {
        StdDevTemplate of( NULL );
        test_negate_flag( EVAL, &of );
    }
    void test_eval_negate_sqr()
    {
        VarianceTemplate of( NULL );
        test_negate_flag( EVAL, &of );
    }
    void test_grad_negate()
    {
        StdDevTemplate of( NULL );
        test_negate_flag( GRAD, &of );
    }
    void test_grad_negate_sqr()
    {
        VarianceTemplate of( NULL );
        test_negate_flag( GRAD, &of );
    }
    void test_diag_negate()
    {
        StdDevTemplate of( NULL );
        test_negate_flag( DIAG, &of );
    }
    void test_diag_negate_sqr()
    {
        VarianceTemplate of( NULL );
        test_negate_flag( DIAG, &of );
    }
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdDevTemplateTest, "StdDevTemplateTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdDevTemplateTest, "Unit" );

static double std_dev_sqr( const double* array, unsigned len )
{
    double sum = 0, sqr_sum = 0;
    for( size_t i = 0; i < len; ++i )
    {
        sum += array[i];
        sqr_sum += array[i] * array[i];
    }

    return sqr_sum / len - ( sum / len ) * ( sum / len );
}

void StdDevTemplateTest::test_evaluate()
{
    StdDevTemplate OF( NULL );

    const double list1[] = { 1.0, 0.0, -5.0, 0.2, 6.0, 3 };
    const unsigned len1  = sizeof( list1 ) / sizeof( list1[0] );
    test_value( list1, len1, sqrt( std_dev_sqr( list1, len1 ) ), EVAL, &OF );

    const double list2[] = { 20, -30, 40, -50, 60, -70, 80, -90 };
    const unsigned len2  = sizeof( list2 ) / sizeof( list2[0] );
    test_value( list2, len2, sqrt( std_dev_sqr( list2, len2 ) ), EVAL, &OF );
}

void StdDevTemplateTest::test_evaluate_sqr()
{
    VarianceTemplate OF( NULL );

    const double list1[] = { 1.0, 0.0, -5.0, 0.2, 6.0, 3 };
    const unsigned len1  = sizeof( list1 ) / sizeof( list1[0] );
    test_value( list1, len1, std_dev_sqr( list1, len1 ), EVAL, &OF );

    const double list2[] = { 20, -30, 40, -50, 60, -70, 80, -90 };
    const unsigned len2  = sizeof( list2 ) / sizeof( list2[0] );
    test_value( list2, len2, std_dev_sqr( list2, len2 ), EVAL, &OF );
}

void StdDevTemplateTest::test_hessian_fails()
{
    MsqError err;
    double value;
    bool rval;
    vector< Vector3D > grad;
    MsqHessian Hess;
    Hess.initialize( patch(), err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );

    OFTestQM metric( &value, 1 );
    StdDevTemplate func( &metric );
    rval = func.evaluate_with_Hessian( ObjectiveFunction::CALCULATE, patch(), value, grad, Hess, err );<--- Variable 'rval' is assigned a value that is never used.
    CPPUNIT_ASSERT( err );
}

void StdDevTemplateTest::test_hessian_fails_sqr()
{
    MsqError err;
    double value;
    bool rval;
    vector< Vector3D > grad;
    MsqHessian Hess;
    Hess.initialize( patch(), err );
    CPPUNIT_ASSERT( !MSQ_CHKERR( err ) );

    OFTestQM metric( &value, 1 );
    VarianceTemplate func( &metric );
    rval = func.evaluate_with_Hessian( ObjectiveFunction::CALCULATE, patch(), value, grad, Hess, err );<--- Variable 'rval' is assigned a value that is never used.
    CPPUNIT_ASSERT( err );
}