Reputation: 544
I have a deep tree structure built with lists, containing lists and floats. I want to make assertions on such a structure, without being able to use a delta for the floats. My problem is, using the output from the failed assert is not enough, as one or two extra decimals are needed. I have to guess these decimals to proceed.
using System.Collections;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestAssertLab
{
public class List : ArrayList {
public List(params object[] list) {
AddRange(list);
}
public override bool Equals(object other) {
List l = other as List;
if (l != null) {
if (Count != l.Count) return false;
return !l.Cast<object>().Where((t, i) => !this[i].Equals(t)).Any();
}
return false;
}
public override string ToString() {
string s = this.Cast<object>().Aggregate("", (current, item) => current + (item + ","));
return "[" + s.TrimEnd(',') + "]";
}
}
[TestClass]
public class AssertLab
{
public List z(params object[] l) {
return new List(l);
}
[TestMethod]
public void TestFails() {
List expected = z(0.1428571f, z(101, 102));
List actual = z(1/7.0f, z(101, 102));
Assert.AreEqual(expected, actual);
// output: Assert.AreEqual failed. Expected:<[0.1428571,[101,102]]>. Actual:<[0.1428571,[101,102]]>.
}
[TestMethod]
public void TestOK()
{
List expected = z(0.142857143f, z(30101, 30102));
List actual = z(1 / 7.0f, z(30101, 30102));
Assert.AreEqual(expected, actual);
}
}
}
Upvotes: 2
Views: 1700
Reputation: 20640
You could cast them to decimal:
(Decimal) 0.1428571f == (Decimal)(1 / 7.0f)
Upvotes: 3