Skip to content

Commit 0d789c0

Browse files
Added Force unit and unit tests.
1 parent 7f2c656 commit 0d789c0

13 files changed

+1397
-0
lines changed

OnixLabs.Units.UnitTests/ForceTests.cs

Lines changed: 545 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 Force<T>
19+
{
20+
/// <summary>
21+
/// Computes the sum of the specified <see cref="Force{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="Force{T}"/> values.</returns>
26+
public static Force<T> Add(Force<T> left, Force<T> right) => new(left.YoctoNewtons + right.YoctoNewtons);
27+
28+
/// <summary>
29+
/// Computes the sum of the specified <see cref="Force{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="Force{T}"/> values.</returns>
34+
public static Force<T> operator +(Force<T> left, Force<T> right) => Add(left, right);
35+
36+
/// <summary>
37+
/// Computes the sum of the current <see cref="Force{T}"/> value and the specified other <see cref="Force{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to add to the current <see cref="Force{T}"/> value.</param>
40+
/// <returns>Returns the sum of the current <see cref="Force{T}"/> value and the specified other <see cref="Force{T}"/> value.</returns>
41+
public Force<T> Add(Force<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 Force<T>
19+
{
20+
/// <summary>
21+
/// Computes the quotient of the specified <see cref="Force{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="Force{T}"/> values.</returns>
26+
public static Force<T> Divide(Force<T> left, Force<T> right) => new(left.YoctoNewtons / right.YoctoNewtons);
27+
28+
/// <summary>
29+
/// Computes the quotient of the specified <see cref="Force{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="Force{T}"/> values.</returns>
34+
public static Force<T> operator /(Force<T> left, Force<T> right) => Divide(left, right);
35+
36+
/// <summary>
37+
/// Computes the quotient of the current <see cref="Force{T}"/> value, divided by the specified other <see cref="Force{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to divide the current <see cref="Force{T}"/> value by.</param>
40+
/// <returns>Returns the quotient of the current <see cref="Force{T}"/> value, divided by the specified other <see cref="Force{T}"/> value.</returns>
41+
public Force<T> Divide(Force<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 Force<T>
19+
{
20+
/// <summary>
21+
/// Computes the product of the specified <see cref="Force{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="Force{T}"/> values.</returns>
26+
public static Force<T> Multiply(Force<T> left, Force<T> right) => new(left.YoctoNewtons * right.YoctoNewtons);
27+
28+
/// <summary>
29+
/// Computes the product of the specified <see cref="Force{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="Force{T}"/> values.</returns>
34+
public static Force<T> operator *(Force<T> left, Force<T> right) => Multiply(left, right);
35+
36+
/// <summary>
37+
/// Computes the product of the current <see cref="Force{T}"/> value, multiplied by the specified other <see cref="Force{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to multiply the current <see cref="Force{T}"/> value by.</param>
40+
/// <returns>Returns the product of the current <see cref="Force{T}"/> value, multiplied by the specified other <see cref="Force{T}"/> value.</returns>
41+
public Force<T> Multiply(Force<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 Force<T>
19+
{
20+
/// <summary>
21+
/// Computes the difference of the specified <see cref="Force{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="Force{T}"/> values.</returns>
26+
public static Force<T> Subtract(Force<T> left, Force<T> right) => new(left.YoctoNewtons - right.YoctoNewtons);
27+
28+
/// <summary>
29+
/// Computes the difference of the specified <see cref="Force{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="Force{T}"/> values.</returns>
34+
public static Force<T> operator -(Force<T> left, Force<T> right) => Subtract(left, right);
35+
36+
/// <summary>
37+
/// Computes the difference of the specified other <see cref="Force{T}"/> value, subtracted from the current <see cref="Force{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to subtract from the current <see cref="Force{T}"/> value.</param>
40+
/// <returns>Returns the difference of the specified other <see cref="Force{T}"/> value, subtracted from the current <see cref="Force{T}"/> value.</returns>
41+
public Force<T> Subtract(Force<T> other) => Subtract(this, other);
42+
}

OnixLabs.Units/Force.Comparable.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
public readonly partial struct Force<T>
20+
{
21+
/// <summary>
22+
/// Compares two <see cref="Force{T}"/> values and returns an integer that indicates
23+
/// whether the left-hand value is less than, equal to, or greater than the right-hand value.
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 a value that indicates the relative order of the objects being compared.</returns>
28+
public static int Compare(Force<T> left, Force<T> right) => left.YoctoNewtons.CompareTo(right.YoctoNewtons);
29+
30+
/// <summary>
31+
/// Compares the current instance with another object of the same type and returns an integer that indicates
32+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
33+
/// other object.
34+
/// </summary>
35+
/// <param name="other">An object 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(Force<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
42+
/// other object.
43+
/// </summary>
44+
/// <param name="obj">An object to compare with this instance.</param>
45+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
46+
// ReSharper disable once HeapView.BoxingAllocation
47+
public int CompareTo(object? obj) => this.CompareToObject(obj);
48+
49+
/// <summary>
50+
/// Determines whether the left-hand value is greater than the right-hand value.
51+
/// </summary>
52+
/// <param name="left">The left-hand value to compare.</param>
53+
/// <param name="right">The right-hand value to compare.</param>
54+
/// <returns>Returns <see langword="true"/> if the left-hand operand is greater than right-hand operand; otherwise, <see langword="false"/>.</returns>
55+
public static bool operator >(Force<T> left, Force<T> right) => Compare(left, right) is 1;
56+
57+
/// <summary>
58+
/// Determines whether the left-hand value is greater than or equal to the right-hand value.
59+
/// </summary>
60+
/// <param name="left">The left-hand value to compare.</param>
61+
/// <param name="right">The right-hand value to compare.</param>
62+
/// <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>
63+
public static bool operator >=(Force<T> left, Force<T> right) => Compare(left, right) is 1 or 0;
64+
65+
/// <summary>
66+
/// Determines whether the left-hand value is less than right-hand value.
67+
/// </summary>
68+
/// <param name="left">The left-hand value to compare.</param>
69+
/// <param name="right">The right-hand value to compare.</param>
70+
/// <returns>Returns <see langword="true"/> if the left-hand operand is less than the right-hand operand; otherwise, <see langword="false"/>.</returns>
71+
public static bool operator <(Force<T> left, Force<T> right) => Compare(left, right) is -1;
72+
73+
/// <summary>
74+
/// Determines whether the left-hand value is less than or equal to the right-hand value.
75+
/// </summary>
76+
/// <param name="left">The left-hand value to compare.</param>
77+
/// <param name="right">The right-hand value to compare.</param>
78+
/// <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>
79+
public static bool operator <=(Force<T> left, Force<T> right) => Compare(left, right) is -1 or 0;
80+
}

OnixLabs.Units/Force.Constants.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 Force<T>
18+
{
19+
/// <summary>
20+
/// Gets a zero <see cref="Force{T}"/> value, equal to zero yoctonewtons.
21+
/// </summary>
22+
public static readonly Force<T> Zero = new(T.Zero);
23+
24+
private const string YoctoNewtonsSpecifier = "yN";
25+
private const string ZeptoNewtonsSpecifier = "zN";
26+
private const string AttoNewtonsSpecifier = "aN";
27+
private const string FemtoNewtonsSpecifier = "fN";
28+
private const string PicoNewtonsSpecifier = "pN";
29+
private const string NanoNewtonsSpecifier = "nN";
30+
private const string MicroNewtonsSpecifier = "uN";
31+
private const string MilliNewtonsSpecifier = "mN";
32+
private const string NewtonsSpecifier = "N";
33+
private const string KiloNewtonsSpecifier = "kN";
34+
private const string MegaNewtonsSpecifier = "MN";
35+
private const string GigaNewtonsSpecifier = "GN";
36+
private const string TeraNewtonsSpecifier = "TN";
37+
private const string PetaNewtonsSpecifier = "PN";
38+
private const string ExaNewtonsSpecifier = "EN";
39+
private const string ZettaNewtonsSpecifier = "ZN";
40+
private const string YottaNewtonsSpecifier = "YN";
41+
private const string DynesSpecifier = "dyn";
42+
private const string KilogramForceSpecifier = "kgf";
43+
private const string GramForceSpecifier = "gf";
44+
private const string TonneForceSpecifier = "tf";
45+
private const string PoundForceSpecifier = "lbf";
46+
private const string OunceForceSpecifier = "ozf";
47+
private const string PoundalsSpecifier = "pdl";
48+
private const string ShortTonForceSpecifier = "tonf";
49+
private const string LongTonForceSpecifier = "ltf";
50+
}

OnixLabs.Units/Force.Equatable.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
public readonly partial struct Force<T>
20+
{
21+
/// <summary>
22+
/// Compares two instances of <see cref="Force{T}"/> to determine whether their values are equal.
23+
/// </summary>
24+
/// <param name="left">The left-hand value to compare.</param>
25+
/// <param name="right">The right-hand value to compare.</param>
26+
/// <returns>Returns <see langword="true"/> if the two specified instances are equal; otherwise, <see langword="false"/>.</returns>
27+
public static bool Equals(Force<T> left, Force<T> right) => Equals(left.YoctoNewtons, right.YoctoNewtons);
28+
29+
/// <summary>
30+
/// Compares the current instance of <see cref="Force{T}"/> with the specified other instance of <see cref="Force{T}"/>.
31+
/// </summary>
32+
/// <param name="other">The other instance of <see cref="Force{T}"/> to compare with the current instance.</param>
33+
/// <returns>Returns <see langword="true"/> if the current instance is equal to the specified other instance; otherwise, <see langword="false"/>.</returns>
34+
public bool Equals(Force<T> other) => Equals(this, other);
35+
36+
/// <summary>
37+
/// Checks for equality between this instance and another object.
38+
/// </summary>
39+
/// <param name="obj">The object to check for equality.</param>
40+
/// <returns>Returns <see langword="true"/> if the object is equal to this instance; otherwise, <see langword="false"/>.</returns>
41+
public override bool Equals([NotNullWhen(true)] object? obj) => obj is Force<T> other && Equals(other);
42+
43+
/// <summary>
44+
/// Serves as a hash code function for this instance.
45+
/// </summary>
46+
/// <returns>Returns a hash code for this instance.</returns>
47+
public override int GetHashCode() => YoctoNewtons.GetHashCode();
48+
49+
/// <summary>
50+
/// Compares two instances of <see cref="Force{T}"/> to determine whether their values are equal.
51+
/// </summary>
52+
/// <param name="left">The left-hand value to compare.</param>
53+
/// <param name="right">The right-hand value to compare.</param>
54+
/// <returns>Returns <see langword="true"/> if the two specified instances are equal; otherwise, <see langword="false"/>.</returns>
55+
public static bool operator ==(Force<T> left, Force<T> right) => Equals(left, right);
56+
57+
/// <summary>
58+
/// Compares two instances of <see cref="Force{T}"/> to determine whether their values are not equal.
59+
/// </summary>
60+
/// <param name="left">The left-hand value to compare.</param>
61+
/// <param name="right">The right-hand value to compare.</param>
62+
/// <returns>Returns <see langword="true"/> if the two specified instances are not equal; otherwise, <see langword="false"/>.</returns>
63+
public static bool operator !=(Force<T> left, Force<T> right) => !Equals(left, right);
64+
}

0 commit comments

Comments
 (0)