/// <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(); }
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)); } }
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)); } }
/// <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."); }
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); } } }
private void TestGeneral_Float(string sut, float expected_value) => Assert.Equal(expected_value, FastFloatParser.ParseFloat(sut));
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));
unsafe public void PaseFloat_Throws_When_Invalid(string sut) => Assert.Throws <System.ArgumentException>(() => FastFloatParser.ParseFloat(sut));
unsafe public void PaseFloat_Throws_When_Empty() => Assert.Throws <System.ArgumentException>(() => FastFloatParser.ParseFloat(string.Empty));
unsafe public void PaseFloat_Throws_When_NULL() => Assert.Throws <System.ArgumentNullException>(() => FastFloatParser.ParseFloat((string)null));
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))); }