11#include "ns3/object.h"
12#include "ns3/string.h"
16#include <boost/units/base_units/us/foot.hpp>
17#include <boost/units/systems/si.hpp>
18#include <boost/units/systems/si/prefixes.hpp>
24#include <initializer_list>
136 const std::initializer_list<std::string>& symbols);
155#ifdef HAVE_BOOST_UNITS
160 void TestConstructLengthFromBoostUnits();
161 void TestConstructLengthFromBoostUnitsMeters();
162 void TestConstructLengthFromBoostUnitsKiloMeters();
163 void TestConstructLengthFromBoostUnitsFeet();
246 const std::string& expectedOutput,
247 const std::string& context);
306 void DoRun()
override;
326 "length constructed from meters has wrong value");
332 using TestEntry = std::tuple<Length, std::string>;
334 const double expectedMeters = 1;
335 const std::initializer_list<TestEntry> inputs{
336 std::make_tuple(
Length(1e9, Unit::Nanometer),
"nanometer"),
337 std::make_tuple(
Length(1e6, Unit::Micrometer),
"micrometer"),
338 std::make_tuple(
Length(1e3, Unit::Millimeter),
"millimeter"),
339 std::make_tuple(
Length(1e2, Unit::Centimeter),
"centimeter"),
340 std::make_tuple(
Length(1e-3, Unit::Kilometer),
"kilometer"),
341 std::make_tuple(
Length((1 / 1852.0), Unit::NauticalMile),
"nautical_mile")};
343 for (
const TestEntry& entry : inputs)
345 const Length& l = std::get<0>(entry);
346 const std::string& context = std::get<1>(entry);
350 context <<
": constructed length from SI unit has wrong value");
357 using TestEntry = std::tuple<Length, std::string>;
359 const double expectedMeters = 0.3048;
360 const double tolerance = 0.0001;
362 const std::initializer_list<TestEntry> inputs{
363 std::make_tuple(
Length(12.0, Unit::Inch),
"inch"),
364 std::make_tuple(
Length(1.0, Unit::Foot),
"foot"),
365 std::make_tuple(
Length((1 / 3.0), Unit::Yard),
"yard"),
366 std::make_tuple(
Length((1 / 5280.0), Unit::Mile),
"mile"),
369 for (
const TestEntry& entry : inputs)
371 const Length& l = std::get<0>(entry);
372 const std::string& context = std::get<1>(entry);
377 "constructed length from US unit (" << context
378 <<
") has wrong value");
385 const double value = 5;
386 Length original(value, Unit::Meter);
392 "copy constructed length has wrong value");
398 const double value = 5;
399 Length original(value, Unit::Meter);
401 Length copy(std::move(original));
410 const std::initializer_list<std::string>& symbols)
412 const std::array<std::string, 2> SEPARATORS{{
"",
" "}};
414 for (
const std::string& symbol : symbols)
416 for (
const std::string& separator : SEPARATORS)
418 std::ostringstream stream;
420 stream << unitValue << separator << symbol;
424 std::ostringstream msg;
425 msg <<
"string constructed length has wrong value: '" << stream.str() <<
"'";
435 const double value = 5;
443 const double value = 5;
444 const double expectedValue = 5e-9;
449 {
"nm",
"nanometer",
"nanometers",
"nanometre",
"nanometres"});
455 const double value = 5;
456 const double expectedValue = 5e-6;
457 const double tolerance = 1e-7;
462 {
"um",
"micrometer",
"micrometers",
"micrometre",
"micrometres"});
468 const double value = 5;
469 const double expectedValue = 5e-3;
470 const double tolerance = 1e-4;
475 {
"mm",
"millimeter",
"millimeters",
"millimetre",
"millimetres"});
481 const double value = 5;
482 const double expectedValue = 5e-2;
483 const double tolerance = 1e-3;
488 {
"cm",
"centimeter",
"centimeters",
"centimetre",
"centimetres"});
494 const double value = 5;
495 const double expectedValue = 5e3;
500 {
"km",
"kilometer",
"kilometers",
"kilometre",
"kilometres"});
506 const double value = 5;
507 const double expectedValue = 9260;
512 {
"nmi",
"nautical mile",
"nautical miles"});
518 const double value = 5;
519 const double expectedValue = 0.127;
520 const double tolerance = 1e-4;
528 const double value = 5;
529 const double expectedValue = 1.524;
530 const double tolerance = 1e-4;
538 const double value = 5;
539 const double expectedValue = 4.572;
540 const double tolerance = 1e-4;
548 const double value = 5;
549 const double expectedValue = 8046.72;
550 const double tolerance = 1e-3;
555#ifdef HAVE_BOOST_UNITS
557LengthTestCase::TestConstructLengthFromBoostUnits()
559 TestConstructLengthFromBoostUnitsMeters();
560 TestConstructLengthFromBoostUnitsKiloMeters();
561 TestConstructLengthFromBoostUnitsFeet();
565LengthTestCase::TestConstructLengthFromBoostUnitsMeters()
567 namespace bu = boost::units;
569 auto meters = 5 * bu::si::meter;
575 "Construction from boost::units meters produced "
580LengthTestCase::TestConstructLengthFromBoostUnitsKiloMeters()
582 namespace bu = boost::units;
583 auto kilometer = bu::si::kilo * bu::si::meter;
585 const double expectedValue = 5000;
586 auto quantity = 5 * kilometer;
592 "Construction from boost::units kilometers produced "
597LengthTestCase::TestConstructLengthFromBoostUnitsFeet()
599 namespace bu = boost::units;
601 bu::us::foot_base_unit::unit_type Foot;
603 const double expectedValue = 3.048;
604 auto feet = 10 * Foot;
611 "Construction from boost::units foot produced "
619 using Builder = std::function<
Length(
double)>;
621 double inputValue = 10;
623 std::map<Unit, Builder> TESTDATA{
637 for (
auto& entry : TESTDATA)
639 Length expected(inputValue, entry.first);
641 Length output = entry.second(inputValue);
645 "The builder free function for "
647 <<
" did not create a Length with the correct value");
656 AssertFalse(l.has_value(),
"TryParse returned true on bad input");
662 using TestInput = std::pair<double, std::string>;
663 using TestArgs = std::pair<double, double>;
664 std::map<TestInput, TestArgs> tests{
667 {{5,
"kilometer"}, {5e3, 0}},
668 {{5,
" kilometer"}, {5e3, 0}},
671 for (
auto& entry : tests)
673 TestInput input = entry.first;
674 TestArgs args = entry.second;
678 AssertTrue(l.has_value(),
"TryParse returned false when expecting true");
680 std::stringstream stream;
681 stream <<
"Parsing input (" << input.first <<
", " << input.second
682 <<
") returned the wrong value";
691 const double value = 5;
693 Length original(value, Unit::Meter);
704 const double value = 5;
706 Length original(value, Unit::Meter);
709 copy = std::move(original);
730 const double value = 5;
731 Length one(value, Unit::Meter);
740 const double value = 5;
741 Length one(value, Unit::Meter);
750 const double value = 5;
751 const double tolerance = 0.1;
753 Length one(value, Unit::Meter);
762 const double value = 5;
763 const double tolerance = 0.01;
765 Length one(value, Unit::Meter);
774 const double value = 5;
776 Length one(value, Unit::Meter);
785 const double value = 5;
787 Length one(value, Unit::Meter);
796 const double tolerance = 0.001;
798 Length one(5.01, Unit::Meter);
802 "IsNotEqual with tolerance returned false for not equal lengths");
808 const double tolerance = 0.01;
810 Length one(5.01, Unit::Meter);
814 "IsNotEqual with tolerance returned true for not equal lengths");
820 const double value = 5;
822 Length one(value, Unit::Meter);
831 const double value = 5;
833 Length one(value, Unit::Meter);
842 const double tolerance = 0.01;
844 Length one(5.1234, Unit::Meter);
853 Length one(2.0, Unit::Meter);
862 Length one(2.0, Unit::Meter);
871 const double tolerance = 0.01;
873 Length one(5.1234, Unit::Meter);
876 AssertFalse(
two.IsGreater(one, tolerance),
"IsGreater returned true");
882 Length l(1.0, Unit::Meter);
884 std::stringstream stream;
894 const double value = 5;
898 std::stringstream stream;
900 stream << value <<
"m";
911 const std::string& expectedOutput,
912 const std::string& context)
914 const std::string msg = context +
": unexpected output when serializing length";
916 std::ostringstream stream;
918 stream << std::fixed << std::setprecision(5) << l.
As(unit);
926 Length l(1.0, Unit::Meter);
944 const double value = 5;
946 Length one(value, Unit::Meter);
949 AssertTrue(one ==
two,
"operator== returned false for equal lengths");
955 const double value = 5;
957 Length one(value, Unit::Meter);
960 AssertFalse(one ==
two,
"operator== returned true for non equal lengths");
966 const double value = 5;
968 Length one(value, Unit::Meter);
971 AssertTrue(one !=
two,
"operator!= returned false for non equal lengths");
977 const double value = 5;
979 Length one(value, Unit::Meter);
982 AssertFalse(one !=
two,
"operator!= returned true for equal lengths");
988 const double value = 5;
990 Length one(value, Unit::Meter);
993 AssertTrue(one <
two,
"operator< returned false for smaller length");
999 const double value = 5;
1001 Length one(value, Unit::Meter);
1004 AssertFalse(
two < one,
"operator< returned true for larger length");
1010 const double value = 5;
1012 Length one(value, Unit::Meter);
1016 AssertTrue(one <=
two,
"operator<= returned false for smaller length");
1018 AssertTrue(one <= three,
"operator<= returned false for equal lengths");
1024 const double value = 5;
1026 Length one(value, Unit::Meter);
1029 AssertFalse(
two <= one,
"operator<= returned true for larger length");
1035 const double value = 5;
1037 Length one(value, Unit::Meter);
1040 AssertTrue(
two > one,
"operator> returned false for larger length");
1046 const double value = 5;
1048 Length one(value, Unit::Meter);
1051 AssertFalse(one >
two,
"operator> returned true for smaller length");
1057 const double value = 5;
1059 Length one(value, Unit::Meter);
1063 AssertTrue(
two >= one,
"operator>= returned false for larger length");
1065 AssertTrue(one >= three,
"operator>= returned false for equal lengths");
1071 const double value = 5;
1073 Length one(value, Unit::Meter);
1076 AssertFalse(one >=
two,
"operator>= returned true for smaller length");
1082 const double value = 1;
1083 const double expectedOutput = 2;
1085 Length one(value, Unit::Meter);
1098 const double value = 1;
1099 const double expectedOutput = 2;
1101 Length one(value, Unit::Meter);
1112 const double value = 1;
1113 const double expectedOutput = 2;
1115 Length one(value, Unit::Meter);
1126 const double value = 1;
1127 const double expectedOutput = 0;
1129 Length one(value, Unit::Meter);
1142 const double value = 1;
1143 const double expectedOutput = 0;
1145 Length one(value, Unit::Meter);
1156 const double value = 1;
1157 const double expectedOutput = 0;
1159 Length one(value, Unit::Meter);
1170 const double value = 1;
1171 const double scalar = 5;
1172 const double expectedOutput = value * scalar;
1174 Length one(value, Unit::Meter);
1175 Length result = one * scalar;
1184 const double value = 1;
1185 const double scalar = 5;
1186 const double expectedOutput = value * scalar;
1188 Length one(value, Unit::Meter);
1189 Length result = scalar * one;
1198 const double value = 10;
1199 const double scalar = 5;
1200 const double expectedOutput = value / scalar;
1202 Length one(value, Unit::Meter);
1203 Length result = one / scalar;
1212 const double valueOne = 100;
1213 const double valueTwo = 2;
1214 const double expectedOutput = valueOne / valueTwo;
1216 Length one(valueOne, Unit::Meter);
1219 double result = one /
two;
1229 const double value = 1;
1231 Length one(value, Unit::Meter);
1234 double result = one /
two;
1236 AssertTrue(std::isnan(result),
"operator/ did not return NaN when dividing by zero");
1242 const double topValue = 100;
1243 const double bottomValue = 20;
1244 const int64_t expectedOutput = 5;
1246 Length numerator(topValue, Unit::Meter);
1247 Length denominator(bottomValue, Unit::Meter);
1249 auto result =
Div(numerator, denominator);
1257 const double topValue = 100;
1258 const double bottomValue = 20;
1259 const int64_t expectedOutput = 5;
1260 const int64_t expectedRemainder = 0;
1262 Length numerator(topValue, Unit::Meter);
1263 Length denominator(bottomValue, Unit::Meter);
1266 auto result =
Div(numerator, denominator, &remainder);
1271 "Div() returned an incorrect remainder");
1277 const double topValue = 110;
1278 const double bottomValue = 20;
1279 const int64_t expectedOutput = 5;
1280 const int64_t expectedRemainder = 10;
1282 Length numerator(topValue, Unit::Meter);
1283 Length denominator(bottomValue, Unit::Meter);
1286 auto result =
Div(numerator, denominator, &remainder);
1291 "Div() returned an incorrect remainder");
1297 Length numerator(10, Unit::Meter);
1298 Length denominator(2, Unit::Meter);
1300 auto result =
Mod(numerator, denominator);
1308 Length numerator(14, Unit::Meter);
1309 Length denominator(3, Unit::Meter);
1310 const double expectedValue = 2;
1312 auto result =
Mod(numerator, denominator);
1344#ifdef HAVE_BOOST_UNITS
1345 TestConstructLengthFromBoostUnits();
1483 void DoRun()
override;
1489 static TypeId tid =
TypeId(
"LengthValueTestCase::TestObject")
1491 .SetGroupName(
"Test")
1493 .AddAttribute(
"Length",
1519 std::string output = value.SerializeToString(checker);
1530 std::ostringstream stream;
1534 bool result = value.DeserializeFromString(stream.str(), checker);
1543 Length expected(5, Unit::Kilometer);
1546 obj->SetAttribute(
"Length",
LengthValue(expected));
1549 obj->GetAttribute(
"Length", val);
1557 Length expected(5, Unit::Kilometer);
1560 std::stringstream stream;
1561 stream << expected.
As(Unit::Kilometer);
1563 obj->SetAttribute(
"Length",
StringValue(stream.str()));
1566 obj->GetAttribute(
"Length", val);
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.
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.
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 DoRun() override
Implementation to actually run this TestCase.
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.
~LengthTestCase() override=default
Destructor.
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.
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.
void DoRun() override
Implementation to actually run this TestCase.
void TestSetAttributeUsingStringValue()
Test that a StringValue is converted to LengthValue.
~LengthValueTestCase() override
Destructor.
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.
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, Duration duration=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.
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.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
#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.
Ptr< const AttributeChecker > MakeLengthChecker()
Ptr< const AttributeAccessor > MakeLengthAccessor(T1 a1)
-ray-to-three-gpp-ch-calibration