A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
int64x64-test-suite.cc
Go to the documentation of this file.
1 #include "ns3/int64x64.h"
2 #include "ns3/test.h"
3 
4 using namespace ns3;
5 
7 {
8 public:
10  virtual void DoRun (void);
11  void CheckFrac (int64_t hi, uint64_t lo);
12 };
13 
14 void
15 Int64x64FracTestCase::CheckFrac (int64_t hi, uint64_t lo)
16 {
17  int64x64_t tmp = int64x64_t (hi,lo);
18  NS_TEST_EXPECT_MSG_EQ (tmp.GetHigh (), hi,
19  "High part does not match");
20  NS_TEST_EXPECT_MSG_EQ (tmp.GetLow (), lo,
21  "Low part does not match");
22 }
23 
25  : TestCase ("Check that we can manipulate the high and low part of every number")
26 {
27 }
28 void
30 {
31  CheckFrac (1, 0);
32  CheckFrac (1, 1);
33  CheckFrac (-1, 0);
34  CheckFrac (-1, 1);
35 }
36 
37 
39 {
40 public:
42  virtual void DoRun (void);
43  void CheckString (std::string str, int64_t hi, uint64_t lo);
44 };
46  : TestCase ("Check that we parse Int64x64 numbers as strings")
47 {
48 }
49 void
50 Int64x64InputTestCase::CheckString (std::string str, int64_t hi, uint64_t lo)
51 {
52  std::istringstream iss;
53  iss.str (str);
54  int64x64_t hp;
55  iss >> hp;
56  NS_TEST_EXPECT_MSG_EQ (hp.GetHigh (), hi, "High parts do not match for input string " << str);
57  NS_TEST_EXPECT_MSG_EQ (hp.GetLow (), lo, "Low parts do not match for input string " << str);
58 }
59 void
61 {
62  CheckString ("1", 1, 0);
63  CheckString ("+1", 1, 0);
64  CheckString ("-1", -1, 0);
65  CheckString ("1.0", 1, 0);
66  CheckString ("+1.0", 1, 0);
67  CheckString ("001.0", 1, 0);
68  CheckString ("+001.0", 1, 0);
69  CheckString ("020.0", 20, 0);
70  CheckString ("+020.0", 20, 0);
71  CheckString ("-1.0", -1, 0);
72  CheckString ("-1.0000", -1, 0);
73  CheckString ("1.0000000", 1, 0);
74  CheckString ("1.08446744073709551615", 1, 8446744073709551615LL);
75  CheckString ("-1.08446744073709551615", -1, 8446744073709551615LL);
76 }
77 
79 {
80 public:
82  virtual void DoRun (void);
83  void CheckString (std::string str);
84 };
86  : TestCase ("Check that we can roundtrip Int64x64 numbers as strings")
87 {
88 }
89 void
91 {
92  std::istringstream iss;
93  iss.str (str);
94  int64x64_t value;
95  iss >> value;
96  std::ostringstream oss;
97  oss << value;
98  NS_TEST_EXPECT_MSG_EQ (oss.str (), str, "Converted string does not match expected string");
99 }
100 void
102 {
103  CheckString ("+1.0");
104  CheckString ("-1.0");
105  CheckString ("+20.0");
106  CheckString ("+1.08446744073709551615");
107  CheckString ("-1.08446744073709551615");
108  CheckString ("+1.18446744073709551615");
109  CheckString ("-1.18446744073709551615");
110 }
111 
112 #define CHECK_EXPECTED(a,b) \
113  NS_TEST_ASSERT_MSG_EQ ((a).GetHigh (),b,"Arithmetic failure: " << ((a).GetHigh ()) << "!=" << (b))
114 
115 #define V(v) \
116  int64x64_t (v)
117 
119 {
120 public:
122  virtual void DoRun (void);
123 };
124 
126  : TestCase ("Check basic arithmetic operations")
127 {
128 }
129 void
131 {
132  int64x64_t a, b;
133 
134  CHECK_EXPECTED (V (1) - V (1), 0);
135  CHECK_EXPECTED (V (1) - V (2), -1);
136  CHECK_EXPECTED (V (1) - V (3), -2);
137  CHECK_EXPECTED (V (1) - V (-1), 2);
138  CHECK_EXPECTED (V (1) - V (-2), 3);
139  CHECK_EXPECTED (V (-3) - V (-4), 1);
140  CHECK_EXPECTED (V (-2) - V (3), -5);
141  CHECK_EXPECTED (V (1) + V (2), 3);
142  CHECK_EXPECTED (V (1) + V (-3), -2);
143  CHECK_EXPECTED (V (0) + V (0), 0);
144  CHECK_EXPECTED (V (0) * V (0), 0);
145  CHECK_EXPECTED (V (0) * V (1), 0);
146  CHECK_EXPECTED (V (0) * V (-1), 0);
147  CHECK_EXPECTED (V (1) * V (0), 0);
148  CHECK_EXPECTED (V (1) * V (1), 1);
149  CHECK_EXPECTED (V (1) * V (-1), -1);
150  CHECK_EXPECTED (V (-1) * V (-1), 1);
151  CHECK_EXPECTED (V (0) * V (1), 0);
152  CHECK_EXPECTED (V (0) * V (-1), 0);
153  CHECK_EXPECTED (V (-1) * V (1), -1);
154 
155 
156  CHECK_EXPECTED (V (2) * V (3) / V (3), 2);
157 
158  // Below, the division loses precision because 2/3 is not
159  // representable exactly in 64.64 integers. So, we got
160  // something super close but the final rounding kills us.
161  a = V (2);
162  b = V (3);
163  a /= b;
164  a *= b;
165  CHECK_EXPECTED (V (2) / V (3) * V (3), 1);
166 
167  // The example below shows that we really do not lose
168  // much precision internally: it is almost always the
169  // final conversion which loses precision.
170  CHECK_EXPECTED (V (2000000000) / V (3) * V (3), 1999999999);
171 }
172 
177 {
178 public:
180  virtual void DoRun (void);
181 };
182 
184  : TestCase ("Test case for bug 455")
185 {
186 }
187 void
189 {
190  int64x64_t a = int64x64_t (0.1);
191  a /= int64x64_t (1.25);
192  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), 0.08, "The original testcase");
193  a = int64x64_t (0.5);
194  a *= int64x64_t (5);
195  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), 2.5, "Simple test for multiplication");
196  a = int64x64_t (-0.5);
197  a *= int64x64_t (5);
198  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), -2.5, "Test sign, first operation negative");
199  a = int64x64_t (-0.5);
200  a *=int64x64_t (-5);
201  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), 2.5, "both operands negative");
202  a = int64x64_t (0.5);
203  a *= int64x64_t (-5);
204  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), -2.5, "only second operand negative");
205 }
206 
211 {
212 public:
214  virtual void DoRun (void);
215 };
216 
218  : TestCase ("Test case for bug 863")
219 {
220 }
221 void
223 {
224  int64x64_t a = int64x64_t (0.9);
225  a /= int64x64_t (1);
226  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), 0.9, "The original testcase");
227  a = int64x64_t (0.5);
228  a /= int64x64_t (0.5);
229  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), 1.0, "Simple test for division");
230  a = int64x64_t (-0.5);
231  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), -0.5, "Check that we actually convert doubles correctly");
232  a /= int64x64_t (0.5);
233  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), -1.0, "first argument negative");
234  a = int64x64_t (0.5);
235  a /= int64x64_t (-0.5);
236  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), -1.0, "second argument negative");
237  a = int64x64_t (-0.5);
238  a /= int64x64_t (-0.5);
239  NS_TEST_ASSERT_MSG_EQ (a.GetDouble (), 1.0, "both arguments negative");
240 }
241 
243 {
244 public:
246  virtual void DoRun (void);
247 };
249  : TestCase ("Check basic compare operations")
250 {
251 }
252 void
254 {
255 
256  NS_TEST_ASSERT_MSG_EQ ((V (-1) < V (1)), true, "a is smaller than b");
257  NS_TEST_ASSERT_MSG_EQ ((V (-1) > V (-2)), true, "a is bigger than b");
258  NS_TEST_ASSERT_MSG_EQ ((V (-1) == V (-1)), true, "a is equal to b");
259 
260  NS_TEST_ASSERT_MSG_EQ ((V (1) > V (-1)), true, "a is bigger than b");
261  NS_TEST_ASSERT_MSG_EQ ((V (1) < V (2)), true, "a is smaller than b");
262 }
263 
265 {
266 public:
268  virtual void DoRun (void);
269 };
270 
272  : TestCase ("Test case for invertion")
273 {
274 }
275 
276 void
278 {
279 #define TEST(factor) \
280  do { \
281  int64x64_t a; \
282  a = int64x64_t::Invert (factor); \
283  int64x64_t b = V (factor); \
284  b.MulByInvert (a); \
285  NS_TEST_ASSERT_MSG_EQ (b.GetHigh (), 1, \
286  "x * 1/x should be 1 for x=" << factor); \
287  int64x64_t c = V (1); \
288  c.MulByInvert (a); \
289  NS_TEST_ASSERT_MSG_EQ (c.GetHigh (), 0, \
290  "1 * 1/x should be 0 for x=" << factor); \
291  int64x64_t d = V (1); \
292  d /= (V (factor)); \
293  NS_TEST_ASSERT_MSG_EQ (d.GetDouble (), c.GetDouble (), \
294  "1 * 1/x should be equal to 1/x for x=" << factor); \
295  int64x64_t e = V (-factor); \
296  e.MulByInvert (a); \
297  NS_TEST_ASSERT_MSG_EQ (e.GetHigh (), -1, \
298  "-x * 1/x should be -1 for x=" << factor); \
299  } \
300  while(false)
301  TEST (2);
302  TEST (3);
303  TEST (4);
304  TEST (5);
305  TEST (6);
306  TEST (10);
307  TEST (99);
308  TEST (100);
309  TEST (1000);
310  TEST (10000);
311  TEST (100000);
312  TEST (100000);
313  TEST (1000000);
314  TEST (10000000);
315  TEST (100000000);
316  TEST (1000000000);
317  TEST (10000000000LL);
318  TEST (100000000000LL);
319  TEST (1000000000000LL);
320  TEST (10000000000000LL);
321  TEST (100000000000000LL);
322  TEST (1000000000000000LL);
323 #undef TEST
324 }
325 
326 
327 
328 static class Int64x64128TestSuite : public TestSuite
329 {
330 public:
332  : TestSuite ("int64x64", UNIT)
333  {
334  AddTestCase (new Int64x64FracTestCase (), TestCase::QUICK);
335  AddTestCase (new Int64x64InputTestCase (), TestCase::QUICK);
336  AddTestCase (new Int64x64InputOutputTestCase (), TestCase::QUICK);
337  AddTestCase (new Int64x64ArithmeticTestCase (), TestCase::QUICK);
338  AddTestCase (new Int64x64Bug455TestCase (), TestCase::QUICK);
339  AddTestCase (new Int64x64Bug863TestCase (), TestCase::QUICK);
340  AddTestCase (new Int64x64CompareTestCase (), TestCase::QUICK);
341  AddTestCase (new Int64x64InvertTestCase (), TestCase::QUICK);
342  }
void CheckFrac(int64_t hi, uint64_t lo)
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
A suite of tests to run.
Definition: test.h:1025
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define TEST(factor)
encapsulates test code
Definition: test.h:849
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define V(v)
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:244
void CheckString(std::string str)
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
Int64x64128TestSuite g_int64x64TestSuite
void CheckString(std::string str, int64_t hi, uint64_t lo)
This test suite implements a Unit Test.
Definition: test.h:1035
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define CHECK_EXPECTED(a, b)
virtual void DoRun(void)
Implementation to actually run this TestCase.
#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:137