Skip to content

Commit

Permalink
added TimePeriod unit
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jun 25, 2024
1 parent 0f026a7 commit c18f6ef
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 92 deletions.
179 changes: 88 additions & 91 deletions Source/Meadow.Units.Tests/LengthTests.cs
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);
//}
}
16 changes: 16 additions & 0 deletions Source/Meadow.Units.Tests/TimePeriodTests.cs
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);
}
}
}
25 changes: 25 additions & 0 deletions Source/Meadow.Units/Conversions/TimePeriodConversions.cs
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
};
}
2 changes: 1 addition & 1 deletion Source/Meadow.Units/Meadow.Units.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>10.0</LangVersion>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<None Include="icon.png" Pack="true" PackagePath="" />
Expand Down
Loading

0 comments on commit c18f6ef

Please sign in to comment.