2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Core.UnitTests
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class PointTests : BaseTestFixture
|
|
|
|
{
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointEquality()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.True(new Point(0, 1) != new Point(1, 0));
|
|
|
|
Assert.True(new Point(5, 5) == new Point(5, 5));
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointDistance()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.That(new Point(2, 2).Distance(new Point(5, 6)), Is.EqualTo(5).Within(0.001));
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointMath()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point(2, 3) + new Size(3, 2);
|
|
|
|
Assert.AreEqual(new Point(5, 5), point);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
point = new Point(3, 4) - new Size(2, 3);
|
|
|
|
Assert.AreEqual(new Point(1, 1), point);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointFromSize()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point(new Size(10, 20));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(10, point.X);
|
|
|
|
Assert.AreEqual(20, point.Y);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointOffset()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point(2, 2);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
point = point.Offset(10, 20);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(new Point(12, 22), point);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointRound()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point(2.4, 2.7);
|
|
|
|
point = point.Round();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(Math.Round(2.4), point.X);
|
|
|
|
Assert.AreEqual(Math.Round(2.7), point.Y);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointEmpty()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.True(point.IsEmpty);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointHashCode([Range(3, 6)] double x1, [Range(3, 6)] double y1, [Range(3, 6)] double x2,
|
|
|
|
[Range(3, 6)] double y2)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
bool result = new Point(x1, y1).GetHashCode() == new Point(x2, y2).GetHashCode();
|
2016-03-22 23:02:25 +03:00
|
|
|
if (x1 == x2 && y1 == y2)
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.True(result);
|
2016-03-22 23:02:25 +03:00
|
|
|
else
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.False(result);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestSizeFromPoint()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point(2, 4);
|
|
|
|
var size = (Size)point;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(new Size(2, 4), size);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointEquals()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var point = new Point(2, 4);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.True(point.Equals(new Point(2, 4)));
|
|
|
|
Assert.False(point.Equals(new Point(3, 4)));
|
|
|
|
Assert.False(point.Equals("Point"));
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
[TestCase(0, 0, ExpectedResult = "{X=0 Y=0}")]
|
|
|
|
[TestCase(5, 2, ExpectedResult = "{X=5 Y=2}")]
|
|
|
|
public string TestPointToString(double x, double y)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
return new Point(x, y).ToString();
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestPointTypeConverter()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var converter = new PointTypeConverter();
|
|
|
|
Assert.True(converter.CanConvertFrom(typeof(string)));
|
|
|
|
Assert.AreEqual(new Point(1, 2), converter.ConvertFromInvariantString("1,2"));
|
|
|
|
Assert.AreEqual(new Point(1, 2), converter.ConvertFromInvariantString("1, 2"));
|
|
|
|
Assert.AreEqual(new Point(1, 2), converter.ConvertFromInvariantString(" 1 , 2 "));
|
|
|
|
Assert.AreEqual(new Point(1.1, 2), converter.ConvertFromInvariantString("1.1,2"));
|
|
|
|
Assert.Throws<InvalidOperationException>(() => converter.ConvertFromInvariantString(""));
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|