1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-25 02:58:02 +02:00
Beef/IDEHelper/Tests/src/Floats.bf

130 lines
2.4 KiB
Beef
Raw Normal View History

2021-07-02 11:20:51 -07:00
using System;
namespace Tests
{
class Floats
{
2021-07-02 11:21:46 -07:00
public static void FloatParseTest(StringView string, float expectedResult)
2021-07-02 11:20:51 -07:00
{
float result = float.Parse(string);
Test.Assert(expectedResult == result);
}
[Test]
public static void TestBasics()
{
2021-07-02 11:21:46 -07:00
FloatParseTest("1.2", 1.2f);
FloatParseTest("-0.2", -0.2f);
FloatParseTest("2.5E2", 2.5E2f);
FloatParseTest("2.7E-10", 2.7E-10f);
FloatParseTest("-0.17E-7", -0.17E-7f);
FloatParseTest("8.7e6", 8.7e6f);
FloatParseTest("3.3e-11", 3.3e-11f);
FloatParseTest("0.002e5", 0.002e5f);
2021-07-02 11:20:51 -07:00
}
2024-01-14 17:06:43 -06:00
public static void FloatParseErrTest(StringView string)
{
Test.Assert(float.Parse(string) case .Err);
}
[Test]
public static void TestErrors()
{
FloatParseErrTest("");
FloatParseErrTest("-");
FloatParseErrTest("+");
FloatParseErrTest(".");
FloatParseErrTest("+.");
FloatParseErrTest("-.");
FloatParseErrTest("E");
FloatParseErrTest("e");
FloatParseErrTest(".E");
FloatParseErrTest(".e");
FloatParseErrTest("-.E");
FloatParseErrTest("-.e");
FloatParseErrTest("+.E");
FloatParseErrTest("+.e");
FloatParseErrTest("5E-");
FloatParseErrTest("5e-");
FloatParseErrTest("6E+");
FloatParseErrTest("6e+");
}
2024-10-16 13:25:17 -04:00
[Test]
public static void TestCmp()
{
float fNeg = -1;
float fNan = float.NaN;
if (fNeg < 0)
{
}
else
{
Test.FatalError();
}
if (fNeg > 0)
Test.FatalError();
if (fNan < 0)
Test.FatalError();
if (fNan <= 0)
Test.FatalError();
if (fNan > 0)
Test.FatalError();
if (fNan >= 0)
Test.FatalError();
if (fNan == 0)
Test.FatalError();
if (fNan != 0)
{
}
else
{
Test.FatalError();
}
if (fNan == fNan)
Test.FatalError();
if (fNan != fNan)
{
}
else
{
Test.FatalError();
}
bool b0 = fNan < 0;
bool b1 = fNan > 0;
bool b2 = fNan == fNan;
bool b3 = fNan != fNan;
bool b4 = fNan != 0;
Test.Assert(!b0);
Test.Assert(!b1);
Test.Assert(!b2);
Test.Assert(b3);
Test.Assert(b4);
}
public static void MinMaxTest<T>(T expectedMinValue, T expectedMaxValue)
where T : IMinMaxValue<T>
where int : operator T <=> T
{
Test.Assert(T.MinValue == expectedMinValue);
Test.Assert(T.MaxValue == expectedMaxValue);
}
[Test]
public static void TestMinMax()
{
MinMaxTest<float>(Float.MinValue, Float.MaxValue);
MinMaxTest<double>(Double.MinValue, Double.MaxValue);
}
2021-07-02 11:20:51 -07:00
}
}