Skip to content

Commit a866b3d

Browse files
dschuermansangularsen
authored andcommitted
Add RotationalStiffness (#414)
* Added RotationalStiffness unit * Updated format, changed XmlDoc to link to wikipedia, updated abbreviation to correct syntax. Added operators to get torque from rot stiff and angle, angle from torque and rot stiff and rot stiff from torque and angle
1 parent 01065cd commit a866b3d

File tree

10 files changed

+1308
-1
lines changed

10 files changed

+1308
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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) 2013 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 RotationalStiffnessTests : RotationalStiffnessTestsBase
48+
{
49+
protected override double NewtonMetersPerRadianInOneNewtonMeterPerRadian => 1;
50+
protected override double MeganewtonMetersPerRadianInOneNewtonMeterPerRadian => 1e-6;
51+
protected override double KilonewtonMetersPerRadianInOneNewtonMeterPerRadian => 1e-3;
52+
}
53+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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) 2013 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 RotationalStiffness.
51+
/// </summary>
52+
// ReSharper disable once PartialTypeWithSinglePart
53+
public abstract partial class RotationalStiffnessTestsBase
54+
{
55+
protected abstract double KilonewtonMetersPerRadianInOneNewtonMeterPerRadian { get; }
56+
protected abstract double MeganewtonMetersPerRadianInOneNewtonMeterPerRadian { get; }
57+
protected abstract double NewtonMetersPerRadianInOneNewtonMeterPerRadian { get; }
58+
59+
// ReSharper disable VirtualMemberNeverOverriden.Global
60+
protected virtual double KilonewtonMetersPerRadianTolerance { get { return 1e-5; } }
61+
protected virtual double MeganewtonMetersPerRadianTolerance { get { return 1e-5; } }
62+
protected virtual double NewtonMetersPerRadianTolerance { get { return 1e-5; } }
63+
// ReSharper restore VirtualMemberNeverOverriden.Global
64+
65+
[Fact]
66+
public void NewtonMeterPerRadianToRotationalStiffnessUnits()
67+
{
68+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
69+
AssertEx.EqualTolerance(KilonewtonMetersPerRadianInOneNewtonMeterPerRadian, newtonmeterperradian.KilonewtonMetersPerRadian, KilonewtonMetersPerRadianTolerance);
70+
AssertEx.EqualTolerance(MeganewtonMetersPerRadianInOneNewtonMeterPerRadian, newtonmeterperradian.MeganewtonMetersPerRadian, MeganewtonMetersPerRadianTolerance);
71+
AssertEx.EqualTolerance(NewtonMetersPerRadianInOneNewtonMeterPerRadian, newtonmeterperradian.NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
72+
}
73+
74+
[Fact]
75+
public void FromValueAndUnit()
76+
{
77+
AssertEx.EqualTolerance(1, RotationalStiffness.From(1, RotationalStiffnessUnit.KilonewtonMeterPerRadian).KilonewtonMetersPerRadian, KilonewtonMetersPerRadianTolerance);
78+
AssertEx.EqualTolerance(1, RotationalStiffness.From(1, RotationalStiffnessUnit.MeganewtonMeterPerRadian).MeganewtonMetersPerRadian, MeganewtonMetersPerRadianTolerance);
79+
AssertEx.EqualTolerance(1, RotationalStiffness.From(1, RotationalStiffnessUnit.NewtonMeterPerRadian).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
80+
}
81+
82+
[Fact]
83+
public void As()
84+
{
85+
var newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
86+
AssertEx.EqualTolerance(KilonewtonMetersPerRadianInOneNewtonMeterPerRadian, newtonmeterperradian.As(RotationalStiffnessUnit.KilonewtonMeterPerRadian), KilonewtonMetersPerRadianTolerance);
87+
AssertEx.EqualTolerance(MeganewtonMetersPerRadianInOneNewtonMeterPerRadian, newtonmeterperradian.As(RotationalStiffnessUnit.MeganewtonMeterPerRadian), MeganewtonMetersPerRadianTolerance);
88+
AssertEx.EqualTolerance(NewtonMetersPerRadianInOneNewtonMeterPerRadian, newtonmeterperradian.As(RotationalStiffnessUnit.NewtonMeterPerRadian), NewtonMetersPerRadianTolerance);
89+
}
90+
91+
[Fact]
92+
public void ConversionRoundTrip()
93+
{
94+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
95+
AssertEx.EqualTolerance(1, RotationalStiffness.FromKilonewtonMetersPerRadian(newtonmeterperradian.KilonewtonMetersPerRadian).NewtonMetersPerRadian, KilonewtonMetersPerRadianTolerance);
96+
AssertEx.EqualTolerance(1, RotationalStiffness.FromMeganewtonMetersPerRadian(newtonmeterperradian.MeganewtonMetersPerRadian).NewtonMetersPerRadian, MeganewtonMetersPerRadianTolerance);
97+
AssertEx.EqualTolerance(1, RotationalStiffness.FromNewtonMetersPerRadian(newtonmeterperradian.NewtonMetersPerRadian).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
98+
}
99+
100+
[Fact]
101+
public void ArithmeticOperators()
102+
{
103+
RotationalStiffness v = RotationalStiffness.FromNewtonMetersPerRadian(1);
104+
AssertEx.EqualTolerance(-1, -v.NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
105+
AssertEx.EqualTolerance(2, (RotationalStiffness.FromNewtonMetersPerRadian(3)-v).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
106+
AssertEx.EqualTolerance(2, (v + v).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
107+
AssertEx.EqualTolerance(10, (v*10).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
108+
AssertEx.EqualTolerance(10, (10*v).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
109+
AssertEx.EqualTolerance(2, (RotationalStiffness.FromNewtonMetersPerRadian(10)/5).NewtonMetersPerRadian, NewtonMetersPerRadianTolerance);
110+
AssertEx.EqualTolerance(2, RotationalStiffness.FromNewtonMetersPerRadian(10)/RotationalStiffness.FromNewtonMetersPerRadian(5), NewtonMetersPerRadianTolerance);
111+
}
112+
113+
[Fact]
114+
public void ComparisonOperators()
115+
{
116+
RotationalStiffness oneNewtonMeterPerRadian = RotationalStiffness.FromNewtonMetersPerRadian(1);
117+
RotationalStiffness twoNewtonMetersPerRadian = RotationalStiffness.FromNewtonMetersPerRadian(2);
118+
119+
Assert.True(oneNewtonMeterPerRadian < twoNewtonMetersPerRadian);
120+
Assert.True(oneNewtonMeterPerRadian <= twoNewtonMetersPerRadian);
121+
Assert.True(twoNewtonMetersPerRadian > oneNewtonMeterPerRadian);
122+
Assert.True(twoNewtonMetersPerRadian >= oneNewtonMeterPerRadian);
123+
124+
Assert.False(oneNewtonMeterPerRadian > twoNewtonMetersPerRadian);
125+
Assert.False(oneNewtonMeterPerRadian >= twoNewtonMetersPerRadian);
126+
Assert.False(twoNewtonMetersPerRadian < oneNewtonMeterPerRadian);
127+
Assert.False(twoNewtonMetersPerRadian <= oneNewtonMeterPerRadian);
128+
}
129+
130+
[Fact]
131+
public void CompareToIsImplemented()
132+
{
133+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
134+
Assert.Equal(0, newtonmeterperradian.CompareTo(newtonmeterperradian));
135+
Assert.True(newtonmeterperradian.CompareTo(RotationalStiffness.Zero) > 0);
136+
Assert.True(RotationalStiffness.Zero.CompareTo(newtonmeterperradian) < 0);
137+
}
138+
139+
[Fact]
140+
public void CompareToThrowsOnTypeMismatch()
141+
{
142+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
143+
Assert.Throws<ArgumentException>(() => newtonmeterperradian.CompareTo(new object()));
144+
}
145+
146+
[Fact]
147+
public void CompareToThrowsOnNull()
148+
{
149+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
150+
Assert.Throws<ArgumentNullException>(() => newtonmeterperradian.CompareTo(null));
151+
}
152+
153+
154+
[Fact]
155+
public void EqualityOperators()
156+
{
157+
RotationalStiffness a = RotationalStiffness.FromNewtonMetersPerRadian(1);
158+
RotationalStiffness b = RotationalStiffness.FromNewtonMetersPerRadian(2);
159+
160+
// ReSharper disable EqualExpressionComparison
161+
Assert.True(a == a);
162+
Assert.True(a != b);
163+
164+
Assert.False(a == b);
165+
Assert.False(a != a);
166+
// ReSharper restore EqualExpressionComparison
167+
}
168+
169+
[Fact]
170+
public void EqualsIsImplemented()
171+
{
172+
RotationalStiffness v = RotationalStiffness.FromNewtonMetersPerRadian(1);
173+
Assert.True(v.Equals(RotationalStiffness.FromNewtonMetersPerRadian(1), RotationalStiffness.FromNewtonMetersPerRadian(NewtonMetersPerRadianTolerance)));
174+
Assert.False(v.Equals(RotationalStiffness.Zero, RotationalStiffness.FromNewtonMetersPerRadian(NewtonMetersPerRadianTolerance)));
175+
}
176+
177+
[Fact]
178+
public void EqualsReturnsFalseOnTypeMismatch()
179+
{
180+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
181+
Assert.False(newtonmeterperradian.Equals(new object()));
182+
}
183+
184+
[Fact]
185+
public void EqualsReturnsFalseOnNull()
186+
{
187+
RotationalStiffness newtonmeterperradian = RotationalStiffness.FromNewtonMetersPerRadian(1);
188+
Assert.False(newtonmeterperradian.Equals(null));
189+
}
190+
}
191+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
23+
namespace UnitsNet
24+
{
25+
// Windows Runtime Component has constraints on public types: https://msdn.microsoft.com/en-us/library/br230301.aspx#Declaring types in Windows Runtime Components
26+
// Public structures can't have any members other than public fields, and those fields must be value types or strings.
27+
// Public classes must be sealed (NotInheritable in Visual Basic). If your programming model requires polymorphism, you can create a public interface and implement that interface on the classes that must be polymorphic.
28+
#if WINDOWS_UWP
29+
public sealed partial class RotationalStiffness
30+
#else
31+
public partial struct RotationalStiffness
32+
#endif
33+
{
34+
// Windows Runtime Component does not allow operator overloads: https://msdn.microsoft.com/en-us/library/br230301.aspx
35+
#if !WINDOWS_UWP
36+
public static Torque operator *(RotationalStiffness rotationalStiffness, Angle angle)
37+
{
38+
return Torque.FromNewtonMeters(rotationalStiffness.NewtonMetersPerRadian * angle.Radians);
39+
}
40+
#endif
41+
}
42+
}

UnitsNet/CustomCode/Quantities/Torque.extra.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public partial struct Torque
4242
{
4343
return Length.FromMeters(torque.NewtonMeters / force.Newtons);
4444
}
45+
46+
public static RotationalStiffness operator /(Torque torque, Angle angle)
47+
{
48+
return RotationalStiffness.FromNewtonMetersPerRadian(torque.NewtonMeters / angle.Radians);
49+
}
50+
51+
public static Angle operator /(Torque torque, RotationalStiffness rotationalStiffness)
52+
{
53+
return Angle.FromRadians(torque.NewtonMeters / rotationalStiffness.NewtonMetersPerRadian);
54+
}
4555
#endif
4656
}
47-
}
57+
}

0 commit comments

Comments
 (0)