Exemplo n.º 1
0
 unsafe public void FloatParser_HandleInvalidInput_works(string input, float res)
 {
     fixed(char *p = input)
     {
         Assert.Equal(res, FastFloatParser.HandleInvalidInput(p, p + input.Length));;
     }
 }
Exemplo n.º 2
0
        public void cas_compute_float_32_1()
        {
            for (int p = -65; p <= 38; p++)
            {
                if (p == 23)
                {
                    p++;
                }

                var am = FastFloatParser.ComputeFloat(q: p, w: 1);

                float?f = FastFloatParser.ToFloat(false, am);



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

                if (f != testing_power_of_ten_float[p + 65])
                {
                    throw new ApplicationException($"bad parsing p=> {p}");
                }
            }
        }
Exemplo n.º 3
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);

            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();
        }
Exemplo n.º 4
0
        unsafe private void All32BitsValues()
        {
            for (uint w = 0; w <= 0xFFFFFFFF; w++)
            {
                uint  word = (uint)(w);
                float f    = BitConverter.Int32BitsToSingle((int)word);

                string sut = f.ToString().Replace(",", ".");

                Assert.Equal(f, FastFloatParser.ParseFloat(sut));
            }
        }
Exemplo n.º 5
0
        unsafe private void All32BitsValues()
        {
            for (uint w = 0; w <= 0xFFFFFFFF; w++)
            {
                float f;
                uint  word = (uint)(w);

                Buffer.MemoryCopy(&word, &f, sizeof(float), sizeof(float));

                string sut = f.ToString().Replace(",", ".");

                Assert.Equal(f, FastFloatParser.ParseFloat(sut));
            }
        }
Exemplo n.º 6
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.");
        }
Exemplo n.º 7
0
        unsafe public void cas_compute_float_32_2()
        {
            for (int p = -1000; p <= 38; p++)
            {
                string sut = $"1e{p}";
                fixed(char *pstart = sut)
                {
                    float?f = FastFloatParser.ParseFloat(pstart, pstart + sut.Length, chars_format.is_general);

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

                    float expected = ((p >= -65) ? testing_power_of_ten_float[p + 65] : (float)Math.Pow(10, p));

                    Assert.Equal(expected, f.Value);
                }
            }
        }
Exemplo n.º 8
0
 private void TestGeneral_Float(string sut, float expected_value) => Assert.Equal(expected_value, FastFloatParser.ParseFloat(sut));
Exemplo n.º 9
0
 private void TestGeneral_Float_appendZeros(string sut, int zeros, string exp, float expected_value) => Assert.Equal(expected_value, FastFloatParser.ParseFloat(sut.PadRight(zeros, '0') + exp));
Exemplo n.º 10
0
 public void TryParse_NeverThrows(string sut)
 {
     Assert.False(FastFloatParser.TryParseFloat(sut, out _));
 }
Exemplo n.º 11
0
 unsafe public void PaseFloat_Throws_When_Invalid(string sut) => Assert.Throws <System.ArgumentException>(() => FastFloatParser.ParseFloat(sut));
Exemplo n.º 12
0
 unsafe public void PaseFloat_Throws_When_Empty() => Assert.Throws <System.ArgumentException>(() => FastFloatParser.ParseFloat(string.Empty));
Exemplo n.º 13
0
 unsafe public void PaseFloat_Throws_When_NULL() => Assert.Throws <System.ArgumentNullException>(() => FastFloatParser.ParseFloat((string)null));
Exemplo n.º 14
0
 private void TestGeneral_Float(string sut, float expected_value)
 {
     Assert.Equal(expected_value, FastFloatParser.ParseFloat(sut));
     Assert.Equal(expected_value, FastFloatParser.ParseFloat(sut.AsSpan()));
     Assert.Equal(expected_value, FastFloatParser.ParseFloat(System.Text.Encoding.UTF8.GetBytes(sut)));
 }