Engauge Digitizer  2
TestExport.cpp
1 #include "CurveConnectAs.h"
2 #include "Document.h"
3 #include "DocumentModelExportFormat.h"
4 #include "ExportFileFunctions.h"
5 #include "ExportFileRelations.h"
6 #include "ExportValuesXOrY.h"
7 #include "LineStyle.h"
8 #include "Logger.h"
9 #include "MainWindow.h"
10 #include "MainWindowModel.h"
11 #include "PointStyle.h"
12 #include <QImage>
13 #include <qmath.h>
14 #include <QtTest/QtTest>
15 #include "Spline.h"
16 #include "SplinePair.h"
17 #include "Test/TestExport.h"
18 #include "Transformation.h"
19 
20 QTEST_MAIN (TestExport)
21 
22 using namespace std;
23 
24 const bool NOT_USING_GNUPLOT = false;
25 
26 TestExport::TestExport(QObject *parent) :
27  QObject(parent),
28  m_document (0)
29 {
30 }
31 
32 bool TestExport::checkCommasInFunctionsForDelimiter (ExportDelimiter delimiter,
33  QLocale::Country country,
34  QString &output)
35 {
36  initData (false,
37  delimiter,
38  country);
39 
40  QTextStream str (&output);
41  bool success = true;
42  QString dummy;
43 
44  try {
45  bool isLogXTheta = (m_modelCoords.coordScaleXTheta() == COORD_SCALE_LOG);
46  bool isLogYRadius = (m_modelCoords.coordScaleYRadius() == COORD_SCALE_LOG);
47  unsigned int numWritesSoFar = 0;
48 
49  ExportFileFunctions exportFile;
50  exportFile.exportAllPerLineXThetaValuesMerged (m_modelExportOverride,
51  *m_document,
52  m_modelMainWindow,
53  m_curvesIncluded,
54  m_xThetaValues,
55  exportDelimiterToText (delimiter, NOT_USING_GNUPLOT),
56  m_transformation,
57  isLogXTheta,
58  isLogYRadius,
59  str,
60  numWritesSoFar);
61  }
62  catch (...)
63  {
64  success = false;
65  }
66 
67  return (success);
68 }
69 
70 bool TestExport::checkCommasInRelationsForDelimiter (ExportDelimiter delimiter,
71  QLocale::Country country,
72  QString &output)
73 {
74  initData (false,
75  delimiter,
76  country);
77 
78  QTextStream str (&output);
79  bool success = true;
80  QString dummy;
81 
82  try {
83  bool isLogXTheta = (m_modelCoords.coordScaleXTheta() == COORD_SCALE_LOG);
84  bool isLogYRadius = (m_modelCoords.coordScaleYRadius() == COORD_SCALE_LOG);
85  unsigned int numWritesSoFar = 0;
86 
87  ExportFileRelations exportFile;
88  exportFile.exportAllPerLineXThetaValuesMerged (m_modelExportOverride,
89  *m_document,
90  m_modelMainWindow,
91  m_curvesIncluded,
92  exportDelimiterToText (delimiter, NOT_USING_GNUPLOT),
93  m_transformation,
94  isLogXTheta,
95  isLogYRadius,
96  str,
97  numWritesSoFar);
98  }
99  catch (...)
100  {
101  success = false;
102  }
103 
104  return (success);
105 }
106 
107 void TestExport::cleanupTestCase ()
108 {
109 }
110 
111 void TestExport::initData (bool isLog,
112  ExportDelimiter delimiter,
113  QLocale::Country country)
114 {
115  const QString CURVE_NAME ("Curve1");
116  const int LINE_WIDTH = 2, POINT_LINE_WIDTH = 1;
117  const ColorPalette LINE_COLOR = COLOR_PALETTE_BLACK, POINT_COLOR = COLOR_PALETTE_BLACK;
118  const int RADIUS = 5;
119 
120  QImage nullImage;
121  m_document = new Document (nullImage);
122 
123  m_curvesIncluded.clear ();
124  m_curvesIncluded << CURVE_NAME;
125 
126  CurveStyles curveStyles (m_document->coordSystem ());
127  CurveStyle curveStyle (LineStyle (LINE_WIDTH,
128  LINE_COLOR,
129  CONNECT_AS_FUNCTION_SMOOTH),
130  PointStyle (POINT_SHAPE_CIRCLE,
131  RADIUS,
132  POINT_LINE_WIDTH,
133  POINT_COLOR));
134  curveStyles.setCurveStyle (CURVE_NAME, curveStyle);
135  if (isLog) {
136  m_modelCoords.setCoordScaleXTheta (COORD_SCALE_LOG);
137  m_modelCoords.setCoordScaleYRadius (COORD_SCALE_LOG);
138  } else {
139  m_modelCoords.setCoordScaleXTheta (COORD_SCALE_LINEAR);
140  m_modelCoords.setCoordScaleYRadius (COORD_SCALE_LINEAR);
141  }
142  m_modelCoords.setCoordsType (COORDS_TYPE_CARTESIAN);
143  m_modelGeneral.setCursorSize (3);
144  m_modelGeneral.setExtraPrecision (1);
145 
146  m_modelExportOverride.setPointsSelectionFunctions (EXPORT_POINTS_SELECTION_FUNCTIONS_INTERPOLATE_FIRST_CURVE);
147  m_modelExportOverride.setDelimiter (delimiter);
148 
149  m_document->setModelExport (m_modelExportOverride);
150  m_document->setModelCurveStyles (curveStyles);
151  m_document->setModelCoords (m_modelCoords);
152  m_document->setModelGeneral (m_modelGeneral);
153 
154  m_modelMainWindow.setLocale (QLocale::English,
155  country);
156 
157  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (845, 305), "Curve1\t0", 0);
158  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (850, 294), "Curve1\t1", 1);
159  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (862, 272), "Curve1\t2", 2);
160  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (876, 244), "Curve1\t3", 3);
161  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (887, 222), "Curve1\t4", 4);
162  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (899, 200), "Curve1\t5", 5);
163  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (910, 177), "Curve1\t6", 6);
164  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (922, 155), "Curve1\t7", 7);
165  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (933, 133), "Curve1\t8", 8);
166  m_document->addPointGraphWithSpecifiedIdentifier (CURVE_NAME, QPointF (945, 110), "Curve1\t9", 9);
167 
168  m_xThetaValues.clear ();
169  m_xThetaValues << 0.0009999; // Just outside of the coordinates added to the document
170  m_xThetaValues << 0.001;
171  m_xThetaValues << 0.01;
172  m_xThetaValues << 0.1;
173  m_xThetaValues << 1;
174  m_xThetaValues << 10;
175  m_xThetaValues << 100;
176  m_xThetaValues << 1000;
177  QTransform matrixScreen ( 51, 52, 1123,
178  548, 10, 548,
179  1, 1, 1);
180  QTransform matrixGraph (qLn (0.001), qLn (0.001), qLn (1000),
181  qLn ( 1), qLn ( 100), qLn ( 1),
182  1, 1, 1);
183  m_transformation.updateTransformFromMatrices (matrixScreen,
184  matrixGraph);
185  m_transformation.setModelCoords (m_modelCoords,
186  m_modelGeneral,
187  m_modelMainWindow);
188 }
189 
190 void TestExport::initTestCase ()
191 {
192  const QString NO_ERROR_REPORT_LOG_FILE;
193  const QString NO_REGRESSION_OPEN_FILE;
194  const bool NO_GNUPLOT_LOG_FILES = false;
195  const bool NO_REGRESSION_IMPORT = false;
196  const bool NO_RESET = false;
197  const bool DEBUG_FLAG = false;
198  const QStringList NO_LOAD_STARTUP_FILES;
199 
200  initializeLogging ("engauge_test",
201  "engauge_test.log",
202  DEBUG_FLAG);
203 
204  MainWindow w (NO_ERROR_REPORT_LOG_FILE,
205  NO_REGRESSION_OPEN_FILE,
206  NO_GNUPLOT_LOG_FILES,
207  NO_REGRESSION_IMPORT,
208  NO_RESET,
209  NO_LOAD_STARTUP_FILES);
210 
211  w.show ();
212 }
213 
214 void TestExport::testCommasInFunctionsForCommasSwitzerland ()
215 {
216  QString outputExpectedIfCommaSeparator =
217  "x,Curve1\n"
218  "\"0,001\",\"-1,27563\"\n"
219  "\"0,001\",\"-1,27563\"\n"
220  "\"0,01\",\"-1,26683\"\n"
221  "\"0,1\",\"-1,17881\"\n"
222  "1,\"-0,29658\"\n"
223  "10,\"-706,15184\"\n"
224  "100,\"-3997814,14355\"\n"
225  "1000,\"-4541901224,06376\"\n";
226  QString outputExpectedIfPeriodSeparator =
227  "x,Curve1\n"
228  "0.001,-1.27563\n"
229  "0.001,-1.27563\n"
230  "0.01,-1.26683\n"
231  "0.1,-1.17881\n"
232  "1,-0.29658\n"
233  "10,-706.15184\n"
234  "100,-3997814.14355\n"
235  "1000,-4541901224.06376\n";
236 
237  QString outputGot;
238  bool success = checkCommasInFunctionsForDelimiter (EXPORT_DELIMITER_COMMA,
239  QLocale::Switzerland,
240  outputGot);
241  if (!success) {
242  outputGot = "";
243  }
244 
245  QVERIFY (outputGot == outputExpectedIfCommaSeparator ||
246  outputGot == outputExpectedIfPeriodSeparator);
247 }
248 
249 void TestExport::testCommasInFunctionsForCommasUnitedStates ()
250 {
251  QString outputExpected =
252  "x,Curve1\n"
253  "0.001,-1.27563\n"
254  "0.001,-1.27563\n"
255  "0.01,-1.26683\n"
256  "0.1,-1.17881\n"
257  "1,-0.29658\n"
258  "10,-706.15184\n"
259  "100,-3997814.14355\n"
260  "1000,-4541901224.06376\n";
261 
262  QString outputGot;
263  bool success = checkCommasInFunctionsForDelimiter (EXPORT_DELIMITER_COMMA,
264  QLocale::UnitedStates,
265  outputGot);
266  if (!success) {
267  outputGot = "";
268  }
269 
270  QVERIFY (outputGot == outputExpected);
271 }
272 
273 void TestExport::testCommasInFunctionsForTabsSwitzerland ()
274 {
275  QString outputExpectedIfCommaSeparator =
276  "x\tCurve1\n"
277  "0,001\t-1,27563\n"
278  "0,001\t-1,27563\n"
279  "0,01\t-1,26683\n"
280  "0,1\t-1,17881\n"
281  "1\t-0,29658\n"
282  "10\t-706,15184\n"
283  "100\t-3997814,14355\n"
284  "1000\t-4541901224,06376\n";
285  QString outputExpectedIfPeriodSeparator =
286  "x\tCurve1\n"
287  "0.001\t-1.27563\n"
288  "0.001\t-1.27563\n"
289  "0.01\t-1.26683\n"
290  "0.1\t-1.17881\n"
291  "1\t-0.29658\n"
292  "10\t-706.15184\n"
293  "100\t-3997814.14355\n"
294  "1000\t-4541901224.06376\n";
295 
296  QString outputGot;
297  bool success = checkCommasInFunctionsForDelimiter (EXPORT_DELIMITER_TAB,
298  QLocale::Switzerland,
299  outputGot);
300  if (!success) {
301  outputGot = "";
302  }
303 
304  QVERIFY (outputGot == outputExpectedIfCommaSeparator ||
305  outputGot == outputExpectedIfPeriodSeparator);
306 }
307 
308 void TestExport::testCommasInFunctionsForTabsUnitedStates ()
309 {
310  QString outputExpected =
311  "x\tCurve1\n"
312  "0.001\t-1.27563\n"
313  "0.001\t-1.27563\n"
314  "0.01\t-1.26683\n"
315  "0.1\t-1.17881\n"
316  "1\t-0.29658\n"
317  "10\t-706.15184\n"
318  "100\t-3997814.14355\n"
319  "1000\t-4541901224.06376\n";
320 
321  QString outputGot;
322  bool success = checkCommasInFunctionsForDelimiter (EXPORT_DELIMITER_TAB,
323  QLocale::UnitedStates,
324  outputGot);
325  if (!success) {
326  outputGot = "";
327  }
328 
329  QVERIFY (outputGot == outputExpected);
330 }
331 
332 void TestExport::testCommasInRelationsForCommasSwitzerland ()
333 {
334  QString outputExpectedIfCommaSeparator =
335  "x,Curve1\n"
336  "\"3,3192\",\"2,08003\"\n"
337  "\"3,3723\",\"2,15796\"\n"
338  "\"3,432\",\"2,23368\"\n"
339  "\"3,4935\",\"2,30883\"\n"
340  "\"3,5539\",\"2,38438\"\n"
341  "\"3,6113\",\"2,46094\"\n"
342  "\"3,6687\",\"2,5375\"\n"
343  "\"3,7261\",\"2,61406\"\n"
344  "\"3,7836\",\"2,69062\"\n"
345  "\"3,841\",\"2,76718\"\n"
346  "\"3,9012\",\"2,84276\"\n"
347  "\"3,9628\",\"2,91791\"\n"
348  "\"4,0231\",\"2,99345\"\n"
349  "\"4,0785\",\"3,07067\"\n"
350  "\"4,1339\",\"3,14789\"\n"
351  "\"4,1932\",\"3,22378\"\n"
352  "\"4,2547\",\"3,29893\"\n"
353  "\"4,3156\",\"3,37426\"\n"
354  "\"4,3731\",\"3,45082\"\n"
355  "\"4,4305\",\"3,52738\"\n"
356  "\"4,4892\",\"3,60349\"\n"
357  "\"4,5486\",\"3,67938\"\n";
358  QString outputExpectedIfPeriodSeparator =
359  "x,Curve1\n"
360  "3.3192,2.08003\n"
361  "3.3723,2.15796\n"
362  "3.432,2.23368\n"
363  "3.4935,2.30883\n"
364  "3.5539,2.38438\n"
365  "3.6113,2.46094\n"
366  "3.6687,2.5375\n"
367  "3.7261,2.61406\n"
368  "3.7836,2.69062\n"
369  "3.841,2.76718\n"
370  "3.9012,2.84276\n"
371  "3.9628,2.91791\n"
372  "4.0231,2.99345\n"
373  "4.0785,3.07067\n"
374  "4.1339,3.14789\n"
375  "4.1932,3.22378\n"
376  "4.2547,3.29893\n"
377  "4.3156,3.37426\n"
378  "4.3731,3.45082\n"
379  "4.4305,3.52738\n"
380  "4.4892,3.60349\n"
381  "4.5486,3.67938\n";
382 
383  QString outputGot;
384  bool success = checkCommasInRelationsForDelimiter (EXPORT_DELIMITER_COMMA,
385  QLocale::Switzerland,
386  outputGot);
387  if (!success) {
388  outputGot = "";
389  }
390 
391  QVERIFY (outputGot == outputExpectedIfCommaSeparator ||
392  outputGot == outputExpectedIfPeriodSeparator);
393 }
394 
395 void TestExport::testCommasInRelationsForCommasUnitedStates ()
396 {
397  QString outputExpected =
398  "x,Curve1\n"
399  "3.3192,2.08003\n"
400  "3.3723,2.15796\n"
401  "3.432,2.23368\n"
402  "3.4935,2.30883\n"
403  "3.5539,2.38438\n"
404  "3.6113,2.46094\n"
405  "3.6687,2.5375\n"
406  "3.7261,2.61406\n"
407  "3.7836,2.69062\n"
408  "3.841,2.76718\n"
409  "3.9012,2.84276\n"
410  "3.9628,2.91791\n"
411  "4.0231,2.99345\n"
412  "4.0785,3.07067\n"
413  "4.1339,3.14789\n"
414  "4.1932,3.22378\n"
415  "4.2547,3.29893\n"
416  "4.3156,3.37426\n"
417  "4.3731,3.45082\n"
418  "4.4305,3.52738\n"
419  "4.4892,3.60349\n"
420  "4.5486,3.67938\n";
421 
422  QString outputGot;
423  bool success = checkCommasInRelationsForDelimiter (EXPORT_DELIMITER_COMMA,
424  QLocale::UnitedStates,
425  outputGot);
426  if (!success) {
427  outputGot = "";
428  }
429 
430  QVERIFY (outputGot == outputExpected);
431 }
432 
433 void TestExport::testCommasInRelationsForTabsSwitzerland ()
434 {
435  QString outputExpectedIfCommaSeparator =
436  "x\tCurve1\n"
437  "3,3192\t2,08003\n"
438  "3,3723\t2,15796\n"
439  "3,432\t2,23368\n"
440  "3,4935\t2,30883\n"
441  "3,5539\t2,38438\n"
442  "3,6113\t2,46094\n"
443  "3,6687\t2,5375\n"
444  "3,7261\t2,61406\n"
445  "3,7836\t2,69062\n"
446  "3,841\t2,76718\n"
447  "3,9012\t2,84276\n"
448  "3,9628\t2,91791\n"
449  "4,0231\t2,99345\n"
450  "4,0785\t3,07067\n"
451  "4,1339\t3,14789\n"
452  "4,1932\t3,22378\n"
453  "4,2547\t3,29893\n"
454  "4,3156\t3,37426\n"
455  "4,3731\t3,45082\n"
456  "4,4305\t3,52738\n"
457  "4,4892\t3,60349\n"
458  "4,5486\t3,67938\n";
459  QString outputExpectedIfPeriodSeparator =
460  "x\tCurve1\n"
461  "3.3192\t2.08003\n"
462  "3.3723\t2.15796\n"
463  "3.432\t2.23368\n"
464  "3.4935\t2.30883\n"
465  "3.5539\t2.38438\n"
466  "3.6113\t2.46094\n"
467  "3.6687\t2.5375\n"
468  "3.7261\t2.61406\n"
469  "3.7836\t2.69062\n"
470  "3.841\t2.76718\n"
471  "3.9012\t2.84276\n"
472  "3.9628\t2.91791\n"
473  "4.0231\t2.99345\n"
474  "4.0785\t3.07067\n"
475  "4.1339\t3.14789\n"
476  "4.1932\t3.22378\n"
477  "4.2547\t3.29893\n"
478  "4.3156\t3.37426\n"
479  "4.3731\t3.45082\n"
480  "4.4305\t3.52738\n"
481  "4.4892\t3.60349\n"
482  "4.5486\t3.67938\n";
483 
484  QString outputGot;
485  bool success = checkCommasInRelationsForDelimiter (EXPORT_DELIMITER_TAB,
486  QLocale::Switzerland,
487  outputGot);
488  if (!success) {
489  outputGot = "";
490  }
491 
492  QVERIFY (outputGot == outputExpectedIfCommaSeparator ||
493  outputGot == outputExpectedIfPeriodSeparator);
494 }
495 
496 void TestExport::testCommasInRelationsForTabsUnitedStates ()
497 {
498  QString outputExpected =
499  "x\tCurve1\n"
500  "3.3192\t2.08003\n"
501  "3.3723\t2.15796\n"
502  "3.432\t2.23368\n"
503  "3.4935\t2.30883\n"
504  "3.5539\t2.38438\n"
505  "3.6113\t2.46094\n"
506  "3.6687\t2.5375\n"
507  "3.7261\t2.61406\n"
508  "3.7836\t2.69062\n"
509  "3.841\t2.76718\n"
510  "3.9012\t2.84276\n"
511  "3.9628\t2.91791\n"
512  "4.0231\t2.99345\n"
513  "4.0785\t3.07067\n"
514  "4.1339\t3.14789\n"
515  "4.1932\t3.22378\n"
516  "4.2547\t3.29893\n"
517  "4.3156\t3.37426\n"
518  "4.3731\t3.45082\n"
519  "4.4305\t3.52738\n"
520  "4.4892\t3.60349\n"
521  "4.5486\t3.67938\n";
522 
523  QString outputGot;
524  bool success = checkCommasInRelationsForDelimiter (EXPORT_DELIMITER_TAB,
525  QLocale::UnitedStates,
526  outputGot);
527  if (!success) {
528  outputGot = "";
529  }
530 
531  QVERIFY (outputGot == outputExpected);
532 }
533 
534 void TestExport::testLogExtrapolationFunctionsAll ()
535 {
536  initData (true,
537  EXPORT_DELIMITER_COMMA,
538  QLocale::UnitedStates);
539 
540  bool success = true;
541  QString dummy;
542  QTextStream str (&dummy);
543 
544  try {
545  bool isLogXTheta = (m_modelCoords.coordScaleXTheta() == COORD_SCALE_LOG);
546  bool isLogYRadius = (m_modelCoords.coordScaleYRadius() == COORD_SCALE_LOG);
547  unsigned int numWritesSoFar = 0;
548 
549  ExportFileFunctions exportFile;
550  exportFile.exportAllPerLineXThetaValuesMerged (m_modelExportOverride,
551  *m_document,
552  m_modelMainWindow,
553  m_curvesIncluded,
554  m_xThetaValues,
555  " ",
556  m_transformation,
557  isLogXTheta,
558  isLogYRadius,
559  str,
560  numWritesSoFar);
561  }
562  catch (...)
563  {
564  success = false;
565  }
566 
567  QVERIFY (success);
568 }
void setPointsSelectionFunctions(ExportPointsSelectionFunctions exportPointsSelectionFunctions)
Set method for point selection for functions.
const CoordSystem & coordSystem() const
Currently active CoordSystem.
Definition: Document.cpp:297
void setCursorSize(int cursorSize)
Set method for effective cursor size.
void setModelGeneral(const DocumentModelGeneral &modelGeneral)
Set method for DocumentModelGeneral.
Definition: Document.cpp:1008
Model for DlgSettingsCurveProperties and CmdSettingsCurveProperties.
Definition: CurveStyles.h:22
void addPointGraphWithSpecifiedIdentifier(const QString &curveName, const QPointF &posScreen, const QString &identifier, double ordinal)
Add a single graph point with the specified point identifer. Note that PointStyle is not applied to t...
Definition: Document.cpp:205
void setCoordScaleYRadius(CoordScale coordScale)
Set method for linear/log scale on y/radius.
void setModelCoords(const DocumentModelCoords &modelCoords)
Set method for DocumentModelCoords.
Definition: Document.cpp:970
void setDelimiter(ExportDelimiter exportDelimiter)
Set method for delimiter.
void setLocale(QLocale::Language language, QLocale::Country country)
Set method for locale given attributes.
Unit test of Export classes.
Definition: TestExport.h:15
TestExport(QObject *parent=0)
Single constructor.
Definition: TestExport.cpp:26
void setModelExport(const DocumentModelExportFormat &modelExport)
Set method for DocumentModelExportFormat.
Definition: Document.cpp:1001
Details for a specific Point.
Definition: PointStyle.h:20
Strategy class for exporting to a file. This strategy is external to the Document class so that class...
void setModelCurveStyles(const CurveStyles &modelCurveStyles)
Set method for CurveStyles.
Definition: Document.cpp:977
CoordScale coordScaleXTheta() const
Get method for linear/log scale on x/theta.
Container for LineStyle and PointStyle for one Curve.
Definition: CurveStyle.h:18
Storage of one imported image and the data attached to that image.
Definition: Document.h:41
Details for a specific Line.
Definition: LineStyle.h:19
CoordScale coordScaleYRadius() const
Get method for linear/log scale on y/radius.
Strategy class for exporting to a file. This strategy is external to the Document class so that class...
void setExtraPrecision(int extraPrecision)
Set method for extra digits of precision.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:89
void setCoordScaleXTheta(CoordScale coordScale)
Set method for linear/log scale on x/theta.
void setCoordsType(CoordsType coordsType)
Set method for coordinates type.