A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
length-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathew Bielejeski<bielejeski1@llnl.gov>
7 */
8
9#include "ns3/length.h"
10#include "ns3/log.h"
11#include "ns3/object.h"
12#include "ns3/string.h"
13#include "ns3/test.h"
14
15#ifdef HAVE_BOOST
16#include <boost/units/base_units/us/foot.hpp>
17#include <boost/units/systems/si.hpp>
18#include <boost/units/systems/si/prefixes.hpp>
19#endif
20
21#include <array>
22#include <cmath>
23#include <functional>
24#include <initializer_list>
25#include <iomanip>
26#include <map>
27#include <sstream>
28#include <string>
29
30/**
31 * @file
32 * @ingroup length-tests
33 * Length class tests.
34 */
35
36/**
37 * @ingroup core-tests length
38 * @defgroup length-tests Length test suite
39 */
40
41using namespace ns3;
42
43/**
44 * Save some typing by defining a short alias for Length::Unit
45 */
47
48/**
49 * Implements tests for the Length class
50 */
52{
53 public:
54 /**
55 * Constructor
56 */
58 : TestCase("length-tests")
59 {
60 }
61
62 /**
63 * Destructor
64 */
65 ~LengthTestCase() override = default;
66
67 protected:
68 /**
69 * Helper function to compare results with false
70 *
71 * @param condition The boolean condition to test
72 * @param msg The message to print if the test fails
73 */
74 void AssertFalse(bool condition, std::string msg)
75 {
76 NS_TEST_ASSERT_MSG_EQ(condition, false, msg);
77 }
78
79 /**
80 * Helper function to compare results with true
81 *
82 * @param condition The boolean condition to test
83 * @param msg The message to print if the test fails
84 */
85 void AssertTrue(bool condition, std::string msg)
86 {
87 NS_TEST_ASSERT_MSG_EQ(condition, true, msg);
88 }
89
90 private:
91 /**
92 * Test that a default constructed Length object has a value of 0
93 */
95
96 /**
97 * Test that a Length object can be constructed from a Quantity object
98 */
100
101 /**
102 * Test that a Length object constructed from various SI units has the
103 * correct value in meters
104 */
106
107 /**
108 * Test that a Length object constructed from various US units has the
109 * correct value in meters
110 */
112
113 /**
114 * Test that the value from one length is copied to another
115 * using the copy constructor.
116 */
118
119 /**
120 * Test that the value from one length is copied to another
121 * using the move constructor.
122 */
124
125 /**
126 * Test that a length object can be constructed from a string
127 * @param unitValue //!< Value to test.
128 * @param meterValue //!< Reference value [m].
129 * @param tolerance //!< Tolerance.
130 * @param symbols //!< Unit symbols.
131 */
132 void TestConstructLengthFromString(double unitValue,
133 double meterValue,
134 double tolerance,
135 const std::initializer_list<std::string>& symbols);
136
137 /**
138 * Test that a length object can be constructed from a string
139 * @{
140 */
152 /** @} */
153
154#ifdef HAVE_BOOST_UNITS
155 /**
156 * Test construction from boost::units
157 * @{
158 */
159 void TestConstructLengthFromBoostUnits();
160 void TestConstructLengthFromBoostUnitsMeters();
161 void TestConstructLengthFromBoostUnitsKiloMeters();
162 void TestConstructLengthFromBoostUnitsFeet();
163 /** @} */
164#endif
165
166 /**
167 * Test constructing length objects using the builder free functions
168 * @{
169 */
171 /** @} */
172
173 /**
174 * Test the TryParse function returns false on bad input
175 */
177
178 /**
179 * Test the TryParse function returns true on success
180 */
182
183 /**
184 * Test that a length object can be updated by assignment from another
185 * length object
186 */
187 void TestCopyAssignment();
188
189 /**
190 * Test that a length object can be updated by assignment from a moved
191 * length object
192 */
193 void TestMoveAssignment();
194
195 /**
196 * Test that a length object can be updated by assignment from a quantity
197 */
199
200 /**
201 * Test member comparison operators
202 * @{
203 */
218 /** @} */
219
220 /**
221 * Test writing length object to a stream produces the expected output
222 */
224
225 /**
226 * Test reading length object from a stream produces the expected length
227 * value
228 */
230
231 /**
232 * Generic function for testing serialization of a Length object in
233 * various units
234 *
235 * @tparam T Type of the length unit that should be output during serialization
236 *
237 * @param l Length object to serialize
238 * @param unit Unit that the length value will be converted to before serialization
239 * @param expectedOutput Expected result of the serialization
240 * @param context Included in the error message if the test fails
241 */
242 template <class T>
243 void TestLengthSerialization(const Length& l,
244 const T& unit,
245 const std::string& expectedOutput,
246 const std::string& context);
247
248 /**
249 * Test serializing a length object to all of the supported unit types
250 */
252
253 /**
254 * Test free function comparison operators
255 * @{
256 */
269 /** @} */
270
271 /**
272 * Test arithmetic operations
273 * @{
274 */
286 /** @} */
287
288 /**
289 * Test Div function
290 * @{
291 */
295 /** @} */
296
297 /**
298 * Test Mod function
299 * @{
300 */
301 void TestModReturnsZero();
303 /** @} */
304
305 /**
306 * Call graph was not generated because of its size.
307 * @hidecallgraph
308 */
309 void DoRun() override;
310};
311
312void
314{
315 Length l;
316
317 NS_TEST_ASSERT_MSG_EQ(l.GetDouble(), 0, "Default value of Length is not 0");
318}
319
320void
322{
323 const Length::Quantity VALUE(5.0, Unit::Meter);
324
325 Length l(VALUE);
326
328 VALUE.Value(),
329 "length constructed from meters has wrong value");
330}
331
332void
334{
335 using TestEntry = std::tuple<Length, std::string>;
336
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")};
345
346 for (const TestEntry& entry : inputs)
347 {
348 const Length& l = std::get<0>(entry);
349 const std::string& context = std::get<1>(entry);
350
352 expectedMeters,
353 context << ": constructed length from SI unit has wrong value");
354 }
355}
356
357void
359{
360 using TestEntry = std::tuple<Length, std::string>;
361
362 const double expectedMeters = 0.3048;
363 const double tolerance = 0.0001;
364
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"),
370 };
371
372 for (const TestEntry& entry : inputs)
373 {
374 const Length& l = std::get<0>(entry);
375 const std::string& context = std::get<1>(entry);
376
378 expectedMeters,
379 tolerance,
380 "constructed length from US unit (" << context
381 << ") has wrong value");
382 }
383}
384
385void
387{
388 const double value = 5;
389 Length original(value, Unit::Meter);
390
391 Length copy(original);
392
394 original.GetDouble(),
395 "copy constructed length has wrong value");
396}
397
398void
400{
401 const double value = 5;
402 Length original(value, Unit::Meter);
403
404 Length copy(std::move(original));
405
406 NS_TEST_ASSERT_MSG_EQ(copy.GetDouble(), value, "move constructed length has wrong value");
407}
408
409void
411 double meterValue,
412 double tolerance,
413 const std::initializer_list<std::string>& symbols)
414{
415 const std::array<std::string, 2> SEPARATORS{{"", " "}};
416
417 for (const std::string& symbol : symbols)
418 {
419 for (const std::string& separator : SEPARATORS)
420 {
421 std::ostringstream stream;
422
423 stream << unitValue << separator << symbol;
424
425 Length l(stream.str());
426
427 std::ostringstream msg;
428 msg << "string constructed length has wrong value: '" << stream.str() << "'";
429
430 NS_TEST_ASSERT_MSG_EQ_TOL(l.GetDouble(), meterValue, tolerance, msg.str());
431 }
432 }
433}
434
435void
437{
438 const double value = 5;
439
440 TestConstructLengthFromString(value, value, 0, {"m", "meter", "meters", "metre", "metres"});
441}
442
443void
445{
446 const double value = 5;
447 const double expectedValue = 5e-9;
448
450 expectedValue,
451 0,
452 {"nm", "nanometer", "nanometers", "nanometre", "nanometres"});
453}
454
455void
457{
458 const double value = 5;
459 const double expectedValue = 5e-6;
460 const double tolerance = 1e-7;
461
463 expectedValue,
464 tolerance,
465 {"um", "micrometer", "micrometers", "micrometre", "micrometres"});
466}
467
468void
470{
471 const double value = 5;
472 const double expectedValue = 5e-3;
473 const double tolerance = 1e-4;
474
476 expectedValue,
477 tolerance,
478 {"mm", "millimeter", "millimeters", "millimetre", "millimetres"});
479}
480
481void
483{
484 const double value = 5;
485 const double expectedValue = 5e-2;
486 const double tolerance = 1e-3;
487
489 expectedValue,
490 tolerance,
491 {"cm", "centimeter", "centimeters", "centimetre", "centimetres"});
492}
493
494void
496{
497 const double value = 5;
498 const double expectedValue = 5e3;
499
501 expectedValue,
502 0,
503 {"km", "kilometer", "kilometers", "kilometre", "kilometres"});
504}
505
506void
508{
509 const double value = 5;
510 const double expectedValue = 9260;
511
513 expectedValue,
514 0,
515 {"nmi", "nautical mile", "nautical miles"});
516}
517
518void
520{
521 const double value = 5;
522 const double expectedValue = 0.127;
523 const double tolerance = 1e-4;
524
525 TestConstructLengthFromString(value, expectedValue, tolerance, {"in", "inch", "inches"});
526}
527
528void
530{
531 const double value = 5;
532 const double expectedValue = 1.524;
533 const double tolerance = 1e-4;
534
535 TestConstructLengthFromString(value, expectedValue, tolerance, {"ft", "foot", "feet"});
536}
537
538void
540{
541 const double value = 5;
542 const double expectedValue = 4.572;
543 const double tolerance = 1e-4;
544
545 TestConstructLengthFromString(value, expectedValue, tolerance, {"yd", "yard", "yards"});
546}
547
548void
550{
551 const double value = 5;
552 const double expectedValue = 8046.72;
553 const double tolerance = 1e-3;
554
555 TestConstructLengthFromString(value, expectedValue, tolerance, {"mi", "mile", "miles"});
556}
557
558#ifdef HAVE_BOOST_UNITS
559void
560LengthTestCase::TestConstructLengthFromBoostUnits()
561{
562 TestConstructLengthFromBoostUnitsMeters();
563 TestConstructLengthFromBoostUnitsKiloMeters();
564 TestConstructLengthFromBoostUnitsFeet();
565}
566
567void
568LengthTestCase::TestConstructLengthFromBoostUnitsMeters()
569{
570 namespace bu = boost::units;
571
572 auto meters = 5 * bu::si::meter;
573
574 Length l(meters);
575
576 NS_TEST_ASSERT_MSG_EQ(l.GetDouble(),
577 meters.value(),
578 "Construction from boost::units meters produced "
579 "incorrect value");
580}
581
582void
583LengthTestCase::TestConstructLengthFromBoostUnitsKiloMeters()
584{
585 namespace bu = boost::units;
586 auto kilometer = bu::si::kilo * bu::si::meter;
587
588 const double expectedValue = 5000;
589 auto quantity = 5 * kilometer;
590
591 Length l(quantity);
592
593 NS_TEST_ASSERT_MSG_EQ(l.GetDouble(),
594 expectedValue,
595 "Construction from boost::units kilometers produced "
596 "incorrect value");
597}
598
599void
600LengthTestCase::TestConstructLengthFromBoostUnitsFeet()
601{
602 namespace bu = boost::units;
603
604 bu::us::foot_base_unit::unit_type Foot;
605
606 const double expectedValue = 3.048;
607 auto feet = 10 * Foot;
608
609 Length l(feet);
610
611 NS_TEST_ASSERT_MSG_EQ_TOL(l.GetDouble(),
612 expectedValue,
613 0.001,
614 "Construction from boost::units foot produced "
615 "incorrect value");
616}
617#endif
618
619void
621{
622 using Builder = std::function<Length(double)>;
623
624 double inputValue = 10;
625
626 std::map<Unit, Builder> TESTDATA{
627 {Unit::Nanometer, NanoMeters},
628 {Unit::Micrometer, MicroMeters},
629 {Unit::Millimeter, MilliMeters},
630 {Unit::Centimeter, CentiMeters},
631 {Unit::Meter, Meters},
632 {Unit::Kilometer, KiloMeters},
633 {Unit::NauticalMile, NauticalMiles},
634 {Unit::Inch, Inches},
635 {Unit::Foot, Feet},
636 {Unit::Yard, Yards},
637 {Unit::Mile, Miles},
638 };
639
640 for (auto& entry : TESTDATA)
641 {
642 Length expected(inputValue, entry.first);
643
644 Length output = entry.second(inputValue);
645
647 expected,
648 "The builder free function for "
649 << entry.first
650 << " did not create a Length with the correct value");
651 }
652}
653
654void
656{
657 auto l = Length::TryParse(1, "");
658
659 AssertFalse(l.has_value(), "TryParse returned true on bad input");
660}
661
662void
664{
665 using TestInput = std::pair<double, std::string>;
666 using TestArgs = std::pair<double, double>;
667 std::map<TestInput, TestArgs> tests{
668 {{5, "m"}, {5, 0}},
669 {{5, " m"}, {5, 0}},
670 {{5, "kilometer"}, {5e3, 0}},
671 {{5, " kilometer"}, {5e3, 0}},
672 };
673
674 for (auto& entry : tests)
675 {
676 TestInput input = entry.first;
677 TestArgs args = entry.second;
678
679 auto l = Length::TryParse(input.first, input.second);
680
681 AssertTrue(l.has_value(), "TryParse returned false when expecting true");
682
683 std::stringstream stream;
684 stream << "Parsing input (" << input.first << ", " << input.second
685 << ") returned the wrong value";
686
687 NS_TEST_ASSERT_MSG_EQ_TOL(l->GetDouble(), args.first, args.second, stream.str());
688 }
689}
690
691void
693{
694 const double value = 5;
695
696 Length original(value, Unit::Meter);
697
698 Length copy;
699 copy = original;
700
701 NS_TEST_ASSERT_MSG_EQ(copy.GetDouble(), original.GetDouble(), "copy assignment failed");
702}
703
704void
706{
707 const double value = 5;
708
709 Length original(value, Unit::Meter);
710
711 Length copy;
712 copy = std::move(original);
713
714 NS_TEST_ASSERT_MSG_EQ(copy.GetDouble(), value, "move assignment failed");
715}
716
717void
719{
720 Length::Quantity input(5, Unit::Kilometer);
721
722 Length l;
723 Length expected(input);
724
725 l = input;
726
727 NS_TEST_ASSERT_MSG_EQ(l, expected, "quantity assignment failed");
728}
729
730void
732{
733 const double value = 5;
734 Length one(value, Unit::Meter);
735 Length two(one);
736
737 AssertTrue(one.IsEqual(two), "IsEqual returned false for equal lengths");
738}
739
740void
742{
743 const double value = 5;
744 Length one(value, Unit::Meter);
745 Length two(value, Unit::Foot);
746
747 AssertFalse(one.IsEqual(two), "IsEqual returned true for unequal lengths");
748}
749
750void
752{
753 const double value = 5;
754 const double tolerance = 0.1;
755
756 Length one(value, Unit::Meter);
757 Length two((value + 0.1), Unit::Meter);
758
759 AssertTrue(one.IsEqual(two, tolerance), "IsEqual returned false for almost equal lengths");
760}
761
762void
764{
765 const double value = 5;
766 const double tolerance = 0.01;
767
768 Length one(value, Unit::Meter);
769 Length two((value + 0.1), Unit::Meter);
770
771 AssertFalse(one.IsEqual(two, tolerance), "IsEqual returned true for almost equal lengths");
772}
773
774void
776{
777 const double value = 5;
778
779 Length one(value, Unit::Meter);
780 Length two((value + 0.1), Unit::Meter);
781
782 AssertTrue(one.IsNotEqual(two), "IsNotEqual returned false for not equal lengths");
783}
784
785void
787{
788 const double value = 5;
789
790 Length one(value, Unit::Meter);
791 Length two(one);
792
793 AssertFalse(one.IsNotEqual(two), "IsNotEqual returned true for equal lengths");
794}
795
796void
798{
799 const double tolerance = 0.001;
800
801 Length one(5.01, Unit::Meter);
802 Length two(5.02, Unit::Meter);
803
804 AssertTrue(one.IsNotEqual(two, tolerance),
805 "IsNotEqual with tolerance returned false for not equal lengths");
806}
807
808void
810{
811 const double tolerance = 0.01;
812
813 Length one(5.01, Unit::Meter);
814 Length two(5.02, Unit::Meter);
815
816 AssertFalse(one.IsNotEqual(two, tolerance),
817 "IsNotEqual with tolerance returned true for not equal lengths");
818}
819
820void
822{
823 const double value = 5;
824
825 Length one(value, Unit::Meter);
826 Length two((value + 0.1), Unit::Meter);
827
828 AssertTrue(one.IsLess(two), "IsLess returned false for non equal lengths");
829}
830
831void
833{
834 const double value = 5;
835
836 Length one(value, Unit::Meter);
837 Length two(one);
838
839 AssertFalse(one.IsLess(two), "IsLess returned true for equal lengths");
840}
841
842void
844{
845 const double tolerance = 0.01;
846
847 Length one(5.1234, Unit::Meter);
848 Length two(5.1278, Unit::Meter);
849
850 AssertFalse(one.IsLess(two, tolerance), "IsLess with tolerance returned true");
851}
852
853void
855{
856 Length one(2.0, Unit::Meter);
857 Length two(1.0, Unit::Meter);
858
859 AssertTrue(one.IsGreater(two), "IsGreater returned false");
860}
861
862void
864{
865 Length one(2.0, Unit::Meter);
866 Length two(1.0, Unit::Meter);
867
868 AssertFalse(two.IsGreater(one), "IsGreater returned true");
869}
870
871void
873{
874 const double tolerance = 0.01;
875
876 Length one(5.1234, Unit::Meter);
877 Length two(5.1278, Unit::Meter);
878
879 AssertFalse(two.IsGreater(one, tolerance), "IsGreater returned true");
880}
881
882void
884{
885 Length l(1.0, Unit::Meter);
886
887 std::stringstream stream;
888
889 stream << l;
890
891 NS_TEST_ASSERT_MSG_EQ(stream.str(), "1 m", "unexpected output from operator<<");
892}
893
894void
896{
897 const double value = 5;
898
899 Length l;
900
901 std::stringstream stream;
902
903 stream << value << "m";
904
905 stream >> l;
906
907 NS_TEST_ASSERT_MSG_EQ(l.GetDouble(), value, "unexpected length from operator>>");
908}
909
910template <class T>
911void
913 const T& unit,
914 const std::string& expectedOutput,
915 const std::string& context)
916{
917 const std::string msg = context + ": unexpected output when serializing length";
918
919 std::ostringstream stream;
920
921 stream << std::fixed << std::setprecision(5) << l.As(unit);
922
923 NS_TEST_ASSERT_MSG_EQ(stream.str(), expectedOutput, msg);
924}
925
926void
928{
929 Length l(1.0, Unit::Meter);
930
931 TestLengthSerialization(l, Unit::Nanometer, "1000000000.00000 nm", "nanometers");
932 TestLengthSerialization(l, Unit::Micrometer, "1000000.00000 um", "micrometers");
933 TestLengthSerialization(l, Unit::Millimeter, "1000.00000 mm", "millimeters");
934 TestLengthSerialization(l, Unit::Centimeter, "100.00000 cm", "centimeters");
935 TestLengthSerialization(l, Unit::Meter, "1.00000 m", "meters");
936 TestLengthSerialization(l, Unit::Kilometer, "0.00100 km", "kilometers");
937 TestLengthSerialization(l, Unit::NauticalMile, "0.00054 nmi", "nautical_mile");
938 TestLengthSerialization(l, Unit::Inch, "39.37008 in", "inches");
939 TestLengthSerialization(l, Unit::Foot, "3.28084 ft", "feet");
940 TestLengthSerialization(l, Unit::Yard, "1.09361 yd", "yards");
941 TestLengthSerialization(l, Unit::Mile, "0.00062 mi", "miles");
942}
943
944void
946{
947 const double value = 5;
948
949 Length one(value, Unit::Meter);
950 Length two(value, Unit::Meter);
951
952 AssertTrue(one == two, "operator== returned false for equal lengths");
953}
954
955void
957{
958 const double value = 5;
959
960 Length one(value, Unit::Meter);
961 Length two(value, Unit::Kilometer);
962
963 AssertFalse(one == two, "operator== returned true for non equal lengths");
964}
965
966void
968{
969 const double value = 5;
970
971 Length one(value, Unit::Meter);
972 Length two(value, Unit::Kilometer);
973
974 AssertTrue(one != two, "operator!= returned false for non equal lengths");
975}
976
977void
979{
980 const double value = 5;
981
982 Length one(value, Unit::Meter);
983 Length two(value, Unit::Meter);
984
985 AssertFalse(one != two, "operator!= returned true for equal lengths");
986}
987
988void
990{
991 const double value = 5;
992
993 Length one(value, Unit::Meter);
994 Length two(value, Unit::Kilometer);
995
996 AssertTrue(one < two, "operator< returned false for smaller length");
997}
998
999void
1001{
1002 const double value = 5;
1003
1004 Length one(value, Unit::Meter);
1005 Length two(value, Unit::Kilometer);
1006
1007 AssertFalse(two < one, "operator< returned true for larger length");
1008}
1009
1010void
1012{
1013 const double value = 5;
1014
1015 Length one(value, Unit::Meter);
1016 Length two(value, Unit::Kilometer);
1017 Length three(one);
1018
1019 AssertTrue(one <= two, "operator<= returned false for smaller length");
1020
1021 AssertTrue(one <= three, "operator<= returned false for equal lengths");
1022}
1023
1024void
1026{
1027 const double value = 5;
1028
1029 Length one(value, Unit::Meter);
1030 Length two(value, Unit::Kilometer);
1031
1032 AssertFalse(two <= one, "operator<= returned true for larger length");
1033}
1034
1035void
1037{
1038 const double value = 5;
1039
1040 Length one(value, Unit::Meter);
1041 Length two(value, Unit::Kilometer);
1042
1043 AssertTrue(two > one, "operator> returned false for larger length");
1044}
1045
1046void
1048{
1049 const double value = 5;
1050
1051 Length one(value, Unit::Meter);
1052 Length two(value, Unit::Kilometer);
1053
1054 AssertFalse(one > two, "operator> returned true for smaller length");
1055}
1056
1057void
1059{
1060 const double value = 5;
1061
1062 Length one(value, Unit::Meter);
1063 Length two(value, Unit::Kilometer);
1064 Length three(one);
1065
1066 AssertTrue(two >= one, "operator>= returned false for larger length");
1067
1068 AssertTrue(one >= three, "operator>= returned false for equal lengths");
1069}
1070
1071void
1073{
1074 const double value = 5;
1075
1076 Length one(value, Unit::Meter);
1077 Length two(value, Unit::Kilometer);
1078
1079 AssertFalse(one >= two, "operator>= returned true for smaller length");
1080}
1081
1082void
1084{
1085 const double value = 1;
1086 const double expectedOutput = 2;
1087
1088 Length one(value, Unit::Meter);
1089 Length two(value, Unit::Meter);
1090
1091 Length result = one + two;
1092
1093 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator+ modified first operand");
1094 NS_TEST_ASSERT_MSG_EQ(two.GetDouble(), value, "operator+ modified second operand");
1095 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator+ returned incorrect value");
1096}
1097
1098void
1100{
1101 const double value = 1;
1102 const double expectedOutput = 2;
1103
1104 Length one(value, Unit::Meter);
1105
1106 Length result = one + Length::Quantity(value, Unit::Meter);
1107
1108 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator+ modified first operand");
1109 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator+ returned incorrect value");
1110}
1111
1112void
1114{
1115 const double value = 1;
1116 const double expectedOutput = 2;
1117
1118 Length one(value, Unit::Meter);
1119
1120 Length result = Length::Quantity(value, Unit::Meter) + one;
1121
1122 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator+ modified first operand");
1123 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator+ returned incorrect value");
1124}
1125
1126void
1128{
1129 const double value = 1;
1130 const double expectedOutput = 0;
1131
1132 Length one(value, Unit::Meter);
1133 Length two(value, Unit::Meter);
1134
1135 Length result = one - two;
1136
1137 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator- modified first operand");
1138 NS_TEST_ASSERT_MSG_EQ(two.GetDouble(), value, "operator- modified second operand");
1139 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator- returned incorrect value");
1140}
1141
1142void
1144{
1145 const double value = 1;
1146 const double expectedOutput = 0;
1147
1148 Length one(value, Unit::Meter);
1149
1150 Length result = one - Length::Quantity(value, Unit::Meter);
1151
1152 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator- modified first operand");
1153 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator- returned incorrect value");
1154}
1155
1156void
1158{
1159 const double value = 1;
1160 const double expectedOutput = 0;
1161
1162 Length one(value, Unit::Meter);
1163
1164 Length result = Length::Quantity(value, Unit::Meter) - one;
1165
1166 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator- modified second operand");
1167 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator- returned incorrect value");
1168}
1169
1170void
1172{
1173 const double value = 1;
1174 const double scalar = 5;
1175 const double expectedOutput = value * scalar;
1176
1177 Length one(value, Unit::Meter);
1178 Length result = one * scalar;
1179
1180 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator* modified first operand");
1181 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator* returned incorrect value");
1182}
1183
1184void
1186{
1187 const double value = 1;
1188 const double scalar = 5;
1189 const double expectedOutput = value * scalar;
1190
1191 Length one(value, Unit::Meter);
1192 Length result = scalar * one;
1193
1194 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator* modified second operand");
1195 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator* returned incorrect value");
1196}
1197
1198void
1200{
1201 const double value = 10;
1202 const double scalar = 5;
1203 const double expectedOutput = value / scalar;
1204
1205 Length one(value, Unit::Meter);
1206 Length result = one / scalar;
1207
1208 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), value, "operator/ modified first operand");
1209 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedOutput, "operator/ returned incorrect value");
1210}
1211
1212void
1214{
1215 const double valueOne = 100;
1216 const double valueTwo = 2;
1217 const double expectedOutput = valueOne / valueTwo;
1218
1219 Length one(valueOne, Unit::Meter);
1220 Length two(valueTwo, Unit::Meter);
1221
1222 double result = one / two;
1223
1224 NS_TEST_ASSERT_MSG_EQ(one.GetDouble(), valueOne, "operator/ modified first operand");
1225 NS_TEST_ASSERT_MSG_EQ(two.GetDouble(), valueTwo, "operator/ modified second operand");
1226 NS_TEST_ASSERT_MSG_EQ(result, expectedOutput, "operator/ returned incorrect value");
1227}
1228
1229void
1231{
1232 const double value = 1;
1233
1234 Length one(value, Unit::Meter);
1235 Length two;
1236
1237 double result = one / two;
1238
1239 AssertTrue(std::isnan(result), "operator/ did not return NaN when dividing by zero");
1240}
1241
1242void
1244{
1245 const double topValue = 100;
1246 const double bottomValue = 20;
1247 const int64_t expectedOutput = 5;
1248
1249 Length numerator(topValue, Unit::Meter);
1250 Length denominator(bottomValue, Unit::Meter);
1251
1252 auto result = Div(numerator, denominator);
1253
1254 NS_TEST_ASSERT_MSG_EQ(result, expectedOutput, "Div() returned an incorrect value");
1255}
1256
1257void
1259{
1260 const double topValue = 100;
1261 const double bottomValue = 20;
1262 const int64_t expectedOutput = 5;
1263 const int64_t expectedRemainder = 0;
1264
1265 Length numerator(topValue, Unit::Meter);
1266 Length denominator(bottomValue, Unit::Meter);
1268
1269 auto result = Div(numerator, denominator, &remainder);
1270
1271 NS_TEST_ASSERT_MSG_EQ(result, expectedOutput, "Div() returned an incorrect value");
1272 NS_TEST_ASSERT_MSG_EQ(remainder.GetDouble(),
1273 expectedRemainder,
1274 "Div() returned an incorrect remainder");
1275}
1276
1277void
1279{
1280 const double topValue = 110;
1281 const double bottomValue = 20;
1282 const int64_t expectedOutput = 5;
1283 const int64_t expectedRemainder = 10;
1284
1285 Length numerator(topValue, Unit::Meter);
1286 Length denominator(bottomValue, Unit::Meter);
1288
1289 auto result = Div(numerator, denominator, &remainder);
1290
1291 NS_TEST_ASSERT_MSG_EQ(result, expectedOutput, "Div() returned an incorrect value");
1292 NS_TEST_ASSERT_MSG_EQ(remainder.GetDouble(),
1293 expectedRemainder,
1294 "Div() returned an incorrect remainder");
1295}
1296
1297void
1299{
1300 Length numerator(10, Unit::Meter);
1301 Length denominator(2, Unit::Meter);
1302
1303 auto result = Mod(numerator, denominator);
1304
1305 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), 0, "Mod() returned a non zero value");
1306}
1307
1308void
1310{
1311 Length numerator(14, Unit::Meter);
1312 Length denominator(3, Unit::Meter);
1313 const double expectedValue = 2;
1314
1315 auto result = Mod(numerator, denominator);
1316
1317 NS_TEST_ASSERT_MSG_EQ(result.GetDouble(), expectedValue, "Mod() returned the wrong value");
1318}
1319
1320void
1322{
1324
1326
1328
1330
1332
1334
1346
1347#ifdef HAVE_BOOST_UNITS
1348 TestConstructLengthFromBoostUnits();
1349#endif
1350
1352
1355
1359
1374
1376
1378
1391
1403
1407
1410}
1411
1412/**
1413 * @ingroup length-tests
1414 *
1415 * Test case for LengthValue attribute
1416 */
1418{
1419 public:
1420 /**
1421 * Default Constructor
1422 */
1424 : TestCase("length-value-tests")
1425 {
1426 }
1427
1428 /**
1429 * Destructor
1430 */
1432 {
1433 }
1434
1435 private:
1436 /// Class with Length attribute
1437 class TestObject : public Object
1438 {
1439 public:
1440 /**
1441 * @brief Get the type ID.
1442 * @return The object TypeId.
1443 */
1444 static TypeId GetTypeId();
1445
1447 : m_length()
1448 {
1449 }
1450
1451 ~TestObject() override
1452 {
1453 }
1454
1455 private:
1456 Length m_length; //!< Length object
1457 };
1458
1459 private:
1460 /**
1461 * Test that a LengthValue can be constructed from a Length instance
1462 */
1464
1465 /**
1466 * Test that a LengthValue can be serialized to a string
1467 */
1469
1470 /**
1471 * Test that a LengthValue can be deserialized from a string
1472 */
1474
1475 /**
1476 * Test that a LengthValue works as an attribute
1477 */
1478 void TestObjectAttribute();
1479
1480 /**
1481 * Test that a StringValue is converted to LengthValue
1482 */
1484
1485 // Inherited function
1486 void DoRun() override;
1487};
1488
1489TypeId
1491{
1492 static TypeId tid = TypeId("LengthValueTestCase::TestObject")
1493 .SetParent<Object>()
1494 .SetGroupName("Test")
1495 .AddConstructor<TestObject>()
1496 .AddAttribute("Length",
1497 "Length value",
1498 LengthValue(),
1501
1502 return tid;
1503}
1504
1505void
1507{
1508 Length l = KiloMeters(2);
1509 LengthValue value(l);
1510
1511 NS_TEST_ASSERT_MSG_EQ(value.Get(), l, "Length attribute has wrong value");
1512}
1513
1514void
1516{
1518
1519 Length l = KiloMeters(2);
1520 LengthValue value(l);
1521
1522 std::string output = value.SerializeToString(checker);
1523
1524 NS_TEST_ASSERT_MSG_EQ(output, "2000 m", "Length attribute serialization has wrong output");
1525}
1526
1527void
1529{
1531
1532 Length l = KiloMeters(2);
1533 std::ostringstream stream;
1534 stream << l;
1535
1536 LengthValue value;
1537 bool result = value.DeserializeFromString(stream.str(), checker);
1538
1539 NS_TEST_ASSERT_MSG_EQ(result, true, "Length attribute deserialization failed");
1540 NS_TEST_ASSERT_MSG_EQ(value.Get(), l, "Length attribute has wrong value after deserialization");
1541}
1542
1543void
1545{
1546 Length expected(5, Unit::Kilometer);
1548
1549 obj->SetAttribute("Length", LengthValue(expected));
1550
1551 LengthValue val;
1552 obj->GetAttribute("Length", val);
1553
1554 NS_TEST_ASSERT_MSG_EQ(val.Get(), expected, "Length attribute does not have expected value");
1555}
1556
1557void
1559{
1560 Length expected(5, Unit::Kilometer);
1562
1563 std::stringstream stream;
1564 stream << expected.As(Unit::Kilometer);
1565
1566 obj->SetAttribute("Length", StringValue(stream.str()));
1567
1568 LengthValue val;
1569 obj->GetAttribute("Length", val);
1570
1571 NS_TEST_ASSERT_MSG_EQ(val.Get(), expected, "Length attribute does not have expected value");
1572}
1573
1574void
1583
1584/**
1585 * @ingroup length-tests
1586 * The Test Suite that runs the test case
1587 */
1589{
1590 public:
1591 /**
1592 * Default Constructor
1593 */
1595};
1596
1603
1604/**
1605 * LengthTestSuite instance
1606 */
cairo_uint64_t remainder
return result
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.
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.
Definition length.h:261
double Value() const
The value of the quantity.
Definition length.h:309
Represents a length in meters.
Definition length.h:233
double GetDouble() const
Current length value.
Definition length.cc:366
bool IsGreater(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is less in value than this instance.
Definition length.cc:342
bool IsEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal in value to this instance.
Definition length.cc:303
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition length.cc:372
static std::optional< Length > TryParse(double value, const std::string &unit)
Attempt to construct a Length object from a value and a unit string.
Definition length.cc:229
Unit
Units of length in various measurement systems that are supported by the Length class.
Definition length.h:240
bool IsLess(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater in value than this instance.
Definition length.cc:326
bool IsNotEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is not equal in value to this instance.
Definition length.cc:318
AttributeValue implementation for Length.
Definition length.h:614
Length Get() const
Definition length.cc:226
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Hold variables of type string.
Definition string.h:45
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
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.
Definition test.cc:494
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
Ptr< const AttributeChecker > MakeLengthChecker()
Definition length.cc:226
Ptr< const AttributeAccessor > MakeLengthAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition length.h:614
Length KiloMeters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:802
Length MilliMeters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:784
Length NauticalMiles(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:808
Length Yards(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:826
Length Feet(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:820
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Definition length.cc:486
Length MicroMeters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:778
Length Miles(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:832
Length Meters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:796
Length CentiMeters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:790
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
Calculate how many times numerator can be split into denominator sized pieces.
Definition length.cc:467
Length NanoMeters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:772
Length Inches(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:814
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#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:133
#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...
Definition test.h:326
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