Engauge Digitizer  2
 All Classes Functions Variables Typedefs Enumerations Friends Pages
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 
21 TestSegmentFill::TestSegmentFill(QObject *parent) :
22  QObject(parent)
23 {
24 }
25 
26 void TestSegmentFill::cleanupTestCase ()
27 {
28 
29 }
30 
31 void TestSegmentFill::initTestCase ()
32 {
33  const QString NO_ERROR_REPORT_LOG_FILE;
34  const QString NO_REGRESSION_OPEN_FILE;
35  const bool NO_GNUPLOT_LOG_FILES = false;
36  const bool NO_REGRESSION_IMPORT = false;
37  const bool NO_RESET = false;
38  const bool NO_EXPORT_ONLY = false;
39  const bool NO_EXTRACT_IMAGE_ONLY = false;
40  const QString NO_EXTRACT_IMAGE_EXTENSION;
41  const bool DEBUG_FLAG = false;
42  const QStringList NO_LOAD_STARTUP_FILES;
43  const QStringList NO_COMMAND_LINE;
44 
45  initializeLogging ("engauge_test",
46  "engauge_test.log",
47  DEBUG_FLAG);
48 
49  MainWindow m (NO_ERROR_REPORT_LOG_FILE,
50  NO_REGRESSION_OPEN_FILE,
51  NO_REGRESSION_IMPORT,
52  NO_GNUPLOT_LOG_FILES,
53  NO_RESET,
54  NO_EXPORT_ONLY,
55  NO_EXTRACT_IMAGE_ONLY,
56  NO_EXTRACT_IMAGE_EXTENSION,
57  NO_LOAD_STARTUP_FILES,
58  NO_COMMAND_LINE);
59  m.show ();
60 }
61 
62 void TestSegmentFill::testFindSegments()
63 {
64  const bool NO_GNUPLOT = false;
65  const bool NO_DLG = false;
66  const QString OUT_FILE_ACTUAL ("../test/test_segment_fill.gnuplot_actual");
67  const QString OUT_FILE_EXPECTED ("../test/test_segment_fill.gnuplot_expected");
68 
69  QList<Segment*> segments;
70 
71  // The relative paths in this method will fail unless the directory is correct
72  QDir::setCurrent (QApplication::applicationDirPath());
73 
74  QImage img ("../samples/corners.png");
75 
76  QGraphicsScene *scene = new QGraphicsScene;
77  SegmentFactory segmentFactory (*scene,
78  NO_GNUPLOT);
79 
80  DocumentModelSegments modelSegments;
81 
82  segmentFactory.clearSegments (segments);
83 
84  // This will crash if dialog box appears since QApplication is not executing and therefore cannot process events
85  segmentFactory.makeSegments (img,
86  modelSegments,
87  segments,
88  NO_DLG);
89 
90  // Open output file
91  QFile out (OUT_FILE_ACTUAL);
92  QTextStream outStr (&out);
93 
94  out.open(QIODevice::WriteOnly | QIODevice::Text);
95 
96  // Output to file
97  for (int indexS = 0; indexS < segments.count(); indexS++) {
98  Segment* segment = segments [indexS];
99 
100  QList<QPoint> points = segment->fillPoints (modelSegments);
101 
102  // Skip segments with only one point since they are apparently random
103  if (points.count() > 1) {
104 
105  for (int indexP = 0; indexP < points.count(); indexP++) {
106  QPoint point = points [indexP];
107 
108  // Output in gnuplot format for plotting. A space precedes each field. This can be plotted with
109  // plot "../test/test_segment_fill.gnuplot_actual" w lp
110  outStr << point.x() << " " << point.y() << endl;
111  }
112 
113  // Blank line between curves
114  outStr << endl;
115  }
116  }
117 
118  out.close();
119 
120  // Hash values
121  QCryptographicHash hashActual (QCryptographicHash::Sha1);
122  QCryptographicHash hashExpected (QCryptographicHash::Sha1);
123  QFile fileActual (OUT_FILE_ACTUAL);
124  QFile fileExpected (OUT_FILE_EXPECTED);
125 
126  bool success = false;
127  if (fileActual.open(QIODevice::ReadOnly) && fileExpected.open(QIODevice::ReadOnly)) {
128  hashActual.addData (fileActual.readAll());
129  hashExpected.addData (fileExpected.readAll());
130  QByteArray signatureActual = hashActual.result();
131  QByteArray signatureExpected = hashExpected.result();
132 
133  // Compare
134  success = (signatureActual == signatureExpected);
135  }
136 
137  QVERIFY (success);
138 }
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
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