コード例 #1
0
        private void issue23_2()
        {
            double?x = FastDoubleParser.ParseDouble("5e0012");

            Assert.True(x.HasValue, "could not parse 5e0012.");
            Assert.True(x == 5e12, "does not map to 5e0012.");
        }
コード例 #2
0
        private void issue32()
        {
            double?x = FastDoubleParser.ParseDouble("-0");

            Assert.True(x.HasValue, "could not parse -zero.");
            Assert.True(x == 0, "-zero does not map to zero.");
        }
コード例 #3
0
        private void issue23()
        {
            double?x = FastDoubleParser.ParseDouble("0e+42949672970");

            Assert.True(x.HasValue, "could not parse zero.");
            Assert.True(x == 0, "zero does not map to zero.");
        }
コード例 #4
0
ファイル: TestFloatParser.cs プロジェクト: LordJZ/csFastFloat
 unsafe public void DoubleParser_HandleInvalidInput_works(string input, double res)
 {
     fixed(char *p = input)
     {
         Assert.Equal(res, FastDoubleParser.HandleInvalidInput(p, p + input.Length));
     }
 }
コード例 #5
0
        private void issue13()
        {
            double?x = FastDoubleParser.ParseDouble("0");

            Assert.True(x.HasValue, "Parsed");
            Assert.True(x == 0, "Maps to 0");
        }
コード例 #6
0
        public void cas_compute_float_64_1()
        {
            for (int p = -306; p <= 308; p++)
            {
                if (p == 23)
                {
                    p++;
                }

                //   var sut = new DoubleBinaryFormat();
                var am = FastDoubleParser.ComputeFloat(q: p, w: 1);

                double?d = FastDoubleParser.ToFloat(false, am);

                if (!d.HasValue)
                {
                    throw new ApplicationException($"Can't parse p=> {p}");
                }

                if (d != testing_power_of_ten[p + 307])
                {
                    throw new ApplicationException($"bad parsing p=> {p}");
                }
            }
        }
コード例 #7
0
ファイル: SupplFilesTest.cs プロジェクト: LordJZ/csFastFloat
        /// <summary>
        /// Check every line of the file which are required to be the following format
        /// 0000 00000000 0000000000000000 0e3
        /// ssss ffffffff dddddddddddddddd $$$$.....
        /// short float double string
        /// </summary>
        /// <param name="fileName">file to test</param>
        private static void VerifyFile(string fileName)
        {
            var fs = System.IO.File.OpenText(fileName);

            while (!fs.EndOfStream)
            {
                string curntLine = fs.ReadLine();

                try
                {
                    var sut = curntLine.Split();
                    if (sut.Length != 4)
                    {
                        throw new Exception($"Invalid file in file {curntLine}");
                    }

                    // Make sense of s,f,d
                    short  _s = ShortFromHexString(sut[0]);
                    float  _f = FloatFromHexString(sut[1]);
                    double _d = DoubleFromHexString(sut[2]);

                    // parse and assert equality
                    float f = FastFloatParser.ParseFloat(sut[3]);
                    Assert.True(_f == f);
                    double d = FastDoubleParser.ParseDouble(sut[3]);
                    Assert.True(_d == d);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"parsing error on : {curntLine}. {ex.Message}");
                }
            }
            fs.Close();
        }
コード例 #8
0
        unsafe public void ParseNumber_Works_Scnenarios()
        {
            Skip.If(base.NoDiffToolDetected(), "No diff tool detected");


            Dictionary <string, string> sut = new Dictionary <string, string>();

            sut.Add("leading zeros", "001");
            sut.Add("leading zeros neg", "-001");

            sut.Add("zero", "0");

            sut.Add("double", "0.00000000000000212312312");
            sut.Add("double neg", "-0.00000000000000212312312");
            sut.Add("int", "1");
            sut.Add("int neg", "-1");

            sut.Add("autreint ", "123124");
            sut.Add("autreint neg", "-123124");

            sut.Add("notation scientifique", "4.56E+2");
            sut.Add("notation scientifique neg", "-4.56E-2");

            sut.Add("notation scientifique 2", "4.5644E+2");
            sut.Add("notation scientifique 2 neg", "-4.5644E-2");

            sut.Add("notation scientifique 3", "4424.5644E+22");
            sut.Add("notation scientifique 3 neg", "-4424.5644E-22");

            sut.Add("notation scientifique 4", "4424.5644E+223");
            sut.Add("notation scientifique 4 neg", "-4424.5644E-223");
            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair <string, string> kv in sut)
            {
                sb.AppendLine($"Scenario : {kv.Key} ");
                sb.AppendLine($"Valeur   : {kv.Value} ");

                fixed(char *p = kv.Value)
                {
                    char *pend = p + kv.Value.Length;
                    var   res  = FastDoubleParser.ParseDouble(p, pend);

                    sb.AppendLine($"Resultat : {res}");
                    sb.AppendLine();
                }
            }

            // We do not want to fail the tests when the user has not
            // configured a diff tool.
            try
            {
                VerifyData(sb.ToString());
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #9
0
 unsafe public void DoubleParser_HandleInvalidInput_works(string input, double sut)
 {
     fixed(char *p = input)
     {
         Assert.True(FastDoubleParser.TryHandleInvalidInput(p, p + input.Length, out int _, out double result));
         Assert.Equal(sut, result);
     }
 }
コード例 #10
0
        private void Issue19()
        {
            string sut = @"3.14e";

            for (int i = 0; i != 16; i++)
            {
                double d = FastDoubleParser.ParseDouble(sut.Substring(0, sut.Length - i));
                Assert.Equal(Math.PI, d);
            }
        }
コード例 #11
0
        public double FastParserUtf8_()
        {
            double max = double.MinValue;

            foreach (byte[] l in _linesUtf8)
            {
                double d = FastDoubleParser.ParseDouble(l);
                max = d > max ? d : max;
            }
            return(max);
        }
コード例 #12
0
        private void Issue_74()
        {
            // Consumed=0 vs raising Exceptions
            // Try parse should retrun false with consumed and result =0
            Assert.False(FastDoubleParser.TryParseDouble("", out int nbChar, out double result));
            Assert.Equal(0, nbChar);
            Assert.Equal(0, result);

            // as ParseDouble should throw with an empty input string
            Assert.Throws <System.ArgumentException>(() => FastDoubleParser.ParseDouble(string.Empty));
        }
コード例 #13
0
        public unsafe double FastParser_()
        {
            double max = double.MinValue;

            foreach (string l in _lines)
            {
                double d = FastDoubleParser.ParseDouble(l);
                max = d > max ? d : max;
            }
            return(max);
        }
コード例 #14
0
ファイル: Program.cs プロジェクト: gfoidl/csFastFloat
        private static double find_max_fast_float(string[] lines)
        {
            double max = double.MinValue;

            foreach (string l in lines)
            {
                double x = FastDoubleParser.ParseDouble(l);
                max = max > x ? max : x;
            }

            return(max);
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: gfoidl/csFastFloat
        private static double find_max_fast_float_utf8(byte[][] lines)
        {
            double max = double.MinValue;

            foreach (var l in lines)
            {
                double x = FastDoubleParser.ParseDouble(l);
                max = max > x ? max : x;
            }

            return(max);
        }
コード例 #16
0
        private static double find_max_fast_float(string[] lines)
        {
            double x;
            double answer = 0;

            foreach (string l in lines)
            {
                x      = FastDoubleParser.ParseDouble(l);
                answer = answer > x ? answer : x;
            }

            return(answer);
        }
コード例 #17
0
        public double FastTryParserUtf8_()
        {
            double max = double.MinValue;

            foreach (byte[] l in _linesUtf8)
            {
                if (FastDoubleParser.TryParseDouble(l, out double d))
                {
                    max = d > max ? d : max;
                }
            }
            return(max);
        }
コード例 #18
0
        public unsafe double FastParser_Try()
        {
            double max = double.MinValue;

            foreach (string l in _lines)
            {
                if (FastDoubleParser.TryParseDouble(l, out double d))
                {
                    max = d > max ? d : max;
                }
            }
            return(max);
        }
コード例 #19
0
ファイル: TestFloatParser.cs プロジェクト: LordJZ/csFastFloat
        unsafe public void ParseNumber_Works_Scnenarios()
        {
            Dictionary <string, string> sut = new Dictionary <string, string>();

            sut.Add("leading spaces", "  1");
            sut.Add("leading spaces neg", "  -1");

            sut.Add("leading zeros", "001");
            sut.Add("leading zeros neg", "-001");

            sut.Add("zero", "0");
            sut.Add("zero neg", "-0");

            sut.Add("double", "0.00000000000000212312312");
            sut.Add("double neg", "-0.00000000000000212312312");
            sut.Add("int", "1");
            sut.Add("int neg", "-1");

            sut.Add("autreint ", "123124");
            sut.Add("autreint neg", "-123124");

            sut.Add("notation scientifique", "4.56E+2");
            sut.Add("notation scientifique neg", "-4.56E-2");

            sut.Add("notation scientifique 2", "4.5644E+2");
            sut.Add("notation scientifique 2 neg", "-4.5644E-2");

            sut.Add("notation scientifique 3", "4424.5644E+22");
            sut.Add("notation scientifique 3 neg", "-4424.5644E-22");

            sut.Add("notation scientifique 4", "4424.5644E+223");
            sut.Add("notation scientifique 4 neg", "-4424.5644E-223");
            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair <string, string> kv in sut)
            {
                sb.AppendLine($"Scenario : {kv.Key} ");
                sb.AppendLine($"Valeur   : {kv.Value} ");

                fixed(char *p = kv.Value)
                {
                    char *pend = p + kv.Value.Length;
                    var   res  = FastDoubleParser.ParseNumber(p, pend);

                    sb.AppendLine($"Resultat : {res}");
                    sb.AppendLine();
                }
            }

            VerifyData(sb.ToString());
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: gfoidl/csFastFloat
        private static double find_max_fast_float_try_utf8(byte[][] lines)
        {
            double max = double.MinValue;

            foreach (var l in lines)
            {
                if (FastDoubleParser.TryParseDouble(l, out double x))
                {
                    max = max > x ? max : x;
                }
                else
                {
                    Console.WriteLine("bug");
                }
            }

            return(max);
        }
コード例 #21
0
        /// <summary>
        /// Check every line of the file which are required to be the following format
        /// 0000 00000000 0000000000000000 0e3
        /// ssss ffffffff dddddddddddddddd $$$$.....
        /// short float double string
        /// </summary>
        /// <param name="fileName">file to test</param>
        private static void VerifyFile(string fileName)
        {
            var fs        = System.IO.File.OpenText(fileName);
            int counter   = 0;
            int testCount = 0;

            while (!fs.EndOfStream)
            {
                string curntLine = fs.ReadLine();
                var    sut       = curntLine.Split();
                if (sut.Length != 4)
                {
                    throw new Exception($"Invalid file in file {curntLine}");
                }

                // Make sense of s,f,d
                short  _s = ShortFromHexString(sut[0]);
                float  _f = FloatFromHexString(sut[1]);
                double _d = DoubleFromHexString(sut[2]);

                // parse and assert equality
                float f = FastFloatParser.ParseFloat(sut[3]);
                Assert.True(_f == f);
                double d = FastDoubleParser.ParseDouble(sut[3]);
                Assert.True(_d == d);

                // parse and assert equality
                float f_span = FastFloatParser.ParseFloat(sut[3].AsSpan());
                Assert.True(_f == f_span);
                double d_span = FastDoubleParser.ParseDouble(sut[3].AsSpan());
                Assert.True(_d == d_span);

                // parse and assert equality
                float f_utf8 = FastFloatParser.ParseFloat(System.Text.Encoding.UTF8.GetBytes(sut[3]));
                Assert.True(_f == f_utf8);
                double d_utf8 = FastDoubleParser.ParseDouble(System.Text.Encoding.UTF8.GetBytes(sut[3]));
                Assert.True(_d == d_utf8);

                counter++;
                testCount += 6;
            }
            fs.Close();
            Console.WriteLine($"processed {counter} numbers, {testCount} tests in total.");
        }
コード例 #22
0
        public void ParseDouble_charConsumed_WholeString()
        {
            Skip.If(base.NoDiffToolDetected(), "No diff tool detected");

            string        sut = "1.23213 321e10 3132e-1";
            StringBuilder sb  = new StringBuilder();

            long pos = 0;

            while (pos < sut.Length)
            {
                int nbCarConsumed;
                var res = FastDoubleParser.ParseDouble(sut.AsSpan().Slice((int)pos), out nbCarConsumed);
                sb.AppendLine($"Sut :{sut.Substring((int)pos)}  Result : {res.ToString(CultureInfo.InvariantCulture)} :  Consumed :  { nbCarConsumed }");
                pos += nbCarConsumed;
            }

            VerifyData(sb.ToString());
        }
コード例 #23
0
        unsafe public void cas_compute_float_64_2()
        {
            for (int p = -1000; p <= 308; p++)
            {
                string sut = $"1e{p}";
                fixed(char *pstart = sut)
                {
                    double?d = FastDoubleParser.ParseDouble(pstart, pstart + sut.Length);

                    if (!d.HasValue)
                    {
                        throw new ApplicationException($"Can't parse p=> 1e{p}");
                    }

                    double expected = ((p >= -307) ? testing_power_of_ten[p + 307] : Math.Pow(10, p));

                    Assert.Equal(expected, d.Value);
                }
            }
        }
コード例 #24
0
        private void Issue8()
        {
            string sut = @"3."
                         + "141592653589793238462643383279502884197169399375105820974944592307816406"
                         + "286208998628034825342117067982148086513282306647093844609550582231725359"
                         + "408128481117450284102701938521105559644622948954930381964428810975665933"
                         + "446128475648233786783165271201909145648566923460348610454326648213393607"
                         + "260249141273724587006606315588174881520920962829254091715364367892590360"
                         + "011330530548820466521384146951941511609433057270365759591953092186117381"
                         + "932611793105118548074462379962749567351885752724891227938183011949129833"
                         + "673362440656643086021394946395224737190702179860943702770539217176293176"
                         + "752384674818467669405132000568127145263560827785771342757789609173637178"
                         + "721468440901224953430146549585371050792279689258923542019956112129021960"
                         + "864034418159813629774771309960518707211349999998372978";

            for (int i = 0; i != 16; i++)
            {
                double d = FastDoubleParser.ParseDouble(sut.Substring(0, sut.Length - i));
                Assert.Equal(Math.PI, d);
            }
        }
コード例 #25
0
 public void TryParse_NeverThrows(string sut)
 {
     Assert.False(FastDoubleParser.TryParseDouble(sut, out _));
 }
コード例 #26
0
 unsafe public void ParseDouble_Throws_When_Invalid(string sut) => Assert.Throws <System.ArgumentException>(() => FastDoubleParser.ParseDouble(sut));
コード例 #27
0
 unsafe public void ParseDouble_Throws_When_Empty() => Assert.Throws <System.ArgumentException>(() => FastDoubleParser.ParseDouble(string.Empty));
コード例 #28
0
 unsafe public void ParseDouble_Throws_When_NULL() => Assert.Throws <System.ArgumentNullException>(() => FastDoubleParser.ParseDouble((string)null));
コード例 #29
0
 public void NegativeZero()
 {
     Assert.Equal(-0, FastDoubleParser.ParseDouble("-0"));
 }
コード例 #30
0
        public void ParseDouble_CharConsumed_Works_Scenarios()
        {
            Skip.If(base.NoDiffToolDetected(), "No diff tool detected");

            Dictionary <string, string> sut = new Dictionary <string, string>();

            sut.Add("leading zeros", "001");
            sut.Add("leading zeros neg", "-001");
            sut.Add("leading spaces", "   1");
            sut.Add("zero", "0");
            sut.Add("double", "0.00000000000000212312312");
            sut.Add("double neg", "-0.00000000000000212312312");
            sut.Add("int", "1");
            sut.Add("int neg", "-1");

            sut.Add("autreint ", "123124");
            sut.Add("autreint neg", "-123124");

            sut.Add("notation scientifique", "4.56E+2");
            sut.Add("notation scientifique neg", "-4.56E-2");

            sut.Add("notation scientifique 2", "4.5644E+2");
            sut.Add("notation scientifique 2 neg", "-4.5644E-2");

            sut.Add("notation scientifique 3", "4424.5644E+22");
            sut.Add("notation scientifique 3 neg", "-4424.5644E-22");

            sut.Add("notation scientifique 4", "4424.5644E+223");
            sut.Add("notation scientifique 4 neg", "-4424.5644E-223");

            sut.Add("with trailling alpha", "4424.5644E+223 some alpha");



            sut.Add("nan", "nan");
            sut.Add("inf", "inf");
            sut.Add("+nan", "+nan");
            sut.Add("-nan", "-nan");
            sut.Add("+inf", "+inf");
            sut.Add("-inf", "-inf");
            sut.Add("infinity", "infinity");
            sut.Add("+infinity", "+infinity");
            sut.Add("-infinity", "-infinity");



            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair <string, string> kv in sut)
            {
                sb.AppendLine($"Scenario : {kv.Key} ");
                sb.AppendLine($"Value   : {kv.Value} ");

                int nbCarConsumed = 0;

                var res = FastDoubleParser.ParseDouble(kv.Value, out nbCarConsumed);

                sb.AppendLine($"Result : {res.ToString(CultureInfo.InvariantCulture)} :  Consumed :  { nbCarConsumed }");
                sb.AppendLine();
            }

            VerifyData(sb.ToString());
        }