From 4c8bc05d0e6155f892af5433ff343e7fc73c5485 Mon Sep 17 00:00:00 2001 From: Johan Larsson Date: Thu, 6 Nov 2014 17:38:53 +0100 Subject: [PATCH] Find Inversion next --- Gu.Units.Generator.Tests/QuantityTests.cs | 33 +- Gu.Units.Generator/Descriptors/Quantity.cs | 11 +- Gu.Units.Generator/GeneratorSettings.xml | 28 +- .../Templates/QuantityGenerator.txt | 5 +- .../Templates/UnitGenerator.txt | 3 + Gu.Units/Flexibility.generated.cs | 412 ++++++++++++++++++ Gu.Units/FlexibilityUnit.generated.cs | 73 ++++ Gu.Units/Gu.Units.csproj | 6 + 8 files changed, 565 insertions(+), 6 deletions(-) create mode 100644 Gu.Units/Flexibility.generated.cs create mode 100644 Gu.Units/FlexibilityUnit.generated.cs diff --git a/Gu.Units.Generator.Tests/QuantityTests.cs b/Gu.Units.Generator.Tests/QuantityTests.cs index 13398643..ca58c788 100644 --- a/Gu.Units.Generator.Tests/QuantityTests.cs +++ b/Gu.Units.Generator.Tests/QuantityTests.cs @@ -55,10 +55,37 @@ public void ComposedQuantityInterface(int p1, int p2, string expected) } [Test] - public void FindOverloads() + public void LengthOverloads() { - var overloads = _settings.Length.OperatorOverloads; - Assert.Inconclusive(); + var actual = _settings.Length.OperatorOverloads.ToArray(); + var expected = new[] + { + new OperatorOverload(_settings.Length, _settings.Time, _settings), + new OperatorOverload(_settings.Length, _settings.Speed, _settings), + new OperatorOverload(_settings.Length, _settings.Energy, _settings), + new OperatorOverload(_settings.Length, _settings.Area, _settings), + new OperatorOverload(_settings.Length, _settings.Volume, _settings) + }; + CollectionAssert.AreEqual(expected.Select(x => x.ToString()), actual.Select(x => x.ToString())); + } + + [Test] + public void TimeOverloads() + { + var actual = _settings.Time.OperatorOverloads.ToArray(); + var expected = new[] + { + new OperatorOverload(_settings.Time, _settings.Length, _settings), + new OperatorOverload(_settings.Time, _settings.ElectricCharge, _settings), + }; + CollectionAssert.AreEqual(expected.Select(x => x.ToString()), actual.Select(x => x.ToString())); + } + + [Test] + public void Inversions() + { + var actual = _settings.Time.Inverse; + Assert.AreEqual(_settings.Quantities); } } } diff --git a/Gu.Units.Generator/Descriptors/Quantity.cs b/Gu.Units.Generator/Descriptors/Quantity.cs index 38317a0d..ed735c74 100644 --- a/Gu.Units.Generator/Descriptors/Quantity.cs +++ b/Gu.Units.Generator/Descriptors/Quantity.cs @@ -36,7 +36,7 @@ public Quantity(IUnit unit) } _unit.PropertyChanged += (_, e) => OnPropertyChanged("Interface"); } - + public string UnitName { get @@ -136,6 +136,15 @@ public IEnumerable OperatorOverloads } } + [XmlIgnore] + public Quantity Inverse + { + get + { + throw new NotImplementedException("message"); + } + } + [XmlIgnore] public Settings Settings { diff --git a/Gu.Units.Generator/GeneratorSettings.xml b/Gu.Units.Generator/GeneratorSettings.xml index 8842b081..faa7ebaf 100644 --- a/Gu.Units.Generator/GeneratorSettings.xml +++ b/Gu.Units.Generator/GeneratorSettings.xml @@ -905,6 +905,32 @@ + + MetresPerNewton + m/N + Flexibility + + + MillimetresPerNewton + mm/N + + 0.001 + 0 + + + + + + + Metres + 1 + + + Newtons + -1 + + + @@ -1133,7 +1159,7 @@ Fractions - /x + ul Fraction diff --git a/Gu.Units.Generator/Templates/QuantityGenerator.txt b/Gu.Units.Generator/Templates/QuantityGenerator.txt index 27f870e1..cc61a8ce 100644 --- a/Gu.Units.Generator/Templates/QuantityGenerator.txt +++ b/Gu.Units.Generator/Templates/QuantityGenerator.txt @@ -1,5 +1,5 @@  -Read Settings found 28 units +Read Settings found 29 units //-> Acceleration.generated.cs //-> Angle.generated.cs @@ -18,6 +18,8 @@ Read Settings found 28 units //-> Energy.generated.cs +//-> Flexibility.generated.cs + //-> Force.generated.cs //-> Fraction.generated.cs @@ -58,6 +60,7 @@ Read Settings found 28 units // Generated helper templates // Gu.Units\Gu.Units\QuantityGenerator.txt4 // Generated items +// Gu.Units\Gu.Units\Flexibility.generated.cs // Gu.Units\Gu.Units\Capacitance.generated.cs // Gu.Units\Gu.Units\Inductance.generated.cs // Gu.Units\Gu.Units\ElectricCharge.generated.cs diff --git a/Gu.Units.Generator/Templates/UnitGenerator.txt b/Gu.Units.Generator/Templates/UnitGenerator.txt index 5e96e6d9..d38cd75f 100644 --- a/Gu.Units.Generator/Templates/UnitGenerator.txt +++ b/Gu.Units.Generator/Templates/UnitGenerator.txt @@ -18,6 +18,8 @@ Read Settings found 8 units //-> EnergyUnit.generated.cs +//-> FlexibilityUnit.generated.cs + //-> ForceUnit.generated.cs //-> FractionUnit.generated.cs @@ -58,6 +60,7 @@ Read Settings found 8 units // Generated helper templates // Gu.Units\Gu.Units\UnitGenerator.txt4 // Generated items +// Gu.Units\Gu.Units\FlexibilityUnit.generated.cs // Gu.Units\Gu.Units\CapacitanceUnit.generated.cs // Gu.Units\Gu.Units\InductanceUnit.generated.cs // Gu.Units\Gu.Units\ElectricChargeUnit.generated.cs diff --git a/Gu.Units/Flexibility.generated.cs b/Gu.Units/Flexibility.generated.cs new file mode 100644 index 00000000..a4b957b2 --- /dev/null +++ b/Gu.Units/Flexibility.generated.cs @@ -0,0 +1,412 @@ +namespace Gu.Units +{ + using System; + using System.Globalization; + using System.Xml; + using System.Xml.Schema; + using System.Xml.Serialization; + + /// + /// A type for the quantity . + /// + [Serializable] + public partial struct Flexibility : IComparable, IEquatable, IFormattable, IXmlSerializable, IQuantity + { + /// + /// The quantity in . + /// + public readonly double MetresPerNewton; + + private Flexibility(double metresPerNewton) + { + MetresPerNewton = metresPerNewton; + } + + /// + /// Initializes a new instance of . + /// + /// + /// . + public Flexibility(double value, FlexibilityUnit unit) + { + MetresPerNewton = unit.ToSiUnit(value); + } + + /// + /// The quantity in MetresPerNewton + /// + public double SiValue + { + get + { + return MetresPerNewton; + } + } + + /// + /// The quantity in millimetresPerNewton + /// + public double MillimetresPerNewton + { + get + { + return FlexibilityUnit.MillimetresPerNewton.FromSiUnit(MetresPerNewton); + } + } + + /// + /// Creates an instance of from its string representation + /// + /// The string representation of the + /// + public static Flexibility Parse(string s) + { + return Parser.Parse(s, From); + } + + /// + /// Reads an instance of from the + /// + /// + /// An instance of + public static Flexibility ReadFrom(XmlReader reader) + { + var v = new Flexibility(); + v.ReadXml(reader); + return v; + } + + /// + /// Creates a new instance of . + /// + /// + /// + public static Flexibility From(double value, FlexibilityUnit unit) + { + return new Flexibility(unit.ToSiUnit(value)); + } + + /// + /// Creates a new instance of . + /// + /// The value in + public static Flexibility FromMetresPerNewton(double metresPerNewton) + { + return new Flexibility(metresPerNewton); + } + + /// + /// Creates a new instance of . + /// + /// The value in mm/N + public static Flexibility FromMillimetresPerNewton(double millimetresPerNewton) + { + return From(millimetresPerNewton, FlexibilityUnit.MillimetresPerNewton); + } + + public static Length operator *(Flexibility left, Force right) + { + return Length.FromMetres(left.MetresPerNewton * right.Newtons); + } + + public static Area operator *(Flexibility left, Energy right) + { + return Area.FromSquareMetres(left.MetresPerNewton * right.Joules); + } + + public static double operator /(Flexibility left, Flexibility right) + { + return left.MetresPerNewton / right.MetresPerNewton; + } + + /// + /// Indicates whether two instances are equal. + /// + /// + /// true if the quantitys of and are equal; otherwise, false. + /// + /// A . + /// A . + public static bool operator ==(Flexibility left, Flexibility right) + { + return left.Equals(right); + } + + /// + /// Indicates whether two instances are not equal. + /// + /// + /// true if the quantitys of and are not equal; otherwise, false. + /// + /// A . + /// A . + public static bool operator !=(Flexibility left, Flexibility right) + { + return !left.Equals(right); + } + + /// + /// Indicates whether a specified is less than another specified . + /// + /// + /// true if the quantity of is less than the quantity of ; otherwise, false. + /// + /// An . + /// An . + public static bool operator <(Flexibility left, Flexibility right) + { + return left.MetresPerNewton < right.MetresPerNewton; + } + + /// + /// Indicates whether a specified is greater than another specified . + /// + /// + /// true if the quantity of is greater than the quantity of ; otherwise, false. + /// + /// An . + /// An . + public static bool operator >(Flexibility left, Flexibility right) + { + return left.MetresPerNewton > right.MetresPerNewton; + } + + /// + /// Indicates whether a specified is less than or equal to another specified . + /// + /// + /// true if the quantity of is less than or equal to the quantity of ; otherwise, false. + /// + /// An . + /// An . + public static bool operator <=(Flexibility left, Flexibility right) + { + return left.MetresPerNewton <= right.MetresPerNewton; + } + + /// + /// Indicates whether a specified is greater than or equal to another specified . + /// + /// + /// true if the quantity of is greater than or equal to the quantity of ; otherwise, false. + /// + /// An . + /// An . + public static bool operator >=(Flexibility left, Flexibility right) + { + return left.MetresPerNewton >= right.MetresPerNewton; + } + + /// + /// Multiplies an instance of with and returns the result. + /// + /// An instance of + /// An instance of + /// Multiplies an instance of with and returns the result. + public static Flexibility operator *(double left, Flexibility right) + { + return new Flexibility(left * right.MetresPerNewton); + } + + /// + /// Multiplies an instance of with and returns the result. + /// + /// An instance of + /// An instance of + /// Multiplies an instance of with and returns the result. + public static Flexibility operator *(Flexibility left, double right) + { + return new Flexibility(left.MetresPerNewton * right); + } + + /// + /// Divides an instance of with and returns the result. + /// + /// An instance of + /// An instance of + /// Divides an instance of with and returns the result. + public static Flexibility operator /(Flexibility left, double right) + { + return new Flexibility(left.MetresPerNewton / right); + } + + /// + /// Adds two specified instances. + /// + /// + /// An whose quantity is the sum of the quantitys of and . + /// + /// A . + /// A TimeSpan. + public static Flexibility operator +(Flexibility left, Flexibility right) + { + return new Flexibility(left.MetresPerNewton + right.MetresPerNewton); + } + + /// + /// Subtracts an Flexibility from another Flexibility and returns the difference. + /// + /// + /// An that is the difference + /// + /// A (the minuend). + /// A (the subtrahend). + public static Flexibility operator -(Flexibility left, Flexibility right) + { + return new Flexibility(left.MetresPerNewton - right.MetresPerNewton); + } + + /// + /// Returns an whose quantity is the negated quantity of the specified instance. + /// + /// + /// An with the same numeric quantity as this instance, but the opposite sign. + /// + /// A + public static Flexibility operator -(Flexibility Flexibility) + { + return new Flexibility(-1 * Flexibility.MetresPerNewton); + } + + /// + /// Returns the specified instance of . + /// + /// + /// Returns . + /// + /// A + public static Flexibility operator +(Flexibility Flexibility) + { + return Flexibility; + } + + public override string ToString() + { + return this.ToString((string)null, (IFormatProvider)NumberFormatInfo.CurrentInfo); + } + + public string ToString(string format) + { + return this.ToString(format, (IFormatProvider)NumberFormatInfo.CurrentInfo); + } + + public string ToString(IFormatProvider provider) + { + return this.ToString((string)null, (IFormatProvider)NumberFormatInfo.GetInstance(provider)); + } + + public string ToString(string format, IFormatProvider formatProvider) + { + return this.ToString(format, formatProvider, FlexibilityUnit.MetresPerNewton); + } + + public string ToString(string format, IFormatProvider formatProvider, FlexibilityUnit unit) + { + var quantity = unit.FromSiUnit(this.MetresPerNewton); + return string.Format("{0}{1}", quantity.ToString(format, formatProvider), unit.Symbol); + } + + /// + /// Compares this instance to a specified object and returns an integer that indicates whether this is smaller than, equal to, or greater than the object. + /// + /// + /// A signed number indicating the relative quantitys of this instance and . + /// + /// Value + /// + /// Description + /// + /// A negative integer + /// + /// This instance is smaller than . + /// + /// Zero + /// + /// This instance is equal to . + /// + /// A positive integer + /// + /// This instance is larger than . + /// + /// + /// A object to compare to this instance. + public int CompareTo(Flexibility quantity) + { + return this.MetresPerNewton.CompareTo(quantity.MetresPerNewton); + } + + /// + /// Returns a quantity indicating whether this instance is equal to a specified object. + /// + /// + /// true if represents the same Flexibility as this instance; otherwise, false. + /// + /// An object to compare with this instance. + public bool Equals(Flexibility other) + { + return this.MetresPerNewton.Equals(other.MetresPerNewton); + } + + /// + /// Returns a quantity indicating whether this instance is equal to a specified object within the given tolerance. + /// + /// + /// true if represents the same Flexibility as this instance; otherwise, false. + /// + /// An object to compare with this instance. + /// The maximum difference for being considered equal + public bool Equals(Flexibility other, double tolerance) + { + return Math.Abs(this.MetresPerNewton - other.MetresPerNewton) < tolerance; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + + return obj is Flexibility && this.Equals((Flexibility)obj); + } + + public override int GetHashCode() + { + return this.MetresPerNewton.GetHashCode(); + } + + /// + /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, + /// you should return null (Nothing in Visual Basic) from this method, and instead, + /// if specifying a custom schema is required, apply the to the class. + /// + /// + /// An that describes the XML representation of the object that is produced by the + /// + /// method and consumed by the method. + /// + public XmlSchema GetSchema() + { + return null; + } + + /// + /// Generates an object from its XML representation. + /// + /// The stream from which the object is deserialized. + public void ReadXml(XmlReader reader) + { + // Hacking set readonly fields here, can't think of a cleaner workaround + XmlExt.SetReadonlyField(ref this, "MetresPerNewton", reader, "Value"); + } + + /// + /// Converts an object into its XML representation. + /// + /// The stream to which the object is serialized. + public void WriteXml(XmlWriter writer) + { + XmlExt.WriteAttribute(writer, "Value", this.MetresPerNewton); + } + } +} diff --git a/Gu.Units/FlexibilityUnit.generated.cs b/Gu.Units/FlexibilityUnit.generated.cs new file mode 100644 index 00000000..7b701ea1 --- /dev/null +++ b/Gu.Units/FlexibilityUnit.generated.cs @@ -0,0 +1,73 @@ +namespace Gu.Units +{ + using System; + /// + /// A type for the unit . + /// Contains conversion logic. + /// + [Serializable] + public struct FlexibilityUnit : IUnit + { + /// + /// The unit + /// Contains coonversion logic to from and formatting. + /// + public static readonly FlexibilityUnit MetresPerNewton = new FlexibilityUnit(1.0, "m/N"); + + /// + /// The unit + /// Contains conversion logic to from and formatting. + /// + public static readonly FlexibilityUnit MillimetresPerNewton = new FlexibilityUnit(0.001, "mm/N"); + + private readonly double _conversionFactor; + private readonly string _symbol; + + public FlexibilityUnit(double conversionFactor, string symbol) + { + _conversionFactor = conversionFactor; + _symbol = symbol; + } + + /// + /// The symbol for . + /// + public string Symbol + { + get + { + return _symbol; + } + } + + public static Flexibility operator *(double left, FlexibilityUnit right) + { + return Flexibility.From(left, right); + } + + /// + /// Converts a value to . + /// + /// + /// The converted value + public double ToSiUnit(double value) + { + return _conversionFactor * value; + } + + /// + /// Converts a value from MetresPerNewton. + /// + /// The value in MetresPerNewton + /// The converted value + public double FromSiUnit(double value) + { + return value / _conversionFactor; + } + + public override string ToString() + { + return string.Format("1{0} == {1}{2}", _symbol, this.ToSiUnit(1), MetresPerNewton.Symbol); + } + } +} \ No newline at end of file diff --git a/Gu.Units/Gu.Units.csproj b/Gu.Units/Gu.Units.csproj index acfc9bc0..2c98c58c 100644 --- a/Gu.Units/Gu.Units.csproj +++ b/Gu.Units/Gu.Units.csproj @@ -112,6 +112,12 @@ UnitGenerator.txt4 + + QuantityGenerator.txt4 + + + UnitGenerator.txt4 + QuantityGenerator.txt4