Unit tests for the StringBuilderHelper weren't properly localized, containing hard-coded decimal separators - fixed; StringBuilderHelper always used a dot as a decimal separate - behavior changed so it uses the system's current locale (StringBuilderHelper should be identical to the normal StringBuilder except not produce any garbage)

git-svn-id: file:///srv/devel/repo-conversion/nusu@267 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-08 09:55:29 +00:00
parent f3652ac4db
commit 24439da822
3 changed files with 56 additions and 47 deletions

View File

@ -24,11 +24,11 @@ using System.Collections.Generic;
namespace Nuclex.Support.Collections { namespace Nuclex.Support.Collections {
/// <summary>Base class for objects that can be parented to an owner</summary> /// <summary>Base class for objects that can be parented to an owner</summary>
/// <typeparam name="ParentType">Type of the parent object</typeparam> /// <typeparam name="TParent">Type of the parent object</typeparam>
public class Parentable<ParentType> { public class Parentable<TParent> {
/// <summary>The parent object that owns this instance</summary> /// <summary>The parent object that owns this instance</summary>
protected ParentType Parent { protected TParent Parent {
get { return this.parent; } get { return this.parent; }
} }
@ -39,18 +39,18 @@ namespace Nuclex.Support.Collections {
/// current parent will be null. /// current parent will be null.
/// </remarks> /// </remarks>
/// <param name="oldParent">Previous owner of the instance</param> /// <param name="oldParent">Previous owner of the instance</param>
protected virtual void OnParentChanged(ParentType oldParent) { } protected virtual void OnParentChanged(TParent oldParent) { }
/// <summary>Assigns a new parent to this instance</summary> /// <summary>Assigns a new parent to this instance</summary>
internal void SetParent(ParentType parent) { internal void SetParent(TParent parent) {
ParentType oldParent = this.parent; TParent oldParent = this.parent;
this.parent = parent; this.parent = parent;
OnParentChanged(oldParent); OnParentChanged(oldParent);
} }
/// <summary>Current parent of this object</summary> /// <summary>Current parent of this object</summary>
private ParentType parent; private TParent parent;
} }

View File

@ -42,12 +42,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append((byte)255, GarbagePolicy.Avoid); builder.Append((byte)255, GarbagePolicy.Avoid);
Assert.AreEqual("255", builder.ToString()); Assert.AreEqual(((byte)255).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append((byte)255, GarbagePolicy.Accept); builder.Append((byte)255, GarbagePolicy.Accept);
Assert.AreEqual("255", builder.ToString()); Assert.AreEqual(((byte)255).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -58,12 +58,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append((byte)0, GarbagePolicy.Avoid); builder.Append((byte)0, GarbagePolicy.Avoid);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual(((byte)0).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append((byte)0, GarbagePolicy.Accept); builder.Append((byte)0, GarbagePolicy.Accept);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual(((byte)0).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -74,12 +74,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(12345, GarbagePolicy.Avoid); builder.Append(12345, GarbagePolicy.Avoid);
Assert.AreEqual("12345", builder.ToString()); Assert.AreEqual((12345).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(12345, GarbagePolicy.Accept); builder.Append(12345, GarbagePolicy.Accept);
Assert.AreEqual("12345", builder.ToString()); Assert.AreEqual((12345).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -90,12 +90,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(0, GarbagePolicy.Avoid); builder.Append(0, GarbagePolicy.Avoid);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual((0).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(0, GarbagePolicy.Accept); builder.Append(0, GarbagePolicy.Accept);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual((0).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -106,12 +106,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(-12345, GarbagePolicy.Avoid); builder.Append(-12345, GarbagePolicy.Avoid);
Assert.AreEqual("-12345", builder.ToString()); Assert.AreEqual((-12345).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(-12345, GarbagePolicy.Accept); builder.Append(-12345, GarbagePolicy.Accept);
Assert.AreEqual("-12345", builder.ToString()); Assert.AreEqual((-12345).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -122,12 +122,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(12345L, GarbagePolicy.Avoid); builder.Append(12345L, GarbagePolicy.Avoid);
Assert.AreEqual("12345", builder.ToString()); Assert.AreEqual((12345L).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(12345L, GarbagePolicy.Accept); builder.Append(12345L, GarbagePolicy.Accept);
Assert.AreEqual("12345", builder.ToString()); Assert.AreEqual((12345L).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -138,12 +138,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(0L, GarbagePolicy.Avoid); builder.Append(0L, GarbagePolicy.Avoid);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual((0L).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(0L, GarbagePolicy.Accept); builder.Append(0L, GarbagePolicy.Accept);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual((0L).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -154,12 +154,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(-12345L, GarbagePolicy.Avoid); builder.Append(-12345L, GarbagePolicy.Avoid);
Assert.AreEqual("-12345", builder.ToString()); Assert.AreEqual((-12345L).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(-12345L, GarbagePolicy.Accept); builder.Append(-12345L, GarbagePolicy.Accept);
Assert.AreEqual("-12345", builder.ToString()); Assert.AreEqual((-12345L).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -170,12 +170,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(-0.125f, GarbagePolicy.Avoid); builder.Append(-0.125f, GarbagePolicy.Avoid);
Assert.AreEqual("-0.125", builder.ToString()); Assert.AreEqual((-0.125f).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(-0.125f, GarbagePolicy.Accept); builder.Append(-0.125f, GarbagePolicy.Accept);
Assert.AreEqual("-0.125", builder.ToString()); Assert.AreEqual((-0.125f).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -186,12 +186,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(10.0625f, GarbagePolicy.Avoid); builder.Append(10.0625f, GarbagePolicy.Avoid);
Assert.AreEqual("10.0625", builder.ToString()); Assert.AreEqual((10.0625f).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(10.0625f, GarbagePolicy.Accept); builder.Append(10.0625f, GarbagePolicy.Accept);
Assert.AreEqual("10.0625", builder.ToString()); Assert.AreEqual((10.0625f).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -202,12 +202,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(0.00390625f, GarbagePolicy.Avoid); builder.Append(0.00390625f, GarbagePolicy.Avoid);
Assert.AreEqual("0.00390625", builder.ToString()); Assert.AreEqual((0.00390625f).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(0.00390625f, GarbagePolicy.Accept); builder.Append(0.00390625f, GarbagePolicy.Accept);
Assert.AreEqual("0.00390625", builder.ToString()); Assert.AreEqual((0.00390625f).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -218,12 +218,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(1000000000.0f, GarbagePolicy.Avoid); builder.Append(1000000000.0f, GarbagePolicy.Avoid);
Assert.AreEqual("1000000000.0", builder.ToString()); Assert.AreEqual((1000000000.0f).ToString("F1"), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(1000000000.0f, GarbagePolicy.Accept); builder.Append(1000000000.0f, GarbagePolicy.Accept);
Assert.AreEqual("1E+09", builder.ToString()); Assert.AreEqual((1000000000.0f).ToString(), builder.ToString());
} }
/// <summary>Tests whether the number of decimal places can be restricted</summary> /// <summary>Tests whether the number of decimal places can be restricted</summary>
@ -231,7 +231,7 @@ namespace Nuclex.Support {
public void TestAppendFloatLimitDecimalPlaces() { public void TestAppendFloatLimitDecimalPlaces() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(0.00390625f, 3); builder.Append(0.00390625f, 3);
Assert.AreEqual("0.003", builder.ToString()); Assert.AreEqual((0.003f).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -241,7 +241,7 @@ namespace Nuclex.Support {
public void TestAppendFloatWithoutDecimalPlaces() { public void TestAppendFloatWithoutDecimalPlaces() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(0.00390625f, 0); builder.Append(0.00390625f, 0);
Assert.AreEqual("0", builder.ToString()); // Note: no rounding! Assert.AreEqual((0.0f).ToString(), builder.ToString()); // Note: no rounding!
} }
/// <summary> /// <summary>
@ -265,12 +265,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(-32.015625, GarbagePolicy.Avoid); builder.Append(-32.015625, GarbagePolicy.Avoid);
Assert.AreEqual("-32.015625", builder.ToString()); Assert.AreEqual((-32.015625).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(-32.015625, GarbagePolicy.Accept); builder.Append(-32.015625, GarbagePolicy.Accept);
Assert.AreEqual("-32.015625", builder.ToString()); Assert.AreEqual((-32.015625).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -282,12 +282,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(10.0625, GarbagePolicy.Avoid); builder.Append(10.0625, GarbagePolicy.Avoid);
Assert.AreEqual("10.0625", builder.ToString()); Assert.AreEqual((10.0625).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(10.0625, GarbagePolicy.Accept); builder.Append(10.0625, GarbagePolicy.Accept);
Assert.AreEqual("10.0625", builder.ToString()); Assert.AreEqual((10.0625).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -299,12 +299,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(0.00390625, GarbagePolicy.Avoid); builder.Append(0.00390625, GarbagePolicy.Avoid);
Assert.AreEqual("0.00390625", builder.ToString()); Assert.AreEqual((0.00390625).ToString(), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(0.00390625, GarbagePolicy.Accept); builder.Append(0.00390625, GarbagePolicy.Accept);
Assert.AreEqual("0.00390625", builder.ToString()); Assert.AreEqual((0.00390625).ToString(), builder.ToString());
} }
/// <summary> /// <summary>
@ -316,12 +316,12 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.Append(1000000000000000000.0, GarbagePolicy.Avoid); builder.Append(1000000000000000000.0, GarbagePolicy.Avoid);
Assert.AreEqual("1000000000000000000.0", builder.ToString()); Assert.AreEqual((1000000000000000000.0).ToString("F1"), builder.ToString());
builder.Clear(); builder.Clear();
builder.Append(1000000000000000000.0, GarbagePolicy.Accept); builder.Append(1000000000000000000.0, GarbagePolicy.Accept);
Assert.AreEqual("1E+18", builder.ToString()); Assert.AreEqual((1000000000000000000.0).ToString(), builder.ToString());
} }
/// <summary>Tests whether the number of decimal places can be restricted</summary> /// <summary>Tests whether the number of decimal places can be restricted</summary>
@ -330,7 +330,7 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
StringBuilderHelper.Append(builder, 0.00390625, 3); StringBuilderHelper.Append(builder, 0.00390625, 3);
Assert.AreEqual("0.003", builder.ToString()); // Note: no rounding! Assert.AreEqual((0.003).ToString(), builder.ToString()); // Note: no rounding!
} }
/// <summary> /// <summary>
@ -341,7 +341,7 @@ namespace Nuclex.Support {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
StringBuilderHelper.Append(builder, 0.00390625, 0); StringBuilderHelper.Append(builder, 0.00390625, 0);
Assert.AreEqual("0", builder.ToString()); Assert.AreEqual((0.0).ToString(), builder.ToString());
} }
/// <summary> /// <summary>

View File

@ -19,8 +19,7 @@ License along with this library
#endregion #endregion
using System; using System;
using System.Collections.Generic; using System.Globalization;
using System.Diagnostics;
using System.Text; using System.Text;
namespace Nuclex.Support { namespace Nuclex.Support {
@ -33,6 +32,9 @@ namespace Nuclex.Support {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
}; };
/// <summary>Characters used to separate decimal places in a number</summary>
private static readonly string decimalSeparator = getDecimalSeparator();
/// <summary>Clears the contents of a string builder</summary> /// <summary>Clears the contents of a string builder</summary>
/// <param name="builder">String builder that will be cleared</param> /// <param name="builder">String builder that will be cleared</param>
public static void Clear(this StringBuilder builder) { public static void Clear(this StringBuilder builder) {
@ -195,7 +197,7 @@ namespace Nuclex.Support {
} }
if(decimalPlaces > 0) { if(decimalPlaces > 0) {
builder.Append('.'); builder.Append(decimalSeparator);
// Build the fractional part // Build the fractional part
if(fractional == 0) { if(fractional == 0) {
@ -303,7 +305,7 @@ namespace Nuclex.Support {
} }
if(decimalPlaces > 0) { if(decimalPlaces > 0) {
builder.Append('.'); builder.Append(decimalSeparator);
// Build the fractional part // Build the fractional part
if(fractional == 0) { if(fractional == 0) {
@ -364,6 +366,13 @@ namespace Nuclex.Support {
builder.Append(numbers[digit]); builder.Append(numbers[digit]);
} }
/// <summary>Looks up the decimal separator used by the current system</summary>
/// <returns></returns>
private static string getDecimalSeparator() {
NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
return numberFormatInfo.NumberDecimalSeparator;
}
} }
} // namespace Nuclex.Support } // namespace Nuclex.Support