Skip to content

Commit bdddc31

Browse files
authored
Add formula to compute electric charge (#810)
1 parent 381e181 commit bdddc31

File tree

6 files changed

+62
-22
lines changed

6 files changed

+62
-22
lines changed

UnitsNet.Tests/CustomCode/DurationTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,12 @@ public void DurationFromStringUsingMultipleAbbreviationsParsedCorrectly(string t
181181

182182
AssertEx.EqualTolerance(expectedSeconds, Duration.Parse(textValue, cultureInfo).Seconds, SecondsTolerance);
183183
}
184+
185+
[Fact]
186+
public void DurationMultipliedByElectricCurrentEqualsElectricCharge()
187+
{
188+
ElectricCharge ah = Duration.FromHours(5) * ElectricCurrent.FromAmperes(4);
189+
Assert.Equal(20, ah.AmpereHours);
190+
}
184191
}
185192
}
Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
1-
//------------------------------------------------------------------------------
2-
// <auto-generated>
3-
// This code was generated (once) by \generate-code.bat, but will not be
4-
// regenerated when it already exists. The purpose of creating this file is to make
5-
// it easier to remember to implement all the unit conversion test cases.
6-
//
7-
// Whenever a new unit is added to this quantity and \generate-code.bat is run,
8-
// the base test class will get a new abstract property and cause a compile error
9-
// in this derived class, reminding the developer to implement the test case
10-
// for the new unit.
11-
//
12-
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
13-
//
14-
// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities.
15-
// Add UnitDefinitions\MyQuantity.json and run GeneratUnits.bat to generate new units or quantities.
16-
//
17-
// </auto-generated>
18-
//------------------------------------------------------------------------------
19-
20-
// Licensed under MIT No Attribution, see LICENSE file at the root.
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
212
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
223

23-
24-
using System;
4+
using Xunit;
255

266
namespace UnitsNet.Tests.CustomCode
277
{
@@ -32,5 +12,19 @@ public class ElectricChargeTests : ElectricChargeTestsBase
3212
protected override double AmpereHoursInOneCoulomb => 2.77777777777e-4;
3313
protected override double KiloampereHoursInOneCoulomb => 2.77777777777e-7;
3414
protected override double MegaampereHoursInOneCoulomb => 2.77777777777e-10;
15+
16+
[Fact]
17+
public void ElectricChargeDividedByElectricCurrentEqualsDuration()
18+
{
19+
Duration t = ElectricCharge.FromAmpereHours(20) / ElectricCurrent.FromAmperes(5);
20+
Assert.Equal(4, t.Hours);
21+
}
22+
23+
[Fact]
24+
public void ElectricChargeDividedByDurationEqualsElectricCurrent()
25+
{
26+
ElectricCurrent i = ElectricCharge.FromAmpereHours(20) / Duration.FromHours(4);
27+
Assert.Equal(5, i.Amperes);
28+
}
3529
}
3630
}

UnitsNet.Tests/CustomCode/ElectricCurrentTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,12 @@ public void ElectricCurrentMultipliedByElectricPotentialEqualsPower()
4141
Power p = ElectricCurrent.FromAmperes(2) * ElectricPotential.FromVolts(10);
4242
Assert.Equal(20, p.Watts);
4343
}
44+
45+
[Fact]
46+
public void ElectricCurrentMultipliedByDurationEqualsElectricCharge()
47+
{
48+
ElectricCharge ah = ElectricCurrent.FromAmperes(4) * Duration.FromHours(5);
49+
Assert.Equal(20, ah.AmpereHours);
50+
}
4451
}
4552
}

UnitsNet/CustomCode/Quantities/Duration.extra.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,11 @@ public static explicit operator Duration(TimeSpan duration)
9797
{
9898
return Volume.FromCubicMeters(volumeFlow.CubicMetersPerSecond * duration.Seconds);
9999
}
100+
101+
/// <summary>Calculate <see cref="ElectricCharge"/> from <see cref="Duration"/> multiplied by <see cref="ElectricCurrent"/>.</summary>
102+
public static ElectricCharge operator *(Duration time, ElectricCurrent current)
103+
{
104+
return ElectricCharge.FromAmpereHours(current.Amperes * time.Hours);
105+
}
100106
}
101107
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
namespace UnitsNet
5+
{
6+
public partial struct ElectricCharge
7+
{
8+
/// <summary>Get <see cref="ElectricCurrent"/> from <see cref="ElectricCharge"/> divided by <see cref="Duration"/>.</summary>
9+
public static ElectricCurrent operator /(ElectricCharge charge, Duration time)
10+
{
11+
return ElectricCurrent.FromAmperes(charge.AmpereHours / time.Hours);
12+
}
13+
14+
/// <summary>Get <see cref="Duration"/> from <see cref="ElectricCharge"/> divided by <see cref="ElectricCurrent"/>.</summary>
15+
public static Duration operator /(ElectricCharge charge, ElectricCurrent current)
16+
{
17+
return Duration.FromHours(charge.AmpereHours / current.Amperes);
18+
}
19+
}
20+
}

UnitsNet/CustomCode/Quantities/ElectricCurrent.extra.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@ public partial struct ElectricCurrent
1818
{
1919
return Power.FromWatts(potential.Volts * current.Amperes);
2020
}
21+
22+
/// <summary>Calculate <see cref="ElectricCharge"/> from <see cref="ElectricCurrent"/> multiplied by <see cref="Duration"/>.</summary>
23+
public static ElectricCharge operator *(ElectricCurrent current, Duration time)
24+
{
25+
return ElectricCharge.FromAmpereHours(current.Amperes * time.Hours);
26+
}
2127
}
2228
}

0 commit comments

Comments
 (0)