From b2374b1f97e40bc61b4ba455ec8dce3879db08f4 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Fri, 19 Jun 2020 20:08:42 -0700 Subject: [PATCH] Run ToString tests within the invariant culture --- .../Simulators.Tests/DebuggingToolsTests.cs | 72 +++++++++++-------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/src/Simulation/Simulators.Tests/DebuggingToolsTests.cs b/src/Simulation/Simulators.Tests/DebuggingToolsTests.cs index 4885cb14d54..279a6185f00 100644 --- a/src/Simulation/Simulators.Tests/DebuggingToolsTests.cs +++ b/src/Simulation/Simulators.Tests/DebuggingToolsTests.cs @@ -2,8 +2,8 @@ // Licensed under the MIT License. using System; -using System.Collections.Generic; -using System.Text; +using System.Globalization; +using System.Threading; using Microsoft.Quantum.Simulation.Core; using Microsoft.Quantum.Tests.CoreOperations; @@ -18,36 +18,35 @@ namespace Microsoft.Quantum.Simulation.Simulators.Tests /// public class DebuggingToolsTests { - [Fact] - public void ToStringTests() + public void ToStringTests() => Helper.RunWithMultipleSimulators(qsim => { - Helper.RunWithMultipleSimulators((qsim) => + var _ = AbstractCallable._; + + var dump = qsim.Get(typeof(Microsoft.Quantum.Extensions.Diagnostics.DumpMachine<>)); + var trace = qsim.Get(typeof(Circuits.Generics.Trace<>)); + var x = qsim.Get(); + var q2 = new FreeQubit(2) as Qubit; + var Q = new Q(q2); + var Qs = new QArray(q2); + var qs = new Qs(Qs); + var udtOp = new U3(x); + var udtQ = new Q(q2); + var t1 = new QTuple<(long, QRange, (Qubit, IUnitary))>((1L, new QRange(10, -2, 4), (q2, x))); + var t4 = new T4((3L, (1.1, false, Result.One))); + var t5 = new T5((Pauli.PauliX, Qs, qs, Q)); + + var d_1 = dump.Partial(_); + var d_2 = d_1.Partial(_); + var x_1 = x.Partial(new Func(q => q)); + var x_2 = x_1.Partial(new Func(q => q)); + var x_3 = x.Partial>(_); + + var t_1 = trace.Adjoint.Partial(_); + var t_2 = t_1.Controlled.Partial(_); + + WithInvariantCulture(() => { - var _ = AbstractCallable._; - - var dump = qsim.Get(typeof(Microsoft.Quantum.Extensions.Diagnostics.DumpMachine<>)); - var trace = qsim.Get(typeof(Circuits.Generics.Trace<>)); - var x = qsim.Get(); - var q2 = new FreeQubit(2) as Qubit; - var Q = new Q(q2); - var Qs = new QArray(q2); - var qs = new Qs(Qs); - var udtOp = new U3(x); - var udtQ = new Q(q2); - var t1 = new QTuple<(long, QRange, (Qubit, IUnitary))>((1L, new QRange(10, -2, 4), (q2, x))); - var t4 = new T4((3L, (1.1, false, Result.One))); - var t5 = new T5((Pauli.PauliX, Qs, qs, Q)); - - var d_1 = dump.Partial(_); - var d_2 = d_1.Partial(_); - var x_1 = x.Partial(new Func(q => q)); - var x_2 = x_1.Partial(new Func(q => q)); - var x_3 = x.Partial>(_); - - var t_1 = trace.Adjoint.Partial(_); - var t_2 = t_1.Controlled.Partial(_); - Assert.Equal("()", QVoid.Instance.ToString()); Assert.Equal("_", _.ToString()); Assert.Equal("U3(X)", udtOp.ToString()); @@ -75,7 +74,7 @@ public void ToStringTests() Assert.Equal("(Adjoint Trace){_}", t_1.ToString()); Assert.Equal("(Adjoint (Controlled (Adjoint Trace){_}){_})", t_2.Adjoint.ToString()); }); - } + }); [Fact] public void QSharpTypeTests() @@ -277,5 +276,18 @@ public void GenericPartialDebuggerProxy() TestOneProxy("", g_3); }); } + + /// + /// Changes the current thread culture to the invariant culture, runs the action, and then restores the original + /// thread culture. + /// + /// The action to run within the invariant culture. + private static void WithInvariantCulture(System.Action action) + { + var culture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; + action(); + Thread.CurrentThread.CurrentCulture = culture; + } } }