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 | #include <iostream>
#include <QDir>
#include <QTextStream>
#include "GoldbachModel.h"
#include "GoldbachTester.h"
GoldbachTester::GoldbachTester(int &argc, char **argv)
: QCoreApplication(argc, argv)
{
}
int GoldbachTester::run()
{
if ( this->arguments().count() <= 1 )
return printHelp();
for ( int index = 1; index < this->arguments().count(); ++index )
this->testDirectory( this->arguments()[index] );
//std::cout << index << ": {" << qPrintable(this->arguments()[index]) << "}\n";
return this->exec();
}
int GoldbachTester::testDirectory(const QString &dirPath)
{
QDir dir(dirPath);
if ( not dir.exists() )
{
std::cerr << "Could not open directory " << qPrintable(dirPath) << std::endl;
return EXIT_FAILURE;
}
dir.setFilter(QDir::Files);
//dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
std::cout << qPrintable(dirPath) << ":" << std::endl;
for (int fileIndex = 0; fileIndex < list.size(); ++fileIndex)
this->testFile( list[fileIndex] );
return EXIT_SUCCESS;
}
int GoldbachTester::printHelp()
{
std::cout << "Usage: GoldbachTester <DIRECTORIES>\n";
return EXIT_FAILURE;
}
int GoldbachTester::testFile(const QFileInfo &fileInfo)
{
bool isValid = true;
long long number = fileInfo.baseName().toLongLong(&isValid);
if ( isValid )
{
return this->testContents(number, fileInfo);
}
else
{
std::cerr << "Error: invalid number in: " << qPrintable(fileInfo.fileName()) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int GoldbachTester::testContents(long long number, const QFileInfo &fileInfo)
{
std::cout << "Testing: " << qPrintable(fileInfo.fileName()) << "..." << std::endl;
GoldbachModel* goldbachModel = new GoldbachModel(this);
this->connect( goldbachModel, &GoldbachModel::calculationDone, this, &GoldbachTester::modelFinished );
this->models.insert(goldbachModel, fileInfo);
goldbachModel->calculate(number);
return EXIT_SUCCESS;
}
void GoldbachTester::modelFinished(long long sumCount)
{
Q_UNUSED(sumCount);
GoldbachModel* goldbachModel = dynamic_cast<GoldbachModel*>( sender() );
Q_ASSERT(goldbachModel);
const QFileInfo& fileInfo = this->models.value( goldbachModel, QFileInfo() );
Q_ASSERT( fileInfo.exists() );
this->compareContents(goldbachModel, fileInfo);
if ( ++this->finishedModelCount >= this->models.count() )
this->quit();
}
bool GoldbachTester::compareContents(GoldbachModel *goldbachModel, const QFileInfo &fileInfo)
{
const QVector<QString>& modelSums = goldbachModel->fetchAllSums();
const QVector<QString>& fileSums = loadLines(fileInfo);
if ( modelSums == fileSums )
return true;
std::cerr << "Test case failed: " << qPrintable(fileInfo.fileName()) << std::endl;
return false;
}
QVector<QString> GoldbachTester::loadLines(const QFileInfo &fileInfo)
{
QVector<QString> lines;
QFile file( fileInfo.absoluteFilePath() );
if ( ! file.open(QIODevice::ReadOnly | QIODevice::Text) )
{
std::cerr << "Could not open: " << qPrintable(fileInfo.fileName()) << std::endl;
return lines;
}
QTextStream textStream( &file );
QString currentLine;
while ( textStream.readLineInto(¤tLine) )
{
const QString& trimmed = currentLine.trimmed();
if ( trimmed.length() > 0 )
lines.append( currentLine );
}
return lines;
}
|