Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: AmountOfSubstance #304

Merged
merged 8 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions UnitsNet.Tests/CustomCode/AmountOfSubstanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated (once) by \generate-code.bat, but will not be
// regenerated when it already exists. The purpose of creating this file is to make
// it easier to remember to implement all the unit conversion test cases.
//
// Whenever a new unit is added to this quantity and \generate-code.bat is run,
// the base test class will get a new abstract property and cause a compile error
// in this derived class, reminding the developer to implement the test case
// for the new unit.
//
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
//
// Add CustomCode\Quantities\MyUnit.extra.cs files to add code to generated quantities.
// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior.
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities.
//
// </auto-generated>
//------------------------------------------------------------------------------

// Copyright (c) 2007 Andreas Gullberg Larsen ([email protected]).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;

namespace UnitsNet.Tests.CustomCode
{
public class AmountOfSubstanceTests : AmountOfSubstanceTestsBase
{
protected override double CentimolesInOneMole => 1e2;
protected override double CentipoundMolesInOneMole => 0.002204622621848776 * 1e2;
protected override double DecimolesInOneMole => 1e1;
protected override double DecipoundMolesInOneMole => 0.002204622621848776 * 1e1;
protected override double KilomolesInOneMole => 1e-3;
protected override double KilopoundMolesInOneMole => 0.002204622621848776 * 1e-3;
protected override double MicromolesInOneMole => 1e6;
protected override double MicropoundMolesInOneMole => 0.002204622621848776 * 1e6;
protected override double MillimolesInOneMole => 1e3;
protected override double MillipoundMolesInOneMole => 0.002204622621848776 * 1e3;
protected override double MolesInOneMole => 1;
protected override double NanomolesInOneMole => 1e9;
protected override double NanopoundMolesInOneMole => 0.002204622621848776 * 1e9;
protected override double PoundMolesInOneMole => 0.002204622621848776;
}
}
257 changes: 257 additions & 0 deletions UnitsNet.Tests/GeneratedCode/AmountOfSubstanceTestsBase.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by \generate-code.bat.
//
// Changes to this file will be lost when the code is regenerated.
// The build server regenerates the code before each build and a pre-build
// step will regenerate the code on each local build.
//
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
//
// Add CustomCode\Quantities\MyUnit.extra.cs files to add code to generated quantities.
// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior.
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities.
//
// </auto-generated>
//------------------------------------------------------------------------------

// Copyright (c) 2007 Andreas Gullberg Larsen ([email protected]).
// https://github.com/angularsen/UnitsNet
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using UnitsNet.Units;
using Xunit;

// Disable build warning CS1718: Comparison made to same variable; did you mean to compare something else?
#pragma warning disable 1718

// ReSharper disable once CheckNamespace
namespace UnitsNet.Tests
{
/// <summary>
/// Test of AmountOfSubstance.
/// </summary>
// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class AmountOfSubstanceTestsBase
{
protected abstract double CentimolesInOneMole { get; }
protected abstract double CentipoundMolesInOneMole { get; }
protected abstract double DecimolesInOneMole { get; }
protected abstract double DecipoundMolesInOneMole { get; }
protected abstract double KilomolesInOneMole { get; }
protected abstract double KilopoundMolesInOneMole { get; }
protected abstract double MicromolesInOneMole { get; }
protected abstract double MicropoundMolesInOneMole { get; }
protected abstract double MillimolesInOneMole { get; }
protected abstract double MillipoundMolesInOneMole { get; }
protected abstract double MolesInOneMole { get; }
protected abstract double NanomolesInOneMole { get; }
protected abstract double NanopoundMolesInOneMole { get; }
protected abstract double PoundMolesInOneMole { get; }

// ReSharper disable VirtualMemberNeverOverriden.Global
protected virtual double CentimolesTolerance { get { return 1e-5; } }
protected virtual double CentipoundMolesTolerance { get { return 1e-5; } }
protected virtual double DecimolesTolerance { get { return 1e-5; } }
protected virtual double DecipoundMolesTolerance { get { return 1e-5; } }
protected virtual double KilomolesTolerance { get { return 1e-5; } }
protected virtual double KilopoundMolesTolerance { get { return 1e-5; } }
protected virtual double MicromolesTolerance { get { return 1e-5; } }
protected virtual double MicropoundMolesTolerance { get { return 1e-5; } }
protected virtual double MillimolesTolerance { get { return 1e-5; } }
protected virtual double MillipoundMolesTolerance { get { return 1e-5; } }
protected virtual double MolesTolerance { get { return 1e-5; } }
protected virtual double NanomolesTolerance { get { return 1e-5; } }
protected virtual double NanopoundMolesTolerance { get { return 1e-5; } }
protected virtual double PoundMolesTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global

[Fact]
public void MoleToAmountOfSubstanceUnits()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
AssertEx.EqualTolerance(CentimolesInOneMole, mole.Centimoles, CentimolesTolerance);
AssertEx.EqualTolerance(CentipoundMolesInOneMole, mole.CentipoundMoles, CentipoundMolesTolerance);
AssertEx.EqualTolerance(DecimolesInOneMole, mole.Decimoles, DecimolesTolerance);
AssertEx.EqualTolerance(DecipoundMolesInOneMole, mole.DecipoundMoles, DecipoundMolesTolerance);
AssertEx.EqualTolerance(KilomolesInOneMole, mole.Kilomoles, KilomolesTolerance);
AssertEx.EqualTolerance(KilopoundMolesInOneMole, mole.KilopoundMoles, KilopoundMolesTolerance);
AssertEx.EqualTolerance(MicromolesInOneMole, mole.Micromoles, MicromolesTolerance);
AssertEx.EqualTolerance(MicropoundMolesInOneMole, mole.MicropoundMoles, MicropoundMolesTolerance);
AssertEx.EqualTolerance(MillimolesInOneMole, mole.Millimoles, MillimolesTolerance);
AssertEx.EqualTolerance(MillipoundMolesInOneMole, mole.MillipoundMoles, MillipoundMolesTolerance);
AssertEx.EqualTolerance(MolesInOneMole, mole.Moles, MolesTolerance);
AssertEx.EqualTolerance(NanomolesInOneMole, mole.Nanomoles, NanomolesTolerance);
AssertEx.EqualTolerance(NanopoundMolesInOneMole, mole.NanopoundMoles, NanopoundMolesTolerance);
AssertEx.EqualTolerance(PoundMolesInOneMole, mole.PoundMoles, PoundMolesTolerance);
}

[Fact]
public void FromValueAndUnit()
{
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Centimole).Centimoles, CentimolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.CentipoundMole).CentipoundMoles, CentipoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Decimole).Decimoles, DecimolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.DecipoundMole).DecipoundMoles, DecipoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Kilomole).Kilomoles, KilomolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.KilopoundMole).KilopoundMoles, KilopoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Micromole).Micromoles, MicromolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.MicropoundMole).MicropoundMoles, MicropoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Millimole).Millimoles, MillimolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.MillipoundMole).MillipoundMoles, MillipoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Mole).Moles, MolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.Nanomole).Nanomoles, NanomolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.NanopoundMole).NanopoundMoles, NanopoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.From(1, AmountOfSubstanceUnit.PoundMole).PoundMoles, PoundMolesTolerance);
}

[Fact]
public void As()
{
var mole = AmountOfSubstance.FromMoles(1);
AssertEx.EqualTolerance(CentimolesInOneMole, mole.As(AmountOfSubstanceUnit.Centimole), CentimolesTolerance);
AssertEx.EqualTolerance(CentipoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.CentipoundMole), CentipoundMolesTolerance);
AssertEx.EqualTolerance(DecimolesInOneMole, mole.As(AmountOfSubstanceUnit.Decimole), DecimolesTolerance);
AssertEx.EqualTolerance(DecipoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.DecipoundMole), DecipoundMolesTolerance);
AssertEx.EqualTolerance(KilomolesInOneMole, mole.As(AmountOfSubstanceUnit.Kilomole), KilomolesTolerance);
AssertEx.EqualTolerance(KilopoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.KilopoundMole), KilopoundMolesTolerance);
AssertEx.EqualTolerance(MicromolesInOneMole, mole.As(AmountOfSubstanceUnit.Micromole), MicromolesTolerance);
AssertEx.EqualTolerance(MicropoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.MicropoundMole), MicropoundMolesTolerance);
AssertEx.EqualTolerance(MillimolesInOneMole, mole.As(AmountOfSubstanceUnit.Millimole), MillimolesTolerance);
AssertEx.EqualTolerance(MillipoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.MillipoundMole), MillipoundMolesTolerance);
AssertEx.EqualTolerance(MolesInOneMole, mole.As(AmountOfSubstanceUnit.Mole), MolesTolerance);
AssertEx.EqualTolerance(NanomolesInOneMole, mole.As(AmountOfSubstanceUnit.Nanomole), NanomolesTolerance);
AssertEx.EqualTolerance(NanopoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.NanopoundMole), NanopoundMolesTolerance);
AssertEx.EqualTolerance(PoundMolesInOneMole, mole.As(AmountOfSubstanceUnit.PoundMole), PoundMolesTolerance);
}

[Fact]
public void ConversionRoundTrip()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromCentimoles(mole.Centimoles).Moles, CentimolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromCentipoundMoles(mole.CentipoundMoles).Moles, CentipoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromDecimoles(mole.Decimoles).Moles, DecimolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromDecipoundMoles(mole.DecipoundMoles).Moles, DecipoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromKilomoles(mole.Kilomoles).Moles, KilomolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromKilopoundMoles(mole.KilopoundMoles).Moles, KilopoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromMicromoles(mole.Micromoles).Moles, MicromolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromMicropoundMoles(mole.MicropoundMoles).Moles, MicropoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromMillimoles(mole.Millimoles).Moles, MillimolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromMillipoundMoles(mole.MillipoundMoles).Moles, MillipoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromMoles(mole.Moles).Moles, MolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromNanomoles(mole.Nanomoles).Moles, NanomolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromNanopoundMoles(mole.NanopoundMoles).Moles, NanopoundMolesTolerance);
AssertEx.EqualTolerance(1, AmountOfSubstance.FromPoundMoles(mole.PoundMoles).Moles, PoundMolesTolerance);
}

[Fact]
public void ArithmeticOperators()
{
AmountOfSubstance v = AmountOfSubstance.FromMoles(1);
AssertEx.EqualTolerance(-1, -v.Moles, MolesTolerance);
AssertEx.EqualTolerance(2, (AmountOfSubstance.FromMoles(3)-v).Moles, MolesTolerance);
AssertEx.EqualTolerance(2, (v + v).Moles, MolesTolerance);
AssertEx.EqualTolerance(10, (v*10).Moles, MolesTolerance);
AssertEx.EqualTolerance(10, (10*v).Moles, MolesTolerance);
AssertEx.EqualTolerance(2, (AmountOfSubstance.FromMoles(10)/5).Moles, MolesTolerance);
AssertEx.EqualTolerance(2, AmountOfSubstance.FromMoles(10)/AmountOfSubstance.FromMoles(5), MolesTolerance);
}

[Fact]
public void ComparisonOperators()
{
AmountOfSubstance oneMole = AmountOfSubstance.FromMoles(1);
AmountOfSubstance twoMoles = AmountOfSubstance.FromMoles(2);

Assert.True(oneMole < twoMoles);
Assert.True(oneMole <= twoMoles);
Assert.True(twoMoles > oneMole);
Assert.True(twoMoles >= oneMole);

Assert.False(oneMole > twoMoles);
Assert.False(oneMole >= twoMoles);
Assert.False(twoMoles < oneMole);
Assert.False(twoMoles <= oneMole);
}

[Fact]
public void CompareToIsImplemented()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
Assert.Equal(0, mole.CompareTo(mole));
Assert.True(mole.CompareTo(AmountOfSubstance.Zero) > 0);
Assert.True(AmountOfSubstance.Zero.CompareTo(mole) < 0);
}

[Fact]
public void CompareToThrowsOnTypeMismatch()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
Assert.Throws<ArgumentException>(() => mole.CompareTo(new object()));
}

[Fact]
public void CompareToThrowsOnNull()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
Assert.Throws<ArgumentNullException>(() => mole.CompareTo(null));
}


[Fact]
public void EqualityOperators()
{
AmountOfSubstance a = AmountOfSubstance.FromMoles(1);
AmountOfSubstance b = AmountOfSubstance.FromMoles(2);

// ReSharper disable EqualExpressionComparison
Assert.True(a == a);
Assert.True(a != b);

Assert.False(a == b);
Assert.False(a != a);
// ReSharper restore EqualExpressionComparison
}

[Fact]
public void EqualsIsImplemented()
{
AmountOfSubstance v = AmountOfSubstance.FromMoles(1);
Assert.True(v.Equals(AmountOfSubstance.FromMoles(1)));
Assert.False(v.Equals(AmountOfSubstance.Zero));
}

[Fact]
public void EqualsReturnsFalseOnTypeMismatch()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
Assert.False(mole.Equals(new object()));
}

[Fact]
public void EqualsReturnsFalseOnNull()
{
AmountOfSubstance mole = AmountOfSubstance.FromMoles(1);
Assert.False(mole.Equals(null));
}
}
}
Loading