Skip to content

Commit

Permalink
Add MolarMass Quantity Type (#305)
Browse files Browse the repository at this point in the history
* added MolarMass
* added russian abbreviations

NanogramPerMole to KilogramPerMole convertion tests were failing, so we set the tolerance to 1e-3 like in massTests.
  • Loading branch information
Ferit Tunçer authored and angularsen committed Nov 7, 2017
1 parent 9a50294 commit 6800561
Show file tree
Hide file tree
Showing 8 changed files with 2,894 additions and 0 deletions.
63 changes: 63 additions & 0 deletions UnitsNet.Tests/CustomCode/MolarMassTests.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 MolarMassTests : MolarMassTestsBase
{
protected override double CentigramsPerMoleInOneKilogramPerMole => 1e5;
protected override double DecagramsPerMoleInOneKilogramPerMole => 1e2;
protected override double DecigramsPerMoleInOneKilogramPerMole => 1e4;
protected override double GramsPerMoleInOneKilogramPerMole => 1e3;
protected override double HectogramsPerMoleInOneKilogramPerMole => 10;
protected override double KilogramsPerMoleInOneKilogramPerMole => 1;
protected override double KilopoundsPerMoleInOneKilogramPerMole => 2.2046226218487757e-3;
protected override double MegapoundsPerMoleInOneKilogramPerMole => 2.2046226218487757e-6;
protected override double MicrogramsPerMoleInOneKilogramPerMole => 1e9;
protected override double MilligramsPerMoleInOneKilogramPerMole => 1e6;
protected override double NanogramsPerMoleTolerance => 1e-3;
protected override double NanogramsPerMoleInOneKilogramPerMole => 1e12;
protected override double PoundsPerMoleInOneKilogramPerMole => 2.2046226218487757;
}
}
245 changes: 245 additions & 0 deletions UnitsNet.Tests/GeneratedCode/MolarMassTestsBase.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
//------------------------------------------------------------------------------
// <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 MolarMass.
/// </summary>
// ReSharper disable once PartialTypeWithSinglePart
public abstract partial class MolarMassTestsBase
{
protected abstract double CentigramsPerMoleInOneKilogramPerMole { get; }
protected abstract double DecagramsPerMoleInOneKilogramPerMole { get; }
protected abstract double DecigramsPerMoleInOneKilogramPerMole { get; }
protected abstract double GramsPerMoleInOneKilogramPerMole { get; }
protected abstract double HectogramsPerMoleInOneKilogramPerMole { get; }
protected abstract double KilogramsPerMoleInOneKilogramPerMole { get; }
protected abstract double KilopoundsPerMoleInOneKilogramPerMole { get; }
protected abstract double MegapoundsPerMoleInOneKilogramPerMole { get; }
protected abstract double MicrogramsPerMoleInOneKilogramPerMole { get; }
protected abstract double MilligramsPerMoleInOneKilogramPerMole { get; }
protected abstract double NanogramsPerMoleInOneKilogramPerMole { get; }
protected abstract double PoundsPerMoleInOneKilogramPerMole { get; }

// ReSharper disable VirtualMemberNeverOverriden.Global
protected virtual double CentigramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double DecagramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double DecigramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double GramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double HectogramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double KilogramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double KilopoundsPerMoleTolerance { get { return 1e-5; } }
protected virtual double MegapoundsPerMoleTolerance { get { return 1e-5; } }
protected virtual double MicrogramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double MilligramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double NanogramsPerMoleTolerance { get { return 1e-5; } }
protected virtual double PoundsPerMoleTolerance { get { return 1e-5; } }
// ReSharper restore VirtualMemberNeverOverriden.Global

[Fact]
public void KilogramPerMoleToMolarMassUnits()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
AssertEx.EqualTolerance(CentigramsPerMoleInOneKilogramPerMole, kilogrampermole.CentigramsPerMole, CentigramsPerMoleTolerance);
AssertEx.EqualTolerance(DecagramsPerMoleInOneKilogramPerMole, kilogrampermole.DecagramsPerMole, DecagramsPerMoleTolerance);
AssertEx.EqualTolerance(DecigramsPerMoleInOneKilogramPerMole, kilogrampermole.DecigramsPerMole, DecigramsPerMoleTolerance);
AssertEx.EqualTolerance(GramsPerMoleInOneKilogramPerMole, kilogrampermole.GramsPerMole, GramsPerMoleTolerance);
AssertEx.EqualTolerance(HectogramsPerMoleInOneKilogramPerMole, kilogrampermole.HectogramsPerMole, HectogramsPerMoleTolerance);
AssertEx.EqualTolerance(KilogramsPerMoleInOneKilogramPerMole, kilogrampermole.KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(KilopoundsPerMoleInOneKilogramPerMole, kilogrampermole.KilopoundsPerMole, KilopoundsPerMoleTolerance);
AssertEx.EqualTolerance(MegapoundsPerMoleInOneKilogramPerMole, kilogrampermole.MegapoundsPerMole, MegapoundsPerMoleTolerance);
AssertEx.EqualTolerance(MicrogramsPerMoleInOneKilogramPerMole, kilogrampermole.MicrogramsPerMole, MicrogramsPerMoleTolerance);
AssertEx.EqualTolerance(MilligramsPerMoleInOneKilogramPerMole, kilogrampermole.MilligramsPerMole, MilligramsPerMoleTolerance);
AssertEx.EqualTolerance(NanogramsPerMoleInOneKilogramPerMole, kilogrampermole.NanogramsPerMole, NanogramsPerMoleTolerance);
AssertEx.EqualTolerance(PoundsPerMoleInOneKilogramPerMole, kilogrampermole.PoundsPerMole, PoundsPerMoleTolerance);
}

[Fact]
public void FromValueAndUnit()
{
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.CentigramPerMole).CentigramsPerMole, CentigramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.DecagramPerMole).DecagramsPerMole, DecagramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.DecigramPerMole).DecigramsPerMole, DecigramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.GramPerMole).GramsPerMole, GramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.HectogramPerMole).HectogramsPerMole, HectogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.KilogramPerMole).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.KilopoundPerMole).KilopoundsPerMole, KilopoundsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.MegapoundPerMole).MegapoundsPerMole, MegapoundsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.MicrogramPerMole).MicrogramsPerMole, MicrogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.MilligramPerMole).MilligramsPerMole, MilligramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.NanogramPerMole).NanogramsPerMole, NanogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.From(1, MolarMassUnit.PoundPerMole).PoundsPerMole, PoundsPerMoleTolerance);
}

[Fact]
public void As()
{
var kilogrampermole = MolarMass.FromKilogramsPerMole(1);
AssertEx.EqualTolerance(CentigramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.CentigramPerMole), CentigramsPerMoleTolerance);
AssertEx.EqualTolerance(DecagramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.DecagramPerMole), DecagramsPerMoleTolerance);
AssertEx.EqualTolerance(DecigramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.DecigramPerMole), DecigramsPerMoleTolerance);
AssertEx.EqualTolerance(GramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.GramPerMole), GramsPerMoleTolerance);
AssertEx.EqualTolerance(HectogramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.HectogramPerMole), HectogramsPerMoleTolerance);
AssertEx.EqualTolerance(KilogramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.KilogramPerMole), KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(KilopoundsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.KilopoundPerMole), KilopoundsPerMoleTolerance);
AssertEx.EqualTolerance(MegapoundsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.MegapoundPerMole), MegapoundsPerMoleTolerance);
AssertEx.EqualTolerance(MicrogramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.MicrogramPerMole), MicrogramsPerMoleTolerance);
AssertEx.EqualTolerance(MilligramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.MilligramPerMole), MilligramsPerMoleTolerance);
AssertEx.EqualTolerance(NanogramsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.NanogramPerMole), NanogramsPerMoleTolerance);
AssertEx.EqualTolerance(PoundsPerMoleInOneKilogramPerMole, kilogrampermole.As(MolarMassUnit.PoundPerMole), PoundsPerMoleTolerance);
}

[Fact]
public void ConversionRoundTrip()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
AssertEx.EqualTolerance(1, MolarMass.FromCentigramsPerMole(kilogrampermole.CentigramsPerMole).KilogramsPerMole, CentigramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromDecagramsPerMole(kilogrampermole.DecagramsPerMole).KilogramsPerMole, DecagramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromDecigramsPerMole(kilogrampermole.DecigramsPerMole).KilogramsPerMole, DecigramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromGramsPerMole(kilogrampermole.GramsPerMole).KilogramsPerMole, GramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromHectogramsPerMole(kilogrampermole.HectogramsPerMole).KilogramsPerMole, HectogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromKilogramsPerMole(kilogrampermole.KilogramsPerMole).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromKilopoundsPerMole(kilogrampermole.KilopoundsPerMole).KilogramsPerMole, KilopoundsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromMegapoundsPerMole(kilogrampermole.MegapoundsPerMole).KilogramsPerMole, MegapoundsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromMicrogramsPerMole(kilogrampermole.MicrogramsPerMole).KilogramsPerMole, MicrogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromMilligramsPerMole(kilogrampermole.MilligramsPerMole).KilogramsPerMole, MilligramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromNanogramsPerMole(kilogrampermole.NanogramsPerMole).KilogramsPerMole, NanogramsPerMoleTolerance);
AssertEx.EqualTolerance(1, MolarMass.FromPoundsPerMole(kilogrampermole.PoundsPerMole).KilogramsPerMole, PoundsPerMoleTolerance);
}

[Fact]
public void ArithmeticOperators()
{
MolarMass v = MolarMass.FromKilogramsPerMole(1);
AssertEx.EqualTolerance(-1, -v.KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(2, (MolarMass.FromKilogramsPerMole(3)-v).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(2, (v + v).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(10, (v*10).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(10, (10*v).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(2, (MolarMass.FromKilogramsPerMole(10)/5).KilogramsPerMole, KilogramsPerMoleTolerance);
AssertEx.EqualTolerance(2, MolarMass.FromKilogramsPerMole(10)/MolarMass.FromKilogramsPerMole(5), KilogramsPerMoleTolerance);
}

[Fact]
public void ComparisonOperators()
{
MolarMass oneKilogramPerMole = MolarMass.FromKilogramsPerMole(1);
MolarMass twoKilogramsPerMole = MolarMass.FromKilogramsPerMole(2);

Assert.True(oneKilogramPerMole < twoKilogramsPerMole);
Assert.True(oneKilogramPerMole <= twoKilogramsPerMole);
Assert.True(twoKilogramsPerMole > oneKilogramPerMole);
Assert.True(twoKilogramsPerMole >= oneKilogramPerMole);

Assert.False(oneKilogramPerMole > twoKilogramsPerMole);
Assert.False(oneKilogramPerMole >= twoKilogramsPerMole);
Assert.False(twoKilogramsPerMole < oneKilogramPerMole);
Assert.False(twoKilogramsPerMole <= oneKilogramPerMole);
}

[Fact]
public void CompareToIsImplemented()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
Assert.Equal(0, kilogrampermole.CompareTo(kilogrampermole));
Assert.True(kilogrampermole.CompareTo(MolarMass.Zero) > 0);
Assert.True(MolarMass.Zero.CompareTo(kilogrampermole) < 0);
}

[Fact]
public void CompareToThrowsOnTypeMismatch()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
Assert.Throws<ArgumentException>(() => kilogrampermole.CompareTo(new object()));
}

[Fact]
public void CompareToThrowsOnNull()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
Assert.Throws<ArgumentNullException>(() => kilogrampermole.CompareTo(null));
}


[Fact]
public void EqualityOperators()
{
MolarMass a = MolarMass.FromKilogramsPerMole(1);
MolarMass b = MolarMass.FromKilogramsPerMole(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()
{
MolarMass v = MolarMass.FromKilogramsPerMole(1);
Assert.True(v.Equals(MolarMass.FromKilogramsPerMole(1)));
Assert.False(v.Equals(MolarMass.Zero));
}

[Fact]
public void EqualsReturnsFalseOnTypeMismatch()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
Assert.False(kilogrampermole.Equals(new object()));
}

[Fact]
public void EqualsReturnsFalseOnNull()
{
MolarMass kilogrampermole = MolarMass.FromKilogramsPerMole(1);
Assert.False(kilogrampermole.Equals(null));
}
}
}
Loading

0 comments on commit 6800561

Please sign in to comment.