cpputest icon indicating copy to clipboard operation
cpputest copied to clipboard

size_t is not long

Open CanastraRF opened this issue 3 years ago • 2 comments

In SimpleStringTest.cpp in TEST(SimpleString, findNormal)

is the following code:

LONG_EQUAL(0, str.find('H'));
LONG_EQUAL(1, str.find('e'));
LONG_EQUAL(SimpleString::npos, str.find('!'));

But the return from SimpleString::find is size_t.

size_t is under Visual Studio

  • 32-Bit build: unsigned 32-Bit
  • 64-Bit build: unsigned 64-Bit

So I get a warning that under 64-bit Build SimpleString::npos is truncated to 32-Bit

I add SIZET_EQUAL(), SIZET_EQUAL_TEXT(), SIZET_EQUAL_LOCATION() with the correct size

UtestMacros.h

#define SIZET_EQUAL(expected, actual)\
  SIZET_EQUAL_LOCATION(expected, actual, NULLPTR, __FILE__, __LINE__)

#define SIZET_EQUAL_TEXT(expected, actual, text)\
  SIZET_EQUAL_LOCATION(expected, actual, text, __FILE__, __LINE__)

#define SIZET_EQUAL_LOCATION(expected, actual, text, file, line)\
        do { UtestShell::getCurrent()->assertSizetEqual((size_t)expected, (size_t)actual, text, file, line); } while(0)

TestHarness_c.cpp

void CHECK_EQUAL_C_SIZET_LOCATION(size_t expected, size_t actual, const char* text, const char* fileName, size_t lineNumber)
{
    UtestShell::getCurrent( )->assertSizetEqual(expected, actual, text, fileName, lineNumber, TestTerminatorWithoutExceptions( ));
}

Utest.cpp

void UtestShell::assertSizetEqual(size_t expected, size_t actual, const char* text, const char* fileName, size_t lineNumber, const TestTerminator& testTerminator)
{
    getTestResult( )->countCheck( );
    if ( expected != actual )
        failWith(SizetEqualFailure(this, fileName, lineNumber, expected, actual, text), testTerminator);
}

TestFailure.h

class SizetEqualFailure : public TestFailure
{
public:
    SizetEqualFailure(UtestShell* test, const char* fileName, size_t lineNumber, size_t expected, size_t actual, const SimpleString& text);
};

Please add this to the source

Reto

CanastraRF avatar Apr 29 '21 14:04 CanastraRF

Thanks. Could you do it as pull request (and also have the appropriate tests with it)?

basvodde avatar May 04 '21 14:05 basvodde

Sorry Im not familar with Git. So I add only an issues. Some tests are done when

LONG_EQUAL(0, str.find('H'));
LONG_EQUAL(1, str.find('e'));
LONG_EQUAL(SimpleString::npos, str.find('!'));

is replaced with

SIZET_EQUAL(0, str.find('H'));
SIZET_EQUAL(1, str.find('e'));
SIZET_EQUAL(SimpleString::npos, str.find('!'));

I don't know to add portable tests for 32bit, 64bit platforms. I'm home on visual studio and ARMCC. One possibility is to check sizeof(size_t) But I'm new in testing this way.

CanastraRF avatar May 04 '21 15:05 CanastraRF