Skip to content

Commit 3fa0ba3

Browse files
Added Speed unit and tests.
1 parent 84decc3 commit 3fa0ba3

13 files changed

+1544
-1
lines changed

OnixLabs.Units.UnitTests/SpeedTests.cs

Lines changed: 616 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Units;
16+
17+
// ReSharper disable MemberCanBePrivate.Global
18+
public readonly partial struct Speed<T>
19+
{
20+
/// <summary>
21+
/// Computes the sum of the specified <see cref="Speed{T}"/> values.
22+
/// </summary>
23+
/// <param name="left">The left-hand value to add to.</param>
24+
/// <param name="right">The right-hand value to add.</param>
25+
/// <returns>Returns the sum of the specified <see cref="Speed{T}"/> values.</returns>
26+
public static Speed<T> Add(Speed<T> left, Speed<T> right) => new(left.QuectoMetersPerSecond + right.QuectoMetersPerSecond);
27+
28+
/// <summary>
29+
/// Computes the sum of the specified <see cref="Speed{T}"/> values.
30+
/// </summary>
31+
/// <param name="left">The left-hand value to add to.</param>
32+
/// <param name="right">The right-hand value to add.</param>
33+
/// <returns>Returns the sum of the specified <see cref="Speed{T}"/> values.</returns>
34+
public static Speed<T> operator +(Speed<T> left, Speed<T> right) => Add(left, right);
35+
36+
/// <summary>
37+
/// Computes the sum of the current <see cref="Speed{T}"/> value and the specified other <see cref="Speed{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to add to the current <see cref="Speed{T}"/> value.</param>
40+
/// <returns>Returns the sum of the current <see cref="Speed{T}"/> value and the specified other <see cref="Speed{T}"/> value.</returns>
41+
public Speed<T> Add(Speed<T> other) => Add(this, other);
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Units;
16+
17+
// ReSharper disable MemberCanBePrivate.Global
18+
public readonly partial struct Speed<T>
19+
{
20+
/// <summary>
21+
/// Computes the quotient of the specified <see cref="Speed{T}"/> values.
22+
/// </summary>
23+
/// <param name="left">The left-hand value to divide.</param>
24+
/// <param name="right">The right-hand value to divide by.</param>
25+
/// <returns>Returns the quotient of the specified <see cref="Speed{T}"/> values.</returns>
26+
public static Speed<T> Divide(Speed<T> left, Speed<T> right) => new(left.QuectoMetersPerSecond / right.QuectoMetersPerSecond);
27+
28+
/// <summary>
29+
/// Computes the quotient of the specified <see cref="Speed{T}"/> values.
30+
/// </summary>
31+
/// <param name="left">The left-hand value to divide.</param>
32+
/// <param name="right">The right-hand value to divide by.</param>
33+
/// <returns>Returns the quotient of the specified <see cref="Speed{T}"/> values.</returns>
34+
public static Speed<T> operator /(Speed<T> left, Speed<T> right) => Divide(left, right);
35+
36+
/// <summary>
37+
/// Computes the quotient of the current <see cref="Speed{T}"/> value divided by the specified other <see cref="Speed{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to divide the current <see cref="Speed{T}"/> value by.</param>
40+
/// <returns>Returns the quotient of the current <see cref="Speed{T}"/> value divided by the specified other <see cref="Speed{T}"/> value.</returns>
41+
public Speed<T> Divide(Speed<T> other) => Divide(this, other);
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Units;
16+
17+
// ReSharper disable MemberCanBePrivate.Global
18+
public readonly partial struct Speed<T>
19+
{
20+
/// <summary>
21+
/// Computes the product of the specified <see cref="Speed{T}"/> values.
22+
/// </summary>
23+
/// <param name="left">The left-hand value to multiply.</param>
24+
/// <param name="right">The right-hand value to multiply by.</param>
25+
/// <returns>Returns the product of the specified <see cref="Speed{T}"/> values.</returns>
26+
public static Speed<T> Multiply(Speed<T> left, Speed<T> right) => new(left.QuectoMetersPerSecond * right.QuectoMetersPerSecond);
27+
28+
/// <summary>
29+
/// Computes the product of the specified <see cref="Speed{T}"/> values.
30+
/// </summary>
31+
/// <param name="left">The left-hand value to multiply.</param>
32+
/// <param name="right">The right-hand value to multiply by.</param>
33+
/// <returns>Returns the product of the specified <see cref="Speed{T}"/> values.</returns>
34+
public static Speed<T> operator *(Speed<T> left, Speed<T> right) => Multiply(left, right);
35+
36+
/// <summary>
37+
/// Computes the product of the current <see cref="Speed{T}"/> value multiplied by the specified other <see cref="Speed{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to multiply the current <see cref="Speed{T}"/> value by.</param>
40+
/// <returns>Returns the product of the current <see cref="Speed{T}"/> value multiplied by the specified other <see cref="Speed{T}"/> value.</returns>
41+
public Speed<T> Multiply(Speed<T> other) => Multiply(this, other);
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Units;
16+
17+
// ReSharper disable MemberCanBePrivate.Global
18+
public readonly partial struct Speed<T>
19+
{
20+
/// <summary>
21+
/// Computes the difference of the specified <see cref="Speed{T}"/> values.
22+
/// </summary>
23+
/// <param name="left">The left-hand value to subtract from.</param>
24+
/// <param name="right">The right-hand value to subtract.</param>
25+
/// <returns>Returns the difference of the specified <see cref="Speed{T}"/> values.</returns>
26+
public static Speed<T> Subtract(Speed<T> left, Speed<T> right) => new(left.QuectoMetersPerSecond - right.QuectoMetersPerSecond);
27+
28+
/// <summary>
29+
/// Computes the difference of the specified <see cref="Speed{T}"/> values.
30+
/// </summary>
31+
/// <param name="left">The left-hand value to subtract from.</param>
32+
/// <param name="right">The right-hand value to subtract.</param>
33+
/// <returns>Returns the difference of the specified <see cref="Speed{T}"/> values.</returns>
34+
public static Speed<T> operator -(Speed<T> left, Speed<T> right) => Subtract(left, right);
35+
36+
/// <summary>
37+
/// Computes the difference of the current <see cref="Speed{T}"/> value and the specified other <see cref="Speed{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to subtract from the current <see cref="Speed{T}"/> value.</param>
40+
/// <returns>Returns the difference of the current <see cref="Speed{T}"/> value and the specified other <see cref="Speed{T}"/> value.</returns>
41+
public Speed<T> Subtract(Speed<T> other) => Subtract(this, other);
42+
}

OnixLabs.Units/Speed.Comparable.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using OnixLabs.Core;
16+
17+
namespace OnixLabs.Units;
18+
19+
// ReSharper disable MemberCanBePrivate.Global
20+
public readonly partial struct Speed<T>
21+
{
22+
/// <summary>
23+
/// Compares two <see cref="Speed{T}"/> values and returns an integer that indicates
24+
/// whether the left-hand value is less than, equal to, or greater than the right-hand value.
25+
/// </summary>
26+
/// <param name="left">The left-hand value to compare.</param>
27+
/// <param name="right">The right-hand value to compare.</param>
28+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
29+
public static int Compare(Speed<T> left, Speed<T> right) => left.QuectoMetersPerSecond.CompareTo(right.QuectoMetersPerSecond);
30+
31+
/// <summary>
32+
/// Compares the current instance with another instance of <see cref="Speed{T}"/> and returns an integer that indicates
33+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
34+
/// </summary>
35+
/// <param name="other">An instance of <see cref="Speed{T}"/> to compare with this instance.</param>
36+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
37+
public int CompareTo(Speed<T> other) => Compare(this, other);
38+
39+
/// <summary>
40+
/// Compares the current instance with another object of the same type and returns an integer that indicates
41+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
42+
/// </summary>
43+
/// <param name="obj">An object to compare with this instance.</param>
44+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
45+
// ReSharper disable once HeapView.BoxingAllocation
46+
public int CompareTo(object? obj) => this.CompareToObject(obj);
47+
48+
/// <summary>
49+
/// Determines whether the left-hand value is greater than the right-hand value.
50+
/// </summary>
51+
/// <param name="left">The left-hand value to compare.</param>
52+
/// <param name="right">The right-hand value to compare.</param>
53+
/// <returns>Returns <see langword="true"/> if the left-hand operand is greater than the right-hand operand; otherwise, <see langword="false"/>.</returns>
54+
public static bool operator >(Speed<T> left, Speed<T> right) => Compare(left, right) is 1;
55+
56+
/// <summary>
57+
/// Determines whether the left-hand value is greater than or equal to the right-hand value.
58+
/// </summary>
59+
/// <param name="left">The left-hand value to compare.</param>
60+
/// <param name="right">The right-hand value to compare.</param>
61+
/// <returns>Returns <see langword="true"/> if the left-hand operand is greater than or equal to the right-hand operand; otherwise, <see langword="false"/>.</returns>
62+
public static bool operator >=(Speed<T> left, Speed<T> right) => Compare(left, right) is 1 or 0;
63+
64+
/// <summary>
65+
/// Determines whether the left-hand value is less than the right-hand value.
66+
/// </summary>
67+
/// <param name="left">The left-hand value to compare.</param>
68+
/// <param name="right">The right-hand value to compare.</param>
69+
/// <returns>Returns <see langword="true"/> if the left-hand operand is less than the right-hand operand; otherwise, <see langword="false"/>.</returns>
70+
public static bool operator <(Speed<T> left, Speed<T> right) => Compare(left, right) is -1;
71+
72+
/// <summary>
73+
/// Determines whether the left-hand value is less than or equal to the right-hand value.
74+
/// </summary>
75+
/// <param name="left">The left-hand value to compare.</param>
76+
/// <param name="right">The right-hand value to compare.</param>
77+
/// <returns>Returns <see langword="true"/> if the left-hand operand is less than or equal to the right-hand operand; otherwise, <see langword="false"/>.</returns>
78+
public static bool operator <=(Speed<T> left, Speed<T> right) => Compare(left, right) is -1 or 0;
79+
}

OnixLabs.Units/Speed.Constants.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace OnixLabs.Units;
16+
17+
public readonly partial struct Speed<T>
18+
{
19+
/// <summary>
20+
/// Gets a zero <see cref="Speed{T}"/> value, equal to zero quectometers per second.
21+
/// </summary>
22+
public static readonly Speed<T> Zero = new(T.Zero);
23+
24+
private const string QuectoMetersPerSecondSpecifier = "qmps";
25+
private const string RontoMetersPerSecondSpecifier = "rmps";
26+
private const string YoctoMetersPerSecondSpecifier = "ymps";
27+
private const string ZeptoMetersPerSecondSpecifier = "zmps";
28+
private const string AttoMetersPerSecondSpecifier = "amps";
29+
private const string FemtoMetersPerSecondSpecifier = "fmps";
30+
private const string PicoMetersPerSecondSpecifier = "pmps";
31+
private const string NanoMetersPerSecondSpecifier = "nmps";
32+
private const string MicroMetersPerSecondSpecifier = "umps";
33+
private const string MilliMetersPerSecondSpecifier = "mmps";
34+
private const string CentiMetersPerSecondSpecifier = "cmps";
35+
private const string DeciMetersPerSecondSpecifier = "dmps";
36+
private const string MetersPerSecondSpecifier = "mps";
37+
private const string DecaMetersPerSecondSpecifier = "damps";
38+
private const string HectoMetersPerSecondSpecifier = "hmps";
39+
private const string KiloMetersPerSecondSpecifier = "kmps";
40+
private const string MegaMetersPerSecondSpecifier = "Mmps";
41+
private const string GigaMetersPerSecondSpecifier = "Gmps";
42+
private const string TeraMetersPerSecondSpecifier = "Tmps";
43+
private const string PetaMetersPerSecondSpecifier = "Pmps";
44+
private const string ExaMetersPerSecondSpecifier = "Emps";
45+
private const string ZettaMetersPerSecondSpecifier = "Zmps";
46+
private const string YottaMetersPerSecondSpecifier = "Ymps";
47+
private const string RonnaMetersPerSecondSpecifier = "Rmps";
48+
private const string QuettaMetersPerSecondSpecifier = "Qmps";
49+
private const string InchesPerSecondSpecifier = "ips";
50+
private const string FeetPerSecondSpecifier = "fps";
51+
private const string KilometersPerHourSpecifier = "kmph";
52+
private const string MilesPerHourSpecifier = "mph";
53+
private const string KnotsSpecifier = "kt";
54+
}

OnixLabs.Units/Speed.Equatable.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2020-2025 ONIXLabs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Diagnostics.CodeAnalysis;
16+
17+
namespace OnixLabs.Units;
18+
19+
// ReSharper disable MemberCanBePrivate.Global
20+
public readonly partial struct Speed<T>
21+
{
22+
/// <summary>
23+
/// Compares two instances of <see cref="Speed{T}"/> to determine whether their values are equal.
24+
/// </summary>
25+
/// <param name="left">The left-hand value to compare.</param>
26+
/// <param name="right">The right-hand value to compare.</param>
27+
/// <returns>Returns <see langword="true"/> if the two specified <see cref="Speed{T}"/> instances are equal; otherwise, <see langword="false"/>.</returns>
28+
public static bool Equals(Speed<T> left, Speed<T> right) => Equals(left.QuectoMetersPerSecond, right.QuectoMetersPerSecond);
29+
30+
/// <summary>
31+
/// Compares the current instance of <see cref="Speed{T}"/> with the specified other instance of <see cref="Speed{T}"/>.
32+
/// </summary>
33+
/// <param name="other">The other instance of <see cref="Speed{T}"/> to compare with the current instance.</param>
34+
/// <returns>Returns <see langword="true"/> if the current instance has the same value as the specified other instance; otherwise, <see langword="false"/>.</returns>
35+
public bool Equals(Speed<T> other) => Equals(this, other);
36+
37+
/// <summary>
38+
/// Checks for equality between this instance and another object.
39+
/// </summary>
40+
/// <param name="obj">The object to check for equality.</param>
41+
/// <returns>Returns <see langword="true"/> if the object is equal to this instance; otherwise, <see langword="false"/>.</returns>
42+
public override bool Equals([NotNullWhen(true)] object? obj) => obj is Speed<T> other && Equals(other);
43+
44+
/// <summary>
45+
/// Serves as a hash code function for this instance.
46+
/// </summary>
47+
/// <returns>Returns a hash code for this instance.</returns>
48+
public override int GetHashCode() => QuectoMetersPerSecond.GetHashCode();
49+
50+
/// <summary>
51+
/// Compares two instances of <see cref="Speed{T}"/> to determine whether their values are equal.
52+
/// </summary>
53+
/// <param name="left">The left-hand value to compare.</param>
54+
/// <param name="right">The right-hand value to compare.</param>
55+
/// <returns>Returns <see langword="true"/> if the two specified <see cref="Speed{T}"/> instances are equal; otherwise, <see langword="false"/>.</returns>
56+
public static bool operator ==(Speed<T> left, Speed<T> right) => Equals(left, right);
57+
58+
/// <summary>
59+
/// Compares two instances of <see cref="Speed{T}"/> to determine whether their values are not equal.
60+
/// </summary>
61+
/// <param name="left">The left-hand value to compare.</param>
62+
/// <param name="right">The right-hand value to compare.</param>
63+
/// <returns>Returns <see langword="true"/> if the two specified <see cref="Speed{T}"/> instances are not equal; otherwise, <see langword="false"/>.</returns>
64+
public static bool operator !=(Speed<T> left, Speed<T> right) => !Equals(left, right);
65+
}

0 commit comments

Comments
 (0)