Skip to content

Commit

Permalink
Merge pull request #193 from jnyrup/FloatingPoint
Browse files Browse the repository at this point in the history
Use InvariantCulture when formatting floating points
  • Loading branch information
gsvgit authored Oct 28, 2019
2 parents ddedfbc + 20bc40a commit 0416692
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/QuickGraph.Graphviz/Dot/GraphvizEdge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace QuickGraph.Graphviz.Dot
using System;
using System.Collections;
using System.Drawing;
using System.Globalization;
using System.IO;

public class GraphvizEdge
Expand Down Expand Up @@ -46,6 +47,18 @@ internal string GenerateDot(Hashtable pairs)
writer.Write("{0}=\"{1}\"", entry.Key.ToString(), entry.Value.ToString());
continue;
}
if (entry.Value is float)
{
float floatValue = (float)entry.Value;
writer.Write("{0}={1}", entry.Key.ToString(), floatValue.ToString(CultureInfo.InvariantCulture));
continue;
}
if (entry.Value is double)
{
double doubleValue = (double)entry.Value;
writer.Write("{0}={1}", entry.Key.ToString(), doubleValue.ToString(CultureInfo.InvariantCulture));
continue;
}
if (entry.Value is GraphvizEdgeDirection)
{
writer.Write("{0}={1}", entry.Key.ToString(), ((GraphvizEdgeDirection) entry.Value).ToString().ToLower());
Expand Down
13 changes: 13 additions & 0 deletions src/QuickGraph.Graphviz/Dot/GraphvizGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace QuickGraph.Graphviz.Dot
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;

public class GraphvizGraph
Expand Down Expand Up @@ -53,6 +54,18 @@ internal string GenerateDot(Hashtable pairs)
entries.Add(String.Format("{0}=\"{1}\"", entry.Key.ToString(), entry.Value.ToString()));
continue;
}
if (entry.Value is float)
{
float floatValue = (float)entry.Value;
entries.Add(String.Format("{0}={1}", entry.Key.ToString(), floatValue.ToString(CultureInfo.InvariantCulture)));
continue;
}
if (entry.Value is double)
{
double doubleValue = (double)entry.Value;
entries.Add(String.Format("{0}={1}", entry.Key.ToString(), doubleValue.ToString(CultureInfo.InvariantCulture)));
continue;
}
if (entry.Value is Color)
{
Color color = (Color) entry.Value;
Expand Down
13 changes: 13 additions & 0 deletions src/QuickGraph.Graphviz/Dot/GraphvizVertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace QuickGraph.Graphviz.Dot
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Globalization;

public class GraphvizVertex
{
Expand Down Expand Up @@ -53,6 +54,18 @@ internal string GenerateDot(Dictionary<string, object> pairs)
writer.Write("{0}=\"{1}\"", entry.Key, entry.Value.ToString());
continue;
}
if (entry.Value is float)
{
float floatValue = (float)entry.Value;
writer.Write("{0}={1}", entry.Key, floatValue.ToString(CultureInfo.InvariantCulture));
continue;
}
if (entry.Value is double)
{
double doubleValue = (double)entry.Value;
writer.Write("{0}={1}", entry.Key, doubleValue.ToString(CultureInfo.InvariantCulture));
continue;
}
if (entry.Value is GraphvizVertexShape)
{
writer.Write("{0}={1}", entry.Key, ((GraphvizVertexShape) entry.Value).ToString().ToLower());
Expand Down
113 changes: 113 additions & 0 deletions tests/QuickGraph.Tests/GraphVizTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickGraph.Graphviz;
using System.Drawing;
using System.Globalization;
using System.Threading;

namespace QuickGraph.Tests
{
[TestClass]
public class GraphVizTests
{
private static CultureInfo oldCulture;

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
oldCulture = Thread.CurrentThread.CurrentCulture;

// Germany (de-DE) uses comma as decimal separator
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
}

[ClassCleanup]
public static void ClassCleanup()
{
Thread.CurrentThread.CurrentCulture = oldCulture;
}

[TestMethod]
public void CommonVertexFormat_FontSize_UseDecimalPointForFloats()
{
var graph = new AdjacencyGraph<string, IEdge<string>>(false);
graph.AddVerticesAndEdge(new Edge<string>("s", "t"));

var gv = new GraphvizAlgorithm<string, IEdge<string>>(graph);
gv.CommonVertexFormat.Font = new Font(SystemFonts.DefaultFont.FontFamily, emSize: 1.75f);

var res = gv.Generate();

StringAssert.Contains(res, "fontsize=1.75", "Formatting floating points should always use dot as decimal separator");
}

[TestMethod]
public void CommonVertexFormat_Z_UseDecimalPointForDoubles()
{
var graph = new AdjacencyGraph<string, IEdge<string>>(false);
graph.AddVerticesAndEdge(new Edge<string>("s", "t"));

var gv = new GraphvizAlgorithm<string, IEdge<string>>(graph);
gv.CommonVertexFormat.Z = 1.75;

var res = gv.Generate();

StringAssert.Contains(res, "z=1.75", "Formatting floating points should always use dot as decimal separator");
}

[TestMethod]
public void CommonEdgeFormat_FontSize_UseDecimalPointForFloats()
{
var graph = new AdjacencyGraph<string, IEdge<string>>(false);
graph.AddVerticesAndEdge(new Edge<string>("s", "t"));

var gv = new GraphvizAlgorithm<string, IEdge<string>>(graph);
gv.CommonEdgeFormat.Font = new Font(SystemFonts.DefaultFont.FontFamily, emSize: 1.75f);

var res = gv.Generate();

StringAssert.Contains(res, "fontsize=1.75", "Formatting floating points should always use dot as decimal separator");
}

[TestMethod]
public void CommonEdgeFormat_Weight_UseDecimalPointForDoubles()
{
var graph = new AdjacencyGraph<string, IEdge<string>>(false);
graph.AddVerticesAndEdge(new Edge<string>("s", "t"));

var gv = new GraphvizAlgorithm<string, IEdge<string>>(graph);
gv.CommonEdgeFormat.Weight = 1.75;

var res = gv.Generate();

StringAssert.Contains(res, "weight=1.75", "Formatting floating points should always use dot as decimal separator");
}

[TestMethod]
public void GraphFormat_RankSeparation_UseDecimalPointForDoubles()
{
var graph = new AdjacencyGraph<string, IEdge<string>>(false);
graph.AddVerticesAndEdge(new Edge<string>("s", "t"));

var gv = new GraphvizAlgorithm<string, IEdge<string>>(graph);
gv.GraphFormat.RankSeparation = 0.75d;

var res = gv.Generate();

StringAssert.Contains(res, "ranksep=0.75", "Formatting floating points should always use dot as decimal separator");
}

[TestMethod]
public void GraphFormat_FontSize_UseDecimalPointForFloats()
{
var graph = new AdjacencyGraph<string, IEdge<string>>(false);
graph.AddVerticesAndEdge(new Edge<string>("s", "t"));

var gv = new GraphvizAlgorithm<string, IEdge<string>>(graph);
gv.GraphFormat.Font = new Font(SystemFonts.DefaultFont.FontFamily, emSize: 1.75f);

var res = gv.Generate();

StringAssert.Contains(res, "fontsize=1.75", "Formatting floating points should always use dot as decimal separator");
}
}
}
1 change: 1 addition & 0 deletions tests/QuickGraph.Tests/QuickGraph.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@
<Compile Include="GraphExtensionsTest.ToAdjacencyGraph04.g.cs">
<DependentUpon>GraphExtensionsTest.cs</DependentUpon>
</Compile>
<Compile Include="GraphVizTests.cs" />
<Compile Include="MutableVertexAndEdgeListGraphTest.cs" />
<Compile Include="Properties\PexAssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 0416692

Please sign in to comment.