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 /* 23 24
91 * 20
92 * :
93 * :
94 * 2 3 4
95 * +----------------------------+ (10,10)
96 * | 11 16 12 |
97 * | |
98 * | |
99 * 19 .... 1 | 15 18 17 | 5 .... 21
100 * | |
101 * | |
102 * | 10 14 13 |
103 * +----------------------------+
104 * 9 7
105 * (0,0)
106 *
107 *
108 *
109 * 8
110 * :
111 * :
112 * 26 22 25
113 */
114 // Left side (1 and 15)
116 TestCase::Duration::QUICK);
118 TestCase::Duration::QUICK);
119 // Right side (5 and 17)
121 TestCase::Duration::QUICK);
123 TestCase::Duration::QUICK);
124 // Bottom side (8 and 14)
126 TestCase::Duration::QUICK);
128 TestCase::Duration::QUICK);
129 // Top side (3 and 16)
131 TestCase::Duration::QUICK);
133 TestCase::Duration::QUICK);
134 // Left-Bottom corner (9 and 10)
136 TestCase::Duration::QUICK);
138 TestCase::Duration::QUICK);
139 // Right-Bottom corner (7 and 13)
141 TestCase::Duration::QUICK);
143 TestCase::Duration::QUICK);
144 // Left-Top corner (2 and 11)
146 TestCase::Duration::QUICK);
148 TestCase::Duration::QUICK);
149 // Right-Top corner (4 and 12)
151 TestCase::Duration::QUICK);
153 TestCase::Duration::QUICK);
154 // Central position (18)
156 TestCase::Duration::QUICK);
157 // For coordinates to left, top, right, bottom (19, 20, 21, 22)
159 TestCase::Duration::QUICK);
161 TestCase::Duration::QUICK);
163 TestCase::Duration::QUICK);
165 TestCase::Duration::QUICK);
166 // For coordinates to left-top, right-top, right-bottom, left-bottom diagonals (23, 24, 25, 26)
168 TestCase::Duration::QUICK);
170 TestCase::Duration::QUICK);
173 TestCase::Duration::QUICK);
176 TestCase::Duration::QUICK);
177}
178
179/**
180 * @ingroup mobility-test
181 * Static variable for test initialization
182 */
184
186 double y,
187 Rectangle rectangle,
188 Rectangle::Side side)
189 : TestCase(BuildNameString(x, y, rectangle, side)),
190 m_x(x),
191 m_y(y),
192 m_rectangle(rectangle),
193 m_side(side)
194{
195}
196
200
201std::string
203 double y,
204 Rectangle rectangle,
205 Rectangle::Side side)
206{
207 std::ostringstream oss;
208 oss << "Rectangle closest border test : checking"
209 << " (x,y) = (" << x << "," << y << ") closest border to the rectangle [(" << rectangle.xMin
210 << ", " << rectangle.yMin << "), (" << rectangle.xMax << ", " << rectangle.yMax
211 << ")]. The expected side = " << side;
212 return oss.str();
213}
214
215void
217{
218 Vector position(m_x, m_y, 0.0);
220
221 NS_TEST_ASSERT_MSG_EQ(side, m_side, "Unexpected result of rectangle side!");
223}
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.