A Discrete-Event Network Simulator
API
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 
22 using namespace ns3;
23 
30 {
31  public:
36 };
37 
44 {
45  public:
53  RectangleClosestBorderTestCase(double x, double y, Rectangle rectangle, Rectangle::Side side);
63  std::string BuildNameString(double x, double y, Rectangle rectangle, Rectangle::Side side);
68 
69  private:
74  void DoRun() override;
75 
76  double m_x{0.0};
77  double m_y{0.0};
79 
85 };
86 
96  : TestSuite("rectangle-closest-border", 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)
119  AddTestCase(new RectangleClosestBorderTestCase(-5, 5, rectangle, Rectangle::LEFTSIDE),
120  TestCase::QUICK);
121  AddTestCase(new RectangleClosestBorderTestCase(2, 5, rectangle, Rectangle::LEFTSIDE),
122  TestCase::QUICK);
123  // Right side (5 and 17)
124  AddTestCase(new RectangleClosestBorderTestCase(17, 5, rectangle, Rectangle::RIGHTSIDE),
125  TestCase::QUICK);
126  AddTestCase(new RectangleClosestBorderTestCase(7, 5, rectangle, Rectangle::RIGHTSIDE),
127  TestCase::QUICK);
128  // Bottom side (8 and 14)
129  AddTestCase(new RectangleClosestBorderTestCase(5, -7, rectangle, Rectangle::BOTTOMSIDE),
130  TestCase::QUICK);
131  AddTestCase(new RectangleClosestBorderTestCase(5, 1, rectangle, Rectangle::BOTTOMSIDE),
132  TestCase::QUICK);
133  // Top side (3 and 16)
134  AddTestCase(new RectangleClosestBorderTestCase(5, 15, rectangle, Rectangle::TOPSIDE),
135  TestCase::QUICK);
136  AddTestCase(new RectangleClosestBorderTestCase(5, 7, rectangle, Rectangle::TOPSIDE),
137  TestCase::QUICK);
138  // Left-Bottom corner (9 and 10)
139  AddTestCase(new RectangleClosestBorderTestCase(-1, -1, rectangle, Rectangle::BOTTOMLEFTCORNER),
140  TestCase::QUICK);
141  AddTestCase(new RectangleClosestBorderTestCase(0, 0, rectangle, Rectangle::BOTTOMLEFTCORNER),
142  TestCase::QUICK);
143  // Right-Bottom corner (7 and 13)
144  AddTestCase(new RectangleClosestBorderTestCase(11, -1, rectangle, Rectangle::BOTTOMRIGHTCORNER),
145  TestCase::QUICK);
146  AddTestCase(new RectangleClosestBorderTestCase(9, 1, rectangle, Rectangle::BOTTOMRIGHTCORNER),
147  TestCase::QUICK);
148  // Left-Top corner (2 and 11)
149  AddTestCase(new RectangleClosestBorderTestCase(-1, 11, rectangle, Rectangle::TOPLEFTCORNER),
150  TestCase::QUICK);
151  AddTestCase(new RectangleClosestBorderTestCase(1, 9, rectangle, Rectangle::TOPLEFTCORNER),
152  TestCase::QUICK);
153  // Right-Top corner (4 and 12)
154  AddTestCase(new RectangleClosestBorderTestCase(11, 11, rectangle, Rectangle::TOPRIGHTCORNER),
155  TestCase::QUICK);
156  AddTestCase(new RectangleClosestBorderTestCase(9, 9, rectangle, Rectangle::TOPRIGHTCORNER),
157  TestCase::QUICK);
158  // Central position (18)
159  AddTestCase(new RectangleClosestBorderTestCase(5, 5, rectangle, Rectangle::TOPSIDE),
160  TestCase::QUICK);
161 }
162 
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 
185 std::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 
199 void
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!");
206  Simulator::Destroy();
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
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
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:144
Every class exported by the ns3 library is enclosed in the ns3 namespace.