[timestamp] add IsValid() and comparison operator overloads (#10325)

This commit enhances the handling of `Timestamp`.

Methods `SetToInvalid()` and `IsValid()` are added to the `Timestamp`
class. The value where all bytes are set to `0xff` is used to
represent an invalid `Timestamp`. This corresponds to max seconds,
max ticks with the authoritative (`U`) flag set.

This helps simplify `DatasetManager`, where we need to track local and
network Active/Pending timestamps, which may not be present. These
are now represented by setting the `Timestamp` to the invalid value
(replacing the earlier model where a `Timestamp` pointer was used
with `nullptr` representing an invalid timestamp).

This allows us to simplify the `Timestamp::Compare()` method, now
getting two `Timestamp &` instead of `Timestamp *`. With this, we can
also define comparison operator overloads to compare two `Timestamp`
objects, which helps make the code simpler and more readable.
This commit is contained in:
Abtin Keshavarzian
2024-06-04 13:48:31 -07:00
committed by GitHub
parent 5bb3667a49
commit fcd3c5c4b7
10 changed files with 243 additions and 182 deletions
+68 -22
View File
@@ -115,40 +115,86 @@ void TestSteeringData(void)
void TestTimestamp(void)
{
MeshCoP::Timestamp t1;
MeshCoP::Timestamp t2;
MeshCoP::Timestamp t1;
MeshCoP::Timestamp t2;
MeshCoP::Timestamp::Info info;
t1.Clear();
t2.Clear();
VerifyOrQuit(t1.GetSeconds() == 0);
VerifyOrQuit(t1.GetTicks() == 0);
VerifyOrQuit(!t1.GetAuthoritative());
VerifyOrQuit(t1.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t1) == 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) == 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, nullptr) > 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(nullptr, &t2) < 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(nullptr, nullptr) == 0);
t1.ConvertTo(info);
VerifyOrQuit(info.mSeconds == 0);
VerifyOrQuit(info.mTicks == 0);
VerifyOrQuit(!info.mAuthoritative);
t1.SetTicks(10);
VerifyOrQuit(t1.GetTicks() == 10);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) > 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t2, &t1) < 0);
t2.SetToInvalid();
VerifyOrQuit(!t2.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t2, t2) == 0);
t2.SetTicks(10);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) == 0);
t2.ConvertTo(info);
VerifyOrQuit(info.mSeconds == 0xffffffffffff);
VerifyOrQuit(info.mTicks == 0x7fff);
VerifyOrQuit(info.mAuthoritative);
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) > 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(t2, t1) < 0);
t2 = t1;
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) == 0);
VerifyOrQuit(t2.IsValid());
VerifyOrQuit(t1.IsValid());
t1.SetSeconds(0x12345678abcd);
VerifyOrQuit(t1.GetSeconds() == 0x12345678abcd);
VerifyOrQuit(t1.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) > 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(t2, t1) < 0);
t2.SetSeconds(0x12345678abcd);
VerifyOrQuit(t1.GetSeconds() == 0x12345678abcd);
VerifyOrQuit(t2.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) == 0);
t1.SetAuthoritative(true);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) > 0);
VerifyOrQuit(t1.GetAuthoritative());
VerifyOrQuit(t1.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) > 0);
t1.SetSeconds(1);
VerifyOrQuit(t1.GetSeconds() == 1);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) > 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t2, &t1) < 0);
t1.SetAuthoritative(false);
VerifyOrQuit(!t1.GetAuthoritative());
VerifyOrQuit(t1.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) == 0);
t2.SetSeconds(1);
t2.SetAuthoritative(true);
VerifyOrQuit(MeshCoP::Timestamp::Compare(&t1, &t2) == 0);
t1.SetTicks(0x7fff);
VerifyOrQuit(t1.GetTicks() == 0x7fff);
VerifyOrQuit(!t1.GetAuthoritative());
VerifyOrQuit(t1.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) > 0);
VerifyOrQuit(MeshCoP::Timestamp::Compare(t2, t1) < 0);
t2.SetTicks(0x7fff);
VerifyOrQuit(t2.GetTicks() == 0x7fff);
VerifyOrQuit(!t2.GetAuthoritative());
VerifyOrQuit(t2.IsValid());
VerifyOrQuit(MeshCoP::Timestamp::Compare(t1, t2) == 0);
t2.ConvertTo(info);
VerifyOrQuit(info.mSeconds == 0x12345678abcd);
VerifyOrQuit(info.mTicks == 0x7fff);
VerifyOrQuit(!info.mAuthoritative);
t1.SetToOrphanAnnounce();
VerifyOrQuit(t1.IsValid());
VerifyOrQuit(t1.IsOrphanAnnounce());
t1.ConvertTo(info);
VerifyOrQuit(info.mSeconds == 0);
VerifyOrQuit(info.mTicks == 0);
VerifyOrQuit(info.mAuthoritative);
printf("TestTimestamp() passed\n");
}