chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes

Hi,

  is it similar case for sharet_ptr? I have a method
 

std::shared_ptr<char[]> MBRegister::ReadData(const int regAddr, const int regCount)
{
    std::lock_guard<std::mutex> lg(lock);
    char* res = nullptr;
    if (regAddr >= 0 && regAddr + (regCount-1) <= registerCount) 
    {
        res = new char[regCount * 2];        

        char* startRead = buffer + (regAddr * 2);
        errno_t error = memcpy_s(res, regCount * 2, startRead, regCount * 2);
        if (error != 0) 
        {
            delete[] res;
            res = nullptr;
        }        
    }

    return std::shared_ptr<char[]>(res);
}


I'm not sure how mock this method by using PRIVATE_WHEN_CALLED, or other construct.

Thank you

Daril

asked by Daril (600 points)

Mocking shared_ptr objects or functions that return shared_ptr objects is not supported yet.

1 Answer

0 votes

Hi Daril,

Although there is a difference between unique_ptr and shared_ptr.
The principal of mocking should be the same.

Have you tried to mock and failed? If so, please share what you have tried.

Cheers,
Alon Sapozhnikov.
Support Specialist.

answered by Alon_TypeMock (10.6k points)

Hi Alon,

  thank you for quick response.
I tried several cases:

  1. std::shared_ptr<char[]> lrmcsName(new char[5]{"test"});
    PRIVATE_WHEN_CALLED(_,ReadData(_,_)).Return(&lrmcsName);
  2. WHEN_CALLED(mbRegister->ReadData(0, 8)).Return(&lrmcsName);
  3. WHEN_CALLED(mbRegister->ReadData(0, 8)).Return(BY_VAL(lrmcsName));
It's always crash on line with mock (WHEN_CALLED).

Thank you 
Daril

Hi Daril,

I understand.
Is it possible for you to provide a sample project which reprodues this issue?
And also the logs of a test run that fails in your machine?
You can enable logs in the Cofiguration Tool of Isolator++.

I'm sending you a private email so you could send the files in a more convenient platform.

Cheers,
Alon Sapozhnikov. 

...