Exemplo n.º 1
0
        public void TestParseJSONNumber()
        {
            if (CBORDataUtilities.ParseJSONNumber("100.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-100.", false, false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "100.e+20",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "-100.e20",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("100.e20", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber("+0.1", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber("0.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-0.", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0g.1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.e+20", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-0.e20", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.e20", false, false) != null)
            {
                Assert.Fail();
            }

            if (CBORDataUtilities.ParseJSONNumber(null, false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(
                    String.Empty,
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("true", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0..1", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.1xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.xyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5exyz", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5q+88", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.5ee88", false, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e-2x") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e+2x") != null)
            {
                Assert.Fail();
            }
            CBORObject cbor = CBORDataUtilities.ParseJSONNumber("2e-2147483648");

            CBORTestCommon.AssertJSONSer(cbor, "2E-2147483648");
            if (CBORDataUtilities.ParseJSONNumber(
                    "0.5e+xyz",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (
                CBORDataUtilities.ParseJSONNumber(
                    "0.5e+88xyz",
                    false,
                    false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0000") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x1") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0xf") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x20") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0x01") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".2") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber(".05") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-.2") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-.05") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.e0") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("23.e1") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("0.") != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("5.2", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("5e+1", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5.2", true, false) != null)
            {
                Assert.Fail();
            }
            if (CBORDataUtilities.ParseJSONNumber("-5e+1", true, false) != null)
            {
                Assert.Fail();
            }

            TestCommon.CompareTestEqual(
                ToObjectTest.TestToFromObjectRoundTrip(230).AsNumber(),
                CBORDataUtilities.ParseJSONNumber("23.0e01").AsNumber());
            TestCommon.CompareTestEqual(
                ToObjectTest.TestToFromObjectRoundTrip(23).AsNumber(),
                CBORDataUtilities.ParseJSONNumber("23.0e00").AsNumber());
            cbor = CBORDataUtilities.ParseJSONNumber(
                "1e+99999999999999999999999999",
                false,
                false);
            Assert.IsTrue(cbor != null);
            Assert.IsFalse(cbor.CanFitInDouble());
            CBORTestCommon.AssertRoundTrip(cbor);
        }
Exemplo n.º 2
0
        public void TestDecimalString()
        {
            var fr = new RandomGenerator();

            // var sw = new System.Diagnostics.Stopwatch();
            // var sw2 = new System.Diagnostics.Stopwatch();
            // var sw3 = new System.Diagnostics.Stopwatch();
            for (var i = 0; i < 10000; ++i)
            {
                if (i % 100 == 0)
                {
                    // Console.WriteLine(i + " sw=" +
                    // sw.ElapsedMilliseconds + ", " + (sw2.ElapsedMilliseconds) +
                    // ", " + (sw3.ElapsedMilliseconds));
                }
                // sw3.Start();
                EDecimal ed = RandomObjects.RandomEDecimal(fr);
                // sw3.Stop();
                // Reduce to Decimal128. Without this reduction,
                // Decimal.Parse would run significantly more slowly
                // on average for random
                // EDecimals than
                // EDecimal.FromString(CliDecimal) does.
                // Decimal128 covers all numbers representable
                // in a CliDecimal.
                ed = ed.RoundToPrecision(EContext.Decimal128);

                if (!ed.IsFinite)
                {
                    continue;
                }
                string  edString = ed.ToString();
                decimal d;
                try {
                    System.Globalization.NumberStyles numstyles =
                        System.Globalization.NumberStyles.AllowExponent |
                        System.Globalization.NumberStyles.Number;
                    // sw.Start();
                    d = Decimal.Parse(
                        edString,
                        numstyles,
                        System.Globalization.CultureInfo.InvariantCulture);
                    // sw.Stop();
                    // sw2.Start();
                    EDecimal ed3 = EDecimal.FromString(
                        edString,
                        EContext.CliDecimal);
                    // sw2.Stop();
                    var edd = (EDecimal)d;
                    if (!edd.Equals(ed3))
                    {
                        string msg = ed.ToString() + " (expanded: " +
                                     EDecimal.FromString(ed.ToString()) + ")";
                        TestCommon.CompareTestEqual(
                            (EDecimal)d,
                            ed3,
                            msg);
                    }
                    // sw3.Stop();
                } catch (OverflowException ex) {
                    EDecimal ed2 = EDecimal.FromString(
                        edString,
                        EContext.CliDecimal);
                    if (!ed2.IsInfinity())
                    {
                        Assert.Fail(edString + "\n" + ex.ToString());
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static String RandomDecimalString(
            IRandomGenExtended r,
            bool extended,
            bool limitedExponent)
        {
            if (r == null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            if (r.GetInt32(100) < 95)
            {
                return(RandomDecimalStringShort(r, extended));
            }
            long count = ((long)r.GetInt32(MaxNumberLength) *
                          r.GetInt32(MaxNumberLength)) / MaxNumberLength;

            count = ((long)count * r.GetInt32(MaxNumberLength)) / MaxNumberLength;
            count = Math.Max(1, count);
            long afterPointCount = 0;
            long exponentCount   = 0;
            var  smallExponent   = false;

            if (r.GetInt32(2) == 0)
            {
                afterPointCount = ((long)r.GetInt32(MaxNumberLength) *
                                   r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                afterPointCount = ((long)afterPointCount *
                                   r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                afterPointCount = Math.Max(1, afterPointCount);
            }
            if (r.GetInt32(2) == 0)
            {
                if (limitedExponent || r.GetInt32(10) > 0)
                {
                    exponentCount = 5;
                }
                else
                {
                    exponentCount = ((long)r.GetInt32(MaxNumberLength) *
                                     r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                    exponentCount = ((long)exponentCount *
                                     r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                    exponentCount = ((long)exponentCount *
                                     r.GetInt32(MaxNumberLength)) / MaxNumberLength;
                    exponentCount = Math.Max(1, exponentCount);
                }
            }
            var bufferSize = (int)Math.Min(
                Int32.MaxValue,
                8 + count + afterPointCount + exponentCount);
            var sb = new StringBuilder(bufferSize);

            if (r.GetInt32(2) == 0)
            {
                sb.Append('-');
            }
            if (!extended)
            {
                sb.Append((char)('1' + r.GetInt32(9)));
                --count;
            }
            AppendRandomDecimalsLong(r, sb, count);
            if (afterPointCount > 0)
            {
                sb.Append('.');
                AppendRandomDecimalsLong(r, sb, afterPointCount);
            }
            if (exponentCount > 0)
            {
                int rr = r.GetInt32(3);
                if (rr == 0)
                {
                    sb.Append('E');
                }
                else if (rr == 1)
                {
                    sb.Append("E+");
                }
                else if (rr == 2)
                {
                    sb.Append("E-");
                }
                if (smallExponent)
                {
                    sb.Append(TestCommon.IntToString(r.GetInt32(10000)));
                }
                else
                {
                    AppendRandomDecimalsLong(r, sb, exponentCount);
                }
            }
            return(sb.ToString());
        }