A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
rectangle-closest-border-test.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
5 */
6
7#include <ns3/rectangle.h>
8#include <ns3/simulator.h>
9#include <ns3/test.h>
10
11using namespace ns3;
12
13/**
14 * \ingroup mobility-test
15 *
16 * \brief Rectangle detection of closest border to a point, inside or outside.
17 */
19{
20 public:
21 /**
22 * Constructor
23 */
25};
26
27/**
28 * \ingroup mobility-test
29 *
30 * \brief TestCase to check the rectangle line intersection
31 */
33{
34 public:
35 /**
36 * \brief Create RectangleClosestBorderTestCase
37 * \param x Index of the first position to generate
38 * \param y Index of the second position to generate
39 * \param rectangle The 2D rectangle
40 * \param side The expected result of the test
41 */
42 RectangleClosestBorderTestCase(double x, double y, Rectangle rectangle, Rectangle::Side side);
43 /**
44 * \brief Builds the test name string based on provided parameter values
45 * \param x Index of the first position to generate
46 * \param y Index of the second position to generate
47 * \param rectangle The 2D rectangle
48 * \param side The expected result of the test
49 *
50 * \return The name string
51 */
52 std::string BuildNameString(double x, double y, Rectangle rectangle, Rectangle::Side side);
53 /**
54 * Destructor
55 */
57
58 private:
59 /**
60 * \brief Setup the simulation according to the configuration set by the
61 * class constructor, run it, and verify the result.
62 */
63 void DoRun() override;
64
65 double m_x{0.0}; //!< X coordinate of the point to be tested
66 double m_y{0.0}; //!< Y coordinate of the point to be tested
67 Rectangle m_rectangle; //!< The rectangle to check the intersection with
68
69 /**
70 * Flag to indicate the intersection.
71 * True, for intersection, false otherwise.
72 */
74};
75
76/**
77 * This TestSuite tests the intersection of a line segment
78 * between two 2D positions with a 2D rectangle. It generates two
79 * positions from a set of predefined positions (see GeneratePosition method),
80 * and then tests the intersection of a line segments between them with a rectangle
81 * of predefined dimensions.
82 */
83
85 : TestSuite("rectangle-closest-border", Type::UNIT)
86{
87 // Rectangle in the positive x-plane to check the intersection with.
88 Rectangle rectangle = Rectangle(0.0, 10.0, 0.0, 10.0);
89
90 /* 2 3 4
91 * +----------------------------+ (10,10)
92 * | 11 16 12 |
93 * | |
94 * | |
95 * 1 | 15 18 17 | 5
96 * | |
97 * | |
98 * | 10 14 13 |
99 * +----------------------------+
100 * 9 7
101 * (0,0)
102 *
103 *
104 *
105 * 8
106 */
107 // Left side (1 and 15)
109 TestCase::Duration::QUICK);
111 TestCase::Duration::QUICK);
112 // Right side (5 and 17)
114 TestCase::Duration::QUICK);
116 TestCase::Duration::QUICK);
117 // Bottom side (8 and 14)
119 TestCase::Duration::QUICK);
121 TestCase::Duration::QUICK);
122 // Top side (3 and 16)
124 TestCase::Duration::QUICK);
126 TestCase::Duration::QUICK);
127 // Left-Bottom corner (9 and 10)
129 TestCase::Duration::QUICK);
131 TestCase::Duration::QUICK);
132 // Right-Bottom corner (7 and 13)
134 TestCase::Duration::QUICK);
136 TestCase::Duration::QUICK);
137 // Left-Top corner (2 and 11)
139 TestCase::Duration::QUICK);
141 TestCase::Duration::QUICK);
142 // Right-Top corner (4 and 12)
144 TestCase::Duration::QUICK);
146 TestCase::Duration::QUICK);
147 // Central position (18)
149 TestCase::Duration::QUICK);
150}
151
152/**
153 * \ingroup mobility-test
154 * Static variable for test initialization
155 */
157
159 double y,
160 Rectangle rectangle,
161 Rectangle::Side side)
162 : TestCase(BuildNameString(x, y, rectangle, side)),
163 m_x(x),
164 m_y(y),
165 m_rectangle(rectangle),
166 m_side(side)
167{
168}
169
173
174std::string
176 double y,
177 Rectangle rectangle,
178 Rectangle::Side side)
179{
180 std::ostringstream oss;
181 oss << "Rectangle closest border test : checking"
182 << " (x,y) = (" << x << "," << y << ") closest border to the rectangle [(" << rectangle.xMin
183 << ", " << rectangle.yMin << "), (" << rectangle.xMax << ", " << rectangle.yMax
184 << ")]. The expected side = " << side;
185 return oss.str();
186}
187
188void
190{
191 Vector position(m_x, m_y, 0.0);
193
194 NS_TEST_ASSERT_MSG_EQ(side, m_side, "Unexpected result of rectangle side!");
196}
TestCase to check the rectangle line intersection.
std::string BuildNameString(double x, double y, Rectangle rectangle, Rectangle::Side side)
Builds the test name string based on provided parameter values.
Rectangle::Side m_side
Flag to indicate the intersection.
double m_x
X coordinate of the point to be tested.
double m_y
Y coordinate of the point to be tested.
RectangleClosestBorderTestCase(double x, double y, Rectangle rectangle, Rectangle::Side side)
Create RectangleClosestBorderTestCase.
void DoRun() override
Setup the simulation according to the configuration set by the class constructor, run it,...
Rectangle m_rectangle
The rectangle to check the intersection with.
Rectangle detection of closest border to a point, inside or outside.
a 2d rectangle
Definition rectangle.h:24
double yMax
The y coordinate of the top bound of the rectangle.
Definition rectangle.h:107
Side GetClosestSideOrCorner(const Vector &position) const
Definition rectangle.cc:53
double xMax
The x coordinate of the right bound of the rectangle.
Definition rectangle.h:105
Side
enum for naming sides
Definition rectangle.h:30
double xMin
The x coordinate of the left bound of the rectangle.
Definition rectangle.h:104
double yMin
The y coordinate of the bottom bound of the rectangle.
Definition rectangle.h:106
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
static RectangleClosestBorderTestSuite rectangleClosestBorderTestSuite
Static variable for test initialization.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
Every class exported by the ns3 library is enclosed in the ns3 namespace.