Why Int16.CompareTo returns different result from Int32.CompareTo

166 views Asked by At

I have two unit tests and both are valid. So, why Int16.CompateTo returns difference between two numbers, while Int32 and Int64 CompareTo method returns strictly -1, 0, 1?

[TestMethod]
public void Int16CompareTo()
{
    Int16 value1 = 60;
    Int16 value2 = 5;
    var result = value1.CompareTo(value2);
    Assert.AreEqual(result, 55);
}

[TestMethod]
public void Int32CompareTo()
{
    Int32 value1 = 60;
    Int32 value2 = 5;
    var result = value1.CompareTo(value2);
    Assert.AreEqual(result, 1);
}
1

There are 1 answers

0
Sam Marion On

The MSN documentation is reflecting the behavior you are receiving. All the methods guarantee is that you will get a value greater than 0 if the number is larger. 0 if the number is equal and less then 0 if the number is less.

If you want I would override the method or create a new one for test cases.

32 bit https://msdn.microsoft.com/en-us/library/y2ky8xsk(v=vs.110).aspx

16 bit https://msdn.microsoft.com/en-us/library/06x7xh8y(v=vs.110).aspx