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
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2004 Sandia Corporation and Argonne National
    Laboratory.  Under the terms of Contract DE-AC04-94AL85000
    with Sandia Corporation, the U.S. Government retains certain
    rights in 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

    [email protected], [email protected], [email protected],
    [email protected], [email protected], [email protected]

  ***************************************************************** */
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/Outputter.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestFailure.h>
#include <cppunit/Test.h>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cstdlib>

#include <vector>
#include <iostream>
using namespace std;

#include "MsqFPE.hpp"

class CPPUNIT_API SummaryOutput : public CppUnit::Outputter
{
  public:
    SummaryOutput( FILE* file, CppUnit::TestResultCollector* result ) : file_( file ), results_( result ) {}
    void write();

  private:
    FILE* file_;
    CppUnit::TestResultCollector* results_;
};

int main( int argc, char** argv )
{
    CppUnit::Test* test;
    vector< CppUnit::Test* > test_list;
    CppUnit::TextUi::TestRunner runner;
    int firsttest = 1;
    bool list     = false;<--- Shadowed declaration
    MBMesquite::MsqFPE trap_fpe( true );

    // Check for command line arguments
    if( argc > 2 && !strcmp( argv[1], "-s" ) )
    {
        FILE* file = fopen( argv[2], "w" );
        if( !file )
        {
            perror( argv[2] );
            exit( 1 );
        }
        runner.setOutputter( new SummaryOutput( file, &runner.result() ) );
        firsttest += 2;
    }
    else if( argc > 1 && !strcmp( argv[1], "-l" ) )
    {
        ++firsttest;
        list = true;
    }

    // If the user requested a specific test...
    if( argc > firsttest )
    {
        while( argc > firsttest )
        {
            argc--;
            CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry( argv[argc] );
            test                                   = registry.makeTest();
            if( !test->countTestCases() )
            {
                std::cerr << argv[argc] << ": does not match any test or group" << std::endl;
                return 1;
            }
            test_list.push_back( test );
        }
    }
    // Otherwise do Unit and Regression suites
    else
    {
        test = CppUnit::TestFactoryRegistry::getRegistry( "Unit" ).makeTest();
        test_list.push_back( test );
        test = CppUnit::TestFactoryRegistry::getRegistry( "Regression" ).makeTest();
        test_list.push_back( test );
    }

    // If user just wants list of tests
    if( list )
    {
        for( vector< CppUnit::Test* >::iterator i = test_list.begin(); i != test_list.end(); ++i )
        {
            CppUnit::TestSuite* suite = dynamic_cast< CppUnit::TestSuite* >( *i );
            if( !suite )
            {
                cout << ( *i )->getName() << endl;
                continue;
            }
            const vector< CppUnit::Test* >& list = suite->getTests();<--- Shadow variable
            for( vector< CppUnit::Test* >::const_iterator j = list.begin(); j != list.end(); ++j )
                cout << ( *j )->getName() << endl;
        }
    }
    // Otherwise run the tests
    else
    {
        for( vector< CppUnit::Test* >::iterator i = test_list.begin(); i != test_list.end(); ++i )
            runner.addTest( *i );
        return !runner.run();
    }

    // Return 0 if there were no errors
    return 0;
}

void SummaryOutput::write()
{
    CppUnit::TestResultCollector::TestFailures fails = results_->failures();
    CppUnit::TestResultCollector::Tests tests        = results_->tests();

    CppUnit::TestResultCollector::TestFailures::const_iterator f_iter = fails.begin();<--- f_iter is initialized
    CppUnit::TestResultCollector::Tests::const_iterator t_iter;

    fprintf( file_, "****Tests Run:\n" );
    for( t_iter = tests.begin(); t_iter != tests.end(); ++t_iter )
        fprintf( file_, "%s\n", ( *t_iter )->getName().c_str() );

    fprintf( file_, "****Tests Failed:\n" );
    for( f_iter = fails.begin(); f_iter != fails.end(); ++f_iter )<--- f_iter is overwritten
        fprintf( file_, "%s\n", ( *f_iter )->failedTestName().c_str() );
}