You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.4 KiB
96 lines
2.4 KiB
#define private public
|
|
|
|
#include "algorithmtests.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include "include/mainwindow.h"
|
|
#include "include/graphwidget.h"
|
|
#include "include/node.h"
|
|
#include "include/edge.h"
|
|
|
|
static const double Pi = 3.14159265358979323846264338327950288419717;
|
|
|
|
AlgorithmTests::AlgorithmTests(QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
}
|
|
|
|
/** edge->paint() calculates the m_angle of th edge,
|
|
* but it is skipped so it must be done manually
|
|
*/
|
|
double angleOfPoints(const QPointF &a, const QPointF &b)
|
|
{
|
|
QLineF line(a, b);
|
|
double angle = ::acos(line.dx() / line.length());
|
|
if (line.dy() >= 0)
|
|
angle = 2 * Pi - angle;
|
|
|
|
return angle;
|
|
}
|
|
|
|
void AlgorithmTests::calculateBiggestAngle()
|
|
{
|
|
MainWindow *mainWindow = new MainWindow;
|
|
GraphWidget *graphWidget = new GraphWidget(mainWindow);
|
|
GraphLogic *graphLogic = new GraphLogic(graphWidget);
|
|
|
|
// no edges
|
|
Node *node1 = new Node(graphLogic);
|
|
node1->setPos(0,0);
|
|
QCOMPARE(node1->calculateBiggestAngle(), Pi * 1.5);
|
|
|
|
|
|
// one egde
|
|
// 1
|
|
Node *node2 = new Node(graphLogic);
|
|
node2->setPos(30,0);
|
|
|
|
Edge *edge1 = new Edge(node1, node2);
|
|
edge1->m_angle = angleOfPoints(node1->pos(), node2->pos());
|
|
|
|
QCOMPARE(edge1->angle(), 2 * Pi);
|
|
QCOMPARE(node1->calculateBiggestAngle(), - Pi);
|
|
QCOMPARE(node2->calculateBiggestAngle(), double(0));
|
|
|
|
// 2
|
|
node2->setPos(30,30);
|
|
edge1->m_angle = angleOfPoints(node1->pos(), node2->pos()); // 45
|
|
|
|
QCOMPARE(edge1->angle(), 1.75 * Pi);
|
|
QCOMPARE(node1->calculateBiggestAngle(), - 0.75 * Pi);
|
|
QCOMPARE(node2->calculateBiggestAngle(), 0.25 * Pi);
|
|
|
|
|
|
// more edges
|
|
node2->setPos(30,0);
|
|
edge1->m_angle = angleOfPoints(node1->pos(), node2->pos());
|
|
|
|
Node *node3 = new Node(graphLogic);
|
|
node3->setPos(-30,0);
|
|
Edge *edge2 = new Edge(node1, node3);
|
|
edge2->m_angle = angleOfPoints(node1->pos(), node3->pos());
|
|
|
|
Node *node4 = new Node(graphLogic);
|
|
node4->setPos(0, -30);
|
|
Edge *edge3 = new Edge(node1, node4);
|
|
edge3->m_angle = angleOfPoints(node1->pos(), node4->pos());
|
|
|
|
QCOMPARE(edge1->angle(), 2 * Pi);
|
|
QCOMPARE(edge2->angle(), Pi);
|
|
QCOMPARE(edge3->angle(), 0.5 * Pi);
|
|
QCOMPARE(node1->calculateBiggestAngle(), 0.5 * Pi);
|
|
|
|
delete node1;
|
|
delete node2;
|
|
delete node3;
|
|
delete node4;
|
|
|
|
delete graphLogic;
|
|
delete graphWidget;
|
|
delete mainWindow;
|
|
}
|
|
|
|
|
|
QTEST_MAIN(AlgorithmTests)
|