21 #include "ns3/length.h"
23 #include "ns3/object.h"
24 #include "ns3/string.h"
28 #include <boost/units/base_units/us/foot.hpp>
29 #include <boost/units/systems/si.hpp>
30 #include <boost/units/systems/si/prefixes.hpp>
36 #include <initializer_list>
106 void TestDefaultLengthIsZero ();
111 void TestConstructLengthFromQuantity ();
117 void TestConstructLengthFromSIUnits ();
123 void TestConstructLengthFromUSUnits ();
129 void TestLengthCopyConstructor ();
135 void TestLengthMoveConstructor ();
144 void TestConstructLengthFromString (
double unitValue,
147 const std::initializer_list<std::string>& symbols);
153 void TestConstructLengthFromMeterString ();
154 void TestConstructLengthFromNanoMeterString ();
155 void TestConstructLengthFromMicroMeterString ();
156 void TestConstructLengthFromMilliMeterString ();
157 void TestConstructLengthFromCentiMeterString ();
158 void TestConstructLengthFromKiloMeterString ();
159 void TestConstructLengthFromNauticalMileString ();
160 void TestConstructLengthFromInchString ();
161 void TestConstructLengthFromFootString ();
162 void TestConstructLengthFromYardString ();
163 void TestConstructLengthFromMileString ();
166 #ifdef HAVE_BOOST_UNITS
171 void TestConstructLengthFromBoostUnits ();
172 void TestConstructLengthFromBoostUnitsMeters ();
173 void TestConstructLengthFromBoostUnitsKiloMeters ();
174 void TestConstructLengthFromBoostUnitsFeet ();
182 void TestBuilderFreeFunctions ();
188 void TestTryParseReturnsFalse ();
193 void TestTryParseReturnsTrue ();
200 void TestCopyAssignment ();
206 void TestMoveAssignment ();
211 void TestQuantityAssignment ();
217 void TestIsEqualReturnsTrue ();
218 void TestIsEqualReturnsFalse ();
219 void TestIsEqualWithToleranceReturnsTrue ();
220 void TestIsEqualWithToleranceReturnsFalse ();
221 void TestIsNotEqualReturnsTrue ();
222 void TestIsNotEqualReturnsFalse ();
223 void TestIsNotEqualWithToleranceReturnsTrue ();
224 void TestIsNotEqualWithToleranceReturnsFalse ();
225 void TestIsLessReturnsTrue ();
226 void TestIsLessReturnsFalse ();
227 void TestIsLessWithToleranceReturnsFalse ();
228 void TestIsGreaterReturnsTrue ();
229 void TestIsGreaterReturnsFalse ();
230 void TestIsGreaterWithToleranceReturnsFalse ();
236 void TestOutputStreamOperator ();
242 void TestInputStreamOperator ();
256 void TestLengthSerialization (
const Length& l,
258 const std::string& expectedOutput,
259 const std::string& context);
264 void TestSerializeLengthWithUnit ();
270 void TestOperatorEqualsReturnsTrue ();
271 void TestOperatorEqualsReturnsFalse ();
272 void TestOperatorNotEqualsReturnsTrue ();
273 void TestOperatorNotEqualsReturnsFalse ();
274 void TestOperatorLessThanReturnsTrue ();
275 void TestOperatorLessThanReturnsFalse ();
276 void TestOperatorLessOrEqualReturnsTrue ();
277 void TestOperatorLessOrEqualReturnsFalse ();
278 void TestOperatorGreaterThanReturnsTrue ();
279 void TestOperatorGreaterThanReturnsFalse ();
280 void TestOperatorGreaterOrEqualReturnsTrue ();
281 void TestOperatorGreaterOrEqualReturnsFalse ();
288 void TestAddingTwoLengths ();
289 void TestAddingLengthAndQuantity ();
290 void TestAddingQuantityAndLength ();
291 void TestSubtractingTwoLengths ();
292 void TestSubtractingLengthAndQuantity ();
293 void TestSubtractingQuantityAndLength ();
294 void TestMultiplyLengthByScalar ();
295 void TestMultiplyScalarByLength ();
296 void TestDivideLengthByScalar ();
297 void TestDivideLengthByLength ();
298 void TestDivideLengthByLengthReturnsNaN ();
305 void TestDivReturnsCorrectResult ();
306 void TestDivReturnsZeroRemainder ();
307 void TestDivReturnsCorrectRemainder ();
314 void TestModReturnsZero ();
315 void TestModReturnsNonZero ();
318 virtual void DoRun ();
337 "length constructed from meters has wrong value");
343 using TestEntry = std::tuple<Length, std::string>;
345 const double expectedMeters = 1;
346 const std::initializer_list<TestEntry> inputs {
347 std::make_tuple (
Length (1e9, Unit::Nanometer),
"nanometer"),
348 std::make_tuple (
Length (1e6, Unit::Micrometer),
"micrometer"),
349 std::make_tuple (
Length (1e3, Unit::Millimeter),
"millimeter"),
350 std::make_tuple (
Length (1e2, Unit::Centimeter),
"centimeter"),
351 std::make_tuple (
Length (1e-3, Unit::Kilometer),
"kilometer"),
352 std::make_tuple (
Length ( (1 / 1852.0), Unit::NauticalMile),
"nautical_mile")
355 for (
const TestEntry& entry : inputs )
357 const Length& l = std::get<0> (entry);
358 const std::string& context = std::get<1> (entry);
361 context <<
": constructed length from SI unit has wrong value");
368 using TestEntry = std::tuple<Length, std::string>;
370 const double expectedMeters = 0.3048;
371 const double tolerance = 0.0001;
373 const std::initializer_list<TestEntry> inputs {
374 std::make_tuple (
Length (12.0, Unit::Inch),
"inch"),
375 std::make_tuple (
Length (1.0, Unit::Foot),
"foot"),
376 std::make_tuple (
Length ((1 / 3.0), Unit::Yard),
"yard"),
377 std::make_tuple (
Length ((1 / 5280.0), Unit::Mile),
"mile"),
380 for (
const TestEntry& entry : inputs )
382 const Length& l = std::get<0> (entry);
383 const std::string& context = std::get<1> (entry);
386 "constructed length from US unit (" << context <<
") has wrong value");
393 const double value = 5;
394 Length original (value, Unit::Meter);
399 "copy constructed length has wrong value");
405 const double value = 5;
406 Length original (value, Unit::Meter);
408 Length copy (std::move (original));
411 "move constructed length has wrong value");
418 const std::initializer_list<std::string>& symbols)
420 const std::array<std::string, 2> SEPARATORS {{
"",
" "}};
422 for (
const std::string& symbol : symbols)
424 for (
const std::string& separator : SEPARATORS )
426 std::ostringstream stream;
428 stream << unitValue << separator << symbol;
432 std::ostringstream msg;
433 msg <<
"string constructed length has wrong value: '" << stream.str () <<
"'";
443 const double value = 5;
445 TestConstructLengthFromString (value, value, 0,
446 {
"m",
"meter",
"meters",
"metre",
"metres"});
452 const double value = 5;
453 const double expectedValue = 5e-9;
455 TestConstructLengthFromString (value, expectedValue, 0,
456 {
"nm",
"nanometer",
"nanometers",
457 "nanometre",
"nanometres"});
463 const double value = 5;
464 const double expectedValue = 5e-6;
465 const double tolerance = 1e-7;
467 TestConstructLengthFromString (value, expectedValue, tolerance,
468 {
"um",
"micrometer",
"micrometers",
469 "micrometre",
"micrometres"});
475 const double value = 5;
476 const double expectedValue = 5e-3;
477 const double tolerance = 1e-4;
479 TestConstructLengthFromString (value, expectedValue, tolerance,
480 {
"mm",
"millimeter",
"millimeters",
481 "millimetre",
"millimetres"});
487 const double value = 5;
488 const double expectedValue = 5e-2;
489 const double tolerance = 1e-3;
491 TestConstructLengthFromString (value, expectedValue, tolerance,
492 {
"cm",
"centimeter",
"centimeters",
493 "centimetre",
"centimetres"});
499 const double value = 5;
500 const double expectedValue = 5e3;
502 TestConstructLengthFromString (value, expectedValue, 0,
503 {
"km",
"kilometer",
"kilometers",
504 "kilometre",
"kilometres"});
510 const double value = 5;
511 const double expectedValue = 9260;
513 TestConstructLengthFromString (value, expectedValue, 0,
514 {
"nmi",
"nautical mile",
"nautical miles"});
519 const double value = 5;
520 const double expectedValue = 0.127;
521 const double tolerance = 1e-4;
523 TestConstructLengthFromString (value, expectedValue, tolerance,
524 {
"in",
"inch",
"inches"});
530 const double value = 5;
531 const double expectedValue = 1.524;
532 const double tolerance = 1e-4;
534 TestConstructLengthFromString (value, expectedValue, tolerance,
535 {
"ft",
"foot",
"feet"});
541 const double value = 5;
542 const double expectedValue = 4.572;
543 const double tolerance = 1e-4;
545 TestConstructLengthFromString (value, expectedValue, tolerance,
546 {
"yd",
"yard",
"yards"});
552 const double value = 5;
553 const double expectedValue = 8046.72;
554 const double tolerance = 1e-3;
556 TestConstructLengthFromString (value, expectedValue, tolerance,
557 {
"mi",
"mile",
"miles"});
560 #ifdef HAVE_BOOST_UNITS
562 LengthTestCase::TestConstructLengthFromBoostUnits ()
564 TestConstructLengthFromBoostUnitsMeters ();
565 TestConstructLengthFromBoostUnitsKiloMeters ();
566 TestConstructLengthFromBoostUnitsFeet ();
570 LengthTestCase::TestConstructLengthFromBoostUnitsMeters ()
572 namespace bu = boost::units;
574 auto meters = 5 * bu::si::meter;
579 "Construction from boost::units meters produced "
584 LengthTestCase::TestConstructLengthFromBoostUnitsKiloMeters ()
586 namespace bu = boost::units;
587 auto kilometer = bu::si::kilo * bu::si::meter;
589 const double expectedValue = 5000;
590 auto quantity = 5 * kilometer;
595 "Construction from boost::units kilometers produced "
600 LengthTestCase::TestConstructLengthFromBoostUnitsFeet ()
602 namespace bu = boost::units;
604 bu::us::foot_base_unit::unit_type Foot;
606 const double expectedValue = 3.048;
607 auto feet = 10 * Foot;
612 "Construction from boost::units foot produced "
620 using Builder = std::function<
Length (
double)>;
622 double inputValue = 10;
624 std::map<Unit, Builder> TESTDATA{
638 for (
auto& entry : TESTDATA)
640 Length expected (inputValue, entry.first);
642 Length output = entry.second (inputValue);
645 "The builder free function for " << entry.first <<
646 " did not create a Length with the correct value");
655 AssertFalse (l.has_value (),
"TryParse returned true on bad input");
661 using TestInput = std::pair<double, std::string>;
662 using TestArgs = std::pair<double, double>;
663 std::map<TestInput, TestArgs>
tests{
666 {{5,
"kilometer"}, {5e3, 0}},
667 {{5,
" kilometer"}, {5e3, 0}}
670 for (
auto& entry :
tests)
672 TestInput input = entry.first;
673 TestArgs args = entry.second;
677 AssertTrue (l.has_value (),
"TryParse returned false when expecting true");
679 std::stringstream stream;
680 stream <<
"Parsing input (" << input.first <<
", " << input.second
681 <<
") returned the wrong value";
691 const double value = 5;
693 Length original (value, Unit::Meter);
699 "copy assignment failed");
705 const double value = 5;
707 Length original (value, Unit::Meter);
710 copy = std::move (original);
713 "move assignment failed");
727 "quantity assignment failed");
733 const double value = 5;
734 Length one (value, Unit::Meter);
737 AssertTrue (one.
IsEqual (two),
"IsEqual returned false for equal lengths");
743 const double value = 5;
744 Length one (value, Unit::Meter);
745 Length two ( value, Unit::Foot );
747 AssertFalse (one.
IsEqual (two),
"IsEqual returned true for unequal lengths");
753 const double value = 5;
754 const double tolerance = 0.1;
756 Length one (value, Unit::Meter);
757 Length two ( (value + 0.1), Unit::Meter);
759 AssertTrue (one.
IsEqual (two, tolerance),
760 "IsEqual returned false for almost equal lengths");
766 const double value = 5;
767 const double tolerance = 0.01;
769 Length one (value, Unit::Meter);
770 Length two ( (value + 0.1), Unit::Meter);
772 AssertFalse (one.
IsEqual (two, tolerance),
773 "IsEqual returned true for almost equal lengths");
779 const double value = 5;
781 Length one (value, Unit::Meter);
782 Length two ( (value + 0.1), Unit::Meter);
785 "IsNotEqual returned false for not equal lengths");
791 const double value = 5;
793 Length one (value, Unit::Meter);
797 "IsNotEqual returned true for equal lengths");
803 const double tolerance = 0.001;
805 Length one ( 5.01, Unit::Meter);
806 Length two ( 5.02, Unit::Meter);
809 "IsNotEqual with tolerance returned false for not equal lengths");
815 const double tolerance = 0.01;
817 Length one ( 5.01, Unit::Meter);
818 Length two ( 5.02, Unit::Meter);
821 "IsNotEqual with tolerance returned true for not equal lengths");
827 const double value = 5;
829 Length one (value, Unit::Meter);
830 Length two ( (value + 0.1), Unit::Meter);
832 AssertTrue (one.
IsLess (two),
833 "IsLess returned false for non equal lengths");
839 const double value = 5;
841 Length one (value, Unit::Meter);
844 AssertFalse (one.
IsLess (two),
845 "IsLess returned true for equal lengths");
851 const double tolerance = 0.01;
853 Length one ( 5.1234, Unit::Meter );
854 Length two ( 5.1278, Unit::Meter );
856 AssertFalse (one.
IsLess (two, tolerance),
857 "IsLess with tolerance returned true");
863 Length one (2.0, Unit::Meter);
864 Length two (1.0, Unit::Meter);
867 "IsGreater returned false");
873 Length one (2.0, Unit::Meter);
874 Length two (1.0, Unit::Meter);
877 "IsGreater returned true");
883 const double tolerance = 0.01;
885 Length one (5.1234, Unit::Meter);
886 Length two (5.1278, Unit::Meter);
888 AssertFalse (two.
IsGreater (one, tolerance),
889 "IsGreater returned true");
895 Length l (1.0, Unit::Meter);
897 std::stringstream stream;
902 "unexpected output from operator<<");
908 const double value = 5;
912 std::stringstream stream;
914 stream << value <<
"m";
919 "unexpected length from operator>>");
926 const std::string& expectedOutput,
927 const std::string& context)
929 const std::string msg = context +
": unexpected output when serializing length";
931 std::ostringstream stream;
934 << std::setprecision (5)
943 Length l (1.0, Unit::Meter);
945 TestLengthSerialization (l, Unit::Nanometer,
"1000000000.00000 nm",
"nanometers");
946 TestLengthSerialization (l, Unit::Micrometer,
"1000000.00000 um",
"micrometers");
947 TestLengthSerialization (l, Unit::Millimeter,
"1000.00000 mm",
"millimeters");
948 TestLengthSerialization (l, Unit::Centimeter,
"100.00000 cm",
"centimeters");
949 TestLengthSerialization (l, Unit::Meter,
"1.00000 m",
"meters");
950 TestLengthSerialization (l, Unit::Kilometer,
"0.00100 km",
"kilometers");
951 TestLengthSerialization (l, Unit::NauticalMile,
"0.00054 nmi",
"nautical_mile");
952 TestLengthSerialization (l, Unit::Inch,
"39.37008 in",
"inches");
953 TestLengthSerialization (l, Unit::Foot,
"3.28084 ft",
"feet");
954 TestLengthSerialization (l, Unit::Yard,
"1.09361 yd",
"yards");
955 TestLengthSerialization (l, Unit::Mile,
"0.00062 mi",
"miles");
961 const double value = 5;
963 Length one ( value, Unit::Meter );
964 Length two ( value, Unit::Meter );
966 AssertTrue ( one == two,
967 "operator== returned false for equal lengths");
973 const double value = 5;
975 Length one ( value, Unit::Meter );
976 Length two ( value, Unit::Kilometer );
978 AssertFalse ( one == two,
979 "operator== returned true for non equal lengths");
985 const double value = 5;
987 Length one ( value, Unit::Meter );
988 Length two ( value, Unit::Kilometer);
990 AssertTrue ( one != two,
991 "operator!= returned false for non equal lengths");
998 const double value = 5;
1000 Length one ( value, Unit::Meter );
1001 Length two ( value, Unit::Meter );
1003 AssertFalse ( one != two,
1004 "operator!= returned true for equal lengths");
1011 const double value = 5;
1013 Length one ( value, Unit::Meter );
1014 Length two ( value, Unit::Kilometer);
1016 AssertTrue ( one < two,
1017 "operator< returned false for smaller length");
1023 const double value = 5;
1025 Length one ( value, Unit::Meter );
1026 Length two ( value, Unit::Kilometer);
1028 AssertFalse ( two < one,
1029 "operator< returned true for larger length");
1035 const double value = 5;
1037 Length one ( value, Unit::Meter );
1038 Length two ( value, Unit::Kilometer);
1041 AssertTrue ( one <= two,
1042 "operator<= returned false for smaller length");
1044 AssertTrue ( one <= three,
1045 "operator<= returned false for equal lengths");
1051 const double value = 5;
1053 Length one ( value, Unit::Meter );
1054 Length two ( value, Unit::Kilometer);
1056 AssertFalse ( two <= one,
1057 "operator<= returned true for larger length");
1063 const double value = 5;
1065 Length one ( value, Unit::Meter );
1066 Length two ( value, Unit::Kilometer);
1068 AssertTrue ( two > one,
1069 "operator> returned false for larger length");
1075 const double value = 5;
1077 Length one ( value, Unit::Meter );
1078 Length two ( value, Unit::Kilometer);
1080 AssertFalse ( one > two,
1081 "operator> returned true for smaller length");
1087 const double value = 5;
1089 Length one ( value, Unit::Meter );
1090 Length two ( value, Unit::Kilometer);
1093 AssertTrue ( two >= one,
1094 "operator>= returned false for larger length");
1096 AssertTrue ( one >= three,
1097 "operator>= returned false for equal lengths");
1103 const double value = 5;
1105 Length one ( value, Unit::Meter );
1106 Length two ( value, Unit::Kilometer);
1108 AssertFalse ( one >= two,
1109 "operator>= returned true for smaller length");
1115 const double value = 1;
1116 const double expectedOutput = 2;
1118 Length one ( value, Unit::Meter );
1119 Length two ( value, Unit::Meter );
1124 "operator+ modified first operand");
1126 "operator+ modified second operand");
1128 "operator+ returned incorrect value");
1134 const double value = 1;
1135 const double expectedOutput = 2;
1137 Length one ( value, Unit::Meter );
1142 "operator+ modified first operand");
1144 "operator+ returned incorrect value");
1150 const double value = 1;
1151 const double expectedOutput = 2;
1153 Length one ( value, Unit::Meter );
1158 "operator+ modified first operand");
1160 "operator+ returned incorrect value");
1166 const double value = 1;
1167 const double expectedOutput = 0;
1169 Length one ( value, Unit::Meter );
1170 Length two ( value, Unit::Meter );
1175 "operator- modified first operand");
1177 "operator- modified second operand");
1179 "operator- returned incorrect value");
1185 const double value = 1;
1186 const double expectedOutput = 0;
1188 Length one ( value, Unit::Meter );
1193 "operator- modified first operand");
1195 "operator- returned incorrect value");
1201 const double value = 1;
1202 const double expectedOutput = 0;
1204 Length one ( value, Unit::Meter );
1209 "operator- modified second operand");
1211 "operator- returned incorrect value");
1217 const double value = 1;
1218 const double scalar = 5;
1219 const double expectedOutput = value * scalar;
1221 Length one ( value, Unit::Meter );
1225 "operator* modified first operand");
1227 "operator* returned incorrect value");
1233 const double value = 1;
1234 const double scalar = 5;
1235 const double expectedOutput = value * scalar;
1237 Length one ( value, Unit::Meter );
1241 "operator* modified second operand");
1243 "operator* returned incorrect value");
1249 const double value = 10;
1250 const double scalar = 5;
1251 const double expectedOutput = value / scalar;
1253 Length one ( value, Unit::Meter );
1257 "operator/ modified first operand");
1259 "operator/ returned incorrect value");
1265 const double valueOne = 100;
1266 const double valueTwo = 2;
1267 const double expectedOutput = valueOne / valueTwo;
1269 Length one ( valueOne, Unit::Meter );
1270 Length two ( valueTwo, Unit::Meter );
1272 double result = one / two;
1275 "operator/ modified first operand");
1277 "operator/ modified second operand");
1279 "operator/ returned incorrect value");
1286 const double value = 1;
1288 Length one ( value, Unit::Meter );
1291 double result = one / two;
1293 AssertTrue ( std::isnan (
result),
1294 "operator/ did not return NaN when dividing by zero");
1300 const double topValue = 100;
1301 const double bottomValue = 20;
1302 const int64_t expectedOutput = 5;
1304 Length numerator (topValue, Unit::Meter);
1305 Length denominator (bottomValue, Unit::Meter);
1307 auto result =
Div (numerator, denominator);
1310 "Div() returned an incorrect value");
1316 const double topValue = 100;
1317 const double bottomValue = 20;
1318 const int64_t expectedOutput = 5;
1319 const int64_t expectedRemainder = 0;
1321 Length numerator (topValue, Unit::Meter);
1322 Length denominator (bottomValue, Unit::Meter);
1325 auto result =
Div (numerator, denominator, &remainder);
1328 "Div() returned an incorrect value");
1330 "Div() returned an incorrect remainder");
1336 const double topValue = 110;
1337 const double bottomValue = 20;
1338 const int64_t expectedOutput = 5;
1339 const int64_t expectedRemainder = 10;
1341 Length numerator (topValue, Unit::Meter);
1342 Length denominator (bottomValue, Unit::Meter);
1345 auto result =
Div (numerator, denominator, &remainder);
1348 "Div() returned an incorrect value");
1350 "Div() returned an incorrect remainder");
1356 Length numerator (10, Unit::Meter);
1357 Length denominator (2, Unit::Meter);
1359 auto result =
Mod (numerator, denominator);
1362 "Mod() returned a non zero value");
1368 Length numerator (14, Unit::Meter);
1369 Length denominator (3, Unit::Meter);
1370 const double expectedValue = 2;
1372 auto result =
Mod (numerator, denominator);
1375 "Mod() returned the wrong value");
1381 TestDefaultLengthIsZero ();
1383 TestConstructLengthFromQuantity ();
1385 TestConstructLengthFromSIUnits ();
1387 TestConstructLengthFromUSUnits ();
1389 TestLengthCopyConstructor ();
1391 TestLengthMoveConstructor ();
1393 TestConstructLengthFromMeterString ();
1394 TestConstructLengthFromNanoMeterString ();
1395 TestConstructLengthFromMicroMeterString ();
1396 TestConstructLengthFromMilliMeterString ();
1397 TestConstructLengthFromCentiMeterString ();
1398 TestConstructLengthFromKiloMeterString ();
1399 TestConstructLengthFromNauticalMileString ();
1400 TestConstructLengthFromInchString ();
1401 TestConstructLengthFromFootString ();
1402 TestConstructLengthFromYardString ();
1403 TestConstructLengthFromMileString ();
1405 #ifdef HAVE_BOOST_UNITS
1406 TestConstructLengthFromBoostUnits ();
1409 TestBuilderFreeFunctions ();
1411 TestTryParseReturnsFalse ();
1412 TestTryParseReturnsTrue ();
1414 TestCopyAssignment ();
1415 TestMoveAssignment ();
1416 TestQuantityAssignment ();
1418 TestIsEqualReturnsTrue ();
1419 TestIsEqualReturnsFalse ();
1420 TestIsEqualWithToleranceReturnsTrue ();
1421 TestIsEqualWithToleranceReturnsFalse ();
1422 TestIsNotEqualReturnsTrue ();
1423 TestIsNotEqualReturnsFalse ();
1424 TestIsNotEqualWithToleranceReturnsTrue ();
1425 TestIsNotEqualWithToleranceReturnsFalse ();
1426 TestIsLessReturnsTrue ();
1427 TestIsLessReturnsFalse ();
1428 TestIsLessWithToleranceReturnsFalse ();
1429 TestIsGreaterReturnsTrue ();
1430 TestIsGreaterReturnsFalse ();
1431 TestIsGreaterWithToleranceReturnsFalse ();
1433 TestOutputStreamOperator ();
1435 TestSerializeLengthWithUnit ();
1437 TestOperatorEqualsReturnsTrue ();
1438 TestOperatorEqualsReturnsFalse ();
1439 TestOperatorNotEqualsReturnsTrue ();
1440 TestOperatorNotEqualsReturnsFalse ();
1441 TestOperatorLessThanReturnsTrue ();
1442 TestOperatorLessThanReturnsFalse ();
1443 TestOperatorLessOrEqualReturnsTrue ();
1444 TestOperatorLessOrEqualReturnsFalse ();
1445 TestOperatorGreaterThanReturnsTrue ();
1446 TestOperatorGreaterThanReturnsFalse ();
1447 TestOperatorGreaterOrEqualReturnsTrue ();
1448 TestOperatorGreaterOrEqualReturnsFalse ();
1450 TestAddingTwoLengths ();
1451 TestAddingLengthAndQuantity ();
1452 TestAddingQuantityAndLength ();
1453 TestSubtractingTwoLengths ();
1454 TestSubtractingLengthAndQuantity ();
1455 TestSubtractingQuantityAndLength ();
1456 TestMultiplyLengthByScalar ();
1457 TestMultiplyScalarByLength ();
1458 TestDivideLengthByScalar ();
1459 TestDivideLengthByLength ();
1460 TestDivideLengthByLengthReturnsNaN ();
1462 TestDivReturnsCorrectResult ();
1463 TestDivReturnsZeroRemainder ();
1464 TestDivReturnsCorrectRemainder ();
1466 TestModReturnsZero ();
1467 TestModReturnsNonZero ();
1500 static TypeId GetTypeId ();
1517 void TestAttributeConstructor ();
1522 void TestAttributeSerialization ();
1527 void TestAttributeDeserialization ();
1532 void TestObjectAttribute ();
1537 void TestSetAttributeUsingStringValue ();
1540 virtual void DoRun ();
1546 static TypeId tid =
TypeId (
"LengthValueTestCase::TestObject")
1548 .SetGroupName (
"Test")
1550 .AddAttribute (
"Length",
1577 std::string output = value.SerializeToString (checker);
1580 "Length attribute serialization has wrong output");
1589 std::ostringstream stream;
1593 bool result = value.DeserializeFromString (stream.str (), checker);
1596 "Length attribute deserialization failed");
1598 "Length attribute has wrong value after deserialization");
1604 Length expected (5, Unit::Kilometer);
1607 obj->SetAttribute (
"Length",
LengthValue (expected));
1610 obj->GetAttribute (
"Length", val);
1613 "Length attribute does not have expected value");
1619 Length expected (5, Unit::Kilometer);
1622 std::stringstream stream;
1623 stream << expected.
As (Unit::Kilometer);
1625 obj->SetAttribute (
"Length",
StringValue (stream.str()));
1628 obj->GetAttribute (
"Length", val);
1631 "Length attribute does not have expected value");
1637 TestAttributeConstructor ();
1638 TestAttributeSerialization ();
1639 TestAttributeDeserialization ();
1640 TestObjectAttribute ();
1641 TestSetAttributeUsingStringValue ();
Implements tests for the Length class.
void TestAddingLengthAndQuantity()
Test arithmetic operations.
void TestIsGreaterReturnsFalse()
Test member comparison operators.
void TestOperatorGreaterOrEqualReturnsTrue()
Test free function comparison operators.
void TestOperatorEqualsReturnsFalse()
Test free function comparison operators.
void TestTryParseReturnsTrue()
Test the TryParse function returns true on success.
void TestConstructLengthFromMeterString()
Test that a length object can be constructed from a string.
void TestDivReturnsZeroRemainder()
Test Div function.
void TestBuilderFreeFunctions()
Test constructing length objects using the builder free functions.
void TestConstructLengthFromMileString()
Test that a length object can be constructed from a string.
void TestIsEqualWithToleranceReturnsFalse()
Test member comparison operators.
void TestDivReturnsCorrectRemainder()
Test Div function.
void TestModReturnsZero()
Test Mod function.
void TestConstructLengthFromCentiMeterString()
Test that a length object can be constructed from a string.
void TestDivideLengthByScalar()
Test arithmetic operations.
void TestModReturnsNonZero()
Test Mod function.
void TestLengthMoveConstructor()
Test that the value from one length is copied to another using the move constructor.
void TestTryParseReturnsFalse()
Test the TryParse function returns false on bad input.
void TestCopyAssignment()
Test that a length object can be updated by assignment from another length object.
virtual void DoRun()
Implementation to actually run this TestCase.
void TestIsNotEqualReturnsTrue()
Test member comparison operators.
void TestConstructLengthFromFootString()
Test that a length object can be constructed from a string.
void TestInputStreamOperator()
Test reading length object from a stream produces the expected length value.
void TestDivideLengthByLengthReturnsNaN()
Test arithmetic operations.
void TestIsNotEqualWithToleranceReturnsFalse()
Test member comparison operators.
void TestOperatorLessOrEqualReturnsTrue()
Test free function comparison operators.
void TestIsLessReturnsFalse()
Test member comparison operators.
void TestSubtractingQuantityAndLength()
Test arithmetic operations.
void TestConstructLengthFromMilliMeterString()
Test that a length object can be constructed from a string.
virtual ~LengthTestCase()=default
Destructor.
void TestConstructLengthFromInchString()
Test that a length object can be constructed from a string.
void TestConstructLengthFromSIUnits()
Test that a Length object constructed from various SI units has the correct value in meters.
void TestConstructLengthFromNanoMeterString()
Test that a length object can be constructed from a string.
void TestMultiplyLengthByScalar()
Test arithmetic operations.
void AssertTrue(bool condition, std::string msg)
Helper function to compare results with true.
void TestIsEqualReturnsFalse()
Test member comparison operators.
void TestConstructLengthFromQuantity()
Test that a Length object can be constructed from a Quantity object.
void TestConstructLengthFromKiloMeterString()
Test that a length object can be constructed from a string.
void TestDefaultLengthIsZero()
Test that a default constructed Length object has a value of 0.
void TestOperatorEqualsReturnsTrue()
Test free function comparison operators.
void TestConstructLengthFromUSUnits()
Test that a Length object constructed from various US units has the correct value in meters.
void AssertFalse(bool condition, std::string msg)
Helper function to compare results with false.
void TestIsEqualReturnsTrue()
Test member comparison operators.
void TestIsNotEqualWithToleranceReturnsTrue()
Test member comparison operators.
void TestOperatorLessThanReturnsFalse()
Test free function comparison operators.
void TestIsGreaterWithToleranceReturnsFalse()
Test member comparison operators.
void TestLengthSerialization(const Length &l, const T &unit, const std::string &expectedOutput, const std::string &context)
Generic function for testing serialization of a Length object in various units.
void TestSubtractingLengthAndQuantity()
Test arithmetic operations.
void TestSubtractingTwoLengths()
Test arithmetic operations.
void TestDivideLengthByLength()
Test arithmetic operations.
void TestDivReturnsCorrectResult()
Test Div function.
void TestAddingQuantityAndLength()
Test arithmetic operations.
void TestIsNotEqualReturnsFalse()
Test member comparison operators.
void TestConstructLengthFromMicroMeterString()
Test that a length object can be constructed from a string.
void TestOperatorGreaterThanReturnsTrue()
Test free function comparison operators.
void TestOutputStreamOperator()
Test writing length object to a stream produces the expected output.
LengthTestCase()
Constructor.
void TestConstructLengthFromYardString()
Test that a length object can be constructed from a string.
void TestAddingTwoLengths()
Test arithmetic operations.
void TestConstructLengthFromNauticalMileString()
Test that a length object can be constructed from a string.
void TestIsLessReturnsTrue()
Test member comparison operators.
void TestOperatorGreaterThanReturnsFalse()
Test free function comparison operators.
void TestOperatorLessThanReturnsTrue()
Test free function comparison operators.
void TestOperatorNotEqualsReturnsFalse()
Test free function comparison operators.
void TestMoveAssignment()
Test that a length object can be updated by assignment from a moved length object.
void TestOperatorLessOrEqualReturnsFalse()
Test free function comparison operators.
void TestConstructLengthFromString(double unitValue, double meterValue, double tolerance, const std::initializer_list< std::string > &symbols)
Test that a length object can be constructed from a string.
void TestSerializeLengthWithUnit()
Test serializing a length object to all of the supported unit types.
void TestOperatorGreaterOrEqualReturnsFalse()
Test free function comparison operators.
void TestLengthCopyConstructor()
Test that the value from one length is copied to another using the copy constructor.
void TestOperatorNotEqualsReturnsTrue()
Test free function comparison operators.
void TestMultiplyScalarByLength()
Test arithmetic operations.
void TestIsLessWithToleranceReturnsFalse()
Test member comparison operators.
void TestIsGreaterReturnsTrue()
Test member comparison operators.
void TestQuantityAssignment()
Test that a length object can be updated by assignment from a quantity.
void TestIsEqualWithToleranceReturnsTrue()
Test member comparison operators.
The Test Suite that runs the test case.
LengthTestSuite()
Default Constructor.
Class with Length attribute.
static TypeId GetTypeId()
Get the type ID.
Length m_length
Length object.
Test case for LengthValue attribute.
virtual ~LengthValueTestCase()
Destructor.
void TestAttributeSerialization()
Test that a LengthValue can be serialized to a string.
void TestObjectAttribute()
Test that a LengthValue works as an attribute.
LengthValueTestCase()
Default Constructor.
void TestAttributeConstructor()
Test that a LengthValue can be constructed from a Length instance.
void TestAttributeDeserialization()
Test that a LengthValue can be deserialized from a string.
virtual void DoRun()
Implementation to actually run this TestCase.
void TestSetAttributeUsingStringValue()
Test that a StringValue is converted to LengthValue.
An immutable class which represents a value in a specific length unit.
double Value() const
The value of the quantity.
Represents a length in meters.
double GetDouble() const
Current length value.
bool IsGreater(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is less in value than this instance.
bool IsEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal in value to this instance.
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
static std::optional< Length > TryParse(double value, const std::string &unit)
Attempt to construct a Length object from a value and a unit string.
Unit
Units of length in various measurement systems that are supported by the Length class.
bool IsLess(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater in value than this instance.
bool IsNotEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is not equal in value to this instance.
AttributeValue implementation for Length.
A base class which provides memory management and object aggregation.
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Ptr< const AttributeChecker > MakeLengthChecker(void)
Ptr< const AttributeAccessor > MakeLengthAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Length KiloMeters(double value)
Construct a length from a value in the indicated unit.
Length MilliMeters(double value)
Construct a length from a value in the indicated unit.
Length NauticalMiles(double value)
Construct a length from a value in the indicated unit.
Length Yards(double value)
Construct a length from a value in the indicated unit.
Length Feet(double value)
Construct a length from a value in the indicated unit.
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Length MicroMeters(double value)
Construct a length from a value in the indicated unit.
Length Miles(double value)
Construct a length from a value in the indicated unit.
Length Meters(double value)
Construct a length from a value in the indicated unit.
Length CentiMeters(double value)
Construct a length from a value in the indicated unit.
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
Calculate how many times numerator can be split into denominator sized pieces.
Length NanoMeters(double value)
Construct a length from a value in the indicated unit.
Length Inches(double value)
Construct a length from a value in the indicated unit.
#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.
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
static LengthTestSuite gLengthTestSuite
LengthTestSuite instance.
Every class exported by the ns3 library is enclosed in the ns3 namespace.