A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
rectangle-closest-border-test.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
16 */
17
18#include <ns3/rectangle.h>
19#include <ns3/simulator.h>
20#include <ns3/test.h>
21
22using namespace ns3;
23
24/**
25 * \ingroup mobility-test
26 *
27 * \brief Rectangle detection of closest border to a point, inside or outside.
28 */
30{
31 public:
32 /**
33 * Constructor
34 */
36};
37
38/**
39 * \ingroup mobility-test
40 *
41 * \brief TestCase to check the rectangle line intersection
42 */
44{
45 public:
46 /**
47 * \brief Create RectangleClosestBorderTestCase
48 * \param x Index of the first position to generate
49 * \param y Index of the second position to generate
50 * \param rectangle The 2D rectangle
51 * \param side The expected result of the test
52 */
53 RectangleClosestBorderTestCase(double x, double y, Rectangle rectangle, Rectangle::Side side);
54 /**
55 * \brief Builds the test name string based on provided parameter values
56 * \param x Index of the first position to generate
57 * \param y Index of the second position to generate
58 * \param rectangle The 2D rectangle
59 * \param side The expected result of the test
60 *
61 * \return The name string
62 */
63 std::string BuildNameString(double x, double y, Rectangle rectangle, Rectangle::Side side);
64 /**
65 * Destructor
66 */
68
69 private:
70 /**
71 * \brief Setup the simulation according to the configuration set by the
72 * class constructor, run it, and verify the result.
73 */
74 void DoRun() override;
75
76 double m_x{0.0}; //!< X coordinate of the point to be tested
77 double m_y{0.0}; //!< Y coordinate of the point to be tested
78 Rectangle m_rectangle; //!< The rectangle to check the intersection with
79
80 /**
81 * Flag to indicate the intersection.
82 * True, for intersection, false otherwise.
83 */
85};
86
87/**
88 * This TestSuite tests the intersection of a line segment
89 * between two 2D positions with a 2D rectangle. It generates two
90 * positions from a set of predefined positions (see GeneratePosition method),
91 * and then tests the intersection of a line segments between them with a rectangle
92 * of predefined dimensions.
93 */
94
96 : TestSuite("rectangle-closest-border", Type::UNIT)
97{
98 // Rectangle in the positive x-plane to check the intersection with.
99 Rectangle rectangle = Rectangle(0.0, 10.0, 0.0, 10.0);
100
101 /* 2 3 4
102 * +----------------------------+ (10,10)
103 * | 11 16 12 |
104 * | |
105 * | |
106 * 1 | 15 18 17 | 5
107 * | |
108 * | |
109 * | 10 14 13 |
110 * +----------------------------+
111 * 9 7
112 * (0,0)
113 *
114 *
115 *
116 * 8
117 */
118 // Left side (1 and 15)
120 TestCase::Duration::QUICK);
122 TestCase::Duration::QUICK);
123 // Right side (5 and 17)
125 TestCase::Duration::QUICK);
127 TestCase::Duration::QUICK);
128 // Bottom side (8 and 14)
130 TestCase::Duration::QUICK);
132 TestCase::Duration::QUICK);
133 // Top side (3 and 16)
135 TestCase::Duration::QUICK);
137 TestCase::Duration::QUICK);
138 // Left-Bottom corner (9 and 10)
140 TestCase::Duration::QUICK);
142 TestCase::Duration::QUICK);
143 // Right-Bottom corner (7 and 13)
145 TestCase::Duration::QUICK);
147 TestCase::Duration::QUICK);
148 // Left-Top corner (2 and 11)
150 TestCase::Duration::QUICK);
152 TestCase::Duration::QUICK);
153 // Right-Top corner (4 and 12)
155 TestCase::Duration::QUICK);
157 TestCase::Duration::QUICK);
158 // Central position (18)
160 TestCase::Duration::QUICK);
161}
162
163/**
164 * \ingroup mobility-test
165 * Static variable for test initialization
166 */
168
170 double y,
171 Rectangle rectangle,
172 Rectangle::Side side)
173 : TestCase(BuildNameString(x, y, rectangle, side)),
174 m_x(x),
175 m_y(y),
176 m_rectangle(rectangle),
177 m_side(side)
178{
179}
180
182{
183}
184
185std::string
187 double y,
188 Rectangle rectangle,
189 Rectangle::Side side)
190{
191 std::ostringstream oss;
192 oss << "Rectangle closest border test : checking"
193 << " (x,y) = (" << x << "," << y << ") closest border to the rectangle [(" << rectangle.xMin
194 << ", " << rectangle.yMin << "), (" << rectangle.xMax << ", " << rectangle.yMax
195 << ")]. The expected side = " << side;
196 return oss.str();
197}
198
199void
201{
202 Vector position(m_x, m_y, 0.0);
204
205 NS_TEST_ASSERT_MSG_EQ(side, m_side, "Unexpected result of rectangle side!");
207}
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:35
double yMax
The y coordinate of the top bound of the rectangle.
Definition: rectangle.h:118
Side GetClosestSideOrCorner(const Vector &position) const
Definition: rectangle.cc:64
double xMax
The x coordinate of the right bound of the rectangle.
Definition: rectangle.h:116
Side
enum for naming sides
Definition: rectangle.h:41
double xMin
The x coordinate of the left bound of the rectangle.
Definition: rectangle.h:115
double yMin
The y coordinate of the bottom bound of the rectangle.
Definition: rectangle.h:117
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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:145
Every class exported by the ns3 library is enclosed in the ns3 namespace.