compareTool.cc 7.2 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper 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 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/
Jan Möbius's avatar
Jan Möbius committed
34

Jan Möbius's avatar
Jan Möbius committed
35
36
37
38
39
40
41
/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/
42

Jan Möbius's avatar
Jan Möbius committed
43
44
45
#include <QString> 
#include <QStringList> 
#include <QSettings>
46
47
#include <QFileInfo>
#include <QTextStream>
Jan Möbius's avatar
Jan Möbius committed
48

49
#include <cmath>
50
#include <limits>
51
52
#include <iostream>

53
54
55
56
57
58
59
60
61
62

/* Test File specification:
Doubles can be given as:

Exact test:
VALUE_DOUBLE=1.0

Tolerance test (0.0001)
VALUE_DOUBLE=1.0,0.0001

Jan Möbius's avatar
Jan Möbius committed
63

64
*/
Jan Möbius's avatar
Jan Möbius committed
65
66


Jan Möbius's avatar
Jan Möbius committed
67
bool compareDouble(QString _key , QVariant _result, QVariant _reference) {
Jan Möbius's avatar
Jan Möbius committed
68

Jan Möbius's avatar
Jan Möbius committed
69
  // maximal allowed double tolerance
Matthias Möller's avatar
Matthias Möller committed
70
  double tolerance = std::numeric_limits<double>::epsilon();
Jan Möbius's avatar
Jan Möbius committed
71
72
73

  // Check if the reference consists of two colon separated values
  // Second value would specify maximal allowed tolerance
74
  QStringList referenceData = _reference.toString().split(':');
Jan Möbius's avatar
Jan Möbius committed
75
76
77
78
79
80
81
  if ( referenceData.size() == 2) {
    tolerance = referenceData[1].toDouble();
  }


  double result    = _result.toDouble();
  double reference = referenceData[0].toDouble();
Jan Möbius's avatar
Jan Möbius committed
82

Jan Möbius's avatar
Jan Möbius committed
83
84
85
  if ( fabs(result-reference) <= tolerance )  {
    return true;
  } else {
86

87
88
89
90
91
92
93
94
95
    //qcerr.setRealNumberPrecision(40);
	std::cerr.precision(40);
    std::cerr << "===================================================================\n";
	std::cerr << "Comparison failed for key " <<  _key.toStdString() << " :\n";
    std::cerr << "Result:                " << result << "\n";
    std::cerr << "Expected:              " << reference << "\n";
    std::cerr << "Difference:            " << fabs(result-reference) << "\n";
    std::cerr << "Allowed tolerance was: " << tolerance << "\n";
    std::cerr << "===================================================================\n";
Jan Möbius's avatar
Jan Möbius committed
96
97
    return false;
  }
Jan Möbius's avatar
Jan Möbius committed
98
99
100
  
}

Jan Möbius's avatar
Jan Möbius committed
101
bool compareString(QString _key ,QVariant _result, QVariant _reference) {
Jan Möbius's avatar
Jan Möbius committed
102
103
104
105

  QString resultStr = _result.toString().simplified();
  QString resultRef = _reference.toString().simplified();
  
Jan Möbius's avatar
Jan Möbius committed
106
107
108
  if (resultStr == resultRef ) {
    return true;
  } else {
109
110
	std::cerr << "Comparison failed for key " <<  _key.toStdString() << " :\n";
    std::cerr << "Result: " << resultStr.toStdString() << " ; Expected: " << resultRef.toStdString() << "\n";
Jan Möbius's avatar
Jan Möbius committed
111
112
113
    return false;
  }

Jan Möbius's avatar
Jan Möbius committed
114
115
116
117
}

int main(int argv, char **args)
{
118
119
  std::cout << "============================================================\n" ;
  std::cout << "Executing compare tool\n";
120
  std::cout << "Comparing results to reference:\n" ;
Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
125
126

  // Flag if everything went fine
  bool ok = true;

  // Return if we did not get exactly two arguments
  if ( argv != 3 ) {
127
128
129
    std::cerr << "Wrong number of arguments!\n";
    std::cerr << "Usage:\n";
    std::cerr << "compareTool ResultFile ReferenceFile\n";
Jan Möbius's avatar
Jan Möbius committed
130
131
132
133
134
135
    return(1);
  }

  QString file1(args[1]);
  QString file2(args[2]);

136
137
  QFileInfo resultFileInfo(file1);
  if ( !resultFileInfo.exists() ) {
138
139
    std::cerr << "ERROR! Result file: " << file1.toStdString() << " does not exist!\n";

140
141
142
143
144
    return 1;
  }

  QFileInfo referenceFileInfo(file2);
  if ( !referenceFileInfo.exists() ) {
145
    std::cerr << "ERROR! Reference file: " << file2.toStdString() << " does not exist!\n";
146
147
148
    return 1;
  }

Jan Möbius's avatar
Jan Möbius committed
149
150
151
  QSettings resultFile(file1,QSettings::IniFormat);
  QSettings referenceFile(file2,QSettings::IniFormat);
  
152
  if ( resultFile.status() != QSettings::NoError) {
153
    std::cerr << "QSettings error when opening result file: " << file1.toStdString() << "\n";
154
155
156
157
    return 1;
  }

  if ( referenceFile.status() != QSettings::NoError) {
158
    std::cerr << "QSettings error when opening result reference file: " << file2.toStdString() << "\n";
159
160
161
    return 1;
  }

Jan Möbius's avatar
Jan Möbius committed
162
163
164
165
166
167
  QStringList toplevelKeys = referenceFile.childKeys();
  QStringList groups       = referenceFile.childGroups();
   
  if ( groups.size() == 0 ) {
   for ( int i = 0 ; i < toplevelKeys.size(); ++i) {
     if ( resultFile.contains(toplevelKeys[i]) ) {
Jan Möbius's avatar
Jan Möbius committed
168
169
170
       if ( toplevelKeys[i].endsWith("_DOUBLE") ) {
         ok &= compareDouble(toplevelKeys[i],resultFile.value(toplevelKeys[i]), referenceFile.value(toplevelKeys[i]));
       } else
171
         ok &= compareString( toplevelKeys[i],resultFile.value(toplevelKeys[i]), referenceFile.value(toplevelKeys[i]));
Jan Möbius's avatar
Jan Möbius committed
172
     } else {
173
       std::cerr << "Missing key in result file: " << toplevelKeys[i].toStdString() << "\n";
Jan Möbius's avatar
Jan Möbius committed
174
175
176
177
178
179
180
       ok = false;
     }
 
   }   
 

  } else {
181
    std::cerr << "Multiple levels!" << "\n";
182
    return 1;
Jan Möbius's avatar
Jan Möbius committed
183
184
185
  }
  

186
  if ( ! ok ) {
187
    std::cerr << "At least one of the tests failed!\n";
188
189
190
    return 1;
  }

Jan Möbius's avatar
Jan Möbius committed
191
192
  return(0);
}