Skip to content

Commit c1f83dd

Browse files
Ferit Tunçerangularsen
Ferit Tunçer
authored andcommitted
Add quantity Entropy (#312)
1 parent 90d5f93 commit c1f83dd

File tree

8 files changed

+2090
-0
lines changed

8 files changed

+2090
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\MyUnit.extra.cs files to add code to generated quantities.
15+
// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior.
16+
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities.
17+
//
18+
// </auto-generated>
19+
//------------------------------------------------------------------------------
20+
21+
// Copyright (c) 2007 Andreas Gullberg Larsen ([email protected]).
22+
// https://github.com/angularsen/UnitsNet
23+
//
24+
// Permission is hereby granted, free of charge, to any person obtaining a copy
25+
// of this software and associated documentation files (the "Software"), to deal
26+
// in the Software without restriction, including without limitation the rights
27+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
// copies of the Software, and to permit persons to whom the Software is
29+
// furnished to do so, subject to the following conditions:
30+
//
31+
// The above copyright notice and this permission notice shall be included in
32+
// all copies or substantial portions of the Software.
33+
//
34+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40+
// THE SOFTWARE.
41+
42+
43+
using System;
44+
45+
namespace UnitsNet.Tests.CustomCode
46+
{
47+
public class EntropyTests : EntropyTestsBase
48+
{
49+
protected override double CaloriesPerKelvinInOneJoulePerKelvin => 0.239006;
50+
protected override double JoulesPerDegreeCelsiusInOneJoulePerKelvin => 1e0;
51+
protected override double JoulesPerKelvinInOneJoulePerKelvin => 1e0;
52+
protected override double KilocaloriesPerKelvinInOneJoulePerKelvin => 0.000239006;
53+
protected override double KilojoulesPerDegreeCelsiusInOneJoulePerKelvin => 1e-3;
54+
protected override double KilojoulesPerKelvinInOneJoulePerKelvin => 1e-3;
55+
protected override double MegajoulesPerKelvinInOneJoulePerKelvin => 1e-6;
56+
}
57+
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by \generate-code.bat.
4+
//
5+
// Changes to this file will be lost when the code is regenerated.
6+
// The build server regenerates the code before each build and a pre-build
7+
// step will regenerate the code on each local build.
8+
//
9+
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
10+
//
11+
// Add CustomCode\Quantities\MyUnit.extra.cs files to add code to generated quantities.
12+
// Add Extensions\MyUnitExtensions.cs to decorate quantities with new behavior.
13+
// Add UnitDefinitions\MyUnit.json and run GeneratUnits.bat to generate new units or quantities.
14+
//
15+
// </auto-generated>
16+
//------------------------------------------------------------------------------
17+
18+
// Copyright (c) 2007 Andreas Gullberg Larsen ([email protected]).
19+
// https://github.com/angularsen/UnitsNet
20+
//
21+
// Permission is hereby granted, free of charge, to any person obtaining a copy
22+
// of this software and associated documentation files (the "Software"), to deal
23+
// in the Software without restriction, including without limitation the rights
24+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25+
// copies of the Software, and to permit persons to whom the Software is
26+
// furnished to do so, subject to the following conditions:
27+
//
28+
// The above copyright notice and this permission notice shall be included in
29+
// all copies or substantial portions of the Software.
30+
//
31+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37+
// THE SOFTWARE.
38+
39+
using System;
40+
using UnitsNet.Units;
41+
using Xunit;
42+
43+
// Disable build warning CS1718: Comparison made to same variable; did you mean to compare something else?
44+
#pragma warning disable 1718
45+
46+
// ReSharper disable once CheckNamespace
47+
namespace UnitsNet.Tests
48+
{
49+
/// <summary>
50+
/// Test of Entropy.
51+
/// </summary>
52+
// ReSharper disable once PartialTypeWithSinglePart
53+
public abstract partial class EntropyTestsBase
54+
{
55+
protected abstract double CaloriesPerKelvinInOneJoulePerKelvin { get; }
56+
protected abstract double JoulesPerDegreeCelsiusInOneJoulePerKelvin { get; }
57+
protected abstract double JoulesPerKelvinInOneJoulePerKelvin { get; }
58+
protected abstract double KilocaloriesPerKelvinInOneJoulePerKelvin { get; }
59+
protected abstract double KilojoulesPerDegreeCelsiusInOneJoulePerKelvin { get; }
60+
protected abstract double KilojoulesPerKelvinInOneJoulePerKelvin { get; }
61+
protected abstract double MegajoulesPerKelvinInOneJoulePerKelvin { get; }
62+
63+
// ReSharper disable VirtualMemberNeverOverriden.Global
64+
protected virtual double CaloriesPerKelvinTolerance { get { return 1e-5; } }
65+
protected virtual double JoulesPerDegreeCelsiusTolerance { get { return 1e-5; } }
66+
protected virtual double JoulesPerKelvinTolerance { get { return 1e-5; } }
67+
protected virtual double KilocaloriesPerKelvinTolerance { get { return 1e-5; } }
68+
protected virtual double KilojoulesPerDegreeCelsiusTolerance { get { return 1e-5; } }
69+
protected virtual double KilojoulesPerKelvinTolerance { get { return 1e-5; } }
70+
protected virtual double MegajoulesPerKelvinTolerance { get { return 1e-5; } }
71+
// ReSharper restore VirtualMemberNeverOverriden.Global
72+
73+
[Fact]
74+
public void JoulePerKelvinToEntropyUnits()
75+
{
76+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
77+
AssertEx.EqualTolerance(CaloriesPerKelvinInOneJoulePerKelvin, jouleperkelvin.CaloriesPerKelvin, CaloriesPerKelvinTolerance);
78+
AssertEx.EqualTolerance(JoulesPerDegreeCelsiusInOneJoulePerKelvin, jouleperkelvin.JoulesPerDegreeCelsius, JoulesPerDegreeCelsiusTolerance);
79+
AssertEx.EqualTolerance(JoulesPerKelvinInOneJoulePerKelvin, jouleperkelvin.JoulesPerKelvin, JoulesPerKelvinTolerance);
80+
AssertEx.EqualTolerance(KilocaloriesPerKelvinInOneJoulePerKelvin, jouleperkelvin.KilocaloriesPerKelvin, KilocaloriesPerKelvinTolerance);
81+
AssertEx.EqualTolerance(KilojoulesPerDegreeCelsiusInOneJoulePerKelvin, jouleperkelvin.KilojoulesPerDegreeCelsius, KilojoulesPerDegreeCelsiusTolerance);
82+
AssertEx.EqualTolerance(KilojoulesPerKelvinInOneJoulePerKelvin, jouleperkelvin.KilojoulesPerKelvin, KilojoulesPerKelvinTolerance);
83+
AssertEx.EqualTolerance(MegajoulesPerKelvinInOneJoulePerKelvin, jouleperkelvin.MegajoulesPerKelvin, MegajoulesPerKelvinTolerance);
84+
}
85+
86+
[Fact]
87+
public void FromValueAndUnit()
88+
{
89+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.CaloriePerKelvin).CaloriesPerKelvin, CaloriesPerKelvinTolerance);
90+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.JoulePerDegreeCelsius).JoulesPerDegreeCelsius, JoulesPerDegreeCelsiusTolerance);
91+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.JoulePerKelvin).JoulesPerKelvin, JoulesPerKelvinTolerance);
92+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.KilocaloriePerKelvin).KilocaloriesPerKelvin, KilocaloriesPerKelvinTolerance);
93+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.KilojoulePerDegreeCelsius).KilojoulesPerDegreeCelsius, KilojoulesPerDegreeCelsiusTolerance);
94+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.KilojoulePerKelvin).KilojoulesPerKelvin, KilojoulesPerKelvinTolerance);
95+
AssertEx.EqualTolerance(1, Entropy.From(1, EntropyUnit.MegajoulePerKelvin).MegajoulesPerKelvin, MegajoulesPerKelvinTolerance);
96+
}
97+
98+
[Fact]
99+
public void As()
100+
{
101+
var jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
102+
AssertEx.EqualTolerance(CaloriesPerKelvinInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.CaloriePerKelvin), CaloriesPerKelvinTolerance);
103+
AssertEx.EqualTolerance(JoulesPerDegreeCelsiusInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.JoulePerDegreeCelsius), JoulesPerDegreeCelsiusTolerance);
104+
AssertEx.EqualTolerance(JoulesPerKelvinInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.JoulePerKelvin), JoulesPerKelvinTolerance);
105+
AssertEx.EqualTolerance(KilocaloriesPerKelvinInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.KilocaloriePerKelvin), KilocaloriesPerKelvinTolerance);
106+
AssertEx.EqualTolerance(KilojoulesPerDegreeCelsiusInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.KilojoulePerDegreeCelsius), KilojoulesPerDegreeCelsiusTolerance);
107+
AssertEx.EqualTolerance(KilojoulesPerKelvinInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.KilojoulePerKelvin), KilojoulesPerKelvinTolerance);
108+
AssertEx.EqualTolerance(MegajoulesPerKelvinInOneJoulePerKelvin, jouleperkelvin.As(EntropyUnit.MegajoulePerKelvin), MegajoulesPerKelvinTolerance);
109+
}
110+
111+
[Fact]
112+
public void ConversionRoundTrip()
113+
{
114+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
115+
AssertEx.EqualTolerance(1, Entropy.FromCaloriesPerKelvin(jouleperkelvin.CaloriesPerKelvin).JoulesPerKelvin, CaloriesPerKelvinTolerance);
116+
AssertEx.EqualTolerance(1, Entropy.FromJoulesPerDegreeCelsius(jouleperkelvin.JoulesPerDegreeCelsius).JoulesPerKelvin, JoulesPerDegreeCelsiusTolerance);
117+
AssertEx.EqualTolerance(1, Entropy.FromJoulesPerKelvin(jouleperkelvin.JoulesPerKelvin).JoulesPerKelvin, JoulesPerKelvinTolerance);
118+
AssertEx.EqualTolerance(1, Entropy.FromKilocaloriesPerKelvin(jouleperkelvin.KilocaloriesPerKelvin).JoulesPerKelvin, KilocaloriesPerKelvinTolerance);
119+
AssertEx.EqualTolerance(1, Entropy.FromKilojoulesPerDegreeCelsius(jouleperkelvin.KilojoulesPerDegreeCelsius).JoulesPerKelvin, KilojoulesPerDegreeCelsiusTolerance);
120+
AssertEx.EqualTolerance(1, Entropy.FromKilojoulesPerKelvin(jouleperkelvin.KilojoulesPerKelvin).JoulesPerKelvin, KilojoulesPerKelvinTolerance);
121+
AssertEx.EqualTolerance(1, Entropy.FromMegajoulesPerKelvin(jouleperkelvin.MegajoulesPerKelvin).JoulesPerKelvin, MegajoulesPerKelvinTolerance);
122+
}
123+
124+
[Fact]
125+
public void ArithmeticOperators()
126+
{
127+
Entropy v = Entropy.FromJoulesPerKelvin(1);
128+
AssertEx.EqualTolerance(-1, -v.JoulesPerKelvin, JoulesPerKelvinTolerance);
129+
AssertEx.EqualTolerance(2, (Entropy.FromJoulesPerKelvin(3)-v).JoulesPerKelvin, JoulesPerKelvinTolerance);
130+
AssertEx.EqualTolerance(2, (v + v).JoulesPerKelvin, JoulesPerKelvinTolerance);
131+
AssertEx.EqualTolerance(10, (v*10).JoulesPerKelvin, JoulesPerKelvinTolerance);
132+
AssertEx.EqualTolerance(10, (10*v).JoulesPerKelvin, JoulesPerKelvinTolerance);
133+
AssertEx.EqualTolerance(2, (Entropy.FromJoulesPerKelvin(10)/5).JoulesPerKelvin, JoulesPerKelvinTolerance);
134+
AssertEx.EqualTolerance(2, Entropy.FromJoulesPerKelvin(10)/Entropy.FromJoulesPerKelvin(5), JoulesPerKelvinTolerance);
135+
}
136+
137+
[Fact]
138+
public void ComparisonOperators()
139+
{
140+
Entropy oneJoulePerKelvin = Entropy.FromJoulesPerKelvin(1);
141+
Entropy twoJoulesPerKelvin = Entropy.FromJoulesPerKelvin(2);
142+
143+
Assert.True(oneJoulePerKelvin < twoJoulesPerKelvin);
144+
Assert.True(oneJoulePerKelvin <= twoJoulesPerKelvin);
145+
Assert.True(twoJoulesPerKelvin > oneJoulePerKelvin);
146+
Assert.True(twoJoulesPerKelvin >= oneJoulePerKelvin);
147+
148+
Assert.False(oneJoulePerKelvin > twoJoulesPerKelvin);
149+
Assert.False(oneJoulePerKelvin >= twoJoulesPerKelvin);
150+
Assert.False(twoJoulesPerKelvin < oneJoulePerKelvin);
151+
Assert.False(twoJoulesPerKelvin <= oneJoulePerKelvin);
152+
}
153+
154+
[Fact]
155+
public void CompareToIsImplemented()
156+
{
157+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
158+
Assert.Equal(0, jouleperkelvin.CompareTo(jouleperkelvin));
159+
Assert.True(jouleperkelvin.CompareTo(Entropy.Zero) > 0);
160+
Assert.True(Entropy.Zero.CompareTo(jouleperkelvin) < 0);
161+
}
162+
163+
[Fact]
164+
public void CompareToThrowsOnTypeMismatch()
165+
{
166+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
167+
Assert.Throws<ArgumentException>(() => jouleperkelvin.CompareTo(new object()));
168+
}
169+
170+
[Fact]
171+
public void CompareToThrowsOnNull()
172+
{
173+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
174+
Assert.Throws<ArgumentNullException>(() => jouleperkelvin.CompareTo(null));
175+
}
176+
177+
178+
[Fact]
179+
public void EqualityOperators()
180+
{
181+
Entropy a = Entropy.FromJoulesPerKelvin(1);
182+
Entropy b = Entropy.FromJoulesPerKelvin(2);
183+
184+
// ReSharper disable EqualExpressionComparison
185+
Assert.True(a == a);
186+
Assert.True(a != b);
187+
188+
Assert.False(a == b);
189+
Assert.False(a != a);
190+
// ReSharper restore EqualExpressionComparison
191+
}
192+
193+
[Fact]
194+
public void EqualsIsImplemented()
195+
{
196+
Entropy v = Entropy.FromJoulesPerKelvin(1);
197+
Assert.True(v.Equals(Entropy.FromJoulesPerKelvin(1)));
198+
Assert.False(v.Equals(Entropy.Zero));
199+
}
200+
201+
[Fact]
202+
public void EqualsReturnsFalseOnTypeMismatch()
203+
{
204+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
205+
Assert.False(jouleperkelvin.Equals(new object()));
206+
}
207+
208+
[Fact]
209+
public void EqualsReturnsFalseOnNull()
210+
{
211+
Entropy jouleperkelvin = Entropy.FromJoulesPerKelvin(1);
212+
Assert.False(jouleperkelvin.Equals(null));
213+
}
214+
}
215+
}

0 commit comments

Comments
 (0)