Skip to content

Commit 84decc3

Browse files
Added Volume unit and unit tests.
1 parent 3a3a791 commit 84decc3

16 files changed

+1543
-4
lines changed

OnixLabs.Units.UnitTests/AreaTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed class AreaTests
2626
public void AreaEmptyShouldProduceExpectedResult()
2727
{
2828
// Given / When
29-
Area<double> area = Area<double>.Empty;
29+
Area<double> area = Area<double>.Zero;
3030

3131
// Then
3232
Assert.Equal(0.0, area.SquareYoctoMeters, Tolerance);

OnixLabs.Units.UnitTests/VolumeTests.cs

Lines changed: 575 additions & 0 deletions
Large diffs are not rendered by default.

OnixLabs.Units/Area.Constants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ namespace OnixLabs.Units;
1717
public readonly partial struct Area<T>
1818
{
1919
/// <summary>
20-
/// Gets an empty <see cref="Area{T}"/> value.
20+
/// Gets a zero <c>0</c> <see cref="Area{T}"/> value.
2121
/// </summary>
22-
public static Area<T> Empty => new(T.Zero);
22+
public static Area<T> Zero => new(T.Zero);
2323

2424
private const string SquareYoctoMetersSpecifier = "sqym";
2525
private const string SquareZeptoMetersSpecifier = "sqzm";

OnixLabs.Units/Force.Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace OnixLabs.Units;
1717
public readonly partial struct Force<T>
1818
{
1919
/// <summary>
20-
/// Gets a zero <see cref="Force{T}"/> value, equal to zero yoctonewtons.
20+
/// Gets a zero <c>0</c> <see cref="Force{T}"/> value.
2121
/// </summary>
2222
public static readonly Force<T> Zero = new(T.Zero);
2323

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 Volume<T>
19+
{
20+
/// <summary>
21+
/// Computes the sum of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
26+
public static Volume<T> Add(Volume<T> left, Volume<T> right) => new(left.CubicYoctoMeters + right.CubicYoctoMeters);
27+
28+
/// <summary>
29+
/// Computes the sum of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
34+
public static Volume<T> operator +(Volume<T> left, Volume<T> right) => Add(left, right);
35+
36+
/// <summary>
37+
/// Computes the sum of the current <see cref="Volume{T}"/> value and the specified other <see cref="Volume{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to add to the current <see cref="Volume{T}"/> value.</param>
40+
/// <returns>Returns the sum of the current <see cref="Volume{T}"/> value and the specified other <see cref="Volume{T}"/> value.</returns>
41+
public Volume<T> Add(Volume<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 Volume<T>
19+
{
20+
/// <summary>
21+
/// Computes the quotient of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
26+
public static Volume<T> Divide(Volume<T> left, Volume<T> right) => new(left.CubicYoctoMeters / right.CubicYoctoMeters);
27+
28+
/// <summary>
29+
/// Computes the quotient of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
34+
public static Volume<T> operator /(Volume<T> left, Volume<T> right) => Divide(left, right);
35+
36+
/// <summary>
37+
/// Computes the quotient of the current <see cref="Volume{T}"/> value, divided by the specified other <see cref="Volume{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to divide the current <see cref="Volume{T}"/> value by.</param>
40+
/// <returns>Returns the quotient of the current <see cref="Volume{T}"/> value divided by the specified other <see cref="Volume{T}"/> value.</returns>
41+
public Volume<T> Divide(Volume<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 Volume<T>
19+
{
20+
/// <summary>
21+
/// Computes the product of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
26+
public static Volume<T> Multiply(Volume<T> left, Volume<T> right) => new(left.CubicYoctoMeters * right.CubicYoctoMeters);
27+
28+
/// <summary>
29+
/// Computes the product of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
34+
public static Volume<T> operator *(Volume<T> left, Volume<T> right) => Multiply(left, right);
35+
36+
/// <summary>
37+
/// Computes the product of the current <see cref="Volume{T}"/> value, multiplied by the specified other <see cref="Volume{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to multiply the current <see cref="Volume{T}"/> value by.</param>
40+
/// <returns>Returns the product of the current <see cref="Volume{T}"/> value multiplied by the specified other <see cref="Volume{T}"/> value.</returns>
41+
public Volume<T> Multiply(Volume<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 Volume<T>
19+
{
20+
/// <summary>
21+
/// Computes the difference of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
26+
public static Volume<T> Subtract(Volume<T> left, Volume<T> right) => new(left.CubicYoctoMeters - right.CubicYoctoMeters);
27+
28+
/// <summary>
29+
/// Computes the difference of the specified <see cref="Volume{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="Volume{T}"/> values.</returns>
34+
public static Volume<T> operator -(Volume<T> left, Volume<T> right) => Subtract(left, right);
35+
36+
/// <summary>
37+
/// Computes the difference of the current <see cref="Volume{T}"/> value and the specified other <see cref="Volume{T}"/> value.
38+
/// </summary>
39+
/// <param name="other">The value to subtract from the current <see cref="Volume{T}"/> value.</param>
40+
/// <returns>Returns the difference of the current <see cref="Volume{T}"/> value and the specified other <see cref="Volume{T}"/> value.</returns>
41+
public Volume<T> Subtract(Volume<T> other) => Subtract(this, other);
42+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 Volume<T>
21+
{
22+
/// <summary>
23+
/// Compares two <see cref="Volume{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(Volume<T> left, Volume<T> right) => left.CubicYoctoMeters.CompareTo(right.CubicYoctoMeters);
30+
31+
/// <summary>
32+
/// Compares the current instance with another object of the same type 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
34+
/// other object.
35+
/// </summary>
36+
/// <param name="other">An object to compare with this instance.</param>
37+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
38+
public int CompareTo(Volume<T> other) => Compare(this, other);
39+
40+
/// <summary>
41+
/// Compares the current instance with another object of the same type and returns an integer that indicates
42+
/// whether the current instance precedes, follows, or occurs in the same position in the sort order as the
43+
/// other object.
44+
/// </summary>
45+
/// <param name="obj">An object to compare with this instance.</param>
46+
/// <returns>Returns a value that indicates the relative order of the objects being compared.</returns>
47+
// ReSharper disable once HeapView.BoxingAllocation
48+
public int CompareTo(object? obj) => this.CompareToObject(obj);
49+
50+
/// <summary>
51+
/// Determines whether the left-hand value is greater than the right-hand value.
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 left-hand operand is greater than the right-hand operand; otherwise, <see langword="false"/>.</returns>
56+
public static bool operator >(Volume<T> left, Volume<T> right) => Compare(left, right) is 1;
57+
58+
/// <summary>
59+
/// Determines whether the left-hand value is greater than or equal to the right-hand value.
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 left-hand operand is greater than or equal to the right-hand operand; otherwise, <see langword="false"/>.</returns>
64+
public static bool operator >=(Volume<T> left, Volume<T> right) => Compare(left, right) is 1 or 0;
65+
66+
/// <summary>
67+
/// Determines whether the left-hand value is less than the right-hand value.
68+
/// </summary>
69+
/// <param name="left">The left-hand value to compare.</param>
70+
/// <param name="right">The right-hand value to compare.</param>
71+
/// <returns>Returns <see langword="true"/> if the left-hand operand is less than the right-hand operand; otherwise, <see langword="false"/>.</returns>
72+
public static bool operator <(Volume<T> left, Volume<T> right) => Compare(left, right) is -1;
73+
74+
/// <summary>
75+
/// Determines whether the left-hand value is less than or equal to the right-hand value.
76+
/// </summary>
77+
/// <param name="left">The left-hand value to compare.</param>
78+
/// <param name="right">The right-hand value to compare.</param>
79+
/// <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>
80+
public static bool operator <=(Volume<T> left, Volume<T> right) => Compare(left, right) is -1 or 0;
81+
}

OnixLabs.Units/Volume.Constants.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;
16+
using System.Numerics;
17+
using OnixLabs.Core;
18+
19+
namespace OnixLabs.Units;
20+
21+
22+
public readonly partial struct Volume<T>
23+
{
24+
/// <summary>
25+
/// Gets a zero <c>0</c> <see cref="Volume{T}"/> value.
26+
/// </summary>
27+
public static readonly Volume<T> Zero = new(T.Zero);
28+
29+
private const string CubicYoctoMetersSpecifier = "cuym";
30+
private const string CubicZeptoMetersSpecifier = "cuzm";
31+
private const string CubicAttoMetersSpecifier = "cuam";
32+
private const string CubicFemtoMetersSpecifier = "cufm";
33+
private const string CubicPicoMetersSpecifier = "cupm";
34+
private const string CubicNanoMetersSpecifier = "cunm";
35+
private const string CubicMicroMetersSpecifier = "cuum";
36+
private const string CubicMilliMetersSpecifier = "cumm";
37+
private const string CubicCentiMetersSpecifier = "cucm";
38+
private const string CubicDeciMetersSpecifier = "cudm";
39+
private const string CubicMetersSpecifier = "cum";
40+
private const string CubicDecaMetersSpecifier = "cudam";
41+
private const string CubicHectoMetersSpecifier = "cuhm";
42+
private const string CubicKiloMetersSpecifier = "cukm";
43+
private const string CubicMegaMetersSpecifier = "cuMm";
44+
private const string CubicGigaMetersSpecifier = "cuGm";
45+
private const string CubicTeraMetersSpecifier = "cuTm";
46+
private const string CubicPetaMetersSpecifier = "cuPm";
47+
private const string CubicExaMetersSpecifier = "cuEm";
48+
private const string CubicZettaMetersSpecifier = "cuZm";
49+
private const string CubicYottaMetersSpecifier = "cuYm";
50+
private const string LitersSpecifier = "l";
51+
private const string MilliLitersSpecifier = "ml";
52+
private const string CentiLitersSpecifier = "cl";
53+
private const string DeciLitersSpecifier = "dl";
54+
private const string CubicInchesSpecifier = "cuin";
55+
private const string CubicFeetSpecifier = "cuft";
56+
private const string CubicYardsSpecifier = "cuyd";
57+
private const string FluidOuncesSpecifier = "floz";
58+
private const string CupsSpecifier = "cup";
59+
private const string PintsSpecifier = "pt";
60+
private const string QuartsSpecifier = "qt";
61+
private const string GallonsSpecifier = "gal";
62+
}

0 commit comments

Comments
 (0)