From c035e52394a03f3e59ca1a321731e5a0ea5d4a8b Mon Sep 17 00:00:00 2001 From: Tristan Milnthorp Date: Thu, 10 Jun 2021 10:52:05 -0400 Subject: [PATCH] Add inverse methods for electric conductivity and resistivity --- .../CustomCode/ElectricConductivityTests.cs | 16 ++++++++++++++ .../CustomCode/ElectricResistivityTests.cs | 16 ++++++++++++++ .../Quantities/ElectricConductivity.extra.cs | 22 +++++++++++++++++++ .../Quantities/ElectricResistivity.extra.cs | 22 +++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 UnitsNet/CustomCode/Quantities/ElectricConductivity.extra.cs create mode 100644 UnitsNet/CustomCode/Quantities/ElectricResistivity.extra.cs diff --git a/UnitsNet.Tests/CustomCode/ElectricConductivityTests.cs b/UnitsNet.Tests/CustomCode/ElectricConductivityTests.cs index bbb32fbf10..e737605987 100644 --- a/UnitsNet.Tests/CustomCode/ElectricConductivityTests.cs +++ b/UnitsNet.Tests/CustomCode/ElectricConductivityTests.cs @@ -22,6 +22,8 @@ using System; +using UnitsNet.Units; +using Xunit; namespace UnitsNet.Tests.CustomCode { @@ -31,5 +33,19 @@ public class ElectricConductivityTests : ElectricConductivityTestsBase protected override double SiemensPerMeterInOneSiemensPerMeter => 1; protected override double SiemensPerInchInOneSiemensPerMeter => 2.54e-2; protected override double SiemensPerFootInOneSiemensPerMeter => 3.048e-1; + + [Theory] + [InlineData( -1.0, -1.0 )] + [InlineData( -2.0, -0.5 )] + [InlineData( 0.0, 0.0 )] + [InlineData( 1.0, 1.0 )] + [InlineData( 2.0, 0.5 )] + public static void InverseTest( double value, double expected ) + { + var unit = new ElectricConductivity( value, ElectricConductivityUnit.SiemensPerMeter ); + var inverse = unit.Inverse(); + + AssertEx.Equals( expected, inverse.OhmMeters ); + } } } diff --git a/UnitsNet.Tests/CustomCode/ElectricResistivityTests.cs b/UnitsNet.Tests/CustomCode/ElectricResistivityTests.cs index 59cca6e1d8..378ec9a82d 100644 --- a/UnitsNet.Tests/CustomCode/ElectricResistivityTests.cs +++ b/UnitsNet.Tests/CustomCode/ElectricResistivityTests.cs @@ -22,6 +22,8 @@ using System; +using UnitsNet.Units; +using Xunit; namespace UnitsNet.Tests.CustomCode { @@ -55,5 +57,19 @@ public class ElectricResistivityTests : ElectricResistivityTestsBase protected override double PicoohmsCentimeterInOneOhmMeter => 1e14; protected override double PicoohmMetersInOneOhmMeter => 1e+12; + + [Theory] + [InlineData( -1.0, -1.0 )] + [InlineData( -2.0, -0.5 )] + [InlineData( 0.0, 0.0 )] + [InlineData( 1.0, 1.0 )] + [InlineData( 2.0, 0.5 )] + public static void InverseTest( double value, double expected ) + { + var unit = new ElectricResistivity( value, ElectricResistivityUnit.OhmMeter ); + var inverse = unit.Inverse(); + + AssertEx.Equals( expected, inverse.SiemensPerMeter ); + } } } diff --git a/UnitsNet/CustomCode/Quantities/ElectricConductivity.extra.cs b/UnitsNet/CustomCode/Quantities/ElectricConductivity.extra.cs new file mode 100644 index 0000000000..766f78c677 --- /dev/null +++ b/UnitsNet/CustomCode/Quantities/ElectricConductivity.extra.cs @@ -0,0 +1,22 @@ +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using UnitsNet.Units; + +namespace UnitsNet +{ + public partial struct ElectricConductivity + { + /// + /// Calculates the inverse or of this unit. + /// + /// The inverse or of this unit. + public ElectricResistivity Inverse() + { + if( SiemensPerMeter == 0.0 ) + return new ElectricResistivity( 0.0, ElectricResistivityUnit.OhmMeter ); + + return new ElectricResistivity( 1 / SiemensPerMeter, ElectricResistivityUnit.OhmMeter ); + } + } +} diff --git a/UnitsNet/CustomCode/Quantities/ElectricResistivity.extra.cs b/UnitsNet/CustomCode/Quantities/ElectricResistivity.extra.cs new file mode 100644 index 0000000000..a58e80c8a9 --- /dev/null +++ b/UnitsNet/CustomCode/Quantities/ElectricResistivity.extra.cs @@ -0,0 +1,22 @@ +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using UnitsNet.Units; + +namespace UnitsNet +{ + public partial struct ElectricResistivity + { + /// + /// Calculates the inverse or of this unit. + /// + /// The inverse or of this unit. + public ElectricConductivity Inverse() + { + if( OhmMeters == 0.0 ) + return new ElectricConductivity( 0, ElectricConductivityUnit.SiemensPerMeter ); + + return new ElectricConductivity( 1 / OhmMeters, ElectricConductivityUnit.SiemensPerMeter ); + } + } +}