1

I have a test function as follow:

    [TestMethod]
    void RipMichaelJacksonTest()
    {
        string expected = "Hello";
        BSTR actual = SysAllocString(L"Hello");
        Assert::AreEqual(expected, actual);
    }

The assert part will of course fail.

Is there any Assert function that i can use? Im new to VC++.

RayOldProf
  • 1,040
  • 4
  • 16
  • 40

1 Answers1

2

The problem is that you are doing a AreEqual. Passing in two parameters will force AreEqual(Object^, Object^) which:

Verifies that two specified objects are equal. The assertion fails if the objects are not equal.

What you are actually looking for is the comparison of a wchar* and a char*. There is not a direct comparison function between the two so it will be necessary to convert from a string to a wstring. There are lots of examples of how to do that, such as: https://stackoverflow.com/a/7159944/2642059 and you'll need to do something similar, for example:

wstring get_wstring(const string& s) {
    wstring buf;
    const char* cs = s.c_str();
    const size_t wn = mbsrtowcs(nullptr, &cs, 0, nullptr);

    if (wn == string::npos) {
        cout << "Error in mbsrtowcs(): " << errno << endl;
    } else {
        buf.resize(wn + 1);

        if(mbsrtowcs(&*buf.begin(), &cs, wn + 1, nullptr) == string::npos) {
            cout << "Error in mbsrtowcs(): " << errno << endl;
            buf.resize(0);
        }
    }

    return buf;
}

The return of get_wstring(expected) and actual will now both be wchars and can thereby be compared in the using AreEqual(String^, String^, bool)

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288