27 changed files with 461 additions and 197 deletions
@ -0,0 +1,119 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Utilities; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Base.UnitTests.Utilities |
||||
|
{ |
||||
|
public class MathUtilitiesTests |
||||
|
{ |
||||
|
private const double AnyValue = 42.42; |
||||
|
private readonly double _calculatedAnyValue; |
||||
|
private readonly double _one; |
||||
|
private readonly double _zero; |
||||
|
|
||||
|
public MathUtilitiesTests() |
||||
|
{ |
||||
|
_calculatedAnyValue = 0.0; |
||||
|
_one = 0.0; |
||||
|
_zero = 1.0; |
||||
|
|
||||
|
const int N = 10; |
||||
|
var dxAny = AnyValue / N; |
||||
|
var dxOne = 1.0 / N; |
||||
|
var dxZero = _zero / N; |
||||
|
|
||||
|
for (var i = 0; i < N; ++i) |
||||
|
{ |
||||
|
_calculatedAnyValue += dxAny; |
||||
|
_one += dxOne; |
||||
|
_zero -= dxZero; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Two_Equivalent_Double_Values_Are_Close() |
||||
|
{ |
||||
|
var actual = MathUtilities.AreClose(AnyValue, _calculatedAnyValue); |
||||
|
|
||||
|
Assert.True(actual); |
||||
|
Assert.Equal(AnyValue, Math.Round(_calculatedAnyValue, 14)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Two_Equivalent_Single_Values_Are_Close() |
||||
|
{ |
||||
|
var expectedValue = (float)AnyValue; |
||||
|
var actualValue = (float)_calculatedAnyValue; |
||||
|
|
||||
|
var actual = MathUtilities.AreClose(expectedValue, actualValue); |
||||
|
|
||||
|
Assert.True(actual); |
||||
|
Assert.Equal((float) Math.Round(expectedValue, 5), (float) Math.Round(actualValue, 4)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Calculated_Double_One_Is_One() |
||||
|
{ |
||||
|
var actual = MathUtilities.IsOne(_one); |
||||
|
|
||||
|
Assert.True(actual); |
||||
|
Assert.Equal(1.0, Math.Round(_one, 15)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Calculated_Single_One_Is_One() |
||||
|
{ |
||||
|
var actualValue = (float)_one; |
||||
|
|
||||
|
var actual = MathUtilities.IsOne(actualValue); |
||||
|
|
||||
|
Assert.True(actual); |
||||
|
Assert.Equal(1.0f, (float) Math.Round(actualValue, 7)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Calculated_Double_Zero_Is_Zero() |
||||
|
{ |
||||
|
var actual = MathUtilities.IsZero(_zero); |
||||
|
|
||||
|
Assert.True(actual); |
||||
|
Assert.Equal(0.0, Math.Round(_zero, 15)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Calculated_Single_Zero_Is_Zero() |
||||
|
{ |
||||
|
var actualValue = (float)_zero; |
||||
|
|
||||
|
var actual = MathUtilities.IsZero(actualValue); |
||||
|
|
||||
|
Assert.True(actual); |
||||
|
Assert.Equal(0.0f, (float) Math.Round(actualValue, 7)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Clamp_Input_NaN_Return_NaN() |
||||
|
{ |
||||
|
var clamp = MathUtilities.Clamp(double.NaN, 0.0, 1.0); |
||||
|
Assert.True(double.IsNaN(clamp)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Clamp_Input_NegativeInfinity_Return_Min() |
||||
|
{ |
||||
|
const double min = 0.0; |
||||
|
const double max = 1.0; |
||||
|
var actual = MathUtilities.Clamp(double.NegativeInfinity, min, max); |
||||
|
Assert.Equal(min, actual); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Clamp_Input_PositiveInfinity_Return_Max() |
||||
|
{ |
||||
|
const double min = 0.0; |
||||
|
const double max = 1.0; |
||||
|
var actual = MathUtilities.Clamp(double.PositiveInfinity, min, max); |
||||
|
Assert.Equal(max, actual); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
using System.ComponentModel; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Avalonia.Benchmarks.Data |
||||
|
{ |
||||
|
internal class AccessorTestObject : INotifyPropertyChanged |
||||
|
{ |
||||
|
private string _test; |
||||
|
|
||||
|
public string Test |
||||
|
{ |
||||
|
get => _test; |
||||
|
set |
||||
|
{ |
||||
|
if (_test == value) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
_test = value; |
||||
|
|
||||
|
OnPropertyChanged(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public event PropertyChangedEventHandler PropertyChanged; |
||||
|
|
||||
|
public void Execute() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public void Execute(object p0) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public void Execute(object p0, object p1) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
[NotifyPropertyChangedInvocator] |
||||
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) |
||||
|
{ |
||||
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using Avalonia.Data.Core.Plugins; |
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
namespace Avalonia.Benchmarks.Data |
||||
|
{ |
||||
|
[MemoryDiagnoser, InProcess] |
||||
|
public class PropertyAccessorPluginBenchmarks |
||||
|
{ |
||||
|
private readonly AccessorTestObject _targetStrongRef = new AccessorTestObject(); |
||||
|
|
||||
|
private readonly List<IPropertyAccessorPlugin> _oldPlugins; |
||||
|
private readonly List<IPropertyAccessorPlugin> _newPlugins; |
||||
|
|
||||
|
public PropertyAccessorPluginBenchmarks() |
||||
|
{ |
||||
|
_oldPlugins = new List<IPropertyAccessorPlugin> |
||||
|
{ |
||||
|
new AvaloniaPropertyAccessorPlugin(), |
||||
|
new MethodAccessorPlugin(), |
||||
|
new InpcPropertyAccessorPlugin() |
||||
|
}; |
||||
|
|
||||
|
_newPlugins = new List<IPropertyAccessorPlugin> |
||||
|
{ |
||||
|
new AvaloniaPropertyAccessorPlugin(), |
||||
|
new InpcPropertyAccessorPlugin(), |
||||
|
new MethodAccessorPlugin() |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
[Benchmark] |
||||
|
public void MatchAccessorOld() |
||||
|
{ |
||||
|
var propertyName = nameof(AccessorTestObject.Test); |
||||
|
|
||||
|
foreach (IPropertyAccessorPlugin x in _oldPlugins) |
||||
|
{ |
||||
|
if (x.Match(_targetStrongRef, propertyName)) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Benchmark] |
||||
|
public void MatchAccessorNew() |
||||
|
{ |
||||
|
var propertyName = nameof(AccessorTestObject.Test); |
||||
|
|
||||
|
foreach (IPropertyAccessorPlugin x in _newPlugins) |
||||
|
{ |
||||
|
if (x.Match(_targetStrongRef, propertyName)) |
||||
|
{ |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue