Engauge Digitizer  2
TestSegmentFill.cpp
1 #include <iostream>
2 #include "Logger.h"
3 #include "MainWindow.h"
4 #include <QCryptographicHash>
5 #include <QGraphicsScene>
6 #include <QGraphicsView>
7 #include <QList>
8 #include <qmath.h>
9 #include <QTextStream>
10 #include <QtTest/QtTest>
11 #include "Segment.h"
12 #include "SegmentFactory.h"
13 #include "Spline.h"
14 #include "SplinePair.h"
15 #include "Test/TestSegmentFill.h"
16 
17 QTEST_MAIN (TestSegmentFill)
18 
19 using namespace std;
20 
22  QObject(parent)
23 {
24 }
25 
26 void TestSegmentFill::cleanupTestCase ()
27 {
28 
29 }
30 
31 void TestSegmentFill::initTestCase ()
32 {
33  const bool NO_DROP_REGRESSION = false;
34  const QString NO_ERROR_REPORT_LOG_FILE;
35  const QString NO_REGRESSION_OPEN_FILE;
36  const bool NO_GNUPLOT_LOG_FILES = false;
37  const bool NO_REGRESSION_IMPORT = false;
38  const bool NO_RESET = false;
39  const bool NO_EXPORT_ONLY = false;
40  const bool NO_EXTRACT_IMAGE_ONLY = false;
41  const QString NO_EXTRACT_IMAGE_EXTENSION;
42  const bool DEBUG_FLAG = false;
43  const QStringList NO_LOAD_STARTUP_FILES;
44  const QStringList NO_COMMAND_LINE;
45 
46  initializeLogging ("engauge_test",
47  "engauge_test.log",
48  DEBUG_FLAG);
49 
50  MainWindow m (NO_ERROR_REPORT_LOG_FILE,
51  NO_REGRESSION_OPEN_FILE,
52  NO_DROP_REGRESSION,
53  NO_REGRESSION_IMPORT,
54  NO_GNUPLOT_LOG_FILES,
55  NO_RESET,
56  NO_EXPORT_ONLY,
57  NO_EXTRACT_IMAGE_ONLY,
58  NO_EXTRACT_IMAGE_EXTENSION,
59  NO_LOAD_STARTUP_FILES,
60  NO_COMMAND_LINE);
61  m.show ();
62 }
63 
64 void TestSegmentFill::testFindSegments()
65 {
66  const bool NO_GNUPLOT = false;
67  const bool NO_DLG = false;
68  const QString OUT_FILE_ACTUAL ("../test/test_segment_fill.gnuplot_actual");
69  const QString OUT_FILE_EXPECTED ("../test/test_segment_fill.gnuplot_expected");
70 
71  QList<Segment*> segments;
72 
73  // The relative paths in this method will fail unless the directory is correct
74  QDir::setCurrent (QApplication::applicationDirPath());
75 
76  QImage img ("../samples/corners.png");
77 
78  QGraphicsScene *scene = new QGraphicsScene;
79  SegmentFactory segmentFactory (*scene,
80  NO_GNUPLOT);
81 
82  DocumentModelSegments modelSegments;
83 
84  segmentFactory.clearSegments (segments);
85 
86  // This will crash if dialog box appears since QApplication is not executing and therefore cannot process events
87  segmentFactory.makeSegments (img,
88  modelSegments,
89  segments,
90  NO_DLG);
91 
92  // Open output file
93  QFile out (OUT_FILE_ACTUAL);
94  QTextStream outStr (&out);
95 
96  out.open(QIODevice::WriteOnly | QIODevice::Text);
97 
98  // Output to file
99  for (int indexS = 0; indexS < segments.count(); indexS++) {
100  Segment* segment = segments [indexS];
101 
102  QList<QPoint> points = segment->fillPoints (modelSegments);
103 
104  // Skip segments with only one point since they are apparently random
105  if (points.count() > 1) {
106 
107  for (int indexP = 0; indexP < points.count(); indexP++) {
108  QPoint point = points [indexP];
109 
110  // Output in gnuplot format for plotting. A space precedes each field. This can be plotted with
111  // plot "../test/test_segment_fill.gnuplot_actual" w lp
112  outStr << point.x() << " " << point.y() << endl;
113  }
114 
115  // Blank line between curves
116  outStr << endl;
117  }
118  }
119 
120  out.close();
121 
122  // Hash values
123  QCryptographicHash hashActual (QCryptographicHash::Sha1);
124  QCryptographicHash hashExpected (QCryptographicHash::Sha1);
125  QFile fileActual (OUT_FILE_ACTUAL);
126  QFile fileExpected (OUT_FILE_EXPECTED);
127 
128  bool success = false;
129  if (fileActual.open(QIODevice::ReadOnly) && fileExpected.open(QIODevice::ReadOnly)) {
130  hashActual.addData (fileActual.readAll());
131  hashExpected.addData (fileExpected.readAll());
132  QByteArray signatureActual = hashActual.result();
133  QByteArray signatureExpected = hashExpected.result();
134 
135  // Compare
136  success = (signatureActual == signatureExpected);
137  }
138 
139  QVERIFY (success);
140 }
Factory class for Segment objects.
Unit test of segment fill feature.
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:21
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments)
Create evenly spaced points along the segment.
Definition: Segment.cpp:205
TestSegmentFill(QObject *parent=0)
Single constructor.
Model for DlgSettingsSegments and CmdSettingsSegments.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...
Definition: MainWindow.h:91