Having issue with comparator on a struct using mocks

514 views
Skip to first unread message

Warren Hein

unread,
Aug 25, 2016, 6:18:54 PM8/25/16
to cpputest
I have a structure that I'm trying to add a comparator to in my unit tests.  It's a basic structure with a few basic elements, that does not seem to be applied when I attempt to use it in the mocks.  I've created a comparator and copier and have written tests to validate them:

TEST(bcm82209_si_parse, comparisonOfTxSetParametersOnCallShouldFail)Copying void *s - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_copier_passes)Copying plp_bcm_tx_t *s - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_comparitor_fails) - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_comparitor_is_equal_fails_if_not_equal_void_handler)isEqual: comparing void* objects - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_comparitor_is_equal_fails_if_not_equal)isEqual: comparing plp_bcm_tx_t* objects - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_comparitor_is_equal_passes_if_equal_void_handler)isEqual: comparing void* objects - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_comparitor_is_equal_passes_if_equal)isEqual: comparing plp_bcm_tx_t* objects - 0 ms
TEST(bcm82209_si_parse, check_plp_bcm_tx_t_comparitor_passes) - 0 ms
TEST(bcm82209_si_parse, parseUnavailableFileReturnsError) - 0 ms


However, when I try to make use of the comparator in my mock functionality, it is not being applied:

TEST_GROUP(bcm82209_si_parse)
{
    int retValue;
    BcmTxSettingsComparator comparator;
    BcmTxSettingsCopier copier;

    void setup()
    {
        mock().enable();
        mock().strictOrder();
        mock().installComparator("plp_bcm_tx_t", comparator);
        mock().installCopier("plp_bcm_tx_t", copier);
    }

    void teardown()
    {
    mock().checkExpectations();
    mock().removeAllComparatorsAndCopiers();
    mock().clear();
    }

};

TEST(bcm82209_si_parse, comparisonOfTxSetParametersOnCallShouldFail)
{
    int rc = 0;
    int p_ctxt_compare = 5;
    int p_ctxt = 5;
    plp_bcm_tx_t tx_to_compare = {4,5,6,0,0,7};
    plp_bcm_tx_t tx_to_program = {0,0,0,0,0,0};
    uint32 phy_id = 2, if_side=0, lane=3;

    mock().expectOneCall("bcm_pm_if_tx_set")
            .withOutputParameterReturning("p_ctxt", &p_ctxt_compare, sizeof(int))
.withUnsignedIntParameter("phy_id", phy_id)
.withUnsignedIntParameter("if_side", if_side)
.withUnsignedIntParameter("lane", lane)
.withOutputParameterOfTypeReturning("plp_bcm_tx_t", "tx", &tx_to_compare)
.withOutputParameterReturning("tx", &tx_to_compare, sizeof(plp_bcm_tx_t))
.andReturnValue(0);
    rc = mock().actualCall("bcm_pm_if_tx_set")
            .withOutputParameter("p_ctxt", &p_ctxt)
    .withUnsignedIntParameter("phy_id", 2)
.withUnsignedIntParameter("if_side", 0)
.withUnsignedIntParameter("lane", 3)
.withOutputParameterOfType("plp_bcm_tx_t", "tx", &tx_to_program)
.returnValue().getIntValue();
    CHECK_EQUAL(0,rc);
}

Here are the classes for the comparator and copier:

class BcmTxSettingsComparator : public MockNamedValueComparator
{
public:
virtual bool isEqual(const plp_bcm_tx_t* obj1, const plp_bcm_tx_t* obj2)
{
bool match = FALSE;
         printf("isEqual: comparing plp_bcm_tx_t* objects\n");
if ((obj1->pre == obj2->pre) &&
(obj1->main  == obj2->main) &&
(obj1->post  == obj2->post) &&
(obj1->post2 == obj2->post2) &&
(obj1->post3 == obj2->post3) &&
(obj1->amp   == obj2->amp)) match = TRUE;
return match;
}

virtual bool isEqual(const void* object1, const void* object2)
{
bool match = FALSE;
         printf("isEqual: comparing void* objects\n");
const plp_bcm_tx_t* obj1 = (const plp_bcm_tx_t*) object1;
const plp_bcm_tx_t* obj2 = (const plp_bcm_tx_t*) object2;
if ((obj1->pre == obj2->pre) &&
(obj1->main  == obj2->main) &&
(obj1->post  == obj2->post) &&
(obj1->post2 == obj2->post2) &&
(obj1->post3 == obj2->post3) &&
(obj1->amp   == obj2->amp)) match = TRUE;
return match;
}

SimpleString valueToString(const void* object)
{
const plp_bcm_tx_t* obj = (const plp_bcm_tx_t*) object;
return StringFromFormat("pre: 0x%X, main 0x%X, post 0x%X, post2 0x%X, post3: 0x%X, amp: 0x%X",
obj->pre, obj->main, obj->post, obj->post2, obj->post3, obj->amp);
}

SimpleString stringFrom(const plp_bcm_tx_t& obj)
{
return StringFromFormat("pre: 0x%X, main 0x%X, post 0x%X, post2 0x%X, post3: 0x%X, amp: 0x%X",
obj.pre, obj.main, obj.post, obj.post2, obj.post3, obj.amp);
}
};


class BcmTxSettingsCopier : public MockNamedValueCopier
{
public:
virtual void copy(void * out, const void* in)
{
plp_bcm_tx_t *tx_out = (plp_bcm_tx_t *)out;
const plp_bcm_tx_t *tx_in = (plp_bcm_tx_t *)in;

printf("Copying void *s\n");
tx_out->pre   = tx_in->pre;
tx_out->main  = tx_in->main;
tx_out->post  = tx_in->post;
tx_out->post2 = tx_in->post2;
tx_out->post3 = tx_in->post3;
tx_out->amp   = tx_in->amp;
}
virtual void copy(plp_bcm_tx_t *tx_out, const plp_bcm_tx_t* tx_in)
{
printf("Copying plp_bcm_tx_t *s\n");
tx_out->pre   = tx_in->pre;
tx_out->main  = tx_in->main;
tx_out->post  = tx_in->post;
tx_out->post2 = tx_in->post2;
tx_out->post3 = tx_in->post3;
tx_out->amp   = tx_in->amp;
}
virtual void copy(plp_bcm_tx_t tx_out, const plp_bcm_tx_t tx_in)
{
printf("Copying plp_bcm_tx_t (non-pointer)\n");
tx_out.pre    = tx_in.pre;
tx_out.main   = tx_in.main;
tx_out.post2  = tx_in.post2;
tx_out.post3  = tx_in.post3;
tx_out.amp    = tx_in.amp;
}
};



I've confirmed that the comparator is not being used during the mock functionality by forcing the return value to "false" in both isEqual functions, and the test case comparisonOfTxSetParametersOnCallShouldFail will still pass.  Could someone please help me identify what is wrong here?

Thanks.

Warren 

Ryan Hartlage

unread,
Aug 26, 2016, 9:18:27 PM8/26/16
to cpputest
Hey Warren,

Are you trying to use output parameters or are you trying to check
that the correct parameter is provided? Your current code uses
with_Output_ParameterOfType() and I'm pretty sure you actually want to
use withParameterOfType().

Ryan
> --
> You received this message because you are subscribed to the Google Groups
> "cpputest" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to cpputest+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Warren Hein

unread,
Aug 29, 2016, 1:56:55 PM8/29/16
to cpputest
Thanks.

I'm trying to ensure that the structure is passed to bcm_pm_if_tx_set() with correct values.  The end intent is to pass by reference.  Using withParameterOfType in both the expected and actual call fixed the issue.  Using withOutputParameterOfType() would result in the expected values of tx_to_compare being copied back into tx_to_program.

For future documentation the solution (shortened) is:

TEST(bcm82209_si_parse, comparisonOfTxSetParametersOnCallShouldPassShortVersion)

{

    int rc = 0;

    plp_bcm_tx_t tx_to_compare = {0,0,0,0,0,0};

    plp_bcm_tx_t tx_to_program = {0,0,0,0,0,0};


    mock().expectOneCall("bcm_pm_if_tx_set").withParameterOfType("plp_bcm_tx_t", "tx", &tx_to_compare).andReturnValue(0);

    rc = mock().actualCall("bcm_pm_if_tx_set").withParameterOfType("plp_bcm_tx_t", "tx", &tx_to_program).returnValue().getIntValue();

    CHECK_EQUAL(0,rc);

}


Am I missing something, but it doesn't appear that there is a default comparator for the values stored in an int *?  Do I actually need to treat these in the same manner as the above structure and create a comparator for an int *?

Warren

Ryan Hartlage

unread,
Aug 30, 2016, 4:11:14 PM8/30/16
to cpputest
Warren,

Unfortunately, I don't believe there's a default comparator. However,
if you know that you're comparing ints, you can dereference the int in
the mock().actualCall(...).withParameter("foo", *someInt). There's no
rule that your mocked calls need to match the actual signature
exactly.

Ryan
Reply all
Reply to author
Forward
0 new messages