Re: Checking for equality of C strings

"Duane Murphy" <[email protected]> Thu, 13 Feb 2003 15:23:11 -0800
Newsgroups gmane.comp.lib.cppunit.devel
Message-ID <[email protected]>
--- At Thu, 13 Feb 2003 17:12:47 -0600, Dave Glowacki wrote:

>If I try to do something like this:
>
>    const char *sample = "Sample String";
>
>    char copy[16];
>    strcpy(copy, sample);
>    CPPUNIT_ASSERT_EQUAL_MESSAGE("Identical strings are not equal",
>                                 (const char *)copy, sample);
>
>I end up getting an assertion like this:
>
>    charcompare.cpp:15:Assertion
>    Test name: CharCompare::testCompare
>    equality assertion failed
>    - Expected: Sample String
>    - Actual  : Sample String
>    - Identical strings are not equal
>
>Is there a variant of CPPUNIT_ASSERT_EQUAL macro which can compare
>C strings?  Extra points if it can compare a 'char[]' and a 'char *'!

You are comparing pointers. The pointers are not equal.

Convert them to std::string and compare them.

    std::string sample = "Sample String";

    std::string copy = sample;
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Identical strings are not equal",
                                 copy, sample);

Welcome to C++ 

:-)

 ...Duane




-------------------------------------------------------
This SF.NET email is sponsored by: FREE  SSL Guide from Thawte
are you planning your Web Server Security? Click here to get a FREE
Thawte SSL guide and find the answers to all your  SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en