public void Parse_CustomPerMilleMarker_IsValid() { Percentage act = Percentage.Parse("44#", GetCustomNumberFormatInfo()); Percentage exp = 0.044; Assert.AreEqual(exp, act); }
public void CalcTest4() { Percentage p1 = Percentage.Parse("30%"); Percentage p2 = Percentage.Parse("40%"); Assert.IsTrue(0.12 == (double)(p1 * p2)); }
public void ToString_FormatValueSpanishEcuador_AreEqual() { var act = Percentage.Parse("1700").ToString("00000.0", new CultureInfo("es-EC")); var exp = "01700,0"; Assert.AreEqual(exp, act); }
public void Parse_WithSpaceBetweenNumberAndMarker_IsValid() { Percentage act = Percentage.Parse("44.05 %", CultureInfo.InvariantCulture); Percentage exp = 0.4405; Assert.AreEqual(exp, act); }
public void Equals_FormattedAndUnformatted_IsTrue() { var l = Percentage.Parse("17", CultureInfo.InvariantCulture); var r = Percentage.Parse("17.0%", CultureInfo.InvariantCulture); Assert.IsTrue(l.Equals(r)); }
public void ParseFrench_Percentage17Comma51_AreEqual() { using (new CultureInfoScope("fr-FR")) { var act = Percentage.Parse("%17,51"); Assert.AreEqual(TestStruct, act); } }
public void CalcTest3() { Percentage p1 = Percentage.Parse("30%"); int i = 100; Assert.IsTrue(30 == p1 * i); Assert.IsTrue(30 == i * p1); }
public void CalcTest2() { Percentage p1 = Percentage.Parse("30%"); Percentage p2 = Percentage.Parse("70%"); Assert.IsTrue((p1 + p2) == Percentage.HundredPercent); Assert.IsTrue((p2 - p1) == Percentage.Parse("40%")); }
public void ToString_FormatValueEnglishGreatBritain_AreEqual() { using (new CultureInfoScope("en-GB")) { var act = Percentage.Parse("800").ToString("0000"); var exp = "0800"; Assert.AreEqual(exp, act); } }
public void CompareTest1() { Percentage p1 = Percentage.Parse("30%"); Percentage p2 = Percentage.Parse("70%"); Assert.IsTrue(p1 < p2); Assert.IsTrue(p1 <= p2); Assert.IsTrue(p1 != p2); }
public void ToString_ValueDutchBelgium_AreEqual() { using (new CultureInfoScope("nl-BE")) { var act = Percentage.Parse("1600,1").ToString(); var exp = "1600,1%"; Assert.AreEqual(exp, act); } }
public void ToString_ValueDutchNetherlands_AreEqual() { using (new CultureInfoScope("nl-NL")) { var act = Percentage.Parse("1600,1").ToString(""); var exp = "1600,1"; Assert.AreEqual(exp, act); } }
public void ToString_ValueEnglishGreatBritain_AreEqual() { using (new CultureInfoScope("en-GB")) { var act = Percentage.Parse("1600.1").ToString(); var exp = "1600.1%"; Assert.AreEqual(exp, act); } }
public void ToString_FormatValueDutchBelgium_AreEqual() { using (new CultureInfoScope("nl-BE")) { var act = Percentage.Parse("800").ToString("0000"); var exp = "0800"; Assert.AreEqual(exp, act); } }
public void ParseTest() { Percentage p1 = Percentage.Parse("10 % "); Assert.IsTrue(0.1 == p1.Value); Percentage p2 = Percentage.Parse("99%"); Assert.AreEqual(0.99, p2.Value); }
//--------------------------------------------------------------------- private void TryParse(string str) { try { Percentage percentage = Percentage.Parse(str); } catch (System.Exception exc) { Data.Output.WriteLine(exc.Message); throw; } }
public void CalcTest1() { Percentage p = Percentage.Parse("99%"); Percentage r = p / 3; Assert.AreEqual(r, 0.33); //Assert.AreEqual(0.33, r);//Note:this will return error!! Since we can not override float type's Equals method. Assert.IsTrue((double)r == 0.33); Assert.IsTrue(0.33 == (double)r); }
/// <summary>Converts a string to a Percentage, using the specified /// context and culture information. /// </summary> /// <param name="context"> /// An System.ComponentModel.ITypeDescriptorContext that provides a format context. /// </param> /// <param name="culture"> /// The System.Globalization.CultureInfo to use as the current culture. /// </param> /// <param name="value"> /// The System.Object to convert. /// </param> /// <returns> /// An System.Object that represents the converted value. /// </returns> /// <exception cref="NotSupportedException"> /// The conversion cannot be performed. /// </exception> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { var str = value as string; if (str != null) { return(Percentage.Parse(str, culture)); } return(base.ConvertFrom(context, culture, value)); }
public void Parse_InvalidInput_ThrowsFormatException() { using (new CultureInfoScope("en-GB")) { Assert.Catch <FormatException> (() => { Percentage.Parse("InvalidInput"); }, "Not a valid percentage"); } }
public void ReadAgeOrRange_RangePercentage() { StringReader reader = new StringReader("30-75(10%)"); int index; eventHandlerCalled = false; expectedRange = new AgeRange(30, 75); expectedPercentage = Percentage.Parse("10%"); InputValue <AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(0, index); Assert.AreEqual(-1, reader.Peek()); }
public void ReadAgeOrRange_RangeWhitespacePercentage() { StringReader reader = new StringReader(" 1-100 (22.2%)Hi"); int index; eventHandlerCalled = false; expectedRange = new AgeRange(1, 100); expectedPercentage = Percentage.Parse("22.2%"); InputValue <AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(1, index); Assert.AreEqual('H', reader.Peek()); }
public void ReadAgeOrRange_AgeWhitespacePercentage() { StringReader reader = new StringReader("66 ( 50% )\t"); int index; eventHandlerCalled = false; expectedRange = new AgeRange(66, 66); expectedPercentage = Percentage.Parse("50%"); InputValue <AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(0, index); Assert.AreEqual('\t', reader.Peek()); }
public void NonWoodyPercentage() { const ushort age = 100; const ushort biomass = 500; CohortData data = new CohortData(age, biomass); Cohort cohort = new Cohort(betualle, data); Percentage nonWoodyPercentage = Percentage.Parse("35%"); mockCalculator.NonWoodyPercentage = nonWoodyPercentage; ushort expectedNonWoody = (ushort)(biomass * nonWoodyPercentage); ushort nonWoody = cohort.ComputeNonWoodyBiomass(activeSite); Assert.AreEqual(expectedNonWoody, nonWoody); }
public void ReadAgeOrRange_Multiple() { StringReader reader = new StringReader(" 1-40 (50%) 50(65%)\t 65-70 71-107 ( 15% ) 109"); int index; //0123456789_123456789_^123456789_123456789_12345678 eventHandlerCalled = false; expectedRange = new AgeRange(1, 40); expectedPercentage = Percentage.Parse("50%"); InputValue <AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(1, index); Assert.AreEqual(' ', reader.Peek()); eventHandlerCalled = false; expectedRange = new AgeRange(50, 50); expectedPercentage = Percentage.Parse("65%"); ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(13, index); Assert.AreEqual('\t', reader.Peek()); eventHandlerCalled = false; expectedRange = new AgeRange(65, 70); expectedPercentage = null; ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(22, index); Assert.AreEqual('7', reader.Peek()); eventHandlerCalled = false; expectedRange = new AgeRange(71, 107); expectedPercentage = Percentage.Parse("15%"); ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(29, index); Assert.AreEqual(' ', reader.Peek()); eventHandlerCalled = false; expectedRange = new AgeRange(109, 109); expectedPercentage = null; ageRange = PartialThinning.ReadAgeOrRange(reader, out index); Assert.IsTrue(eventHandlerCalled); Assert.AreEqual(45, index); Assert.AreEqual(-1, reader.Peek()); }
/// <exception cref="ArgumentException"> /// The given <paramref name="type" /> is not natively deserializable. /// </exception> /// <exception cref="XmlSerializationException"> /// A collection can not be deserialized as an attribute. /// </exception> private Object ReadNativeObject(Type type, Boolean asAttribute, XmlItemDefAttributeCollection itemDefAttributes) { // Check for null value. if (asAttribute) { if (this.XmlReader.Value == this.Settings.NullAttributeName) { return(null); } } else { if (this.XmlReader.GetAttribute(this.Settings.NullAttributeName) != null) { return(null); } } if (typeof(System.Collections.IList).IsAssignableFrom(type) || typeof(System.Collections.IDictionary).IsAssignableFrom(type)) { if (asAttribute) { throw new XmlSerializationException("A collection can not be deserialized as an attribute."); } Contract.Assert(this.XmlReader.NodeType == XmlNodeType.Element); Object collection; Boolean isDictionary = typeof(System.Collections.IDictionary).IsAssignableFrom(type); if (type.IsClass) { ConstructorInfo constructorInfo = type.GetConstructor(new Type[] {}); if (constructorInfo == null) { var ex = new XmlSerializationException("An empty constructor is required to deserialize the given IList."); ex.Data.Add("Collection Type", type); throw ex; } try { collection = constructorInfo.Invoke(new Object[] {}); } catch (Exception exception) { var ex = new XmlSerializationException("Parameterless constructor of deserializable collection threw exception. See inner exception for more details.", exception); ex.Data.Add("Collection Type", type); throw ex; } } else { // Value type constructor, will never throw exceptions. collection = Activator.CreateInstance(type); } if (!this.XmlReader.IsEmptyElement) { while (this.XmlReader.Read() && this.XmlReader.NodeType != XmlNodeType.EndElement) { if (this.XmlReader.NodeType != XmlNodeType.Element) { continue; } XmlItemDefAttribute attribute; if (!itemDefAttributes.TryGetAttribute(this.XmlReader.LocalName, out attribute)) { if (this.Settings.RequiresExplicitCollectionItemDefinition) { var ex = new XmlSerializationException("The collection-node in the XML-Data contains a sub-node with a name which is not explictly defined by an XmlItemDefAttribute which is required by the current serialization settings."); ex.Data.Add("Collection Type", type); ex.Data.Add("Node Name", this.XmlReader.LocalName); throw ex; } } if (!isDictionary) { ((System.Collections.IList)collection).Add(this.ReadElementContent(attribute.Type)); } else { Object key = null; Object value = null; Type[] genericArgumentTypes = type.GetGenericArguments(); while (this.XmlReader.Read() && this.XmlReader.NodeType != XmlNodeType.EndElement) { if (this.XmlReader.NodeType != XmlNodeType.Element) { continue; } if (this.XmlReader.Name == "Key") { key = this.ReadElementContent(genericArgumentTypes[0]); } else if (this.XmlReader.Name == "Value") { value = this.ReadElementContent(genericArgumentTypes[1]); } } if (this.XmlReader.NodeType == XmlNodeType.EndElement) { this.XmlReader.ReadEndElement(); } try { ((System.Collections.IDictionary)collection).Add(key, value); } catch (Exception exception) { var ex = new XmlSerializationException("Add method of IDictionary implementing collection threw exception. See inner exception for more details.", exception); ex.Data.Add("Collection Type", type); throw ex; } } } if (this.XmlReader.NodeType == XmlNodeType.EndElement) { this.XmlReader.ReadEndElement(); } } return(collection); } if (type.Name == "KeyValuePair`2" && type.Namespace == "System.Collections.Generic") { if (asAttribute) { throw new XmlSerializationException("The native type KeyValuePair<,> is not deserializable as attribute."); } Object key = null; Object value = null; Type[] genericArgumentTypes = type.GetGenericArguments(); while (this.XmlReader.Read() && this.XmlReader.NodeType != XmlNodeType.EndElement) { if (this.XmlReader.NodeType != XmlNodeType.Element) { continue; } if (this.XmlReader.Name == "Key") { key = this.ReadElementContent(genericArgumentTypes[0]); } else if (this.XmlReader.Name == "Value") { value = this.ReadElementContent(genericArgumentTypes[1]); } } if (this.XmlReader.NodeType == XmlNodeType.EndElement) { this.XmlReader.ReadEndElement(); } return(Activator.CreateInstance(type, key, value)); } String fullTextValue; if (!asAttribute) { fullTextValue = this.XmlReader.ReadElementString(); } else { fullTextValue = this.XmlReader.Value; } try { if (type.IsEnum) { return(Enum.Parse(type, fullTextValue)); } switch (type.FullName) { case "System.String": return(fullTextValue); case "System.Boolean": return(Boolean.Parse(fullTextValue)); case "System.Byte": return(Byte.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.SByte": return(SByte.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Int16": return(Int16.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Int32": return(Int32.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Int64": return(Int64.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.UInt16": return(UInt16.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.UInt32": return(UInt32.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.UInt64": return(UInt64.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Single": return(Single.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Double": return(Double.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Decimal": return(Decimal.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Char": return(Char.Parse(fullTextValue)); case "System.DateTime": return(DateTime.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.DBNull": return(DBNull.Value); case "System.Version": return(new Version(fullTextValue)); case "System.DateTimeOffset": return(DateTimeOffset.Parse(fullTextValue, this.Settings.CultureInfo)); case "System.Guid": return(new Guid(fullTextValue)); case "System.Drawing.Color": return(System.Drawing.ColorTranslator.FromHtml(fullTextValue)); case "System.Drawing.Point": String[] pointData = fullTextValue.Split(','); return(new System.Drawing.Point(Int32.Parse(pointData[0]), Int32.Parse(pointData[1]))); case "System.Drawing.PointF": String[] pointFData = fullTextValue.Split(','); return(new System.Drawing.PointF(Single.Parse(pointFData[0]), Single.Parse(pointFData[1]))); case "System.Drawing.Rectangle": String[] rectangleData = fullTextValue.Split(','); return(new System.Drawing.Rectangle( Int32.Parse(rectangleData[0]), Int32.Parse(rectangleData[1]), Int32.Parse(rectangleData[2]), Int32.Parse(rectangleData[3]) )); case "System.Drawing.RectangleF": String[] rectangleFData = fullTextValue.Split(','); return(new System.Drawing.RectangleF( Single.Parse(rectangleFData[0]), Single.Parse(rectangleFData[1]), Single.Parse(rectangleFData[2]), Single.Parse(rectangleFData[3]) )); /* * case "System.Windows.Point": * return System.Windows.Point.Parse(fullTextValue); * case "System.Windows.Rect": * return System.Windows.Rect.Parse(fullTextValue); * case "System.Windows.Media.Color": * System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml(fullTextValue); * return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B); * case "System.Windows.Media.Matrix": * return System.Windows.Media.Matrix.Parse(fullTextValue); * case "System.Windows.Media.Media3D.Matrix3D": * return System.Windows.Media.Media3D.Matrix3D.Parse(fullTextValue);*/ case "Common.Percentage": return(Percentage.Parse(fullTextValue, this.Settings.CultureInfo)); case "Common.IO.Path": return(new Path(fullTextValue)); } } catch (Exception exception) { var ex = new XmlSerializationException("Parsing of value failed.", exception); ex.Data.Add("Type", type); ex.Data.Add("Value", fullTextValue); throw ex; } var excep = new ArgumentException("The given type is not natively deserializable.", "type"); excep.Data.Add("Type", type); throw excep; }
public void Parse_50Percent() { Percentage percentage = Percentage.Parse("50%"); Check50Percent(percentage); }
public void Parse_50Percent_Whitespace() { Percentage percentage = Percentage.Parse("\t 50% \n"); Check50Percent(percentage); }
//--------------------------------------------------------------------- static SpecificAgesCohortSelector() { defaultPercentage = Percentage.Parse("100%"); }
public void Parse_When_CultureInfoIsSpecified_Then_ResultShouldBeExpectedResult(string input, string culture, double expectedResult) { var result = Percentage.Parse(input, new CultureInfo(culture)); result.Value.Should().Be(expectedResult); }
//--------------------------------------------------------------------- /// <summary> /// Reads a percentage for partial thinning of a cohort age or age /// range. /// </summary> /// <remarks> /// The percentage is bracketed by parentheses. /// </remarks> public static InputValue <Percentage> ReadPercentage(StringReader reader, out int index) { TextReader.SkipWhitespace(reader); index = reader.Index; // Read left parenthesis int nextChar = reader.Peek(); if (nextChar == -1) { throw new InputValueException(); // Missing value } if (nextChar != '(') { throw MakeInputValueException(TextReader.ReadWord(reader), "Value does not start with \"(\""); } StringBuilder valueAsStr = new StringBuilder(); valueAsStr.Append((char)(reader.Read())); // Read whitespace between '(' and percentage valueAsStr.Append(ReadWhitespace(reader)); // Read percentage string word = ReadWord(reader, ')'); if (word == "") { throw MakeInputValueException(valueAsStr.ToString(), "No percentage after \"(\""); } valueAsStr.Append(word); Percentage percentage; try { percentage = Percentage.Parse(word); } catch (System.FormatException exc) { throw MakeInputValueException(valueAsStr.ToString(), exc.Message); } if (percentage < 0.0 || percentage > 1.0) { throw MakeInputValueException(valueAsStr.ToString(), string.Format("{0} is not between 0% and 100%", word)); } // Read whitespace and ')' valueAsStr.Append(ReadWhitespace(reader)); char?ch = TextReader.ReadChar(reader); if (!ch.HasValue) { throw MakeInputValueException(valueAsStr.ToString(), "Missing \")\""); } valueAsStr.Append(ch.Value); if (ch != ')') { throw MakeInputValueException(valueAsStr.ToString(), string.Format("Value ends with \"{0}\" instead of \")\"", ch)); } return(new InputValue <Percentage>(percentage, valueAsStr.ToString())); }