core: Move data rate tuple testcase to network module

This commit is contained in:
Stefano Avallone
2025-02-11 10:18:19 +01:00
parent 18139260d9
commit b2b3300a3c
2 changed files with 83 additions and 28 deletions

View File

@@ -7,7 +7,6 @@
*/
#include "ns3/attribute-container.h"
#include "ns3/data-rate.h"
#include "ns3/double.h"
#include "ns3/integer.h"
#include "ns3/log.h"
@@ -84,9 +83,6 @@ class AttributeContainerObject : public Object
*/
std::vector<int> GetIntVec() const;
std::tuple<DataRate, DataRate, std::string> m_tupleRatesString; //!< tuple of two data rates
//!< and a string
private:
std::list<double> m_doublelist; //!< List of doubles.
std::vector<int> m_intvec; //!< Vector of ints.
@@ -154,17 +150,7 @@ AttributeContainerObject::GetTypeId()
MakeAttributeContainerChecker<IntVecMapValue, ';'>(
MakePairChecker<IntegerValue, AttributeContainerValue<IntegerValue>>(
MakeIntegerChecker<int>(),
MakeAttributeContainerChecker<IntegerValue>(MakeIntegerChecker<int>()))))
.AddAttribute(
"TupleRatesString",
"An example of tuple of two data rates (comprising value and unit, possibly "
"separated by a white space) and a string (possibly containing a white space).",
StringValue("{1 Mb/s, 10kb/s, test string}"),
MakeTupleAccessor<DataRateValue, DataRateValue, StringValue>(
&AttributeContainerObject::m_tupleRatesString),
MakeTupleChecker<DataRateValue, DataRateValue, StringValue>(MakeDataRateChecker(),
MakeDataRateChecker(),
MakeStringChecker()));
MakeAttributeContainerChecker<IntegerValue>(MakeIntegerChecker<int>()))));
return tid;
}
@@ -556,19 +542,6 @@ AttributeContainerSetGetTestCase::DoRun()
++iter;
}
}
{
// check that the tuple of two data rates and a string was correctly initialized
NS_TEST_EXPECT_MSG_EQ(std::get<0>(obj->m_tupleRatesString),
DataRate(1e6),
"Unexpected value for the first data rate");
NS_TEST_EXPECT_MSG_EQ(std::get<1>(obj->m_tupleRatesString),
DataRate(1e4),
"Unexpected value for the second data rate");
NS_TEST_EXPECT_MSG_EQ(std::get<2>(obj->m_tupleRatesString),
"test string",
"Unexpected value for the string");
}
}
/**

View File

@@ -10,6 +10,7 @@
#include "ns3/log.h"
#include "ns3/simulator.h"
#include "ns3/test.h"
#include "ns3/tuple.h"
using namespace ns3;
@@ -258,6 +259,86 @@ DataRateTestCase2::DoRun()
MultiplicationDoubleTest("6Gb/s", 1.0 / 7.0, "857142857.14b/s");
}
/**
* @ingroup network-test
* @ingroup tests
*
* Object with an attribute that is a tuple of data rates and a string.
*/
class DataRateTupleObject : public Object
{
public:
~DataRateTupleObject() override = default;
/**
* @brief Get the type ID.
* @return The object TypeId.
*/
static TypeId GetTypeId();
std::tuple<DataRate, DataRate, std::string> m_tupleRatesString; //!< tuple of two data rates
//!< and a string
};
TypeId
DataRateTupleObject::GetTypeId()
{
static TypeId tid =
TypeId("ns3::DataRateTupleObject")
.SetParent<Object>()
.SetGroupName("Test")
.AddConstructor<DataRateTupleObject>()
.AddAttribute(
"TupleRatesString",
"An example of tuple of two data rates (comprising value and unit, possibly "
"separated by a white space) and a string (possibly containing a white space).",
StringValue("{1 Mb/s, 10kb/s, test string}"),
MakeTupleAccessor<DataRateValue, DataRateValue, StringValue>(
&DataRateTupleObject::m_tupleRatesString),
MakeTupleChecker<DataRateValue, DataRateValue, StringValue>(MakeDataRateChecker(),
MakeDataRateChecker(),
MakeStringChecker()));
return tid;
}
/**
* @ingroup network-test
* @ingroup tests
*
* Attribute set and get TestCase.
*/
class DataRateTupleSetGetTestCase : public TestCase
{
public:
DataRateTupleSetGetTestCase();
~DataRateTupleSetGetTestCase() override = default;
private:
void DoRun() override;
};
DataRateTupleSetGetTestCase::DataRateTupleSetGetTestCase()
: TestCase("test attribute set and get")
{
}
void
DataRateTupleSetGetTestCase::DoRun()
{
Ptr<DataRateTupleObject> obj = CreateObject<DataRateTupleObject>();
// check that the tuple of two data rates and a string was correctly initialized
NS_TEST_EXPECT_MSG_EQ(std::get<0>(obj->m_tupleRatesString),
DataRate(1e6),
"Unexpected value for the first data rate");
NS_TEST_EXPECT_MSG_EQ(std::get<1>(obj->m_tupleRatesString),
DataRate(1e4),
"Unexpected value for the second data rate");
NS_TEST_EXPECT_MSG_EQ(std::get<2>(obj->m_tupleRatesString),
"test string",
"Unexpected value for the string");
}
/**
* @ingroup network-test
* @ingroup tests
@@ -275,6 +356,7 @@ DataRateTestSuite::DataRateTestSuite()
{
AddTestCase(new DataRateTestCase1(), TestCase::Duration::QUICK);
AddTestCase(new DataRateTestCase2(), TestCase::Duration::QUICK);
AddTestCase(new DataRateTupleSetGetTestCase(), TestCase::Duration::QUICK);
}
static DataRateTestSuite sDataRateTestSuite; //!< Static variable for test initialization