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>
135 const std::initializer_list<std::string>& symbols);
154#ifdef HAVE_BOOST_UNITS
159 void TestConstructLengthFromBoostUnits();
160 void TestConstructLengthFromBoostUnitsMeters();
161 void TestConstructLengthFromBoostUnitsKiloMeters();
162 void TestConstructLengthFromBoostUnitsFeet();
245 const std::string& expectedOutput,
246 const std::string& context);
309 void DoRun()
override;
329 "length constructed from meters has wrong value");
335 using TestEntry = std::tuple<Length, std::string>;
337 const double expectedMeters = 1;
338 const std::initializer_list<TestEntry> inputs{
339 std::make_tuple(
Length(1e9, Unit::Nanometer),
"nanometer"),
340 std::make_tuple(
Length(1e6, Unit::Micrometer),
"micrometer"),
341 std::make_tuple(
Length(1e3, Unit::Millimeter),
"millimeter"),
342 std::make_tuple(
Length(1e2, Unit::Centimeter),
"centimeter"),
343 std::make_tuple(
Length(1e-3, Unit::Kilometer),
"kilometer"),
344 std::make_tuple(
Length((1 / 1852.0), Unit::NauticalMile),
"nautical_mile")};
346 for (
const TestEntry& entry : inputs)
348 const Length& l = std::get<0>(entry);
349 const std::string& context = std::get<1>(entry);
353 context <<
": constructed length from SI unit has wrong value");
360 using TestEntry = std::tuple<Length, std::string>;
362 const double expectedMeters = 0.3048;
363 const double tolerance = 0.0001;
365 const std::initializer_list<TestEntry> inputs{
366 std::make_tuple(
Length(12.0, Unit::Inch),
"inch"),
367 std::make_tuple(
Length(1.0, Unit::Foot),
"foot"),
368 std::make_tuple(
Length((1 / 3.0), Unit::Yard),
"yard"),
369 std::make_tuple(
Length((1 / 5280.0), Unit::Mile),
"mile"),
372 for (
const TestEntry& entry : inputs)
374 const Length& l = std::get<0>(entry);
375 const std::string& context = std::get<1>(entry);
380 "constructed length from US unit (" << context
381 <<
") has wrong value");
388 const double value = 5;
389 Length original(value, Unit::Meter);
395 "copy constructed length has wrong value");
401 const double value = 5;
402 Length original(value, Unit::Meter);
404 Length copy(std::move(original));
413 const std::initializer_list<std::string>& symbols)
415 const std::array<std::string, 2> SEPARATORS{{
"",
" "}};
417 for (
const std::string& symbol : symbols)
419 for (
const std::string& separator : SEPARATORS)
421 std::ostringstream stream;
423 stream << unitValue << separator << symbol;
427 std::ostringstream msg;
428 msg <<
"string constructed length has wrong value: '" << stream.str() <<
"'";
438 const double value = 5;
446 const double value = 5;
447 const double expectedValue = 5e-9;
452 {
"nm",
"nanometer",
"nanometers",
"nanometre",
"nanometres"});
458 const double value = 5;
459 const double expectedValue = 5e-6;
460 const double tolerance = 1e-7;
465 {
"um",
"micrometer",
"micrometers",
"micrometre",
"micrometres"});
471 const double value = 5;
472 const double expectedValue = 5e-3;
473 const double tolerance = 1e-4;
478 {
"mm",
"millimeter",
"millimeters",
"millimetre",
"millimetres"});
484 const double value = 5;
485 const double expectedValue = 5e-2;
486 const double tolerance = 1e-3;
491 {
"cm",
"centimeter",
"centimeters",
"centimetre",
"centimetres"});
497 const double value = 5;
498 const double expectedValue = 5e3;
503 {
"km",
"kilometer",
"kilometers",
"kilometre",
"kilometres"});
509 const double value = 5;
510 const double expectedValue = 9260;
515 {
"nmi",
"nautical mile",
"nautical miles"});
521 const double value = 5;
522 const double expectedValue = 0.127;
523 const double tolerance = 1e-4;
531 const double value = 5;
532 const double expectedValue = 1.524;
533 const double tolerance = 1e-4;
541 const double value = 5;
542 const double expectedValue = 4.572;
543 const double tolerance = 1e-4;
551 const double value = 5;
552 const double expectedValue = 8046.72;
553 const double tolerance = 1e-3;
558#ifdef HAVE_BOOST_UNITS
560LengthTestCase::TestConstructLengthFromBoostUnits()
562 TestConstructLengthFromBoostUnitsMeters();
563 TestConstructLengthFromBoostUnitsKiloMeters();
564 TestConstructLengthFromBoostUnitsFeet();
568LengthTestCase::TestConstructLengthFromBoostUnitsMeters()
570 namespace bu = boost::units;
572 auto meters = 5 * bu::si::meter;
578 "Construction from boost::units meters produced "
583LengthTestCase::TestConstructLengthFromBoostUnitsKiloMeters()
585 namespace bu = boost::units;
586 auto kilometer = bu::si::kilo * bu::si::meter;
588 const double expectedValue = 5000;
589 auto quantity = 5 * kilometer;
595 "Construction from boost::units kilometers produced "
600LengthTestCase::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;
614 "Construction from boost::units foot produced "
622 using Builder = std::function<
Length(
double)>;
624 double inputValue = 10;
626 std::map<Unit, Builder> TESTDATA{
640 for (
auto& entry : TESTDATA)
642 Length expected(inputValue, entry.first);
644 Length output = entry.second(inputValue);
648 "The builder free function for "
650 <<
" did not create a Length with the correct value");
659 AssertFalse(l.has_value(),
"TryParse returned true on bad input");
665 using TestInput = std::pair<double, std::string>;
666 using TestArgs = std::pair<double, double>;
667 std::map<TestInput, TestArgs>
tests{
670 {{5,
"kilometer"}, {5e3, 0}},
671 {{5,
" kilometer"}, {5e3, 0}},
674 for (
auto& entry :
tests)
676 TestInput input = entry.first;
677 TestArgs args = entry.second;
681 AssertTrue(l.has_value(),
"TryParse returned false when expecting true");
683 std::stringstream stream;
684 stream <<
"Parsing input (" << input.first <<
", " << input.second
685 <<
") returned the wrong value";
694 const double value = 5;
696 Length original(value, Unit::Meter);
707 const double value = 5;
709 Length original(value, Unit::Meter);
712 copy = std::move(original);
733 const double value = 5;
734 Length one(value, Unit::Meter);
743 const double value = 5;
744 Length one(value, Unit::Meter);
753 const double value = 5;
754 const double tolerance = 0.1;
756 Length one(value, Unit::Meter);
765 const double value = 5;
766 const double tolerance = 0.01;
768 Length one(value, Unit::Meter);
777 const double value = 5;
779 Length one(value, Unit::Meter);
788 const double value = 5;
790 Length one(value, Unit::Meter);
799 const double tolerance = 0.001;
801 Length one(5.01, Unit::Meter);
805 "IsNotEqual with tolerance returned false for not equal lengths");
811 const double tolerance = 0.01;
813 Length one(5.01, Unit::Meter);
817 "IsNotEqual with tolerance returned true for not equal lengths");
823 const double value = 5;
825 Length one(value, Unit::Meter);
834 const double value = 5;
836 Length one(value, Unit::Meter);
845 const double tolerance = 0.01;
847 Length one(5.1234, Unit::Meter);
856 Length one(2.0, Unit::Meter);
865 Length one(2.0, Unit::Meter);
874 const double tolerance = 0.01;
876 Length one(5.1234, Unit::Meter);
879 AssertFalse(
two.IsGreater(one, tolerance),
"IsGreater returned true");
885 Length l(1.0, Unit::Meter);
887 std::stringstream stream;
897 const double value = 5;
901 std::stringstream stream;
903 stream << value <<
"m";
914 const std::string& expectedOutput,
915 const std::string& context)
917 const std::string msg = context +
": unexpected output when serializing length";
919 std::ostringstream stream;
921 stream << std::fixed << std::setprecision(5) << l.
As(unit);
929 Length l(1.0, Unit::Meter);
947 const double value = 5;
949 Length one(value, Unit::Meter);
952 AssertTrue(one ==
two,
"operator== returned false for equal lengths");
958 const double value = 5;
960 Length one(value, Unit::Meter);
963 AssertFalse(one ==
two,
"operator== returned true for non equal lengths");
969 const double value = 5;
971 Length one(value, Unit::Meter);
974 AssertTrue(one !=
two,
"operator!= returned false for non equal lengths");
980 const double value = 5;
982 Length one(value, Unit::Meter);
985 AssertFalse(one !=
two,
"operator!= returned true for equal lengths");
991 const double value = 5;
993 Length one(value, Unit::Meter);
996 AssertTrue(one <
two,
"operator< returned false for smaller length");
1002 const double value = 5;
1004 Length one(value, Unit::Meter);
1007 AssertFalse(
two < one,
"operator< returned true for larger length");
1013 const double value = 5;
1015 Length one(value, Unit::Meter);
1019 AssertTrue(one <=
two,
"operator<= returned false for smaller length");
1021 AssertTrue(one <= three,
"operator<= returned false for equal lengths");
1027 const double value = 5;
1029 Length one(value, Unit::Meter);
1032 AssertFalse(
two <= one,
"operator<= returned true for larger length");
1038 const double value = 5;
1040 Length one(value, Unit::Meter);
1043 AssertTrue(
two > one,
"operator> returned false for larger length");
1049 const double value = 5;
1051 Length one(value, Unit::Meter);
1054 AssertFalse(one >
two,
"operator> returned true for smaller length");
1060 const double value = 5;
1062 Length one(value, Unit::Meter);
1066 AssertTrue(
two >= one,
"operator>= returned false for larger length");
1068 AssertTrue(one >= three,
"operator>= returned false for equal lengths");
1074 const double value = 5;
1076 Length one(value, Unit::Meter);
1079 AssertFalse(one >=
two,
"operator>= returned true for smaller length");
1085 const double value = 1;
1086 const double expectedOutput = 2;
1088 Length one(value, Unit::Meter);
1101 const double value = 1;
1102 const double expectedOutput = 2;
1104 Length one(value, Unit::Meter);
1115 const double value = 1;
1116 const double expectedOutput = 2;
1118 Length one(value, Unit::Meter);
1129 const double value = 1;
1130 const double expectedOutput = 0;
1132 Length one(value, Unit::Meter);
1145 const double value = 1;
1146 const double expectedOutput = 0;
1148 Length one(value, Unit::Meter);
1159 const double value = 1;
1160 const double expectedOutput = 0;
1162 Length one(value, Unit::Meter);
1173 const double value = 1;
1174 const double scalar = 5;
1175 const double expectedOutput = value * scalar;
1177 Length one(value, Unit::Meter);
1187 const double value = 1;
1188 const double scalar = 5;
1189 const double expectedOutput = value * scalar;
1191 Length one(value, Unit::Meter);
1201 const double value = 10;
1202 const double scalar = 5;
1203 const double expectedOutput = value / scalar;
1205 Length one(value, Unit::Meter);
1215 const double valueOne = 100;
1216 const double valueTwo = 2;
1217 const double expectedOutput = valueOne / valueTwo;
1219 Length one(valueOne, Unit::Meter);
1232 const double value = 1;
1234 Length one(value, Unit::Meter);
1239 AssertTrue(std::isnan(
result),
"operator/ did not return NaN when dividing by zero");
1245 const double topValue = 100;
1246 const double bottomValue = 20;
1247 const int64_t expectedOutput = 5;
1249 Length numerator(topValue, Unit::Meter);
1250 Length denominator(bottomValue, Unit::Meter);
1252 auto result =
Div(numerator, denominator);
1260 const double topValue = 100;
1261 const double bottomValue = 20;
1262 const int64_t expectedOutput = 5;
1263 const int64_t expectedRemainder = 0;
1265 Length numerator(topValue, Unit::Meter);
1266 Length denominator(bottomValue, Unit::Meter);
1274 "Div() returned an incorrect remainder");
1280 const double topValue = 110;
1281 const double bottomValue = 20;
1282 const int64_t expectedOutput = 5;
1283 const int64_t expectedRemainder = 10;
1285 Length numerator(topValue, Unit::Meter);
1286 Length denominator(bottomValue, Unit::Meter);
1294 "Div() returned an incorrect remainder");
1300 Length numerator(10, Unit::Meter);
1301 Length denominator(2, Unit::Meter);
1303 auto result =
Mod(numerator, denominator);
1311 Length numerator(14, Unit::Meter);
1312 Length denominator(3, Unit::Meter);
1313 const double expectedValue = 2;
1315 auto result =
Mod(numerator, denominator);
1347#ifdef HAVE_BOOST_UNITS
1348 TestConstructLengthFromBoostUnits();
1486 void DoRun()
override;
1492 static TypeId tid =
TypeId(
"LengthValueTestCase::TestObject")
1494 .SetGroupName(
"Test")
1496 .AddAttribute(
"Length",
1522 std::string output = value.SerializeToString(checker);
1533 std::ostringstream stream;
1537 bool result = value.DeserializeFromString(stream.str(), checker);
1546 Length expected(5, Unit::Kilometer);
1549 obj->SetAttribute(
"Length",
LengthValue(expected));
1552 obj->GetAttribute(
"Length", val);
1560 Length expected(5, Unit::Kilometer);
1563 std::stringstream stream;
1564 stream << expected.
As(Unit::Kilometer);
1566 obj->SetAttribute(
"Length",
StringValue(stream.str()));
1569 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
Call graph was not generated because of its size.
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.
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.
AttributeValue implementation for Length.
Object()
Caller graph was not generated because of its size.
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.
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Ptr< const AttributeChecker > MakeLengthChecker()
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.
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...
Length::Unit Unit
Save some typing by defining a short alias for Length::Unit.
static LengthTestSuite gLengthTestSuite
LengthTestSuite instance.
Namespace for test files, TestCases and TestSuites.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
-ray-to-three-gpp-ch-calibration