Example #1
0
        public void TryReadDoubleFormat()
        {
            var paddedFormat = DoubleFormatCache.GetOrCreate("e5");

            paddedFormat = DoubleFormatCache.GetOrCreate("F5");
            paddedFormat = DoubleFormatCache.GetOrCreate("#0.00#");
            paddedFormat = DoubleFormatCache.GetOrCreate("#0.0#");
            var sw = Stopwatch.StartNew();
            var n  = 1000000;

            for (int i = 0; i < n; i++)
            {
                paddedFormat = DoubleFormatCache.GetOrCreate("e5");
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| TryRead(\"e5\")     {n:N0} times {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                paddedFormat = DoubleFormatCache.GetOrCreate("#0.00#");
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| TryRead(\"#0.00#\") {n:N0} times {sw.ElapsedMilliseconds} ms");
        }
        public static void TryRead(string text, int pos, string expected, int expectedPos)
        {
            var actual = DoubleFormatCache.GetOrCreate(text, ref pos);

            Assert.AreEqual(expected, actual.Format);
            Assert.AreEqual(expectedPos, pos);
        }
        public static void TryReadError(string text, int pos, string expectedFormatted)
        {
            var actual = DoubleFormatCache.GetOrCreate(text, ref pos);

            Assert.AreEqual(text, actual.Format);
            Assert.AreEqual(0, pos);
            if (expectedFormatted is null)
            {
                // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                Assert.Throws <FormatException>(() => 1.2.ToString(text, CultureInfo.InvariantCulture));
            }
            else
            {
                Assert.AreEqual(expectedFormatted, 1.2.ToString(text, CultureInfo.InvariantCulture));
            }
        }
        public void TryReadError(string text, int pos, string expectedFormatted)
        {
            PaddedFormat actual = DoubleFormatCache.GetOrCreate(text, ref pos);

            Assert.AreEqual(text, actual.Format);
            Assert.AreEqual(0, pos);
            string formatted = null;

            try
            {
                formatted = 1.2.ToString(text);
            }
            catch
            {
            }

            Assert.AreEqual(expectedFormatted, formatted);
        }
        public void TryReadError(string text, int pos, string expectedFormatted)
        {
            PaddedFormat actual = DoubleFormatCache.GetOrCreate(text, ref pos);

            Assert.AreEqual(text, actual.Format);
            Assert.AreEqual(0, pos);
            string formatted = null;

            try
            {
                formatted = 1.2.ToString(text);
            }

            //// ReSharper disable once EmptyGeneralCatchClause dunno what this does.
            catch
            {
            }

            Assert.AreEqual(expectedFormatted, formatted);
        }