Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Comparing wide strings

Help
Egg Ware
2008-12-17
2013-04-22
  • Egg Ware
    Egg Ware
    2008-12-17

    Using Visual Studio 2005 with MFC.

    I downloaded the latest version and compiled it with the compiler setting for Unicode, I then created a test executable and linked the cppunit library.

    I created a test where I want to compare 2 strings, e.g.

    -----------------------
    void CReverseSortStrings_tests::long_strings_first()
    {
        prss->doSort(ppa); // Sort a list of CStrings

        // Get the value to compare (the actual)
        CString* psActual = (CString*)ppa->GetAt(0);

        CPPUNIT_ASSERT_EQUAL_MESSAGE(std::string("Longest string is first") , CString(L"E.abcde") , *psActual); // << ;
    }
    -----------------------

    I am expecting the test to fail and print out the contents of the strings but I get numbers, e.g.

    -----------------------
    .\CReverseSortStrings_tests.cpp(70) : error : Assertion
    Test name: CReverseSortStrings_tests::long_strings_first
    equality assertion failed
    - Expected: 00BCAB20
    - Actual  : 00BCA9D8
    - Longest string is first
    -----------------------

    I tried using std:wstring string instead of CString but I get compiler errors, e.g.

    1>{project}\cppunit\cppunit-1.12.1\include\cppunit/TestAssert.h(49) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::wstring' (or there is no acceptable conversion)

    Does CppUnit work with Unicode correctly or am I using the wrong macros to compare Unicode (wide) strings?

    Any help would be greatly appreciated.

    //*eggware

     
    • Anderson Ito
      Anderson Ito
      2009-02-03

      I am using Visual C++ under MFC and we ran into the same problem.

      I did a workaround that is:

      a) extend CPPUnit to support comparison between 2 const char*

      b) created a new macro: for example CPPUNIT_ASSERT_MFC_STRING_EQUAL and CPPUNIT_ASSERT_MFC_STRING_EQUAL_MESSAGE

      c) In my tests when I need to compare two CString objects I use:

      CString str1;
      CString str2;

      CPPUNIT_ASSERT_MFC_STRING_EQUAL(str1.GetBuffer(str1.GetLength()), str2.GetBuffer(str2.GetLength()));

      or

      CPPUNIT_ASSERT_MFC_STRING_EQUAL((LPCTSTR) str1, (LPCTSTR) str2);

      This might solving the issue regarding the memory address number of the 2 strings.

      Hope this helps.

      Anderson.