Skip to content

Commit

Permalink
Added travis integration
Browse files Browse the repository at this point in the history
Added more tests
  • Loading branch information
AlanRynne committed Feb 17, 2020
1 parent 90a0a41 commit 4d06bb2
Show file tree
Hide file tree
Showing 6 changed files with 722 additions and 476 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ branches:
only:
- master
- development

notifications:
slack: paramdigma:FtUcdwSDOynOHw2M479kXHxs
916 changes: 454 additions & 462 deletions coverage/cobertura.xml

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions src/Geometry/2D/Vector2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ public class Vector2d
/// Gets or sets the X Coordininate of the vector.
/// </summary>
/// <value>X coordinate.</value>
public double X { get; set; }
public double X
{
get; set;
}

/// <summary>
/// Gets or sets the Y coordinate of the vector.
/// </summary>
/// <value>Y coordinate.</value>
public double Y { get; set; }
public double Y
{
get; set;
}

/// <summary>
/// Initializes a new instance of the <see cref="Vector2d"/> class from another vector.
Expand Down Expand Up @@ -160,7 +166,7 @@ public double PerpProduct(Vector2d vector)
/// <returns></returns>
public override string ToString()
{
return "Vector3d [{X}, {Y}]";
return $"Vector3d [{X}, {Y}]";
}

/// <summary>
Expand All @@ -170,13 +176,14 @@ public override string ToString()
/// <returns>True if equal.</returns>
public override bool Equals(object obj)
{
if (obj is Vector2d)
if (!(obj is Vector2d))
{
Vector2d vect = obj as Vector2d;
return this.X == vect.X && this.Y == vect.Y;
return false;
}

return false;
Vector2d vect = obj as Vector2d;
return Math.Abs(X - vect.X) <= Settings.Tolerance
&& Math.Abs(Y - vect.Y) <= Settings.Tolerance;
}

/// <summary>
Expand All @@ -187,13 +194,17 @@ public override int GetHashCode()
{
unchecked
{
// Choose large primes to avoid hashing collisions
// Choose large primes to avoid hashing collisions
const int hashingBase = (int)2166136261;
const int hashingMultiplier = 16777619;
double tol = Settings.Tolerance * 2;
double tX = (int)(X * (1 / tol)) * tol;
double tY = (int)(Y * (1 / tol)) * tol;

int hash = hashingBase;
hash = (hash * hashingMultiplier) ^ (!ReferenceEquals(null, X) ? X.GetHashCode() : 0);
hash = (hash * hashingMultiplier) ^ (!ReferenceEquals(null, Y) ? Y.GetHashCode() : 0);
hash = (hash * hashingMultiplier) ^ tX.GetHashCode();
hash = (hash * hashingMultiplier) ^ tY.GetHashCode();
return hash;
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/Geometry/3D/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,37 @@ public class Plane
/// Gets or sets the plane origin.
/// </summary>
/// <value></value>
public Point3d Origin { get; set; }
public Point3d Origin
{
get; set;
}

/// <summary>
/// Gets or sets the plane X axis.
/// </summary>
/// <value></value>
public Vector3d XAxis { get; set; }
public Vector3d XAxis
{
get; set;
}

/// <summary>
/// Gets or sets the plane Y axis.
/// </summary>
/// <value></value>
public Vector3d YAxis { get; set; }
public Vector3d YAxis
{
get; set;
}

/// <summary>
/// Gets or sets the plane Z axis.
/// </summary>
/// <value></value>
public Vector3d ZAxis { get; set; }
public Vector3d ZAxis
{
get; set;
}

/// <summary>
/// Gets plane with axis' UnitX and UnitY.
Expand All @@ -57,7 +69,7 @@ public class Plane
/// </summary>
/// <param name="plane">Plane to copy values from.</param>
public Plane(Plane plane)
: this(plane.Origin, plane.XAxis, plane.YAxis, plane.ZAxis)
: this(new Point3d(plane.Origin), new Vector3d(plane.XAxis), new Vector3d(plane.YAxis), new Vector3d(plane.ZAxis))
{
}

Expand Down
157 changes: 157 additions & 0 deletions tests/Geometry/2D/Vector2dTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using Paramdigma.Core.Geometry;
using Xunit;

namespace Paramdigma.Core.Tests.Geometry
{
public class Vector2dTests
{
[Fact]
public void Create_Origin()
{
var empty = new Vector2d(0, 0);
var expected = new Vector2d(0, 0);
Assert.True(empty == expected);
}

[Fact]
public void Create_FromPoint()
{
var expected = new Vector2d(2.4, 2.5);
var copy = new Vector2d(expected);
Assert.True(expected == copy);
}

[Fact]
public void CanBe_Added()
{
const double a = 3.3;
const double b = 2.2;
const double c = 4.11;
var ptA = new Vector2d(a, b);
var ptB = new Vector2d(b, c);
var s = ptA - ptB;
var ptResult = new Vector2d(a + b, b + c);
Assert.True(ptA + ptB == ptResult);
}

[Fact]
public void CanBe_Substracted()
{
const double a = 3.3;
const double b = 2.2;
const double c = 4.11;
var ptA = new Vector2d(a, b);
var ptB = new Vector2d(b, c);
var ptResult = new Vector2d(a - b, b - c);
Assert.True(ptA - ptB == ptResult);
}

[Fact]
public void CanBe_Multiplied()
{
const double a = 3.3;
const double b = 2.2;
const double m = 1.45;
var ptA = new Vector2d(a, b);
var ptResult = new Vector2d(a * m, b * m);
Assert.True(ptA * m == ptResult);
Assert.True(m * ptA == ptResult);
}

[Fact]
public void CanBe_Divided()
{
const double a = 3.3;
const double b = 2.2;
const double m = 1.45;
var ptA = new Vector2d(a, b);
var ptResult = new Vector2d(a / m, b / m);
Assert.True(ptA / m == ptResult);
}

[Fact]
public void CanBe_Negated()
{
const double a = 3.3;
const double b = 2.2;
var ptA = new Vector2d(a, b);
var ptResult = new Vector2d(-a, -b);
Assert.True(-ptA == ptResult);
}

[Fact]
public void EqualsAndHashCode_HaveConsistentResults()
{
var pt = new Vector2d(1.000000009, -1);
var pt2 = new Vector2d(1, -1);
var b1 = pt == pt2;
var b2 = pt.GetHashCode() == pt2.GetHashCode();
var other = "S";
Assert.True(b1 && b1 == b2);
Assert.False(pt != pt2);
Assert.False(pt.Equals(other));
}

[Fact]
public void CanBe_ConvertedTo()
{
var pt = new Vector2d(0, 1);
Point2d v = (Point2d)pt;
Vector2d pt2 = v;
Assert.True(pt == pt2);
}

[Fact]
public void Can_AddVector()
{
var pt = new Vector2d(0, 1);
Point2d v = (Point2d)pt;
Vector2d pt2 = pt + v;
var expected = new Vector2d(0, 2);
Assert.True(pt2 == expected);
}

[Fact]
public void CanCompute_DotProduct()
{
var v = new Vector2d(1, 2);
var v2 = new Vector2d(3.211, -2.22);
var dot = v.DotProduct(v2);
}

[Fact]
public void CanCompute_PerpProduct()
{
var v = new Vector2d(1, 2);
var v2 = new Vector2d(3.211, -2.22);
var dot = v.PerpProduct(v2);
}

[Fact]
public void CanUnitize()
{
var v = new Vector2d(5.2333, 0);
v.Unitize();
var expected = new Vector2d(1, 0);
Assert.True(v == expected);
}

[Fact]
public void CanCompute_PerpendicularVector()
{
var v = new Vector2d(1, 2);
var expected = new Vector2d(-2, 1);
var perp = v.Perp();
Assert.True(perp == expected);
}

[Fact]
public void CanBe_ConvertedToString()
{
var result = "Vector3d [1, 0]";
var v = new Vector2d(1, 0);
var s = v.ToString();
Assert.True(s == result);
}
}
}
71 changes: 71 additions & 0 deletions tests/Geometry/3D/PlaneTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using Paramdigma.Core.Geometry;
using Xunit;

namespace Paramdigma.Core.Tests.Geometry
{
public class PlaneTests
{
[Fact]
public void CanBe_Created()
{
var plane = new Plane(Point3d.WorldOrigin);
var planeB = new Plane(Point3d.WorldOrigin, Vector3d.UnitZ, Vector3d.UnitX);

var ptA = new Point3d(1, 0, 0);
var ptB = new Point3d(0, 1, 0);
var ptC = new Point3d(0, 0, 0);
var planeC = new Plane(ptC, ptA, ptB);
}

[Fact]
public void CanCreate_SpecialPlanes()
{
var ptXY = Plane.WorldXY;
var ptYZ = Plane.WorldYZ;
var ptXZ = Plane.WorldXZ;
}

[Fact]
public void CanConvert_ToString()
{
var pln = Plane.WorldXY;
var s = pln.ToString();
Console.WriteLine(s);
}

[Fact]
public void CanCompute_Points()
{
var pln = Plane.WorldXY;
var pt = pln.PointAt(1, 1);
var expectedPt = new Point3d(1, 1, 0);
var ptB = pln.PointAt(1, 1, 1);
var expectedPtB = new Point3d(1, 1, 1);
Assert.True(pt == expectedPt);
Assert.True(ptB == expectedPtB);
}

[Fact]
public void CanBe_Cloned()
{
var pln = Plane.WorldXY;
var plnB = pln.Clone();
var plnC = new Plane(pln);
Assert.True(!ReferenceEquals(pln, plnB));
Assert.True(!ReferenceEquals(pln, plnC));
}

[Fact]
public void CanCompute_ClosestPoint()
{
var pln = Plane.WorldXY;
var pt = new Point3d(1, 1, 1);
var expected = new Point3d(1, 1, 0);
var result = pln.ClosestPoint(pt);
var dist = pln.DistanceTo(pt);
Assert.True(result == expected);
Assert.True(dist == 1);
}
}
}

0 comments on commit 4d06bb2

Please sign in to comment.