断言:: AreSame()失败的自定义类与重载&操作者

时间:2017-10-21 13:52:46

标签: c++ unit-testing visual-studio-2015

为什么Assert::AreSame()失败?

要测试的C ++类:

template <typename T>
class XPtr
{
private:
    T* p;
public:
    XPtr(T* ptr) {
        p = ptr;
    }

    XPtr(const XPtr& ptr) {
        p = ptr.p;
    }

    T* operator&() {
        return p;
    }

    // The following declaration compiles but still 
    // is it the correct way of declaring friend function?
    template<typename T>
    friend std::wstring getString(const XPtr<T>& p); 
};

template<typename T>
std::wstring getString(const XPtr<T>& p) {
    std::wstringstream ss;
    ss << L"{" << p.p;
    if (p.p != 0) { ss << L"," << *p.p; }
    ss << L"}";
    return ss.str();
}

Native C ++单元测试如下:

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace Microsoft {
    namespace VisualStudio {
        namespace CppUnitTestFramework {
            //Actually I would like to specialize for XPtr<T> instead of XPtr<int>, 
            //how to do it?
            template<>
            static std::wstring ToString<XPtr<int> >(const class XPtr<int> & t) 
            { return getString<int>(t); }
        }
    }
}

namespace TestRCPtr1
{       
    TEST_CLASS(UnitTest1)
    {
    public:

        TEST_METHOD(TestMethod6)
        {
            XPtr<int> p1(new int(1));
            XPtr<int> p2(p1); 
            Assert::AreEqual(&p1, &p2); // Test passes
            Assert::AreSame(p1, p2);    // Test fails, see below for error message
        }
    };
}
  

结果消息:断言失败。预期:其中{000000001BE465D0,1}&GT;实际:其中{000000001BE465D0,1}&GT;

问题1:根据我的理解Assert::AreSame()应与逻辑&p1==&p2进行比较并成功通过。这里出了什么问题?

问题2:如何对Microsoft::VisualStudio::CppUnitTestFramework::ToString()而不是XPtr<T>专门化XPtr<int>(请参阅单元测试代码顶部)?

0 个答案:

没有答案