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
/* 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)
108
AddTestCase
(
new
RectangleClosestBorderTestCase
(-5, 5, rectangle,
Rectangle::LEFTSIDE
),
109
TestCase::Duration::QUICK);
110
AddTestCase
(
new
RectangleClosestBorderTestCase
(2, 5, rectangle,
Rectangle::LEFTSIDE
),
111
TestCase::Duration::QUICK);
112
// Right side (5 and 17)
113
AddTestCase
(
new
RectangleClosestBorderTestCase
(17, 5, rectangle,
Rectangle::RIGHTSIDE
),
114
TestCase::Duration::QUICK);
115
AddTestCase
(
new
RectangleClosestBorderTestCase
(7, 5, rectangle,
Rectangle::RIGHTSIDE
),
116
TestCase::Duration::QUICK);
117
// Bottom side (8 and 14)
118
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, -7, rectangle,
Rectangle::BOTTOMSIDE
),
119
TestCase::Duration::QUICK);
120
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 1, rectangle,
Rectangle::BOTTOMSIDE
),
121
TestCase::Duration::QUICK);
122
// Top side (3 and 16)
123
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 15, rectangle,
Rectangle::TOPSIDE
),
124
TestCase::Duration::QUICK);
125
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 7, rectangle,
Rectangle::TOPSIDE
),
126
TestCase::Duration::QUICK);
127
// Left-Bottom corner (9 and 10)
128
AddTestCase
(
new
RectangleClosestBorderTestCase
(-1, -1, rectangle,
Rectangle::BOTTOMLEFTCORNER
),
129
TestCase::Duration::QUICK);
130
AddTestCase
(
new
RectangleClosestBorderTestCase
(0, 0, rectangle,
Rectangle::BOTTOMLEFTCORNER
),
131
TestCase::Duration::QUICK);
132
// Right-Bottom corner (7 and 13)
133
AddTestCase
(
new
RectangleClosestBorderTestCase
(11, -1, rectangle,
Rectangle::BOTTOMRIGHTCORNER
),
134
TestCase::Duration::QUICK);
135
AddTestCase
(
new
RectangleClosestBorderTestCase
(9, 1, rectangle,
Rectangle::BOTTOMRIGHTCORNER
),
136
TestCase::Duration::QUICK);
137
// Left-Top corner (2 and 11)
138
AddTestCase
(
new
RectangleClosestBorderTestCase
(-1, 11, rectangle,
Rectangle::TOPLEFTCORNER
),
139
TestCase::Duration::QUICK);
140
AddTestCase
(
new
RectangleClosestBorderTestCase
(1, 9, rectangle,
Rectangle::TOPLEFTCORNER
),
141
TestCase::Duration::QUICK);
142
// Right-Top corner (4 and 12)
143
AddTestCase
(
new
RectangleClosestBorderTestCase
(11, 11, rectangle,
Rectangle::TOPRIGHTCORNER
),
144
TestCase::Duration::QUICK);
145
AddTestCase
(
new
RectangleClosestBorderTestCase
(9, 9, rectangle,
Rectangle::TOPRIGHTCORNER
),
146
TestCase::Duration::QUICK);
147
// Central position (18)
148
AddTestCase
(
new
RectangleClosestBorderTestCase
(5, 5, rectangle,
Rectangle::TOPSIDE
),
149
TestCase::Duration::QUICK);
150
}
151
152
/**
153
* \ingroup mobility-test
154
* Static variable for test initialization
155
*/
156
static
RectangleClosestBorderTestSuite
rectangleClosestBorderTestSuite
;
157
158
RectangleClosestBorderTestCase::RectangleClosestBorderTestCase
(
double
x,
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
170
RectangleClosestBorderTestCase::~RectangleClosestBorderTestCase
()
171
{
172
}
173
174
std::string
175
RectangleClosestBorderTestCase::BuildNameString
(
double
x,
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
188
void
189
RectangleClosestBorderTestCase::DoRun
()
190
{
191
Vector position(
m_x
,
m_y
, 0.0);
192
Rectangle::Side
side =
m_rectangle
.
GetClosestSideOrCorner
(position);
193
194
NS_TEST_ASSERT_MSG_EQ
(side,
m_side
,
"Unexpected result of rectangle side!"
);
195
Simulator::Destroy
();
196
}
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:175
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:158
RectangleClosestBorderTestCase::~RectangleClosestBorderTestCase
~RectangleClosestBorderTestCase() override
Destructor.
Definition
rectangle-closest-border-test.cc:170
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:189
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:156
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 Fri Nov 8 2024 13:59:04 for ns-3 by
1.11.0