Beispiel #1
0
// Checks whether a `Child` matches the `TestChild` struct.
static bool ChildMatches(const Child &aChild, const TestChild &aTestChild)
{
    return (aChild.GetState() == aTestChild.mState) && (aChild.GetRloc16() == aTestChild.mRloc16) &&
           (aChild.GetExtAddress() == static_cast<const Mac::ExtAddress &>(aTestChild.mExtAddress));
}
Beispiel #2
0
// Verifies that `ChildTable` contains a given list of `TestChild` entries.
void VerifyChildTableContent(ChildTable &aTable, uint8_t aChildListLength, const TestChild *aChildList)
{
    printf("Test ChildTable with %d entries", aChildListLength);

    for (uint8_t k = 0; k < OT_ARRAY_LENGTH(kAllFilters); k++)
    {
        ChildTable::StateFilter filter = kAllFilters[k];

        // Verify that we can find all children from given list by rloc or extended address.

        for (uint8_t listIndex = 0; listIndex < aChildListLength; listIndex++)
        {
            Child *      child;
            Mac::Address address;

            if (!StateMatchesFilter(aChildList[listIndex].mState, filter))
            {
                continue;
            }

            child = aTable.FindChild(aChildList[listIndex].mRloc16, filter);
            VerifyOrQuit(child != NULL, "FindChild(rloc) failed");
            VerifyOrQuit(ChildMatches(*child, aChildList[listIndex]), "FindChild(rloc) returned incorrect child");

            child = aTable.FindChild(static_cast<const Mac::ExtAddress &>(aChildList[listIndex].mExtAddress), filter);
            VerifyOrQuit(child != NULL, "FindChild(ExtAddress) failed");
            VerifyOrQuit(ChildMatches(*child, aChildList[listIndex]), "FindChild(ExtAddress) returned incorrect child");

            address.SetShort(aChildList[listIndex].mRloc16);
            child = aTable.FindChild(address, filter);
            VerifyOrQuit(child != NULL, "FindChild(address) failed");
            VerifyOrQuit(ChildMatches(*child, aChildList[listIndex]), "FindChild(address) returned incorrect child");

            address.SetExtended(static_cast<const Mac::ExtAddress &>(aChildList[listIndex].mExtAddress));
            child = aTable.FindChild(address, filter);
            VerifyOrQuit(child != NULL, "FindChild(address) failed");
            VerifyOrQuit(ChildMatches(*child, aChildList[listIndex]), "FindChild(address) returned incorrect child");
        }

        // Verify `ChildTable::Iterator` behavior when starting from different child entries.

        for (uint8_t listIndex = 0; listIndex <= aChildListLength; listIndex++)
        {
            Child *startingChild = NULL;

            if (listIndex < aChildListLength)
            {
                startingChild = aTable.FindChild(aChildList[listIndex].mRloc16, ChildTable::kInStateAnyExceptInvalid);
                VerifyOrQuit(startingChild != NULL, "FindChild() failed");
            }

            // Test an iterator starting from `startingChild`.

            {
                ChildTable::Iterator iter(*sInstance, filter, startingChild);
                bool                 childObserved[kMaxChildren];
                uint8_t              numChildren = 0;

                memset(childObserved, 0, sizeof(childObserved));

                // Check if the first entry matches the `startingChild`

                if ((startingChild != NULL) && StateMatchesFilter(startingChild->GetState(), filter))
                {
                    VerifyOrQuit(!iter.IsDone(), "iterator IsDone() failed");
                    VerifyOrQuit(iter.GetChild() != NULL, "iterator GetChild() failed");
                    VerifyOrQuit(iter.GetChild() == startingChild,
                                 "Iterator failed to start from the given child entry");

                    iter++;
                    iter.Reset();
                    VerifyOrQuit(iter.GetChild() == startingChild, "iterator Reset() failed");
                }

                // Use the iterator and verify that each returned `Child` entry is in the expected list.

                for (; !iter.IsDone(); iter++)
                {
                    Child * child   = iter.GetChild();
                    bool    didFind = false;
                    uint8_t childIndex;

                    VerifyOrQuit(child != NULL, "iter.GetChild() failed");

                    childIndex = aTable.GetChildIndex(*child);
                    VerifyOrQuit(childIndex < aTable.GetMaxChildrenAllowed(), "Child Index is out of bound");
                    VerifyOrQuit(aTable.GetChildAtIndex(childIndex) == child, "GetChildAtIndex() failed");

                    for (uint8_t index = 0; index < aChildListLength; index++)
                    {
                        if (ChildMatches(*iter.GetChild(), aChildList[index]))
                        {
                            childObserved[index] = true;
                            numChildren++;
                            didFind = true;
                            break;
                        }
                    }

                    VerifyOrQuit(didFind, "ChildTable::Iterator returned an entry not in the expected list");
                }

                // Verify that when iterator is done, it points to `NULL`.

                VerifyOrQuit(iter.GetChild() == NULL, "iterator GetChild() failed");

                iter++;
                VerifyOrQuit(iter.IsDone(), "iterator Advance() (after iterator is done) failed");
                VerifyOrQuit(iter.GetChild() == NULL, "iterator GetChild() failed");

                // Verify that the number of children matches the number of entries we get from iterator.

                VerifyOrQuit(aTable.GetNumChildren(filter) == numChildren, "GetNumChildren() failed");
                VerifyOrQuit(aTable.HasChildren(filter) == (numChildren != 0), "HasChildren() failed");

                // Verify that there is no missing or extra entry between the expected list
                // and what was observed/returned by the iterator.

                for (uint8_t index = 0; index < aChildListLength; index++)
                {
                    if (StateMatchesFilter(aChildList[index].mState, filter))
                    {
                        VerifyOrQuit(childObserved[index], "iterator failed to return an expected entry");
                    }
                    else
                    {
                        VerifyOrQuit(!childObserved[index], "iterator returned an extra unexpected entry");
                    }
                }
            }
        }
    }

    printf(" -- PASS\n");
}