A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
11
using namespace
ns3
;
12
13
/**
14
* @ingroup mobility-test
15
*
16
* @brief Rectangle detection of closest border to a point, inside or outside.
17
*/
18
class
RectangleClosestBorderTestSuite
:
public
TestSuite
19
{
20
public
:
21
/**
22
* Constructor
23
*/
24
RectangleClosestBorderTestSuite
();
25
};
26
27
/**
28
* @ingroup mobility-test
29
*
30
* @brief TestCase to check the rectangle line intersection
31
*/
32
class
RectangleClosestBorderTestCase
:
public
TestCase
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
*/
56
~RectangleClosestBorderTestCase
()
override
;
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
*/
73
Rectangle::Side
m_side
{
ns3::Rectangle::TOPSIDE
};
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
84
RectangleClosestBorderTestSuite::RectangleClosestBorderTestSuite
()
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)
115
AddTestCase
(
new
RectangleClosestBorderTestCase
(-5, 5, rectangle,
Rectangle::LEFTSIDE
),
116
TestCase::Duration::QUICK);
117
AddTestCase
(
new
RectangleClosestBorderTestCase
(2, 5, rectangle,
Rectangle::LEFTSIDE
),
118
TestCase::Duration::QUICK);
119
// Right side (5 and 17)
120
AddTestCase
(
new
RectangleClosestBorderTestCase
(17, 5, rectangle,
Rectangle::RIGHTSIDE
),
121
TestCase::Duration::QUICK);
122
AddTestCase
(
new
RectangleClosestBorderTestCase
(7, 5, rectangle,
Rectangle::RIGHTSIDE
),
123
TestCase::Duration::QUICK);
124
// Bottom side (8 and 14)
125
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, -7, rectangle,
Rectangle::BOTTOMSIDE
),
126
TestCase::Duration::QUICK);
127
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 1, rectangle,
Rectangle::BOTTOMSIDE
),
128
TestCase::Duration::QUICK);
129
// Top side (3 and 16)
130
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 15, rectangle,
Rectangle::TOPSIDE
),
131
TestCase::Duration::QUICK);
132
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 7, rectangle,
Rectangle::TOPSIDE
),
133
TestCase::Duration::QUICK);
134
// Left-Bottom corner (9 and 10)
135
AddTestCase
(
new
RectangleClosestBorderTestCase
(-1, -1, rectangle,
Rectangle::BOTTOMLEFTCORNER
),
136
TestCase::Duration::QUICK);
137
AddTestCase
(
new
RectangleClosestBorderTestCase
(0, 0, rectangle,
Rectangle::BOTTOMLEFTCORNER
),
138
TestCase::Duration::QUICK);
139
// Right-Bottom corner (7 and 13)
140
AddTestCase
(
new
RectangleClosestBorderTestCase
(11, -1, rectangle,
Rectangle::BOTTOMRIGHTCORNER
),
141
TestCase::Duration::QUICK);
142
AddTestCase
(
new
RectangleClosestBorderTestCase
(9, 1, rectangle,
Rectangle::BOTTOMRIGHTCORNER
),
143
TestCase::Duration::QUICK);
144
// Left-Top corner (2 and 11)
145
AddTestCase
(
new
RectangleClosestBorderTestCase
(-1, 11, rectangle,
Rectangle::TOPLEFTCORNER
),
146
TestCase::Duration::QUICK);
147
AddTestCase
(
new
RectangleClosestBorderTestCase
(1, 9, rectangle,
Rectangle::TOPLEFTCORNER
),
148
TestCase::Duration::QUICK);
149
// Right-Top corner (4 and 12)
150
AddTestCase
(
new
RectangleClosestBorderTestCase
(11, 11, rectangle,
Rectangle::TOPRIGHTCORNER
),
151
TestCase::Duration::QUICK);
152
AddTestCase
(
new
RectangleClosestBorderTestCase
(9, 9, rectangle,
Rectangle::TOPRIGHTCORNER
),
153
TestCase::Duration::QUICK);
154
// Central position (18)
155
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 5, rectangle,
Rectangle::TOPSIDE
),
156
TestCase::Duration::QUICK);
157
// For coordinates to left, top, right, bottom (19, 20, 21, 22)
158
AddTestCase
(
new
RectangleClosestBorderTestCase
(-30, 5, rectangle,
Rectangle::LEFTSIDE
),
159
TestCase::Duration::QUICK);
160
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 30, rectangle,
Rectangle::TOPSIDE
),
161
TestCase::Duration::QUICK);
162
AddTestCase
(
new
RectangleClosestBorderTestCase
(30, 5, rectangle,
Rectangle::RIGHTSIDE
),
163
TestCase::Duration::QUICK);
164
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, -30, rectangle,
Rectangle::BOTTOMSIDE
),
165
TestCase::Duration::QUICK);
166
// For coordinates to left-top, right-top, right-bottom, left-bottom diagonals (23, 24, 25, 26)
167
AddTestCase
(
new
RectangleClosestBorderTestCase
(-30, 40, rectangle,
Rectangle::TOPLEFTCORNER
),
168
TestCase::Duration::QUICK);
169
AddTestCase
(
new
RectangleClosestBorderTestCase
(40, 40, rectangle,
Rectangle::TOPRIGHTCORNER
),
170
TestCase::Duration::QUICK);
171
AddTestCase
(
172
new
RectangleClosestBorderTestCase
(40, -30, rectangle,
Rectangle::BOTTOMRIGHTCORNER
),
173
TestCase::Duration::QUICK);
174
AddTestCase
(
175
new
RectangleClosestBorderTestCase
(-30, -30, rectangle,
Rectangle::BOTTOMLEFTCORNER
),
176
TestCase::Duration::QUICK);
177
}
178
179
/**
180
* @ingroup mobility-test
181
* Static variable for test initialization
182
*/
183
static
RectangleClosestBorderTestSuite
rectangleClosestBorderTestSuite
;
184
185
RectangleClosestBorderTestCase::RectangleClosestBorderTestCase
(
double
x,
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
197
RectangleClosestBorderTestCase::~RectangleClosestBorderTestCase
()
198
{
199
}
200
201
std::string
202
RectangleClosestBorderTestCase::BuildNameString
(
double
x,
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
215
void
216
RectangleClosestBorderTestCase::DoRun
()
217
{
218
Vector position(
m_x
,
m_y
, 0.0);
219
Rectangle::Side
side =
m_rectangle
.
GetClosestSideOrCorner
(position);
220
221
NS_TEST_ASSERT_MSG_EQ
(side,
m_side
,
"Unexpected result of rectangle side!"
);
222
Simulator::Destroy
();
223
}
RectangleClosestBorderTestCase
TestCase to check the rectangle line intersection.
Definition
rectangle-closest-border-test.cc:33
RectangleClosestBorderTestCase::BuildNameString
std::string BuildNameString(double x, double y, Rectangle rectangle, Rectangle::Side side)
Builds the test name string based on provided parameter values.
Definition
rectangle-closest-border-test.cc:202
RectangleClosestBorderTestCase::m_side
Rectangle::Side m_side
Flag to indicate the intersection.
Definition
rectangle-closest-border-test.cc:73
RectangleClosestBorderTestCase::m_x
double m_x
X coordinate of the point to be tested.
Definition
rectangle-closest-border-test.cc:65
RectangleClosestBorderTestCase::m_y
double m_y
Y coordinate of the point to be tested.
Definition
rectangle-closest-border-test.cc:66
RectangleClosestBorderTestCase::RectangleClosestBorderTestCase
RectangleClosestBorderTestCase(double x, double y, Rectangle rectangle, Rectangle::Side side)
Create RectangleClosestBorderTestCase.
Definition
rectangle-closest-border-test.cc:185
RectangleClosestBorderTestCase::~RectangleClosestBorderTestCase
~RectangleClosestBorderTestCase() override
Destructor.
Definition
rectangle-closest-border-test.cc:197
RectangleClosestBorderTestCase::DoRun
void DoRun() override
Setup the simulation according to the configuration set by the class constructor, run it,...
Definition
rectangle-closest-border-test.cc:216
RectangleClosestBorderTestCase::m_rectangle
Rectangle m_rectangle
The rectangle to check the intersection with.
Definition
rectangle-closest-border-test.cc:67
RectangleClosestBorderTestSuite
Rectangle detection of closest border to a point, inside or outside.
Definition
rectangle-closest-border-test.cc:19
RectangleClosestBorderTestSuite::RectangleClosestBorderTestSuite
RectangleClosestBorderTestSuite()
Constructor.
Definition
rectangle-closest-border-test.cc:84
ns3::Rectangle
a 2d rectangle
Definition
rectangle.h:24
ns3::Rectangle::yMax
double yMax
The y coordinate of the top bound of the rectangle.
Definition
rectangle.h:107
ns3::Rectangle::GetClosestSideOrCorner
Side GetClosestSideOrCorner(const Vector &position) const
Definition
rectangle.cc:53
ns3::Rectangle::xMax
double xMax
The x coordinate of the right bound of the rectangle.
Definition
rectangle.h:105
ns3::Rectangle::Side
Side
enum for naming sides
Definition
rectangle.h:30
ns3::Rectangle::BOTTOMSIDE
@ BOTTOMSIDE
Definition
rectangle.h:34
ns3::Rectangle::TOPSIDE
@ TOPSIDE
Definition
rectangle.h:33
ns3::Rectangle::LEFTSIDE
@ LEFTSIDE
Definition
rectangle.h:32
ns3::Rectangle::BOTTOMLEFTCORNER
@ BOTTOMLEFTCORNER
Definition
rectangle.h:38
ns3::Rectangle::BOTTOMRIGHTCORNER
@ BOTTOMRIGHTCORNER
Definition
rectangle.h:37
ns3::Rectangle::TOPLEFTCORNER
@ TOPLEFTCORNER
Definition
rectangle.h:36
ns3::Rectangle::RIGHTSIDE
@ RIGHTSIDE
Definition
rectangle.h:31
ns3::Rectangle::TOPRIGHTCORNER
@ TOPRIGHTCORNER
Definition
rectangle.h:35
ns3::Rectangle::xMin
double xMin
The x coordinate of the left bound of the rectangle.
Definition
rectangle.h:104
ns3::Rectangle::yMin
double yMin
The y coordinate of the bottom bound of the rectangle.
Definition
rectangle.h:106
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
rectangleClosestBorderTestSuite
static RectangleClosestBorderTestSuite rectangleClosestBorderTestSuite
Static variable for test initialization.
Definition
rectangle-closest-border-test.cc:183
NS_TEST_ASSERT_MSG_EQ
#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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
mobility
test
rectangle-closest-border-test.cc
Generated on Tue Apr 29 2025 18:20:49 for ns-3 by
1.11.0