-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
398 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,95 @@ | ||
using Xunit; | ||
using System; | ||
using Meadow.Units; | ||
using LU = Meadow.Units.Length.UnitType; | ||
|
||
namespace Meadow.Units.Tests | ||
namespace Meadow.Units.Tests; | ||
|
||
public class LengthTests | ||
{ | ||
public class LengthTests | ||
[Fact()] | ||
public void LengthCtors() | ||
{ | ||
Length t = new Length(10, LU.Meters); | ||
Assert.True(t.Meters == 10); | ||
|
||
Length t2 = new Length(40); | ||
Assert.True(t2.Meters == 40); | ||
} | ||
|
||
[Fact()] | ||
public void LengthConversions() | ||
{ | ||
Length t = new Length(10, LU.Meters); | ||
Assert.True(t.Kilometers == 0.01); | ||
Assert.True(t.Centimeters == 1000); | ||
Assert.True(t.Decimeters == 100); | ||
Assert.True(t.Millimeters == 10000); | ||
Assert.True(t.Microns == 10000000); | ||
Assert.True(t.Nanometers == 10000000000); | ||
Assert.True(t.Miles.Equals4DigitPrecision(0.00621371)); | ||
Assert.True(t.NauticalMiles.Equals4DigitPrecision(0.00539957)); | ||
Assert.True(t.Yards.Equals4DigitPrecision(10.9361)); | ||
Assert.True(t.Feet.Equals4DigitPrecision(32.8084)); | ||
} | ||
|
||
[Fact()] | ||
public void LengthMathOps() | ||
{ | ||
[Fact()] | ||
public void LengthCtors() | ||
{ | ||
Length t = new Length(10, LU.Meters); | ||
Assert.True(t.Meters == 10); | ||
|
||
Length t2 = new Length(40); | ||
Assert.True(t2.Meters == 40); | ||
} | ||
|
||
[Fact()] | ||
public void LengthConversions() | ||
{ | ||
Length t = new Length(10, LU.Meters); | ||
Assert.True(t.Kilometers == 0.01); | ||
Assert.True(t.Centimeters == 1000); | ||
Assert.True(t.Decimeters == 100); | ||
Assert.True(t.Millimeters == 10000); | ||
Assert.True(t.Microns == 10000000); | ||
Assert.True(t.Nanometers == 10000000000); | ||
Assert.True(t.Miles.Equals4DigitPrecision(0.00621371)); | ||
Assert.True(t.NauticalMiles.Equals4DigitPrecision(0.00539957)); | ||
Assert.True(t.Yards.Equals4DigitPrecision(10.9361)); | ||
Assert.True(t.Feet.Equals4DigitPrecision(32.8084)); | ||
} | ||
|
||
[Fact()] | ||
public void LengthMathOps() | ||
{ | ||
Length t1 = new Length(1); | ||
Length t2 = new Length(10); | ||
Length t3 = new Length(-3); | ||
Assert.True(t1 != t2); | ||
Assert.True((t1 + t2) == new Length(11)); | ||
Assert.True((t2 - t1) == new Length(9)); | ||
|
||
Assert.True(t3.Abs() == new Length(3)); | ||
} | ||
|
||
[Fact()] | ||
public void LengthComparisons() | ||
{ | ||
Length t1 = new Length(1); | ||
Length t2 = new Length(10); | ||
Length t3 = new Length(10); | ||
|
||
Assert.True(t1 < t2); | ||
Assert.True(t2 <= t3); | ||
Assert.True(t2 > t1); | ||
Assert.True(t3 >= t2); | ||
|
||
Assert.True(t2 == t3); | ||
|
||
Assert.True(t2 == new Length(10, LU.Meters)); | ||
Assert.True(t2 > new Length(5, LU.Meters)); | ||
|
||
Assert.True(t2.CompareTo(t3) == 0); | ||
Assert.True(t2.CompareTo(t1) > 0); | ||
Assert.True(t1.CompareTo(t2) < 0); | ||
|
||
Assert.True(t2.Equals(t3)); | ||
Assert.True(!t2.Equals(t1)); | ||
} | ||
|
||
[Fact()] | ||
public void RandomTests() | ||
{ | ||
// assignment | ||
Length i1 = new Length(100, LU.Meters); | ||
i1 = new Length(25); | ||
Assert.True(i1 == new Length(25)); | ||
|
||
// more assignment | ||
Length i2 = i1; | ||
Assert.True(i2 == new Length(25, LU.Meters)); | ||
|
||
Length i3 = new Length(i2); | ||
Assert.True(i3 == i2); | ||
} | ||
|
||
//[Fact()] | ||
//public void LengthTypeConversions() | ||
//{ | ||
// Length t1 = new Length(50, LU.Meters); | ||
|
||
// Assert.True(t1.ToInt32(FormatProi) == 50); | ||
//} | ||
Length t1 = new Length(1); | ||
Length t2 = new Length(10); | ||
Length t3 = new Length(-3); | ||
Assert.True(t1 != t2); | ||
Assert.True((t1 + t2) == new Length(11)); | ||
Assert.True((t2 - t1) == new Length(9)); | ||
|
||
Assert.True(t3.Abs() == new Length(3)); | ||
} | ||
|
||
[Fact()] | ||
public void LengthComparisons() | ||
{ | ||
Length t1 = new Length(1); | ||
Length t2 = new Length(10); | ||
Length t3 = new Length(10); | ||
|
||
Assert.True(t1 < t2); | ||
Assert.True(t2 <= t3); | ||
Assert.True(t2 > t1); | ||
Assert.True(t3 >= t2); | ||
|
||
Assert.True(t2 == t3); | ||
|
||
Assert.True(t2 == new Length(10, LU.Meters)); | ||
Assert.True(t2 > new Length(5, LU.Meters)); | ||
|
||
Assert.True(t2.CompareTo(t3) == 0); | ||
Assert.True(t2.CompareTo(t1) > 0); | ||
Assert.True(t1.CompareTo(t2) < 0); | ||
|
||
Assert.True(t2.Equals(t3)); | ||
Assert.True(!t2.Equals(t1)); | ||
} | ||
|
||
[Fact()] | ||
public void RandomTests() | ||
{ | ||
// assignment | ||
Length i1 = new Length(100, LU.Meters); | ||
i1 = new Length(25); | ||
Assert.True(i1 == new Length(25)); | ||
|
||
// more assignment | ||
Length i2 = i1; | ||
Assert.True(i2 == new Length(25, LU.Meters)); | ||
|
||
Length i3 = new Length(i2); | ||
Assert.True(i3 == i2); | ||
} | ||
|
||
//[Fact()] | ||
//public void LengthTypeConversions() | ||
//{ | ||
// Length t1 = new Length(50, LU.Meters); | ||
|
||
// Assert.True(t1.ToInt32(FormatProi) == 50); | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Xunit; | ||
|
||
namespace Meadow.Units.Tests | ||
{ | ||
public class TimePeriodTests | ||
{ | ||
[Fact()] | ||
public void MicroSecondsCreationTests() | ||
{ | ||
var p = TimePeriod.FromMicroseconds(1); | ||
Assert.Equal(0.000001d, p.Seconds); | ||
Assert.Equal(0.001d, p.Milliseconds); | ||
Assert.Equal(1000, p.Nanoseconds); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Meadow.Units.Conversions; | ||
|
||
internal static class TimePeriodConversions | ||
{ | ||
public static double Convert(double value, TimePeriod.UnitType from, TimePeriod.UnitType to) | ||
{ | ||
if (from == to) | ||
{ | ||
return value; | ||
} | ||
return value * timeConversions[(int)to] / timeConversions[(int)from]; | ||
} | ||
|
||
//must align to enum | ||
private static readonly double[] timeConversions = | ||
{ | ||
1_000_000_000,//ns | ||
1_000_000,//us | ||
1_000,//ms | ||
1,//s | ||
1/60d, //min | ||
1/(60d * 60d), //hrs | ||
1/(60d * 60d *24d), //days | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.