VS 2010单元测试和无符号类型

发布于 2024-07-26 17:30:40 字数 2160 浏览 4 评论 0原文

我正在将单元测试从 NUnit 转换为 VisualStudio 2010 单元测试框架。 以下 ByteToUShortTest() 方法失败并显示消息:

Assert.AreEqual 失败。 预期:。 实际:

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    Assert.AreEqual(ByteToUShort(array), new ushort[1]);
}

测试调用的代码是:

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array)
{
    return ByteToUShort(array, 0, array.Length, EndianType.LittleEndian);
}

public enum EndianType
{
    LittleEndian,
    BigEndian
}

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array, int offset, int length, EndianType endianType)
{
    // argument validation
    if ((length + offset) > array.Length)
        throw new ArgumentException("The length and offset provided extend past the end of the array.");
    if ((length % 2) != 0)
        throw new ArgumentException("The number of bytes to convert must be a multiple of 2.", "length");

    var temp = new ushort[length / 2];

    for (int i = 0, j = offset; i < temp.Length; i++)
    {
        if (endianType == EndianType.LittleEndian)
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) | (((uint)array[j++] & 0xFF) << 8));
        }
        else
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) << 8 | ((uint)array[j++] & 0xFF));
        }
    }

    return temp;
}

此测试已使用 NUnit 成功运行。 有什么想法为什么类型应该不同吗?

解决方案

对于单维和多维数组,以及任何ICollection,VisualStudio 2010 单元测试框架提供 CollectionAssert 类。

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    CollectionAssert.AreEqual(ByteToUShort(array), new ushort[1]);
}

I am converting my unit tests from NUnit to the VisualStudio 2010 unit test framework. The following ByteToUShortTest() method fails with the message:

Assert.AreEqual failed. Expected:<System.UInt16[]>. Actual:<System.UInt16[]>.

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    Assert.AreEqual(ByteToUShort(array), new ushort[1]);
}

The code called by the test is:

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array)
{
    return ByteToUShort(array, 0, array.Length, EndianType.LittleEndian);
}

public enum EndianType
{
    LittleEndian,
    BigEndian
}

[CLSCompliant(false)]
public static ushort[] ByteToUShort(byte[] array, int offset, int length, EndianType endianType)
{
    // argument validation
    if ((length + offset) > array.Length)
        throw new ArgumentException("The length and offset provided extend past the end of the array.");
    if ((length % 2) != 0)
        throw new ArgumentException("The number of bytes to convert must be a multiple of 2.", "length");

    var temp = new ushort[length / 2];

    for (int i = 0, j = offset; i < temp.Length; i++)
    {
        if (endianType == EndianType.LittleEndian)
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) | (((uint)array[j++] & 0xFF) << 8));
        }
        else
        {
            temp[i] = (ushort)(((uint)array[j++] & 0xFF) << 8 | ((uint)array[j++] & 0xFF));
        }
    }

    return temp;
}

This test was running successfully with NUnit. Any ideas why the types are supposed to be different?

Solution

For single and multi-dimensional arrays, as well as for any ICollection, the VisualStudio 2010 unit test framework provides the CollectionAssert class.

[TestMethod, CLSCompliant(false)]
public void ByteToUShortTest()
{
    var array = new byte[2];
    CollectionAssert.AreEqual(ByteToUShort(array), new ushort[1]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-08-02 17:30:40

不同的不是类型,而是实例。 您正在比较两个不同的数组。

It's not the types that are different, it's the instances. You're comparing two different arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文