Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 1.12 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. assert with custom comparison function
  2. Assert.That(somevector.EqualWithinTolerance(new Vec3(0f, 1f, 0f)), Is.True);
  3.        
  4. Assert.That(somevector, Is.EqualTo(new Vec3(0f, 1f, 0f)));
  5.        
  6. Expected: True
  7. But was:  False
  8.        
  9. Expected: 0 1 0
  10. But was:  1 0 9,536743E-07
  11.        
  12. /// <summary>
  13.     /// Equality comparer with a tolerance equivalent to using the 'EqualWithTolerance' method
  14.     ///
  15.     /// Note: since it's pretty much impossible to have working hash codes
  16.     /// for a "fuzzy" comparer the GetHashCode method throws an exception.
  17.     /// </summary>
  18.     public class EqualityComparerWithTolerance : IEqualityComparer<Vec3>
  19.     {
  20.         private float tolerance;
  21.  
  22.         public EqualityComparerWithTolerance(float tolerance = MathFunctions.Epsilon)
  23.         {
  24.             this.tolerance = tolerance;
  25.         }
  26.  
  27.         public bool Equals(Vec3 v1, Vec3 v2)
  28.         {
  29.             return v1.EqualWithinTolerance(v2, tolerance);
  30.         }
  31.  
  32.         public int GetHashCode(Vec3 obj)
  33.         {
  34.             throw new NotImplementedException();
  35.         }
  36.     }
  37.        
  38. Assert.That(somevector, Is.EqualTo(new Vec3(0f, 1f, 0f)).Using(fuzzyVectorComparer));