From 9a671e00dd999692576dc03c6a5b3a8f716a8828 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 18 Jul 2014 15:45:33 +0300 Subject: [PATCH 1/3] Add information unit Bit, byte, kilobyte...,petabyte, exabyte. --- Src/Scripts/UnitDefinitions/Information.json | 32 ++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Src/Scripts/UnitDefinitions/Information.json diff --git a/Src/Scripts/UnitDefinitions/Information.json b/Src/Scripts/UnitDefinitions/Information.json new file mode 100644 index 0000000000..73a279c2a0 --- /dev/null +++ b/Src/Scripts/UnitDefinitions/Information.json @@ -0,0 +1,32 @@ +{ + "Name": "Information", + "BaseUnit": "Bit", + "XmlDoc": "In computing and telecommunications, a unit of information is the capacity of some standard data storage system or communication channel, used to measure the capacities of other systems and channels. In information theory, units of information are also used to measure the information contents or entropy of random variables.", + "Units": [ + { + "SingularName": "Byte", + "PluralName": "Bytes", + "FromUnitToBaseFunc": "x*8", + "FromBaseToUnitFunc": "x/8", + "Prefixes": ["Kilo","Mega","Giga","Tera","Peta","Exa"], + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": ["B"] + } + ] + }, + { + "SingularName": "Bit", + "PluralName": "Bits", + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": ["b"] + } + ] + } + ] +} \ No newline at end of file From 8830116bd42958534b95f2cbef6e8a5e557c1883 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sat, 19 Jul 2014 21:24:22 +0300 Subject: [PATCH 2/3] Generate code --- .../GeneratedCode/Enums/InformationUnit.g.cs | 37 ++ .../UnitClasses/Information.g.cs | 403 ++++++++++++++++++ .../GeneratedCode/UnitSystem.Default.g.cs | 44 ++ Tests/CustomCode/InformationTests.cs | 31 ++ Tests/GeneratedCode/InformationTestsBase.g.cs | 209 +++++++++ 5 files changed, 724 insertions(+) create mode 100644 Src/UnitsNet/GeneratedCode/Enums/InformationUnit.g.cs create mode 100644 Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs create mode 100644 Tests/CustomCode/InformationTests.cs create mode 100644 Tests/GeneratedCode/InformationTestsBase.g.cs diff --git a/Src/UnitsNet/GeneratedCode/Enums/InformationUnit.g.cs b/Src/UnitsNet/GeneratedCode/Enums/InformationUnit.g.cs new file mode 100644 index 0000000000..40d714975a --- /dev/null +++ b/Src/UnitsNet/GeneratedCode/Enums/InformationUnit.g.cs @@ -0,0 +1,37 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/InitialForce/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. + +// ReSharper disable once CheckNamespace +namespace UnitsNet.Units +{ + public enum InformationUnit + { + Undefined = 0, + Bit, + Byte, + Exabyte, + Gigabyte, + Kilobyte, + Megabyte, + Petabyte, + Terabyte, + } +} diff --git a/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs new file mode 100644 index 0000000000..d9d570b141 --- /dev/null +++ b/Src/UnitsNet/GeneratedCode/UnitClasses/Information.g.cs @@ -0,0 +1,403 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/InitialForce/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 System.Globalization; +using System.Linq; +using UnitsNet.Annotations; +using UnitsNet.Units; + +// ReSharper disable once CheckNamespace + +namespace UnitsNet +{ + /// + /// In computing and telecommunications, a unit of information is the capacity of some standard data storage system or communication channel, used to measure the capacities of other systems and channels. In information theory, units of information are also used to measure the information contents or entropy of random variables. + /// + // ReSharper disable once PartialTypeWithSinglePart + public partial struct Information : IComparable, IComparable + { + /// + /// Base unit of Information. + /// + [UsedImplicitly] public readonly double Bits; + + public Information(double bits) : this() + { + Bits = bits; + } + + #region Properties + + /// + /// Get Information in Bytes. + /// + public double Bytes + { + get { return Bits/8; } + } + + /// + /// Get Information in Exabytes. + /// + public double Exabytes + { + get { return (Bits/8) / 1e18; } + } + + /// + /// Get Information in Gigabytes. + /// + public double Gigabytes + { + get { return (Bits/8) / 1e9; } + } + + /// + /// Get Information in Kilobytes. + /// + public double Kilobytes + { + get { return (Bits/8) / 1e3; } + } + + /// + /// Get Information in Megabytes. + /// + public double Megabytes + { + get { return (Bits/8) / 1e6; } + } + + /// + /// Get Information in Petabytes. + /// + public double Petabytes + { + get { return (Bits/8) / 1e15; } + } + + /// + /// Get Information in Terabytes. + /// + public double Terabytes + { + get { return (Bits/8) / 1e12; } + } + + #endregion + + #region Static + + public static Information Zero + { + get { return new Information(); } + } + + /// + /// Get Information from Bits. + /// + public static Information FromBits(double bits) + { + return new Information(bits); + } + + /// + /// Get Information from Bytes. + /// + public static Information FromBytes(double bytes) + { + return new Information(bytes*8); + } + + /// + /// Get Information from Exabytes. + /// + public static Information FromExabytes(double exabytes) + { + return new Information((exabytes*8) * 1e18); + } + + /// + /// Get Information from Gigabytes. + /// + public static Information FromGigabytes(double gigabytes) + { + return new Information((gigabytes*8) * 1e9); + } + + /// + /// Get Information from Kilobytes. + /// + public static Information FromKilobytes(double kilobytes) + { + return new Information((kilobytes*8) * 1e3); + } + + /// + /// Get Information from Megabytes. + /// + public static Information FromMegabytes(double megabytes) + { + return new Information((megabytes*8) * 1e6); + } + + /// + /// Get Information from Petabytes. + /// + public static Information FromPetabytes(double petabytes) + { + return new Information((petabytes*8) * 1e15); + } + + /// + /// Get Information from Terabytes. + /// + public static Information FromTerabytes(double terabytes) + { + return new Information((terabytes*8) * 1e12); + } + + + /// + /// Dynamically convert from value and unit enum to . + /// + /// Value to convert from. + /// Unit to convert from. + /// Information unit value. + public static Information From(double value, InformationUnit fromUnit) + { + switch (fromUnit) + { + case InformationUnit.Bit: + return FromBits(value); + case InformationUnit.Byte: + return FromBytes(value); + case InformationUnit.Exabyte: + return FromExabytes(value); + case InformationUnit.Gigabyte: + return FromGigabytes(value); + case InformationUnit.Kilobyte: + return FromKilobytes(value); + case InformationUnit.Megabyte: + return FromMegabytes(value); + case InformationUnit.Petabyte: + return FromPetabytes(value); + case InformationUnit.Terabyte: + return FromTerabytes(value); + + default: + throw new NotImplementedException("fromUnit: " + fromUnit); + } + } + + /// + /// Get unit abbreviation string. + /// + /// Unit to get abbreviation for. + /// Culture to use for localization. Defaults to Thread.CurrentUICulture. + /// Unit abbreviation string. + [UsedImplicitly] + public static string GetAbbreviation(InformationUnit unit, CultureInfo culture = null) + { + return UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit); + } + + #endregion + + #region Arithmetic Operators + + public static Information operator -(Information right) + { + return new Information(-right.Bits); + } + + public static Information operator +(Information left, Information right) + { + return new Information(left.Bits + right.Bits); + } + + public static Information operator -(Information left, Information right) + { + return new Information(left.Bits - right.Bits); + } + + public static Information operator *(double left, Information right) + { + return new Information(left*right.Bits); + } + + public static Information operator *(Information left, double right) + { + return new Information(left.Bits*right); + } + + public static Information operator /(Information left, double right) + { + return new Information(left.Bits/right); + } + + public static double operator /(Information left, Information right) + { + return left.Bits/right.Bits; + } + + #endregion + + #region Equality / IComparable + + public int CompareTo(object obj) + { + if (obj == null) throw new ArgumentNullException("obj"); + if (!(obj is Information)) throw new ArgumentException("Expected type Information.", "obj"); + return CompareTo((Information) obj); + } + + public int CompareTo(Information other) + { + return Bits.CompareTo(other.Bits); + } + + public static bool operator <=(Information left, Information right) + { + return left.Bits <= right.Bits; + } + + public static bool operator >=(Information left, Information right) + { + return left.Bits >= right.Bits; + } + + public static bool operator <(Information left, Information right) + { + return left.Bits < right.Bits; + } + + public static bool operator >(Information left, Information right) + { + return left.Bits > right.Bits; + } + + public static bool operator ==(Information left, Information right) + { + // ReSharper disable once CompareOfFloatsByEqualityOperator + return left.Bits == right.Bits; + } + + public static bool operator !=(Information left, Information right) + { + // ReSharper disable once CompareOfFloatsByEqualityOperator + return left.Bits != right.Bits; + } + + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + + return Bits.Equals(((Information) obj).Bits); + } + + public override int GetHashCode() + { + return Bits.GetHashCode(); + } + + #endregion + + #region Conversion + + /// + /// Convert to the unit representation . + /// + /// Value in new unit if successful, exception otherwise. + /// If conversion was not successful. + public double As(InformationUnit unit) + { + switch (unit) + { + case InformationUnit.Bit: + return Bits; + case InformationUnit.Byte: + return Bytes; + case InformationUnit.Exabyte: + return Exabytes; + case InformationUnit.Gigabyte: + return Gigabytes; + case InformationUnit.Kilobyte: + return Kilobytes; + case InformationUnit.Megabyte: + return Megabytes; + case InformationUnit.Petabyte: + return Petabytes; + case InformationUnit.Terabyte: + return Terabytes; + + default: + throw new NotImplementedException("unit: " + unit); + } + } + + #endregion + + /// + /// Get string representation of value and unit. + /// + /// Culture to use for localization and number formatting. + /// Unit representation to use. + /// String representation. + [UsedImplicitly] + public string ToString(InformationUnit unit, CultureInfo culture = null) + { + return ToString(unit, culture, "{0:0.##} {1}"); + } + + /// + /// Get string representation of value and unit. + /// + /// Culture to use for localization and number formatting. + /// Unit representation to use. + /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." + /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. + /// String representation. + [UsedImplicitly] + public string ToString(InformationUnit unit, CultureInfo culture, string format, params object[] args) + { + string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit); + object[] finalArgs = new object[] {As(unit), abbreviation} + .Concat(args) + .ToArray(); + + return string.Format(culture, format, finalArgs); + } + + /// + /// Get default string representation of value and unit. + /// + /// String representation. + public override string ToString() + { + return ToString(InformationUnit.Bit); + } + } +} diff --git a/Src/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs b/Src/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs index a9e485514c..8c929d5356 100644 --- a/Src/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs +++ b/Src/UnitsNet/GeneratedCode/UnitSystem.Default.g.cs @@ -250,6 +250,50 @@ private static readonly ReadOnlyCollection DefaultLocalization new AbbreviationsForCulture("ru-RU", "фунт-сила"), }), }), + new UnitLocalization(typeof (InformationUnit), + new[] + { + new CulturesForEnumValue((int) InformationUnit.Bit, + new[] + { + new AbbreviationsForCulture("en-US", "b"), + }), + new CulturesForEnumValue((int) InformationUnit.Byte, + new[] + { + new AbbreviationsForCulture("en-US", "B"), + }), + new CulturesForEnumValue((int) InformationUnit.Exabyte, + new[] + { + new AbbreviationsForCulture("en-US", "EB"), + }), + new CulturesForEnumValue((int) InformationUnit.Gigabyte, + new[] + { + new AbbreviationsForCulture("en-US", "GB"), + }), + new CulturesForEnumValue((int) InformationUnit.Kilobyte, + new[] + { + new AbbreviationsForCulture("en-US", "kB"), + }), + new CulturesForEnumValue((int) InformationUnit.Megabyte, + new[] + { + new AbbreviationsForCulture("en-US", "MB"), + }), + new CulturesForEnumValue((int) InformationUnit.Petabyte, + new[] + { + new AbbreviationsForCulture("en-US", "PB"), + }), + new CulturesForEnumValue((int) InformationUnit.Terabyte, + new[] + { + new AbbreviationsForCulture("en-US", "TB"), + }), + }), new UnitLocalization(typeof (LengthUnit), new[] { diff --git a/Tests/CustomCode/InformationTests.cs b/Tests/CustomCode/InformationTests.cs new file mode 100644 index 0000000000..c9745aeba1 --- /dev/null +++ b/Tests/CustomCode/InformationTests.cs @@ -0,0 +1,31 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/InitialForce/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 InformationTests : InformationTestsBase + { + // TODO Override properties in base class here + } +} diff --git a/Tests/GeneratedCode/InformationTestsBase.g.cs b/Tests/GeneratedCode/InformationTestsBase.g.cs new file mode 100644 index 0000000000..738fda2a7f --- /dev/null +++ b/Tests/GeneratedCode/InformationTestsBase.g.cs @@ -0,0 +1,209 @@ +// Copyright © 2007 by Initial Force AS. All rights reserved. +// https://github.com/InitialForce/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 NUnit.Framework; +using UnitsNet.Units; + +// 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 +{ + /// + /// Test of Information. + /// + [TestFixture] +// ReSharper disable once PartialTypeWithSinglePart + public abstract partial class InformationTestsBase + { + protected abstract double BitsInOneBit { get; } + protected abstract double BytesInOneBit { get; } + protected abstract double ExabytesInOneBit { get; } + protected abstract double GigabytesInOneBit { get; } + protected abstract double KilobytesInOneBit { get; } + protected abstract double MegabytesInOneBit { get; } + protected abstract double PetabytesInOneBit { get; } + protected abstract double TerabytesInOneBit { get; } + +// ReSharper disable VirtualMemberNeverOverriden.Global + protected virtual double BitsTolerance { get { return 1E-5; } } + protected virtual double BytesTolerance { get { return 1E-5; } } + protected virtual double ExabytesTolerance { get { return 1E-5; } } + protected virtual double GigabytesTolerance { get { return 1E-5; } } + protected virtual double KilobytesTolerance { get { return 1E-5; } } + protected virtual double MegabytesTolerance { get { return 1E-5; } } + protected virtual double PetabytesTolerance { get { return 1E-5; } } + protected virtual double TerabytesTolerance { get { return 1E-5; } } +// ReSharper restore VirtualMemberNeverOverriden.Global + + [Test] + public void BitToInformationUnits() + { + Information bit = Information.FromBits(1); + Assert.AreEqual(BitsInOneBit, bit.Bits, BitsTolerance); + Assert.AreEqual(BytesInOneBit, bit.Bytes, BytesTolerance); + Assert.AreEqual(ExabytesInOneBit, bit.Exabytes, ExabytesTolerance); + Assert.AreEqual(GigabytesInOneBit, bit.Gigabytes, GigabytesTolerance); + Assert.AreEqual(KilobytesInOneBit, bit.Kilobytes, KilobytesTolerance); + Assert.AreEqual(MegabytesInOneBit, bit.Megabytes, MegabytesTolerance); + Assert.AreEqual(PetabytesInOneBit, bit.Petabytes, PetabytesTolerance); + Assert.AreEqual(TerabytesInOneBit, bit.Terabytes, TerabytesTolerance); + } + + [Test] + public void FromValueAndUnit() + { + Assert.AreEqual(1, Information.From(1, InformationUnit.Bit).Bits, BitsTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Byte).Bytes, BytesTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Exabyte).Exabytes, ExabytesTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Gigabyte).Gigabytes, GigabytesTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Kilobyte).Kilobytes, KilobytesTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Megabyte).Megabytes, MegabytesTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Petabyte).Petabytes, PetabytesTolerance); + Assert.AreEqual(1, Information.From(1, InformationUnit.Terabyte).Terabytes, TerabytesTolerance); + } + + [Test] + public void As() + { + var bit = Information.FromBits(1); + Assert.AreEqual(BitsInOneBit, bit.As(InformationUnit.Bit), BitsTolerance); + Assert.AreEqual(BytesInOneBit, bit.As(InformationUnit.Byte), BytesTolerance); + Assert.AreEqual(ExabytesInOneBit, bit.As(InformationUnit.Exabyte), ExabytesTolerance); + Assert.AreEqual(GigabytesInOneBit, bit.As(InformationUnit.Gigabyte), GigabytesTolerance); + Assert.AreEqual(KilobytesInOneBit, bit.As(InformationUnit.Kilobyte), KilobytesTolerance); + Assert.AreEqual(MegabytesInOneBit, bit.As(InformationUnit.Megabyte), MegabytesTolerance); + Assert.AreEqual(PetabytesInOneBit, bit.As(InformationUnit.Petabyte), PetabytesTolerance); + Assert.AreEqual(TerabytesInOneBit, bit.As(InformationUnit.Terabyte), TerabytesTolerance); + } + + [Test] + public void ConversionRoundTrip() + { + Information bit = Information.FromBits(1); + Assert.AreEqual(1, Information.FromBits(bit.Bits).Bits, BitsTolerance); + Assert.AreEqual(1, Information.FromBytes(bit.Bytes).Bits, BytesTolerance); + Assert.AreEqual(1, Information.FromExabytes(bit.Exabytes).Bits, ExabytesTolerance); + Assert.AreEqual(1, Information.FromGigabytes(bit.Gigabytes).Bits, GigabytesTolerance); + Assert.AreEqual(1, Information.FromKilobytes(bit.Kilobytes).Bits, KilobytesTolerance); + Assert.AreEqual(1, Information.FromMegabytes(bit.Megabytes).Bits, MegabytesTolerance); + Assert.AreEqual(1, Information.FromPetabytes(bit.Petabytes).Bits, PetabytesTolerance); + Assert.AreEqual(1, Information.FromTerabytes(bit.Terabytes).Bits, TerabytesTolerance); + } + + [Test] + public void ArithmeticOperators() + { + Information v = Information.FromBits(1); + Assert.AreEqual(-1, -v.Bits, TerabytesTolerance); + Assert.AreEqual(2, (Information.FromBits(3)-v).Bits, TerabytesTolerance); + Assert.AreEqual(2, (v + v).Bits, TerabytesTolerance); + Assert.AreEqual(10, (v*10).Bits, TerabytesTolerance); + Assert.AreEqual(10, (10*v).Bits, TerabytesTolerance); + Assert.AreEqual(2, (Information.FromBits(10)/5).Bits, TerabytesTolerance); + Assert.AreEqual(2, Information.FromBits(10)/Information.FromBits(5), TerabytesTolerance); + } + + [Test] + public void ComparisonOperators() + { + Information oneBit = Information.FromBits(1); + Information twoBits = Information.FromBits(2); + + Assert.True(oneBit < twoBits); + Assert.True(oneBit <= twoBits); + Assert.True(twoBits > oneBit); + Assert.True(twoBits >= oneBit); + + Assert.False(oneBit > twoBits); + Assert.False(oneBit >= twoBits); + Assert.False(twoBits < oneBit); + Assert.False(twoBits <= oneBit); + } + + [Test] + public void CompareToIsImplemented() + { + Information bit = Information.FromBits(1); + Assert.AreEqual(0, bit.CompareTo(bit)); + Assert.Greater(bit.CompareTo(Information.Zero), 0); + Assert.Less(Information.Zero.CompareTo(bit), 0); + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void CompareToThrowsOnTypeMismatch() + { + Information bit = Information.FromBits(1); +// ReSharper disable once ReturnValueOfPureMethodIsNotUsed + bit.CompareTo(new object()); + } + + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void CompareToThrowsOnNull() + { + Information bit = Information.FromBits(1); +// ReSharper disable once ReturnValueOfPureMethodIsNotUsed + bit.CompareTo(null); + } + + + [Test] + public void EqualityOperators() + { + Information a = Information.FromBits(1); + Information b = Information.FromBits(2); + +// ReSharper disable EqualExpressionComparison + Assert.True(a == a); + Assert.True(a != b); + + Assert.False(a == b); + Assert.False(a != a); +// ReSharper restore EqualExpressionComparison + } + + [Test] + public void EqualsIsImplemented() + { + Information v = Information.FromBits(1); + Assert.IsTrue(v.Equals(Information.FromBits(1))); + Assert.IsFalse(v.Equals(Information.Zero)); + } + + [Test] + public void EqualsReturnsFalseOnTypeMismatch() + { + Information bit = Information.FromBits(1); + Assert.IsFalse(bit.Equals(new object())); + } + + [Test] + public void EqualsReturnsFalseOnNull() + { + Information bit = Information.FromBits(1); + Assert.IsFalse(bit.Equals(null)); + } + } +} From a3cd55e58538262c971c729ac6c0e2ec367b2b27 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Sat, 19 Jul 2014 21:27:06 +0300 Subject: [PATCH 3/3] Implement constants for tests Fixes intentional compile error by generated code for new units, which ensures all units have tests. --- Tests/CustomCode/InformationTests.cs | 40 +++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/Tests/CustomCode/InformationTests.cs b/Tests/CustomCode/InformationTests.cs index c9745aeba1..21a2b249e6 100644 --- a/Tests/CustomCode/InformationTests.cs +++ b/Tests/CustomCode/InformationTests.cs @@ -26,6 +26,44 @@ namespace UnitsNet.Tests.CustomCode { public class InformationTests : InformationTestsBase { - // TODO Override properties in base class here + protected override double BitsInOneBit + { + get { return 1; } + } + + protected override double BytesInOneBit + { + get { return 0.125; } + } + + protected override double ExabytesInOneBit + { + get { return 0.125*1e-18; } + } + + protected override double GigabytesInOneBit + { + get { return 0.125*1e-9; } + } + + protected override double KilobytesInOneBit + { + get { return 0.000125; } + } + + protected override double MegabytesInOneBit + { + get { return 0.125*1e-6; } + } + + protected override double PetabytesInOneBit + { + get { return 0.125*1e-15; } + } + + protected override double TerabytesInOneBit + { + get { return 0.125*1e-12; } + } } }