geopro/tests/app/test_chart_pick_geometry.cpp

80 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <gtest/gtest.h>
#include <QPointF>
#include <QRectF>
#include "panels/chart/ChartPickGeometry.hpp"
using namespace geopro::app;
using geopro::core::ScatterField;
namespace {
ScatterField makeField() {
ScatterField f;
f.x = {0.0, 1.0, 2.0, 3.0};
f.y = {0.0, 1.0, 2.0, 3.0};
f.id = {"a", "b", "c", "d"};
f.displayStatus = {0, 0, 0, 0};
return f;
}
} // namespace
TEST(ChartPickGeometry, PointsInRectSelectsInterior) {
auto f = makeField();
// 矩形 [0.5,2.5]×[0.5,2.5] → 命中下标 1(1,1) 与 2(2,2)。
auto hits = pointsInRect(f, QRectF(QPointF(0.5, 0.5), QPointF(2.5, 2.5)));
ASSERT_EQ(hits.size(), 2u);
EXPECT_EQ(hits[0], 1);
EXPECT_EQ(hits[1], 2);
}
TEST(ChartPickGeometry, PointsInRectEmptyWhenOutside) {
auto f = makeField();
auto hits = pointsInRect(f, QRectF(QPointF(10.0, 10.0), QPointF(20.0, 20.0)));
EXPECT_TRUE(hits.empty());
}
TEST(ChartPickGeometry, PointsInRectSkipsHidden) {
auto f = makeField();
f.displayStatus = {0, 1, 0, 0}; // 下标 1 隐藏 → 不参与框选
auto hits = pointsInRect(f, QRectF(QPointF(0.5, 0.5), QPointF(2.5, 2.5)));
ASSERT_EQ(hits.size(), 1u);
EXPECT_EQ(hits[0], 2);
}
TEST(ChartPickGeometry, PointsInRectSkipsNonFinite) {
ScatterField f;
f.x = {0.0, std::nan("")};
f.y = {0.0, 0.0};
auto hits = pointsInRect(f, QRectF(QPointF(-1.0, -1.0), QPointF(1.0, 1.0)));
ASSERT_EQ(hits.size(), 1u);
EXPECT_EQ(hits[0], 0);
}
TEST(ChartPickGeometry, MinPointsForMarkType) {
EXPECT_EQ(minPointsForMarkType(1), 1); // 点
EXPECT_EQ(minPointsForMarkType(2), 2); // 线
EXPECT_EQ(minPointsForMarkType(3), 3); // 面
EXPECT_EQ(minPointsForMarkType(4), 1); // 文字
}
TEST(ChartPickGeometry, NormalizePointTakesFirstOnly) {
std::vector<QPointF> pts{{1.0, 2.0}, {3.0, 4.0}};
auto out = normalizeDrawnPoints(pts, /*point*/ 1);
ASSERT_EQ(out.size(), 1u);
EXPECT_DOUBLE_EQ(out[0].x(), 1.0);
// 文字同样取首点。
EXPECT_EQ(normalizeDrawnPoints(pts, /*text*/ 4).size(), 1u);
}
TEST(ChartPickGeometry, NormalizeLineKeepsAll) {
std::vector<QPointF> pts{{1.0, 2.0}, {3.0, 4.0}, {5.0, 6.0}};
EXPECT_EQ(normalizeDrawnPoints(pts, /*line*/ 2).size(), 3u);
EXPECT_EQ(normalizeDrawnPoints(pts, /*polygon*/ 3).size(), 3u);
}
TEST(ChartPickGeometry, CanClosePolygon) {
EXPECT_FALSE(canClosePolygon(2));
EXPECT_TRUE(canClosePolygon(3));
}