1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Bugfixes for number parsing

This commit is contained in:
Ron Zuckerman 2024-01-14 17:06:43 -06:00
parent d5c336ffab
commit 8bd12814b8
No known key found for this signature in database
GPG key ID: 7EC17CB87028ABF8
11 changed files with 169 additions and 1 deletions

View file

@ -22,5 +22,33 @@ namespace Tests
FloatParseTest("3.3e-11", 3.3e-11f);
FloatParseTest("0.002e5", 0.002e5f);
}
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+");
}
}
}

View file

@ -1,6 +1,7 @@
#pragma warning disable 168
using System;
using System.Globalization;
namespace Tests
{
@ -43,5 +44,74 @@ namespace Tests
int i1 = i0 % 1;
Test.Assert(i1 == 0);
}
public static void Int64ParseTest(StringView string, int64 expectedResult, NumberStyles style = .Number)
{
int64 result = int64.Parse(string, style);
Test.Assert(expectedResult == result);
}
[Test]
public static void TestInt64Parse()
{
Int64ParseTest("1234567890", 1234567890L);
Int64ParseTest("+1234567890", 1234567890L);
Int64ParseTest("-9876543210", -9876543210L);
Int64ParseTest("0x123456789abcdef", 81985529216486895L, .HexNumber);
Int64ParseTest("0X123456789ABCDEF", 81985529216486895L, .HexNumber);
Int64ParseTest("+0x123456789abcdef", 81985529216486895L, .HexNumber);
Int64ParseTest("-0x76543210fedcba", -33306621262093498L, .HexNumber);
}
public static void Int64ParseErrorTest(StringView string, NumberStyles style = .Number)
{
Test.Assert(int64.Parse(string, style) case .Err);
}
[Test]
public static void TestInt64ParseError()
{
Int64ParseErrorTest("");
Int64ParseErrorTest("-");
Int64ParseErrorTest("+");
Int64ParseErrorTest("0x", .HexNumber);
Int64ParseErrorTest("0X", .HexNumber);
Int64ParseErrorTest("+0x", .HexNumber);
Int64ParseErrorTest("+0X", .HexNumber);
Int64ParseErrorTest("-0x", .HexNumber);
Int64ParseErrorTest("-0X", .HexNumber);
}
public static void Uint64ParseTest(StringView string, uint64 expectedResult, NumberStyles style = .Number)
{
uint64 result = uint64.Parse(string, style);
Test.Assert(expectedResult == result);
}
[Test]
public static void TestUint64Parse()
{
Uint64ParseTest("1234567890", 1234567890UL);
Uint64ParseTest("+9876543210", 9876543210UL);
Uint64ParseTest("0x123456789abcdef", 81985529216486895UL, .HexNumber);
Uint64ParseTest("0X123456789ABCDEF", 81985529216486895UL, .HexNumber);
Uint64ParseTest("+0xfedcba9876543210", 18364758544493064720UL, .HexNumber);
}
public static void Uint64ParseErrorTest(StringView string, NumberStyles style = .Number)
{
Test.Assert(uint64.Parse(string, style) case .Err);
}
[Test]
public static void TestUint64ParseError()
{
Uint64ParseErrorTest("");
Uint64ParseErrorTest("+");
Uint64ParseErrorTest("0x", .HexNumber);
Uint64ParseErrorTest("0X", .HexNumber);
Uint64ParseErrorTest("+0x", .HexNumber);
Uint64ParseErrorTest("+0X", .HexNumber);
}
}
}