Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/tests/test_mathutils.cpp
T
torstenandCopilot 09c244ff18 refactor: clean up verbose migration comments
Remove redundant Qt6 migration comments that restate obvious API changes:
- QtConcurrent::run parameter order explanations (preserved where lambda wrapping needed)
- QStringRef/QStringView API changes (shortened to essential info)
- QComboBox::activated signal signature change
- QMap::unite() removal semantics
- QAction include path change
- QMouseEvent/QWheelEvent position() API
- QList/QVector equivalence in Qt6
- QFuture continuation API
- Code generation comments

Preserved comments explaining non-obvious Qt6 behavior or necessary
API migration details.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-04 12:05:23 +00:00

229 lines
6.1 KiB
C++

/**
* @RDE_HEADER@
* Unit Tests for MathUtils Module - Qt6 Migration
* $Id$
* $HeadURL$
* $Author$
*/
#include <gtest/gtest.h>
#include <QCoreApplication>
#include <QList>
#include <QVector>
#include <QPointF>
#include <QPolygonF>
#include <cmath>
namespace
{
// Test helper: Calculate distance between two points
double calculateDistance(const QPointF& p1, const QPointF& p2)
{
double dx = p2.x() - p1.x();
double dy = p2.y() - p1.y();
return std::sqrt(dx * dx + dy * dy);
}
// Test helper: Calculate polygon area (shoelace formula)
double calculatePolygonArea(const QPolygonF& polygon)
{
double area = 0.0;
int n = polygon.size();
for (int i = 0; i < n; ++i)
{
int j = (i + 1) % n;
area += polygon[i].x() * polygon[j].y();
area -= polygon[j].x() * polygon[i].y();
}
return std::abs(area) / 2.0;
}
}
// Test MathUtils basic geometry operations
TEST(MathUtilsTest, Geometry_BasicOperations)
{
QPointF p1(0.0, 0.0);
QPointF p2(3.0, 4.0);
double distance = calculateDistance(p1, p2);
EXPECT_DOUBLE_EQ(distance, 5.0); // 3-4-5 triangle
}
// Test MathUtils polygon operations
TEST(MathUtilsTest, Polygon_Area_Calculation)
{
// Square with side 10
QPolygonF square;
square << QPointF(0, 0) << QPointF(10, 0)
<< QPointF(10, 10) << QPointF(0, 10);
double area = calculatePolygonArea(square);
EXPECT_DOUBLE_EQ(area, 100.0);
// Triangle
QPolygonF triangle;
triangle << QPointF(0, 0) << QPointF(10, 0) << QPointF(0, 10);
area = calculatePolygonArea(triangle);
EXPECT_DOUBLE_EQ(area, 50.0);
}
// Test MathUtils Qt6 QList/QVector compatibility
TEST(MathUtilsTest, Qt6_List_Vector_Compatibility)
{
// This test verifies that ambiguous overloads are resolved
QList<double> list = {1.0, 2.0, 3.0, 4.0, 5.0};
QVector<double> vector = {1.0, 2.0, 3.0, 4.0, 5.0};
EXPECT_EQ(list.size(), vector.size());
for (int i = 0; i < list.size(); ++i)
{
EXPECT_DOUBLE_EQ(list[i], vector[i]);
}
// Test that both can be used interchangeably
auto processList = [](const QList<double>& data) {
double sum = 0.0;
for (double val : data) sum += val;
return sum;
};
auto processVector = [](const QVector<double>& data) {
double sum = 0.0;
for (double val : data) sum += val;
return sum;
};
EXPECT_DOUBLE_EQ(processList(list), 15.0);
EXPECT_DOUBLE_EQ(processVector(vector), 15.0);
}
// Test MathUtils buffer operations (simulating bufferGeodetic)
TEST(MathUtilsTest, Buffer_Operation_Simulation)
{
// Simulate buffer operation around a point
QPointF center(0.0, 0.0);
double bufferDistance = 10.0;
int segments = 8;
QPolygonF buffer;
for (int i = 0; i < segments; ++i)
{
double angle = (2.0 * M_PI * i) / segments;
double x = center.x() + bufferDistance * std::cos(angle);
double y = center.y() + bufferDistance * std::sin(angle);
buffer << QPointF(x, y);
}
EXPECT_EQ(buffer.size(), segments);
// Verify all points are approximately at bufferDistance from center
for (const auto& point : buffer)
{
double distance = calculateDistance(center, point);
EXPECT_NEAR(distance, bufferDistance, 0.1);
}
}
// Test MathUtils coordinate transformations
TEST(MathUtilsTest, Coordinate_Transformations)
{
// Test basic coordinate operations
QPointF latLon(45.5, -122.3); // Portland
// Verify coordinates are preserved
EXPECT_DOUBLE_EQ(latLon.x(), 45.5);
EXPECT_DOUBLE_EQ(latLon.y(), -122.3);
// Test coordinate list
QList<QPointF> coordinates;
coordinates << QPointF(45.0, -122.0)
<< QPointF(46.0, -123.0)
<< QPointF(47.0, -124.0);
EXPECT_EQ(coordinates.size(), 3);
// Verify each coordinate
EXPECT_DOUBLE_EQ(coordinates[0].x(), 45.0);
EXPECT_DOUBLE_EQ(coordinates[0].y(), -122.0);
EXPECT_DOUBLE_EQ(coordinates[1].x(), 46.0);
EXPECT_DOUBLE_EQ(coordinates[1].y(), -123.0);
EXPECT_DOUBLE_EQ(coordinates[2].x(), 47.0);
EXPECT_DOUBLE_EQ(coordinates[2].y(), -124.0);
}
// Test MathUtils numeric operations
TEST(MathUtilsTest, Numeric_Operations)
{
// Test various numeric operations used in MathUtils
// Distance calculation
QPointF p1(0, 0);
QPointF p2(3, 4);
double dist = std::sqrt(std::pow(p2.x() - p1.x(), 2) + std::pow(p2.y() - p1.y(), 2));
EXPECT_DOUBLE_EQ(dist, 5.0);
// Angle calculation
QPointF p3(1, 1);
double angle = std::atan2(p3.y(), p3.x());
EXPECT_NEAR(angle, M_PI / 4.0, 0.0001); // 45 degrees in radians
// Normalize angle to [0, 2*PI)
double normalized = angle < 0 ? angle + 2 * M_PI : angle;
EXPECT_NEAR(normalized, M_PI / 4.0, 0.0001);
}
// Test MathUtils container operations (Qt6 specific)
TEST(MathUtilsTest, Qt6_Container_Operations)
{
// Test QList operations (Qt6)
QList<int> numbers;
numbers << 1 << 2 << 3 << 4 << 5;
EXPECT_EQ(numbers.size(), 5);
EXPECT_EQ(numbers.front(), 1);
EXPECT_EQ(numbers.back(), 5);
// Test iteration
int sum = 0;
for (int num : numbers)
{
sum += num;
}
EXPECT_EQ(sum, 15);
// Test QVector operations (Qt6)
QVector<double> values = {1.5, 2.5, 3.5};
EXPECT_EQ(values.size(), 3);
double total = 0.0;
for (double val : values)
{
total += val;
}
EXPECT_DOUBLE_EQ(total, 7.5);
}
// Test MathUtils edge cases
TEST(MathUtilsTest, Edge_Cases)
{
// Empty polygon
QPolygonF emptyPoly;
EXPECT_EQ(emptyPoly.size(), 0);
EXPECT_TRUE(emptyPoly.isEmpty());
// Single point
QPolygonF singlePoint;
singlePoint << QPointF(0, 0);
EXPECT_EQ(singlePoint.size(), 1);
EXPECT_FALSE(singlePoint.isEmpty());
// Very small distances
QPointF p1(0.0, 0.0);
QPointF p2(0.0001, 0.0001);
double smallDist = calculateDistance(p1, p2);
EXPECT_GT(smallDist, 0.0);
EXPECT_LT(smallDist, 0.001);
}