7 changed files with 214 additions and 5 deletions
@ -0,0 +1,42 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="TypeUtilities.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Utilities |
|||
{ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
|
|||
/// <summary>
|
|||
/// Provides math utilities not provided in System.Math.
|
|||
/// </summary>
|
|||
public static class MathUtilities |
|||
{ |
|||
/// <summary>
|
|||
/// Clamps a value between a minimum and maximum value.
|
|||
/// </summary>
|
|||
/// <param name="val">The value.</param>
|
|||
/// <param name="min">The minimum value.</param>
|
|||
/// <param name="max">The maximum value.</param>
|
|||
/// <returns>The clamped value.</returns>
|
|||
public static double Clamp(double val, double min, double max) |
|||
{ |
|||
if (val < min) |
|||
{ |
|||
return min; |
|||
} |
|||
else if (val > max) |
|||
{ |
|||
return max; |
|||
} |
|||
else |
|||
{ |
|||
return val; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,97 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="RangeBaseTests.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls.UnitTests.Primitives |
|||
{ |
|||
using Perspex.Controls.Primitives; |
|||
using System; |
|||
using Xunit; |
|||
|
|||
public class RangeBaseTests |
|||
{ |
|||
[Fact] |
|||
public void Maximum_Should_Be_Coerced_To_Minimum() |
|||
{ |
|||
var target = new TestRange |
|||
{ |
|||
Minimum = 100, |
|||
Maximum = 50, |
|||
}; |
|||
|
|||
Assert.Equal(100, target.Minimum); |
|||
Assert.Equal(100, target.Maximum); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Value_Should_Be_Coerced_To_Range() |
|||
{ |
|||
var target = new TestRange |
|||
{ |
|||
Minimum = 0, |
|||
Maximum = 50, |
|||
Value = 100, |
|||
}; |
|||
|
|||
Assert.Equal(0, target.Minimum); |
|||
Assert.Equal(50, target.Maximum); |
|||
Assert.Equal(50, target.Value); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Changing_Minimum_Should_Coerce_Value_And_Maximum() |
|||
{ |
|||
var target = new TestRange |
|||
{ |
|||
Minimum = 0, |
|||
Maximum = 100, |
|||
Value = 50, |
|||
}; |
|||
|
|||
target.Minimum = 200; |
|||
|
|||
Assert.Equal(200, target.Minimum); |
|||
Assert.Equal(200, target.Maximum); |
|||
Assert.Equal(200, target.Value); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Changing_Maximum_Should_Coerce_Value() |
|||
{ |
|||
var target = new TestRange |
|||
{ |
|||
Minimum = 0, |
|||
Maximum = 100, |
|||
Value = 100, |
|||
}; |
|||
|
|||
target.Maximum = 50; |
|||
|
|||
Assert.Equal(0, target.Minimum); |
|||
Assert.Equal(50, target.Maximum); |
|||
Assert.Equal(50, target.Value); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Properties_Should_Not_Accept_Nan_And_Inifinity() |
|||
{ |
|||
var target = new TestRange(); |
|||
|
|||
Assert.Throws<ArgumentException>(() => target.Minimum = double.NaN); |
|||
Assert.Throws<ArgumentException>(() => target.Minimum = double.PositiveInfinity); |
|||
Assert.Throws<ArgumentException>(() => target.Minimum = double.NegativeInfinity); |
|||
Assert.Throws<ArgumentException>(() => target.Maximum = double.NaN); |
|||
Assert.Throws<ArgumentException>(() => target.Maximum = double.PositiveInfinity); |
|||
Assert.Throws<ArgumentException>(() => target.Maximum = double.NegativeInfinity); |
|||
Assert.Throws<ArgumentException>(() => target.Value = double.NaN); |
|||
Assert.Throws<ArgumentException>(() => target.Value = double.PositiveInfinity); |
|||
Assert.Throws<ArgumentException>(() => target.Value = double.NegativeInfinity); |
|||
} |
|||
|
|||
private class TestRange : RangeBase |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue